# How do I construct the dft response of hanning window

5 views (last 30 days)

Show older comments

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?

##### 0 Comments

### Accepted Answer

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));

%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
on 8 Jun 2022

### More Answers (0)

### See Also

### Community Treasure Hunt

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

Start Hunting!