Sunday, 21 January 2018

What's All This FFT Scaling Stuff Anyhow ?

In these earlier blog posts I briefly discussed FFT scaling however in this post I will look into it in more detail.



If I run the following Matlab or Octave code :

x = [0 0.7071 1 0.7071 0 -0.7071 -1 -0.7071];
y = fft(x)
z = ifft(y)

We see that y contains the expected DFT/FFT output (I.E. scaled by N) but z contains the iFFT output scaled to match the original time domain input :

y =
   0.0000 + 0.0000i   0.0000 - 4.0000i   0.0000 + 0.0000i   0.0000 + 0.0000i   0.0000 + 0.0000i   0.0000 - 0.0000i   0.0000 + 0.0000i   0.0000 + 4.0000i

z =
   0.00000   0.70710   1.00000   0.70710   0.00000  -0.70710  -1.00000  -0.70710

So Matlab and Octave both include additional operations on the output to multiply the result of the iFFT function by a factor of 1/N so that x == ifft(fft(x)) this makes a lot of sense and is documented in many DSP text books.

If we do the same thing in the SigLib DSP library :

Pseudo code :
    SLData_t x [] = {0., 0.7071, 1.,  0.7071,  0., -0.7071, -1., -0.7071};
    y = SDA_Rfft(x);
    z = SDA_Cifft(y);

We see the following output From SigLib :

y :
[0] =  0.000000 + j 0.000000
[1] =  0.000000 - j 3.999981
[2] =  0.000000 + j 0.000000
[3] =  0.000000 + j 0.000019
[4] =  0.000000 + j 0.000000
[5] =  0.000000 - j 0.000019
[6] =  0.000000 + j 0.000000
[7] = -0.000000 + j 3.999981

z :
[0] = 0.000000
[1] = 5.656800
[2] = 8.000000
[3] = 5.656800
[4] = 0.000000
[5] = -5.656800
[6] = -8.000000
[7] = -5.656800

This shows that the output of the iFFT has not been multiplied by 1/N. Now while his results in a different solution to Matlab, it also makes sense for real-time systems.
In a real-time system there are two considerations :

  1. In floating point systems, scaling can be left until the end of all of the processing and applied to give the correct result for the applications
  2. In fixed point systems, scaling is fundamental to achieving the best signal to noise performance for the algorithm, without overflowing the fixed point number system

If you do want to scale the SigLib output to match that of Matlab and Octave it is as simple as :

    SDA_Multiply (z,                        // Pointer to source array
                  SIGLIB_ONE / FFT_LENGTH,  // Inverse FFT length
                  z,                        // Pointer to destination array
                  FFT_LENGTH);              // Dataset length

And, of course, if this is a fixed point number system you can just right shift by log2(N) to achieve the same, which is very efficient.

If you have found this solution useful then please do hit the Google (+1) button so that others may be able to find it as well.

Evaluate The Numerix-DSP Libraries : http://www.numerix-dsp.com/eval/

No comments:

Post a comment