gpt4 book ai didi

javascript - 用于信号处理的奇函数?

转载 作者:数据小太阳 更新时间:2023-10-29 05:17:20 25 4
gpt4 key购买 nike

喂!我希望这是一个可以接受的问题。

通过一些用于信号处理的代码,我发现了一个奇怪的函数:

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 的结果完全错误。

谢谢!

(注意:让我知道更多代码是否有帮助。为了读者的缘故,尽量保持简短!)

最佳答案

这实现了 butterfly FFT算法的操作。

例如,运行...

console.log([0,1,2,3,4,5,6,7].map(i => kInd(i, 3)))

...打印...

[ 0, 4, 2, 6, 1, 5, 3, 7 ]

...这是图中的映射:

http://www.alwayslearn.com/DFT%20and%20FFT%20Tutorial/DFTandFFT_FFT_Butterfly_8_Input.html

关于javascript - 用于信号处理的奇函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52380535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com