首页 > 解决方案 > 用于信号处理的奇函数?

问题描述

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

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

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

谢谢!

(注意:如果更多代码有帮助,请告诉我。为了读者的利益,尽量保持简短!)

标签: javascriptsignal-processingfft

解决方案


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


推荐阅读