Hi, This is a great forum. I am learning a lot about DSP here. But I am puzzled by the effect of windowing in the code below. (P.S., this is not a homework problem. I am trying to learn how to do spectral analysis on random signals). I am much obliged to your help. 1. Why did spectral leakage and gain loss occurred in when I windowed the sinusoid? (I think I coded it correctly). 2. My time domain signal contains no noise. So why I don't get complete zero magnitude at frequency other than 50 and 120Hz? (This is screwing up my phase plot) 3. I need help in interpreting the phase plot. All I understand is that phase is correct at 50Hz and 120 Hz. But if I have a random signal where I have no a priori knowledge on which frequency is meaningful, what do I do? format long; Fs = 1000; % Sampling frequency T = 1/Fs; % Sample time L = 1000; % Length of signal t = (0:L-1)*T; % Time vector % Sum of a 50 Hz sinusoid and a 120 Hz sinusoid x = 0.7*cos(2*pi*50*t+0.25*pi) + cos(2*pi*120*t+0.75*pi); windowHanning = window(@hann, length(x)); y = windowHanning.*x'; % multiply by Hanning window. z = x'; % Not windowed version. NFFT=length(y); Y = fft(y,NFFT)/L; f = Fs/2*linspace(0,1,NFFT/2+1); NFFTz=length(z); Z = fft(z,NFFTz)/L; fz = Fs/2*linspace(0,1,NFFTz/2+1); % Plot single-sided amplitude spectrum. figure(1); plot(f,2*abs(Y(1:NFFT/2+1)), fz, 2*abs(Z(1:NFFTz/2+1)), '-r'); legend('Windowed', 'not windowed'); title('Single-Sided Amplitude Spectrum of y(t)') xlabel('Frequency (Hz)') ylabel('|Y(f)|') %Plot non-windowed phase spectrum. phi=angle(Z); figure(2); subplot(211); plot(f, phi(1:NFFT/2+1)/pi); %plot in radian. title('Single-Sided Phase Spectrum of non-windowed orignal signal'); xlabel('Frequency (Hz)'); ylabel('Phase (rad)'); subplot(212); phiy=angle(Y); plot(f, phiy(1:NFFT/2+1)/pi); title('Single-Sided Phase Spectrum of windowed orignal signal'); xlabel('Frequency (Hz)'); ylabel('Phase (rad)'); abs(Y(51))*2%Magnitude at 50Hz. abs(Y(121))*2%Magnitude at 120Hz. angle(Y(51))/pi %Phase angle (rad) at 50Hz angle(Y(121))/pi %Phase angle (rad) at120Hz

# Windowing effect on spectral leakage and phase after FFT

Started by ●November 18, 2009

Reply by ●November 18, 20092009-11-18

shinchan75034 schrieb:> Hi, > This is a great forum. I am learning a lot about DSP here. But I am > puzzled by the effect of windowing in the code below. (P.S., this is not a > homework problem. I am trying to learn how to do spectral analysis on > random signals). I am much obliged to your help. > > 1. Why did spectral leakage and gain loss occurred in when I windowed the > sinusoid? (I think I coded it correctly). > > 2. My time domain signal contains no noise. So why I don't get complete > zero magnitude at frequency other than 50 and 120Hz? (This is screwing up > my phase plot) > > 3. I need help in interpreting the phase plot. All I understand is that > phase is correct at 50Hz and 120 Hz. But if I have a random signal where I > have no a priori knowledge on which frequency is meaningful, what do I do? > > format long; > Fs = 1000; % Sampling frequency > T = 1/Fs; % Sample time > L = 1000; % Length of signal > t = (0:L-1)*T; % Time vector > % Sum of a 50 Hz sinusoid and a 120 Hz sinusoid > x = 0.7*cos(2*pi*50*t+0.25*pi) + cos(2*pi*120*t+0.75*pi); > windowHanning = window(@hann, length(x)); > y = windowHanning.*x'; % multiply by Hanning window. > z = x'; % Not windowed version. > > NFFT=length(y); > Y = fft(y,NFFT)/L; > f = Fs/2*linspace(0,1,NFFT/2+1); > > NFFTz=length(z); > Z = fft(z,NFFTz)/L; > fz = Fs/2*linspace(0,1,NFFTz/2+1); > > % Plot single-sided amplitude spectrum. > figure(1); plot(f,2*abs(Y(1:NFFT/2+1)), fz, 2*abs(Z(1:NFFTz/2+1)), '-r'); > > legend('Windowed', 'not windowed'); > title('Single-Sided Amplitude Spectrum of y(t)') > xlabel('Frequency (Hz)') > ylabel('|Y(f)|') > > %Plot non-windowed phase spectrum. > phi=angle(Z); > figure(2); subplot(211); plot(f, phi(1:NFFT/2+1)/pi); %plot in radian. > title('Single-Sided Phase Spectrum of non-windowed orignal signal'); > xlabel('Frequency (Hz)'); > ylabel('Phase (rad)'); > subplot(212); > phiy=angle(Y); > plot(f, phiy(1:NFFT/2+1)/pi); > title('Single-Sided Phase Spectrum of windowed orignal signal'); > xlabel('Frequency (Hz)'); > ylabel('Phase (rad)'); > abs(Y(51))*2%Magnitude at 50Hz. > abs(Y(121))*2%Magnitude at 120Hz. > angle(Y(51))/pi %Phase angle (rad) at 50Hz > angle(Y(121))/pi %Phase angle (rad) at120Hz1-2: Get a pencil and paper and write down what windowing in the time domain is doing with your spectrum and you might understand why windowing does mess up the spectrum (hint: multiplication --> convolution). 3: When you do not have a reference frequency or your signal is purely random phase is not a good feature to describe the signal.

