How do I construct the dft response of hanning window

5 views (last 30 days)
sahil sharma
sahil sharma on 7 Jun 2022
Commented: Paul on 8 Jun 2022
Hi,
I am trying to do an fft of the Hanning window function.
Below is the code that I have implemented:
N=input("Enter the number of samples = ");
T=input("Enter the time sampling = ");
n=0:T:N;
wn=0.5*(1-(cos(2*pi*n/N)));
figure(1);
subplot(2,1,1);
plot(n,wn);
%fft of hann window
wf=fft(wn,N);
fshift = linspace(-length(wf)/2,(length(wf)-1)/2,length(wf));
subplot(2,1,2);
plot(fshift, fftshift(abs(wf)));
disp("The length of wn "+length(wn)+" Length of wf "+length(wf));
%Creating hanning function with hann function
figure(2);
fn=hann(N);
subplot(2,1,1);
plot(fn);
fk=fft(fn);
subplot(2,1,2);
plot(fftshift(abs(fk)));
The plots that I am getting are:
and
I wanted to know why the fft is not generating the side lobes of the Hanning window that we are generally accustomed to seeing?

Accepted Answer

Paul
Paul on 7 Jun 2022
Edited: Paul on 7 Jun 2022
Hi Sahil,
The code doesn't show the values of N and T used, so I'll make up my own.
N = 64;
% T = 1;
Changed the next line so that numel(n) == N. I deleted the use of T, because it didn't look correct.
n=0:N-1;
Modified the definition of wn, should divide by N-1, at least to be consistent with Matlb's hann(64)
wn=0.5*(1-(cos(2*pi*n/(N-1))));
figure(1);
subplot(2,1,1);
plot(n,wn);
%fft of hann window
wf=fft(wn,N);
The fshift vector needs to be normalized by N. I'll also scale it to radians (per sample). Also, in this case N is even, so that requires another modification
fshift = linspace(-length(wf)/2,length(wf)/2-1,length(wf))/length(wf)*2*pi;
subplot(2,1,2);
First plot the DTFT (positive frequencies) of wn so we can see the sidelobes, and plot in dB
[hdtft,wdtft] = freqz(wn,1,4096);
plot(wdtft,db(abs(hdtft)))
Now show that wf are samples of hdtft
hold on
plot(fshift, db(fftshift(abs(wf))),'o');
We see that the DFT samples lie on the DTFT as expected..
disp("The length of wn "+length(wn)+" Length of wf "+length(wf));
The length of wn 64 Length of wf 64
%Creating hanning function with hann function
figure(2);
fn=hann(N);
subplot(2,1,1);
plot(fn);
fk=fft(fn);
subplot(2,1,2);
[hdtft,wdtft] = freqz(fn,1,4096);
plot(wdtft,db(abs(hdtft)))
hold on
plot(fshift,db(fftshift(abs(fk))),'o');
Same plots as above.
  2 Comments
Paul
Paul on 8 Jun 2022
As I said, the code you posted didn't show the values of N and T, so I couldn't recreate your resutls to start with. Having said that, I'm not exactly sure what you are expecting to see wrt the sidelobes. Because of the scale of the DFT, plotting in dB (or on a log scale) seems like a good idea. Other than that (and the other corrections I made to the code) I suppose you can use larger and larger values of N, and then the red circles will become more denesely sampled on the blue line.

Sign in to comment.

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by