This discussion is archived
1 2 Previous Next 19 Replies Latest reply: May 17, 2010 3:47 PM by 843853 RSS

audio files real and imaginary data for FFT

801494 Newbie
Currently Being Moderated
Hi everyone, I am trying to write a program that will perform the FFT algorithm on a piece of audio.I have looked at a lot of examples and have done research and all lof the examples require you to already have the real and imaginary cos and sin data (usually in a 2 double arrays.)
How you get the real and imaginary frequency data from a file (e.g. wav) into the double arrays so I can use these algorithms?

i really need assistance and any would be greatly appreciated.
  • 1. Re: audio files real and imaginary data for FFT
    843853 Newbie
    Currently Being Moderated
    Real data points are just complex data points with imaginary parts of zero!
  • 2. Re: audio files real and imaginary data for FFT
    801494 Newbie
    Currently Being Moderated
    right.
    // Simple Fast Fourier Transform.
    class Fft {
        public static void complexToComplex(int sign, int n,
                                            float ar[], float ai[]) {
            float scale = (float)Math.sqrt(1.0f/n);
    
            int i,j;
            for (i=j=0; i<n; ++i) {
                if (j>=i) {
                     float tempr = ar[j]*scale;
                     float tempi = ai[j]*scale;
                     ar[j] = ar*scale;
         ai[j] = ai[i]*scale;
         ar[i] = tempr;
         ai[i] = tempi;
    }
    int m = n/2;
    while (m>=1 && j>=m) {
         j -= m;
         m /= 2;
    }
    j += m;
    }

    int mmax,istep;
    for (mmax=1,istep=2*mmax; mmax<n; mmax=istep,istep=2*mmax) {
    float delta = (float)sign*3.141592654f/(float)mmax;
    for (int m=0; m<mmax; ++m) {
         float w = (float)m*delta;
         float wr = (float)Math.cos(w);
         float wi = (float)Math.sin(w);
         for (i=m; i<n; i+=istep) {
         j = i+mmax;
         float tr = wr*ar[j]-wi*ai[j];
         float ti = wr*ai[j]+wi*ar[j];
         ar[j] = ar[i]-tr;
         ai[j] = ai[i]-ti;
         ar[i] += tr;
         ai[i] += ti;
         }
    }
    mmax = istep;
    }
    }
    }
    this is the algorithm I am trying to change so it accepts input from a file. 
    
    so I can set the imaginary parts array (ai) to zero?
    
    how would I get the complex data points from the sound file?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
  • 3. Re: audio files real and imaginary data for FFT
    843853 Newbie
    Currently Being Moderated
    ultiron wrote:
    this is the algorithm I am trying to change so it accepts input from a file.
    I don't think so. In your position I would not be be changing this code at all. I would just be trying to arrange to invoke the method complexToComplex() with the correct arguments.

    >
    so I can set the imaginary parts array (ai) to zero?

    how would I get the complex data points from the sound file?
    The sound file only contains the real parts which you will read and place in a float array. The corresponding imaginary parts are, well, zero! Just create an array floats with each value set to zeros!
  • 4. Re: audio files real and imaginary data for FFT
    801494 Newbie
    Currently Being Moderated
    Thanks for the help sabre. I think i'm almost there.
    Ive been working at it and now I have the sound file stored in a byte array with endianness accounted for. How would I convert this data into a float array? I imagine its more difficult that a generic byte array to float array conversion?
  • 5. Re: audio files real and imaginary data for FFT
    843853 Newbie
    Currently Being Moderated
    ultiron wrote:
    Thanks for the help sabre. I think i'm almost there.
    Ive been working at it and now I have the sound file stored in a byte array with endianness accounted for. How would I convert this data into a float array? I imagine its more difficult that a generic byte array to float array conversion?
    Assuming each value is two bytes (that is the only reason the 'endianness' matters) then first convert each pair of bytes to a short using something like
    short valueAsShort = (short)( (s[i] << 8)  | (s[i+1] & 0xff));
    and then to a float
    float valueAsFloat = (float)valueAsShort;
    Of course I have assumed big-endian signed but only you know what your actual format is.

    You do know that with that FFT implementation you can only process 2^N points!
    You do know how to interpret the resultant complex data of the FFT don't you?
  • 6. Re: audio files real and imaginary data for FFT
    801494 Newbie
    Currently Being Moderated
    cheers for the help on the floats. I'm working on that now.

    Im totally unsure on how to interpret the complex data. I was going to work it out when I got that actual FFT itself working.

    If you've got any pointers on how to interpret the data and what exactly the data will represent then they would be greatly appreciated. I need all the help I can get.
  • 7. Re: audio files real and imaginary data for FFT
    801494 Newbie
    Currently Being Moderated
    and I did know it can only process 2^n points. I read about zero padding and was planning to use that if the data wasnt exactly 2^n points.
    How would I find out how many points the sound file is??
  • 8. Re: audio files real and imaginary data for FFT
    843853 Newbie
    Currently Being Moderated
    ultiron wrote:
    cheers for the help on the floats. I'm working on that now.

    Im totally unsure on how to interpret the complex data. I was going to work it out when I got that actual FFT itself working.
    Since you don't know how to interpret the complex FFT result, one of the difficulties you will find is knowing whether or not your conversion from bytes to float is correct. The FFT does not care if you put rubbish into it; it will still transform it.

    >
    If you've got any pointers on how to interpret the data and what exactly the data will represent then they would be greatly appreciated. I need all the help I can get.
    Forums are not a good place to learn the basics of signal processing. You need to get a book and/or tutorial and spend a lot of time reading. I assume that this is a school/college/university project so you should consult your tutor.
  • 9. Re: audio files real and imaginary data for FFT
    801494 Newbie
    Currently Being Moderated
    right. Cheers for the help!
  • 10. Re: audio files real and imaginary data for FFT
    801494 Newbie
    Currently Being Moderated
    Hi, Sabre, was just looking for your opinion on the folowing code for converting a byte array to a float array?
    public float [] bytetofloat(byte [] convert){
    
        float [] data = new float [convert.length/2];
        for(int i = 0;i<convert.length;i+=2){
            for(int j = 0; j <data.length;j++){
    
    
    
            short valueAsShort = (short)( (convert[i] << 8)  | (convert[i+1] & 0xff));
            float valueAsFloat = (float)valueAsShort;
    
            valueAsFloat = data[j];
    
             }
        }
    return data;
    }
  • 11. Re: audio files real and imaginary data for FFT
    843853 Newbie
    Currently Being Moderated
    ultiron wrote:
    Hi, Sabre, was just looking for your opinion on the folowing code for converting a byte array to a float array?
    valueAsFloat = data[j];
    Err .. isn't that back to front?
  • 12. Re: audio files real and imaginary data for FFT
    801494 Newbie
    Currently Being Moderated
    yes your absolutely right. The nested loop was wrong as well.
    public static void bytetofloat(byte [] convert){
    
        float [] data = new float [convert.length/2];
        for(int i = 0;i<convert.length;i+=2){
         
    
    
    
            short valueAsShort = (short)( (convert[i] << 8)  | (convert[i+1] & 0xff));
            float valueAsFloat = (float)valueAsShort;
            System.out.println(valueAsFloat);
    
             }
        }
    which produced float results ranging from

    -1370 to a bout 5000.

    If i was to get there values into an array would this be OK to put into the FFT?
  • 13. Re: audio files real and imaginary data for FFT
    843853 Newbie
    Currently Being Moderated
    ultiron wrote:
    yes your absolutely right. The nested loop was wrong as well.
    public static void bytetofloat(byte [] convert){
    
    float [] data = new float [convert.length/2];
    for(int i = 0;i<convert.length;i+=2){
    
    
    
    
    short valueAsShort = (short)( (convert[i] << 8)  | (convert[i+1] & 0xff));
    float valueAsFloat = (float)valueAsShort;
    System.out.println(valueAsFloat);
    
    }
    }
    which produced float results ranging from

    -1370 to a bout 5000.

    If i was to get there values into an array would this be OK to put into the FFT?
    Of course! What would be the point of creating the arrays if you couldn't?
  • 14. Re: audio files real and imaginary data for FFT
    801494 Newbie
    Currently Being Moderated
    cheers for this - you've helped me an awful lot!

    one more thing - am i right in thinking the float values represent amplitudes of the sound?

    if not, what do they represent?
1 2 Previous Next