Reply by ●November 18, 20092009-11-18

On 18 Nov, 18:24, "shinchan75034" <shinchan75...@gmail.com> wrote:> Hi, > This is a great forum. I am learning a lot about DSP here. But I am > puzzled by the effect of windowing in the code below. (P.S., this is not a > homework problem. I am trying to learn how to do spectral analysis on > random signals). I am much obliged to your help. > > 1. Why did spectral leakage and gain loss occurred in when I windowed the > sinusoid? (I think I coded it correctly).Because that's what windows do. The purpose of applying windows is to suppress side lobes in the spectrum. This is achieved at the expense of spectral leakage etc.> 2. My time domain signal contains no noise. So why I don't get complete > zero magnitude at frequency other than 50 and 120Hz? (This is screwing up > my phase plot)Because that's what windows do. Again, windows are used for specific purposes, which are achived at some expense.> 3. I need help in interpreting the phase plot. All I understand is that > phase is correct at 50Hz and 120 Hz. But if I have a random signal where I > have no a priori knowledge on which frequency is meaningful, what do I do?Disregard the phase plots for the spectrum coefficients with 'small' magnitude. Rune

Reply by ●November 18, 20092009-11-18

>On 18 Nov, 18:24, "shinchan75034" <shinchan75...@gmail.com> wrote: >> Hi, >> This is a great forum. I am learning a lot about DSP here. But I am >> puzzled by the effect of windowing in the code below. (P.S., this isnot a>> homework problem. I am trying to learn how to do spectral analysis on >> random signals). I am much obliged to your help. >> >> 1. Why did spectral leakage and gain loss occurred in when I windowedthe>> sinusoid? (I think I coded it correctly). > >Because that's what windows do. The purpose of applying windows >is to suppress side lobes in the spectrum. This is achieved >at the expense of spectral leakage etc. > >> 2. My time domain signal contains no noise. So why I don't getcomplete>> zero magnitude at frequency other than 50 and 120Hz? (This is screwingup>> my phase plot) > >Because that's what windows do. Again, windows are used >for specific purposes, which are achived at some expense. > >> 3. I need help in interpreting the phase plot. All I understand isthat>> phase is correct at 50Hz and 120 Hz. But if I have a random signalwhere I>> have no a priori knowledge on which frequency is meaningful, what do Ido?> >Disregard the phase plots for the spectrum coefficients >with 'small' magnitude. > >Rune >I should have multiply amplitude correction factor of 2 for Hanning window. But I am still puzzled. I thought you apply window to reduce spectral leakage by removing the discontinuity (See www.ee.iitm.ac.in/~nitin/_media/ee462/fftwindows.pdf)?

Reply by ●November 19, 20092009-11-19

On 18 Nov, 20:00, "shinchan75034" <shinchan75...@gmail.com> wrote:> >On 18 Nov, 18:24, "shinchan75034" <shinchan75...@gmail.com> wrote: > >> Hi, > >> This is a great forum. I am learning a lot about DSP here. But I am > >> puzzled by the effect of windowing in the code below. (P.S., this is > not a > >> homework problem. I am trying to learn how to do spectral analysis on > >> random signals). I am much obliged to your help. > > >> 1. Why did spectral leakage and gain loss occurred in when I windowed > the > >> sinusoid? (I think I coded it correctly). > > >Because that's what windows do. The purpose of applying windows > >is to suppress side lobes in the spectrum. This is achieved > >at the expense of spectral leakage etc. > > >> 2. My time domain signal contains no noise. So why I don't get > complete > >> zero magnitude at frequency other than 50 and 120Hz? (This is screwing > up > >> my phase plot) > > >Because that's what windows do. Again, windows are used > >for specific purposes, which are achived at some expense. > > >> 3. I need help in interpreting the phase plot. All I understand is > that > >> phase is correct at 50Hz and 120 Hz. But if I have a random signal > where I > >> have no a priori knowledge on which frequency is meaningful, what do I > do? > > >Disregard the phase plots for the spectrum coefficients > >with 'small' magnitude. > > >Rune > > I should have multiply amplitude correction factor of 2 for Hanning > window. But I am still puzzled. I thought you apply window to reduce > spectral leakage by removing the discontinuity (Seewww.ee.iitm.ac.in/~nitin/_media/ee462/fftwindows.pdf)?- Hide quoted text - > > - Show quoted text -You apply a window to change the shape of the frequency spectrum. It convolves the frequency spectrum of the signal with the window 'kernel' (whcih is the frequency spectrum of the window). That might 'reduce' leakage, or it might not - I don't find 'reducing leakage' a highly informative way to describe what is going on. Try reading harris on window functions (google f j harris window functions). Or read my notes on this at: http://www.bores.com/courses/intro/freq/3_window.htm and http://www.bores.com/courses/advanced/windows/index.htm harris, and the second of my notes above, discuss the 'figures f merit' for window functions - these include gain-related figures such as Processing Loss, Coherent Gain, and Scalloping Loss as well as leakage in terms of sidelobes. The way I sometimes think of it, you choose a window function whose kernel best matches the shape that either you think the signal's spectrum has, or that you want the signal's spectrum to have. Chris ======================= Chris Bore BORES Signal Processing www.bores.com