最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript : interpolate an array of numbers - Stack Overflow

programmeradmin3浏览0评论

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).

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.

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).

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.

Share Improve this question asked Nov 15, 2014 at 0:39 midomido 25k15 gold badges99 silver badges122 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 20

the source of my solution.

the fiddle demo.

the code for interpolating arrays,

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;
};

example of using it:

var originalArry = [1,5,3];
var newArry = interpolateArray([1,5,3],5);
发布评论

评论列表(0)

  1. 暂无评论