Saturday, 25 June 2016

MATLAB: $tt fft$ and $tt ifft$ scaling


In MATLAB, the outputs of the fft and/or ifft functions often require additional processing before being considered for analysis.


I have heard many differing opinions on what is correct:




  • Scaling


    Mathworks states that fft and ifft functions are based on the following equations: \begin{align} X[k] &= \frac{1}{1} \cdot \sum_{n=1}^{N} x[n] \cdot e^{\frac{-j \cdot 2 \pi \cdot (k-1) \cdot (n-1)}{N}}, \quad\textrm{where}\quad 1\leq k\leq N\\ x[n] &= \frac{1}{N} \cdot \sum_{k=1}^{N} X[k] \cdot e^{\frac{+j \cdot 2 \pi \cdot (k-1) \cdot (n-1)}{N}},\quad \textrm{where}\quad 1 \leq n\leq N \end{align}





  • Scaling by signal length


    My peers typically scale the data by $\small \frac{1}{N}$ immediately after the processing the fft.
    (We do not consider the raw fft data before scaling.)



    %% Perform fft
    X_f = fft(x, n_sample, 1) / n_sample;
    % fft must be normalized by the number of samples in the data.
    % This convention was set by the software developer (Mathworks).


    Is this correct?



    1. If so, why does the MATLAB ifft function expect that we have not scaled by $1/N$ already?

    2. Is there a MATLAB ifft function or function option which does not automatically scale by $1/N$?


    Alternatively, is there a better convention which we should be using in placing the $1/N$? For example, placing the $1/N$ in the fft rather than the ifft, or placing an $1/\sqrt{N}$ in both equations, instead of an $1/N$?




  • Scaling by sampling period


    I have heard that the fft and ifft functions assume that the sampling period $T_{\rm sampling} = 1/f_{\rm sampling} = 1$, and that for the functions to be true, the following would need to apply:





\begin{align} X[k] &= \frac{1}{T_{\rm sampling}} \cdot \sum_{n=1}^{N} x[n] \cdot e^{\frac{-j \cdot 2 \pi \cdot (k-1) \cdot (n-1)}{N}},\quad \textrm{where}\quad 1 \leq k \leq N\\ x[n] &= \frac{T_{\rm sampling}}{N} \cdot \sum_{k=1}^{N} X[k] \cdot e^{\frac{+j \cdot 2 \pi \cdot (k-1) \cdot (n-1)}{N}},\quad\textrm{where}\quad 1 \leq n \leq N \end{align}


See links:



  • Link 1 (see comment to Matt Szelistowski by Dr Seis)

  • Link 2 (see answer by Rick Rosson vs that of Dr Seis)

  • Link 3 (see comment by Matt (Message: 7/16) and comment by Poorya (14/16)

  • Link 4 (see pg. 10, slide [1,1])

  • Link 5 (see pg. 8+9) [it seems he is using inverse convention for fft and ifft].



Is this true?


I'm particularly piqued because I cannot find any DFT or DTFT equations on Wikipedia which include the sampling period.



Answer



Whether or not to scale the forward FFT by 1/N depends on which result you want for further analysis: energy (preserving Parseval's identity), or amplitude (measuring height or volts, etc.).


If you want to measure or analyze energy, then don't scale by 1/N, and a longer sinusoid of the same amplitude will produce a larger FFT result, proportional to the greater energy of a longer signal.


Slightly more commonly, if you want to measure or analyze amplitudes, then to get a longer sinusoid (thus with more total energy at the exact same amplitude) to produce about the same FFT result as a shorter signal, you will need to scale down the FFT summation by an ratio proportional to the length. The ratio could be reference_length/N, which is sometimes 1/N if the system input gain is 1.0 for whatever dimensions or units, including the time interval dimensions, that you choose to use in your further analysis. You need to scale down proportionally because a DFT is a summation: the more you sum up similar items, the bigger the result.


So. Energy or amplitude. Which do you want?


Now if you scale down the forward FFT, then you should not scale the inverse so that IFFT(FFT(x)) == x. Or do vice versa.


The 1/sqrt(N) for scaling seems to me to be for either when one needs a formal symmetry for some proof, or when building some sort of hardware pipeline where the latency and/or number of arithmetic units/gates for the DFT and for the IDFT needs to be identical. But you get neither a good direct measurement of either energy or amplitude for any typical type of engineering analysis.



No comments:

Post a Comment

readings - Appending 内 to a company name is read ない or うち?

For example, if I say マイクロソフト内のパートナーシップは強いです, is the 内 here read as うち or ない? Answer 「内」 in the form: 「Proper Noun + 内」 is always read 「ない...