0

I am doing FFT and IFFT with audio signal. The following is the code that i am using. But my question is before FFT, it extracts 1 byte from short array. As you know short is 2 bytes.

why it is doing it? Because of the extraction, After IFFT(FFT(original_audio_buffer)) is not same with original_audio_buffer. why it makes data loss?

public void calculateSignal(short[] original_audio_buffer)
{

    final int mNumberOfFFTPoints = 512;
    double mMaxFFTSample;
    int mPeakPos;

    if(converted_audio_buffer == null)
    {
        converted_audio_buffer = new short[mNumberOfFFTPoints * 2];
    }

    double temp;
    Complex[] y;
    Complex[] complexSignal = new Complex[mNumberOfFFTPoints];
    double[] absSignal = new double[mNumberOfFFTPoints/2];

    for(int i = 0; i < mNumberOfFFTPoints; i++)
    {
        temp = (double)((original_audio_buffer[2*i] & 0xFF) | (original_audio_buffer[2*i+1] << 8)) / 32768.0F;
        complexSignal[i] = new Complex(temp,0.0);
    }

    y = FFT.fft(complexSignal);

    Complex[] z = FFT.ifft(y);

    for(int i = 0; i < z.length; i++)
    {
        double tmp_re = z[i].re();
        tmp_re = tmp_re *  32768.0F;

        int tmpIntRe = (int)tmp_re;

        converted_audio_buffer[2*i] = (short)(tmpIntRe & 0x000000FF);
        converted_audio_buffer[2*i+1] = (short)( (tmpIntRe) >> 8);
    }
Knowledge Drilling
  • 986
  • 1
  • 8
  • 22
  • Where did you get this code from? – Cris Luengo Feb 02 '19 at 18:50
  • It is quite widely used around many opensources. like https://github.com/drandyhaas/Haasoscope/blob/master/android/example/src/main/java/com/felhr/serialportexample/FFT.java.. But since FFT.ifft() function I made the code. – Knowledge Drilling Feb 03 '19 at 09:20
  • In the linked code, the weird line you are asking about is commented out. Don’t use that line, is my recommendation, until you understand what it does and why you would need it. It makes no sense whatsoever to me. – Cris Luengo Feb 03 '19 at 14:18
  • The line you are asking about is for reading input from a BYTE array. You don't have a byte array, so you shouldn't be doing this. – Matt Timmermans Feb 03 '19 at 21:16
  • You are also generating the output incorrectly, as if you were writing into a byte array. – Matt Timmermans Feb 03 '19 at 21:18

0 Answers0