Thursday, March 31, 2011

FFT and Spectral Leakage

Introduction:
Frequency Transform is  used to study a signal's frequency domain characteristics. When using FFT to study the frequency domain characteristics of a signal, there are two limits : 1) The detectability of a small signal in the presence of a larger one ; 2) frequency resolution - which distinguishes two different frequencies.

In practice, the measured signals are limited in time and the FFT calculates the frequency transform over a certain number of discreet frequencies called bins.

Spectral Leakage:
In reality, signals are of time-limited nature and nothing can be known about the signal beyond the measured interval. For example, if the measurement of a never ending continuous train of sinusoidal wave is of interest, at some point of time we need to terminate our measurement to do further analysis. The limit on the time is also posed by limitations of the measurement system itself (like buffer size) besides other factors. Some assumptions have to made about the nature of the signal outside the measured interval. Fourier Transforms implicitly assumes that the signal essentially repeats itself after the measured interval.

Figure 1 illustrates the scenario in which a continuous train of sinusoidal signal is observed over a finite interval of time ("measured signal"). As discussed, the FFT assumes the signal to be continuous (conceptually, it does this by juxtaposing the measured signal repetitively). Observe the glitches in the assumed signal. These glitches are the manifestations of the measurement time relative to the frequency of the actual signal. If measurement time is an integral multiple of the rate of the actual signal (i.e. the inverse of the frequency of the signal), then no glitch will be observed in the assumed signal. In Figure 1, the measurement time is purposefully made to be a non-integral multiple of the actual signal rate. These sharp discontinuities will spread out in the frequency domain. This is called spectral leakage.

Figure 1: Impact of observation interval on FFT

Lets visualize the concept of spectral leakage by taking a pure sinusoidal signal as an example. Here we consider a sinusoid of 7Hz frequency (7 cycles in 1 second) and sampling it with a sampling frequency Fs=100Hz. We observe the signal for 100 seconds (700 cycles in total) and take the FFT of the observed signal. Figure 2 illustrates the frequency spectrum of the observed/measured signal. Essentially, the frequency spectrum contains a distinct peak at 7Hz.

Figure 2: Frequency Spectrum of a 7 Hz sinusoid observed for 100 seconds

Next, the measurement window is shrunk to 1 second. Now the FFT is taken on this observed signal. Figure 3 illustrates the frequency spectrum of the observed signal (which has 7 cycles only). The frequency spectrum contains some spectral leakage because of limited observation interval.


 Figure 3 : A 7 Hz sinusoid observed for 1 second and its Frequency Spectrum

In the previous illustrations (Figure 2 and 3), the observation time interval contained an integral number of sinewave cycles (i.e. in Figure 2 the observation interval contained exactly 700 cycles of sinusoid and in Figure 3 the observation interval contained exactly 7 cycles of sinusoid in the time domain).

For the next illustration the measurement time interval is adjusted in such a way that number of cycles in the observation window is no longer an integer. In Figure 4, the signal is observed for 1.4 seconds, which implies that there are 9.8 cycles. Now the observed signal does not end at zero amplitude at t=1.4 seconds. This scenario gives rise to gliches in the FFT's assumed signal (which constructs a periodic signal from the observed signal) and obviously results in more spectral leakage (Compare the Frequency spectrum in Figure 3 and Figure 4).
 

Figure 4 : A 7 Hz sinusoid observed for 1.4 seconds and its Frequency Spectrum.

 The effect of spectral leakage may be lessened if the observed signal does not contain any discontinuity at the end of the measurement time (this scenario rarely occurs in any real application). Another scenario in which the spectral leakage can be reduced is by having a signal that gradually reduces to zero at the ends of the measurement time.( All the windows (like Hamming, Hanning, Bartlett, etc.., essentially attempts to do this). Such signal would have no discontinuity when made periodic and so does not suffer spectral leakages.

Conclusion:
Spectral leakage is not due to FFT but due to the finite observation time. Spectral Leakage gives rise to two problems : 1) The spectral component of the desired signal no longer contains the complete energy, rather it also contains the energy of adjacent components and noise, thereby reducing the Signal to noise ratio; 2) Spectral leakage from a larger signal component may significantly overshadow other smaller signals making them difficult to identify or detect. 

Windowing Techniques are used to mitigate the effects of spectral leakage and therefore the restriction of having a limited observation interval.

Matlab Code:
Here is a matlab code to simulate the charts given in this discussion

%Illustration of Spectral Leakage and its effects on Frequency Spectrum
%(FFT)
%Author: Mathuranathan Viswanathan
%http:gaussianwaves.blosgpot.com
%License - Creative Commons - cc-by-nc-sa

observationTime=1; %Input : Observation time interval change it to 100 and 1.4
%and see the effect on FFT
Fx=7; %Frequency of the sinusoid

Fs=100; %Sampling Frequency
t=0:1/Fs:observationTime;
x=1*sin(2*pi*Fx*t);
plot(t,x)
title('SineWave - Frequency = 7Hz');
xlabel('Times (s)');ylabel('Amplitude');

