10

I wanted to use Apache math commons implementation for FFT (FastFourierTransformer class) to process some dummy data whose 8 data samples are contributing to one complete sinusoidal wave. The maximum being amplitude 230. The code snippet that I tried is below :

private double[] transform() 
{   
    double [] input = new double[8];
    input[0] = 0.0;
    input[1] = 162.6345596729059;
    input[2] = 230.0;
    input[3] = 162.63455967290594;
    input[4] = 2.8166876380389125E-14;
    input[5] = -162.6345596729059;
    input[6] = -230.0;
    input[7] = -162.63455967290597;

    double[] tempConversion = new double[input.length];

    FastFourierTransformer transformer = new FastFourierTransformer();
    try {           
        Complex[] complx = transformer.transform(input);

        for (int i = 0; i < complx.length; i++) {               
            double rr = (complx[i].getReal());
            double ri = (complx[i].getImaginary());

            tempConversion[i] = Math.sqrt((rr * rr) + (ri * ri));
        }

    } catch (IllegalArgumentException e) {
        System.out.println(e);
    }

    return tempConversion;
}

1) Now the data returned by method transform is an array of complex number. Does that array contains the frequency component information about input data? or the tempConversion array that I created will contain the frequency information? The values in tempConversion array is :

 2.5483305001488234E-16
 920.0
 4.0014578493024757E-14
 2.2914314707516465E-13
 5.658858581079313E-14
 2.2914314707516465E-13
 4.0014578493024757E-14
 920.0

2) I searched a lot but at most of the places there is no clear documentation on what format of data algorithm expects (in terms of sample code to understand better) and how do I use the array of results to calculate the frequencies contained in the signal?

Syati
  • 113
  • 1
  • 6
  • This looks like two different questions: (1) how to prepare input data for the FFT routine and (2) how to interpret the output data ? You should only really ask one specific question at a time on SO. – Paul R Aug 21 '12 at 06:52
  • Also you need to be more specific about your problem with (1) - just saying "I am not able to ..." doesn't really tell us anything - what have you tried and what problem(s) did you encounter ? – Paul R Aug 21 '12 at 06:52
  • Paul, I rephrased my problem and came up with specific question. Is it more clear now? – Syati Aug 23 '12 at 05:23
  • Well done - down-vote removed. – Paul R Aug 23 '12 at 07:22

1 Answers1

13

Your output data looks correct. You've calculated the magnitude of the complex FFT output at each frequency bin which corresponds to the energy in the input signal at the corresponding frequency for that bin. Since your input is purely real, the output is complex conjugate symmetric, and the last 3 output values are redundant.

So you have:

Bin     Freq        Magnitude
  0     0 (DC)        2.5483305001488234E-16
  1     Fs/8        920.0
  2     Fs/4          4.0014578493024757E-14
  3     3Fs/8         2.2914314707516465E-13
  4     Fs/2 (Nyq)    5.658858581079313E-14
  5     3Fs/8         2.2914314707516465E-13  # redundant - mirror image of bin 3
  6     Fs/4          4.0014578493024757E-14  # redundant - mirror image of bin 2
  7     Fs/8        920.0                     # redundant - mirror image of bin 1

All the values are effectively 0 apart from bin 1 (and bin 6) which corresponds to a frequency of Fs/8 as expected.

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • Thanks Paul. If I plot above data in frequency domain, I will get a spike at frequency bin 1, whose magnitude will be 920.0. Since the maximum amplitude in input data in our case is 230.0, am I not suppose to see that value as amplitude value for the plotted spike? Is there any relation between these two values? – Syati Aug 23 '12 at 11:29
  • There's probably a scaling factor of N = 8 in your FFT, and if you're interested in absolute values then you need to either add bin 1 and bin 6 (or just multiply bin 1 by 2). So your magnitude is 920 * 2 / 8 = 230. – Paul R Aug 23 '12 at 17:49
  • What exactly is Fs? The number of samples seems consistent with the results here but then I presume you need to multiply it by something to get it into true units (since the scale of the input isn't specified) – Richard Tingle Jun 24 '14 at 13:45
  • 2
    Further research suggests fs is the sampling frequency – Richard Tingle Jun 24 '14 at 13:52
  • Sorry, yes, `Fs` is commonly used to denote sample rate in Hz. – Paul R Jun 24 '14 at 15:43
  • how do you know what frequency a bin denotes? if i have 512 audio frames (input.length) how do i deduce the `Fs` ? – pstanton Jan 06 '15 at 05:13
  • @pstanton: `f = Fs * i / n` - see [this answer](http://stackoverflow.com/questions/4364823/how-to-get-frequency-from-fft-result/4371627#4371627) for more details. – Paul R Jan 06 '15 at 06:33