如何通过将java中的fft数据归零来过滤音频(android)

时间:2015-07-09 08:23:47

标签: android audio filter filtering fft

我正在为Android平台开发一个基于音频的应用程序。在这段代码中我首先用wav格式录制我的声音,然后我得到fft(用fftbegir方法),现在我想在0-过滤我的声音4khz与零fft数据然后执行ifft但是当我播放新的wav文件时,我可以听到质量很差的声音,声音很大.fft类在这里http://introcs.cs.princeton.edu/java/97data/FFT.java.html,这是我的代码:

private void filter(Complex[] x, int size) throws IOException {

double d, b;
String strI;
byte[] bytes = new byte[2];
int i = 0;
double k = -3.14159;
Complex[] f = new Complex[size];
Complex[] iff;
byte[] ddd;
double[] kkk = new double[size];
FFT q = new FFT();
short shor;
double data9[] = new double[size];
d = 2 * 3.14159 / size;
totalAudioLen = size;
totalDataLen = totalAudioLen + 36;
while (i < size) {//////to make its lenght length power of 2
    data9[i] = k;
    k = k + d;
    i++;
}
i = 0;
while (i < (size / 2) - 2000) {
    f[i] = new Complex(x[i].re(), x[i].im());
    i++;
}
while (i < (size / 2) + 2000) {  ///i want to remov 2000 sample of fft 
    f[i] = new Complex(0, 0);
    i++;
}
while (i < size) {
    f[i] = new Complex(x[i].re(), x[i].im());
    i++;
}

iff = q.ifft(f);

try {
    out9 = new FileOutputStream(getridemal());
    out10 = new FileOutputStream(getwavfilter());
    out11 = new FileOutputStream(getkhodesh());

    WriteWaveFileHeader(out10, totalAudioLen, totalDataLen,
            longSampleRate, channels, byteRate);


    for (i = 0; i < size; i++) {
        b = iff[i].re();
        shor = (short) (b * 32768.0);
        bytes = ByteConvert.convertToByteArray(shor);
        out10.write(bytes, 0, 2);


    }



} finally {
    out9.close();
    out10.close();
    out11.close();
}
}



  private void fftbegir(String input, String output) {

double[] data8;

int i = 0;
int r, k, l;
double b;
int m = 2;


try {
    in5 = new FileInputStream(input);
    out5 = new FileOutputStream(output);

    AppLog.logString("File size: " + totalDataLen);
    totalAudioLen = in5.getChannel().size();

    data8 = SoundDataUtils.load16BitPCMRawDataFileAsDoubleArray();
    l = data8.length;

    while (l > m) {
        m = m * 2;
    }

    Complex[] x = new Complex[m];
    while (i < l) {
        x[i] = new Complex(data8[i], 0);
        i++;
    }
    in5.close();
    i--;

    for (i = l; i < m; i++) {
        x[i] = new Complex(0, 0);
    }
    FFT f = new FFT();
    Complex[] y = f.fft(x);
    filter(y, m);
    out5.close();
    }
   }

感谢:)

1 个答案:

答案 0 :(得分:0)

正在进行的频域过滤效果不佳。

虽然将FFT应用于FFT的结果会产生相同的样本(也就是说,它是可逆的),但是当系数被修改时,这不再成立。

这里至少有一些问题:

  1. Gibbs Phenomemum由于从通带到阻带的急剧转变而产生
  2. 首先,FFT是一个相当糟糕的带通滤波器。阻带中频率的分量出现在几个相邻的频带中,因此保留在信号中。
  3. 每个FFT bin包含实部和虚部。 (0,0)的复数值的大小为0,但也会丢失过程中的阶段信息。
  4. 你最好使用IIR带阻滤波器,它在时域中运行。除了按预期工作外,计算也便宜得多。