%Perform FFT
NFFX=2.^(ceil(log(length(x))/log(2)));
FFTX=fft(x,NFFX);%pad with zeros
NumUniquePts=ceil((NFFX+1)/2);
FFTX=FFTX(1:NumUniquePts);
MY=abs(FFTX);
MY=MY*2;
MY(1)=MY(1)/2;
MY(length(MY))=MY(length(MY))/2;
MY=MY/length(x);
f1=(0:NumUniquePts-1)*Fs/NFFX;

figure;
stem(f1,MY,'k');
title('FFT of the sine wave');
axis([0 50 0 1]);
xlabel('Frequency');ylabel('Amplitude');

Reference:
[1] Fredric J Harris, "On the use of windows for Harmonic Analysis with the Discrete Wavelet Transform", Proceedings of IEEE, Vol 66,No 1, January 1978 - click here

Recommended Readings:
 Introduction to Digital Signal Processing and Filter DesignDigital Filters: Basics and DesignDigital Filters Design for Signal and Image Processing (Digital Signal & Image Processing Series (ISTE-DSP))
Practical Analog And Digital Filter Design (Artech House Microwave Library)Applied Signal Processing: A MATLAB-Based Proof of Concept (Signals and Communication Technology)Digital Signal Processing Using MATLAB and Wavelets (with CD-ROM)(Electrical Engineering) (Computer Science)
Analog & Digital Signal ProcessingDigital Filters and Signal Processing: With MATLAB Exercises, 3rd Edition

Read more: http://gaussianwaves.blogspot.com/2011/01/fft-and-spectral-leakage.html#ixzz1ICb1Capm
Under Creative Commons License: Attribution Non-Commercial Share Alike
source:http://gaussianwaves.blogspot.com/2011/01/fft-and-spectral-leakage.html

6 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. The example depicted with Figures 2 and 3 implies that the spectral leakage is due to a smaller duration of data.

    But its actually the zero padding of the fft that causes greater leakage.

    Take the fft without zero-padding the data.

    If you take the fft of the 1s sine wave, call it Y1 and the fft of the 100s sine wave, call it Y2, and look at the value of the ffts at a certain frequency, say f1, you'll find that Y1(f1) = Y2(f1). The reason why Y1 does not have a better looking plot is because the frequency bins for that fft do not align with the major frequency ie 7Hz.

    ReplyDelete
  3. From where you got this formula NFFX=2.^(ceil(log(length(x))/log(2)));

    ReplyDelete
  4. You'll find that Y1(f1) = Y2(f1). The reason why Y1 does not have a better looking plot is because the frequency bins for that FFT do not align with the major frequency ie( 7Hz ).

    ReplyDelete
  5. observationTime=1; %Input : Observation time interval change it to 100 and 1.4
    %and see the effect on FFT
    Fx=7; %Frequency of the sinusoid

    Fs=100; %Sampling Frequency
    t=0:1/Fs:observationTime;
    x=1*sin(2*pi*Fx*t);
    plot(t,x)
    title('SineWave - Frequency = 7Hz');
    xlabel('Times (s)');ylabel('Amplitude');

    %Perform FFT
    NFFX=2.^(ceil(log(length(x))/log(2)));
    FFTX=fft(x,NFFX);%pad with zeros
    NumUniquePts=ceil((NFFX+1)/2);
    FFTX=FFTX(1:NumUniquePts);
    MY=abs(FFTX);
    MY=MY*2;
    MY(1)=MY(1)/2;
    MY(length(MY))=MY(length(MY))/2;
    MY=MY/length(x);
    f1=(0:NumUniquePts-1)*Fs/NFFX;

    figure;
    stem(f1,MY,'k');
    title('FFT of the sine wave');
    axis([0 50 0 1]);
    xlabel('Frequency');ylabel('Amplitude');

    ReplyDelete
  6. MATLAB Code for FFT and Spectral Leakage :


    %Illustration of Spectral Leakage and its effects on Frequency Spectrum
    %(FFT)
    %Author: Mathuranathan Viswanathan
    %http:gaussianwaves.blosgpot.com
    %License - Creative Commons - cc-by-nc-sa

    observationTime=1; %Input : Observation time interval change it to 100 and 1.4
    %and see the effect on FFT
    Fx=7; %Frequency of the sinusoid

    Fs=100; %Sampling Frequency
    t=0:1/Fs:observationTime;
    x=1*sin(2*pi*Fx*t);
    plot(t,x)
    title('SineWave - Frequency = 7Hz');
    xlabel('Times (s)');ylabel('Amplitude');

    %Perform FFT
    NFFX=2.^(ceil(log(length(x))/log(2)));
    FFTX=fft(x,NFFX);%pad with zeros
    NumUniquePts=ceil((NFFX+1)/2);
    FFTX=FFTX(1:NumUniquePts);
    MY=abs(FFTX);
    MY=MY*2;
    MY(1)=MY(1)/2;
    MY(length(MY))=MY(length(MY))/2;
    MY=MY/length(x);
    f1=(0:NumUniquePts-1)*Fs/NFFX;

    figure;
    stem(f1,MY,'k');
    title('FFT of the sine wave');
    axis([0 50 0 1]);
    xlabel('Frequency');ylabel('Amplitude');

    ReplyDelete