【发布时间】:2018-09-18 06:51:05
【问题描述】:
你好!我希望这是一个可以接受的问题。
通过一些用于信号处理的代码,我发现了一个奇怪的函数:
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 的结果完全错误。
谢谢!
(注意:如果更多代码有帮助,请告诉我。为了读者的利益,尽量保持简短!)
【问题讨论】:
-
为什么不问问写它的程序员呢?
-
我愿意!不再可用。
-
只看代码,从右到左遍历k1的每一位,然后设置k2 = k2*2为0,k2 = k2*2+1为1,不知道为什么这个数字是相关的。
-
它看起来像位反转的(一种变体),例如en.wikipedia.org/wiki/Bit-reversal_permutation
标签: javascript signal-processing fft