javascript - 用于信号处理的奇函数?
问题描述
你好!我希望这是一个可以接受的问题。
通过一些用于信号处理的代码,我发现了一个奇怪的函数:
let kInd = (k1, pow) => {
let k2 = 0;
let k3 = 0;
for (let i = 0; i < pow; i++) {
k3 = k1 >> 1;
k2 = 2 * (k2 - k3) + k1;
k1 = k3;
}
return k2;
};
在傅立叶变换计算结束时调用此函数以交换实数+虚数数组对中的索引:
let fft = samples => {
let pow = Math.log2(samples.length); // `samples.length` is expected to be 2^int
// ... a bunch of code to generate `rBuff` and `iBuff` arrays representing
// real and imaginary components of fourier values
// Now make use of `kInd`; conditionally swap some indexes in `rBuff` and `iBuff`:
for (let i = 0; i < rBuff.length; i++) {
let k = kInd(i, pow);
if (k >= i) continue;
[ rBuff[i], rBuff[k] ] = [ rBuff[k], rBuff[i] ];
[ iBuff[i], iBuff[k] ] = [ iBuff[k], iBuff[i] ];
}
// ... A bit of code to convert to power spectrum and return result
};
我的问题是:到底在kInd
做什么?我已经运行它来输出一些示例值;看起来它随着k1
参数的增加以几乎随机的顺序输出 2 的幂和。小的更改会kInd
导致完全错误的结果fft
。
谢谢!
(注意:如果更多代码有帮助,请告诉我。为了读者的利益,尽量保持简短!)
解决方案
This implements the butterfly operation of the FFT algorithm.
For example, running...
console.log([0,1,2,3,4,5,6,7].map(i => kInd(i, 3)))
...prints...
[ 0, 4, 2, 6, 1, 5, 3, 7 ]
... which is the mapping in the diagram here:
http://www.alwayslearn.com/DFT%20and%20FFT%20Tutorial/DFTandFFT_FFT_Butterfly_8_Input.html
推荐阅读
- javascript - 不同的javascript方法中的不同正则表达式行为
- flutter - Flame 如何支持 Rive 动画?
- html - 为美国观众使用适当的语言子标签
- php - 如何通过改变捐赠总额来减少现金分配总额
- jenkins-plugins - jenkins 自定义插件 java 代码从从机读取文件
- r - R 如何获取美国市场的最后交易日?
- c# - 如何修复 Assets\Scripts\PlayerController.cs(48,9):错误 CS0103:当前上下文中不存在名称“CheckIfWallSliding”?
- matlab - 如何避免在 MATLAB 中重叠绘图标签?
- python - 使用 Django 将字符串从 HTML 输入传递到 python 脚本返回到网页
- python - 我可以在 OneHotEcoded 数据上使用 tf-idf 吗?