Javascript中的离散傅里叶变换

时间:2017-06-03 12:13:44

标签: javascript math fft

我想通过使用highcharts在网页上显示脉搏波形图。 对于精细数据,图表是好的,我可以通过波峰来计算脉冲,如下所示 image

但是一旦有一些坏点来计算峰值脉冲就无法工作(97是一个错误的数字) image with bad points

所以我决定用傅里叶变换。

是否有任何图书馆可以进行DFT?或者我必须自己写。

请告诉我图书馆名称或解决这个问题的方法,以及如何得到这个想法。

非常感谢!

2 个答案:

答案 0 :(得分:1)

如果需要,您可以自己实施。这是一个如何在ES6中实现离散傅立叶变换功能的示例:

import ComplexNumber from '../complex-number/ComplexNumber';

const CLOSE_TO_ZERO_THRESHOLD = 1e-10;

/**
 * Discrete Fourier Transform (DFT): time to frequencies.
 *
 * Time complexity: O(N^2)
 *
 * @param {number[]} inputAmplitudes - Input signal amplitudes over time (complex
 * numbers with real parts only).
 * @param {number} zeroThreshold - Threshold that is used to convert real and imaginary numbers
 * to zero in case if they are smaller then this.
 *
 * @return {ComplexNumber[]} - Array of complex number. Each of the number represents the frequency
 * or signal. All signals together will form input signal over discrete time periods. Each signal's
 * complex number has radius (amplitude) and phase (angle) in polar form that describes the signal.
 *
 * @see https://gist.github.com/anonymous/129d477ddb1c8025c9ac
 * @see https://betterexplained.com/articles/an-interactive-guide-to-the-fourier-transform/
 */
export default function dft(inputAmplitudes, zeroThreshold = CLOSE_TO_ZERO_THRESHOLD) {
  const N = inputAmplitudes.length;
  const signals = [];

  // Go through every discrete frequency.
  for (let frequency = 0; frequency < N; frequency += 1) {
    // Compound signal at current frequency that will ultimately
    // take part in forming input amplitudes.
    let frequencySignal = new ComplexNumber();

    // Go through every discrete point in time.
    for (let timer = 0; timer < N; timer += 1) {
      const currentAmplitude = inputAmplitudes[timer];

      // Calculate rotation angle.
      const rotationAngle = -1 * (2 * Math.PI) * frequency * (timer / N);

      // Remember that e^ix = cos(x) + i * sin(x);
      const dataPointContribution = new ComplexNumber({
        re: Math.cos(rotationAngle),
        im: Math.sin(rotationAngle),
      }).multiply(currentAmplitude);

      // Add this data point's contribution.
      frequencySignal = frequencySignal.add(dataPointContribution);
    }

    // Close to zero? You're zero.
    if (Math.abs(frequencySignal.re) < zeroThreshold) {
      frequencySignal.re = 0;
    }

    if (Math.abs(frequencySignal.im) < zeroThreshold) {
      frequencySignal.im = 0;
    }

    // Average contribution at this frequency.
    // The 1/N factor is usually moved to the reverse transform (going from frequencies
    // back to time). This is allowed, though it would be nice to have 1/N in the forward
    // transform since it gives the actual sizes for the time spikes.
    frequencySignal = frequencySignal.divide(N);

    // Add current frequency signal to the list of compound signals.
    signals[frequency] = frequencySignal;
  }

  return signals;
}

此函数只是直接实现公式:

enter image description here

该函数非常慢并且具有O(n^2)的时间复杂度。如果速度对您来说很重要,您可以改用Fast Fourier Transform

更多详细信息,请参见https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/fourier-transform

答案 1 :(得分:0)

我在Stack Overflow上的另一个帖子中提出了一个建议:sample in my end 它接受1D阵列的输入,假设它们间隔相等,并输出DFT。也许这就是你要找的东西?