Android中录制的wav文件的快速傅里叶变换

时间:2012-08-07 09:43:34

标签: android fft wav analysis

SD卡上有一个wav文件。首先,我连接到一个文件并读取字节:

File file = null;file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/202.wav");
       byte[] byteData = new byte[(int) file.length()];
       FileInputStream in = null;
       try {
       in = new FileInputStream( file );
       in.read( byteData );
       in.close();}

然后我使用double类型的字节类型的数据转换,因为FFT过程不适用于字节类型:

double[] transformed = new double[(int) file.length()];
      for (int j=1;j<8194;j++) {
              transformed[j] = (double)byteData[j]; }

然后FFT处理这些数据的过程:

public void spektr(double[] x, double[] y, int Dim, int D, double[] xx, double[] yy) {

     int I,J,N,L,K,LE,LE1,IP,NV2,NM1;
     double Arg,U1,U2,U3,C,S,T1,T2,T3,T4;


     N = (int) Math.pow(2,Dim);
     Log.v("N", "N "+N);
     for (L = 1; L < Dim; L++){
      LE = (int) Math.pow(2,Dim+1-L);
      LE1 = LE/2;
      U1 = (double)1.0;
      U2 = (double)0.0;
      Arg = (double)Math.PI/LE1;
      C = (double)Math.cos(Arg);
      S = D*(double)Math.sin(Arg);
      for(J = 1; J < LE1; J++){
       I = J;
       do{
            IP = I+LE1;
            T1 = x[I] + x[IP];
            T2 = y[I] + y[IP];
            T3 = x[I] - x[IP];
            T4 = y[I] - y[IP];
            x[IP] = T3*U1 - T4*U2;
            y[IP] = T4*U1 + T3*U2;
            x[I] = T1;
            y[I] = T2;
            I = I + LE;
       }while(I <= N);
       U3 = U1*C - U2*S;
       U2 = U2*C - U1*S;
       U1 = U3;
      }

     }

     NV2 = N / 2;
     Log.v("NV2", "NV2 "+NV2);
     NM1 = N-1;
     Log.v("NM1", "NM1 "+NM1);
     J = 1;
     for(I = 1; I < NM1; I++){
      if (I < J){
       T1 = x[J];
       T2 = y[J];
       x[J] = x[I];
       y[J] = y[I];
       x[I] = T1;
       y[I] = T2;
      }
      K = NV2;
      while (K < J){
       J = J - K;
       K = K / 2;
      };

      J = J + K;
     }

     for(I = 1; I < N; I++){
      x[I] = x[I] / N*2;
      y[I] = y[I] / N*2;

     }
        for(I = 1; I < N; I++){

      xx[I] = x[I];
      yy[I] = y[I];

     }

     xx[1] = (double)0.0;
    }

成功读取文件,但FFT的处理过程无效。 如何找到FFT程序,或者给代码检查FFT结果?

1 个答案:

答案 0 :(得分:1)