javascript:内插数字数组

编程入门 行业动态 更新时间:2024-10-10 02:23:06
本文介绍了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:内插数字数组

本文发布于:2023-11-30 18:21:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1651044.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:内插   数组   数字   javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!