FFT阵列的实现

时间:2014-05-21 13:23:09

标签: android fft

在Android中实现FFT算法时遇到问题。 假设我有一个长度为8.000字节的wav文件。 我知道你必须选择FFT算法的大小(并且必须是2的幂)。我的问题是,我不确定如何从现在开始进一步开展。 假设我选择了N = 1024的FFT大小。 我基本上可以选择: 1)将FFT算法直接应用于8.000字节的整个数组 2)将1024字节数组的wav文件划分为1024字节的块(并填充0的最后一块,直到8个精确的块), 然后将fft应用于每个块,最后再次整理所有不同的块以使用一个单字节数组来表示。 8000 * 2 * 1秒= 8192

我认为这是选项2,但我不完全确定。

这是我正在使用的fft阵列:

package com.example.acoustics;

public class FFT {

  int n, m;

  // Lookup tables. Only need to recompute when size of FFT changes.
  double[] cos;
  double[] sin;

  public FFT(int n) {
      this.n = n;
      this.m = (int) (Math.log(n) / Math.log(2));

      // Make sure n is a power of 2
      if (n != (1 << m))
          throw new RuntimeException("FFT length must be power of 2");

      // precompute tables
      cos = new double[n / 2];
      sin = new double[n / 2];

      for (int i = 0; i < n / 2; i++) {
          cos[i] = Math.cos(-2 * Math.PI * i / n);
          sin[i] = Math.sin(-2 * Math.PI * i / n);
      }

  }

  /***************************************************************
     * fft.c
     * Douglas L. Jones 
     * University of Illinois at Urbana-Champaign 
     * January 19, 1992 
     * http://cnx.rice.edu/content/m12016/latest/
     * 
     *   fft: in-place radix-2 DIT DFT of a complex input 
     * 
     *   input: 
     * n: length of FFT: must be a power of two 
     * m: n = 2**m 
     *   input/output 
     * x: double array of length n with real part of data 
     * y: double array of length n with imag part of data 
     * 
     *   Permission to copy and use this program is granted 
     *   as long as this header is included. 
     ****************************************************************/

  public void fft(double[] x, double[] y) {
      int i, j, k, n1, n2, a;
      double c, s, t1, t2;

      // Bit-reverse
      j = 0;
      n2 = n / 2;
      for (i = 1; i < n - 1; i++) {
          n1 = n2;
          while (j >= n1) {
              j = j - n1;
              n1 = n1 / 2;
          }
          j = j + n1;

          if (i < j) {
              t1 = x[i];
              x[i] = x[j];
              x[j] = t1;
              t1 = y[i];
              y[i] = y[j];
              y[j] = t1;
          }
      }

      // FFT
      n1 = 0;
      n2 = 1;

      for (i = 0; i < m; i++) {
          n1 = n2;
          n2 = n2 + n2;
          a = 0;

          for (j = 0; j < n1; j++) {
              c = cos[a];
              s = sin[a];
              a += 1 << (m - i - 1);

              for (k = j; k < n; k = k + n2) {
                  t1 = c * x[k + n1] - s * y[k + n1];
                  t2 = s * x[k + n1] + c * y[k + n1];
                  x[k + n1] = x[k] - t1;
                  y[k + n1] = y[k] - t2;
                  x[k] = x[k] + t1;
                  y[k] = y[k] + t2;
              }
          }
      }
  }
}

1 个答案:

答案 0 :(得分:1)

我认为您可以将整个阵列与FFT一起使用。没有问题,你可以使用2 ^ 13 = 8192并用零完成数组,这个处理也称为零填充,用于FFT的多个实现。如果您的程序运行良好,运行整个阵列没有问题,但如果您使用1024大小的部分来计算FFT,那么您将得到一个分段傅里叶变换,它不能很好地描述整个信号频谱,因为FFT使用数组中的所有位置来计算新变换数组中的每个值,然后在位置1中没有得到正确的答案,例如,如果你不使用整个信号数组。

这是我对你的问题的分析我不是百分之百确定,但我对傅里叶系列的了解告诉我,如果你计算傅里叶变换的分段形式而不是整个系列,那几乎就是这样。