本文介绍了javascript:内插数字数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
几个月前,我一直在寻找一种对使用HTML5从麦克风捕获的音频数据进行下采样的方法.如果输入是该频率的直接倍数(即48000Hz),我就需要输出速率为12000Hz,我没有问题,但是对于其他速率(例如44100Hz),它似乎很复杂.
few months back, I was looking for a way to downsample the audio data captured from mic using HTML5. I needed output rate as 12000Hz, if input was a direct multiplication of that (i.e 48000Hz) I had no problem, but it seemed complicated for other rates( e.g 44100Hz).
在这些情况下,直接向下采样(每4个仅保留1个)将不起作用,因此我想到了插值,但是那时stackoverflow没有解决方案.所以我自己回答.
In these scenarios, direct down sampling( retaining only 1 out every 4) won't work, so I thought of interpolation, but stackoverflow had no solution at that time. So answering it myself.
推荐答案我的解决方案的来源.
小提琴演示.
插值数组的代码
function interpolateArray(data, fitCount) { var linearInterpolate = function (before, after, atPoint) { return before + (after - before) * atPoint; }; var newData = new Array(); var springFactor = new Number((data.length - 1) / (fitCount - 1)); newData[0] = data[0]; // for new allocation for ( var i = 1; i < fitCount - 1; i++) { var tmp = i * springFactor; var before = new Number(Math.floor(tmp)).toFixed(); var after = new Number(Math.ceil(tmp)).toFixed(); var atPoint = tmp - before; newData[i] = linearInterpolate(data[before], data[after], atPoint); } newData[fitCount - 1] = data[data.length - 1]; // for new allocation return newData; };使用示例:
var originalArry = [1,5,3]; var newArry = interpolateArray([1,5,3],5);更多推荐
javascript:内插数字数组
发布评论