
Hamming + Hanning + Rectangular And Triangular Window Code In MatLAB
BASIC
Windows reduces DFT leakage by minimizing the magnitude of the function.
There are four types of windows. They are,
- Rectangular Window
- Triangular Window
- Hanning Window
- Hamming Window
Rectangular Window
w(n) =1; for n=0,1,2,…………………………N-1
Triangular Window
w(n) = n/(N/2); for n=0,1,2,…………………………..N/2
w(n) = 2 – n/(N/2); for n=N/2+1,n/2+2,…………………………..N-1
Hanning Window
w(n) = 0.5 – 0.5*cos(2*πn/N); for n=0,1,2,…………………………N-1
Hamming Window
w(n) = 0.54 – 0.46*cos(2*πn/N); for n=0,1,2,…………………………N-1
MATLAB CODE
AT first we need a value (N) to what we want to find the window function. We will ask for it to the user.
N = input('Enter the value of N: ');
Now, determine the range of values on which windowing depends. The start value is 0 and the the end value is N-1. It will be plotted on x-axis while plotting.
n = 0:1:N-1;
Use a for loop to find the rectangular window. Continue the loop from 1 to n (will be plotted on 0 to N-1) and declare all of the values to 1. You can also use the ones() function to declare.
for i=1:N w(i) = 1; endfor
Plot the rectangle window function. Here you have divided the plot into four subplot. Do that if you need and put this window on first subplot. Set the title.
subplot(2,2,1); plot(n,w); title('Rectangal Window');
Rectangular Window Code
N = input('Enter the value of N: '); n = 0:1:N-1; for i=1:N w(i) = 1; endfor subplot(2,2,1); plot(n,w); title('Rectangal Window');
For triangular window we need total three loop. Take the first one. It is for first half of the function (0 to N/2). Fill the value of w(n) as the formula above.
for i=1:1:N/2+1 w(i) = (i-1)/(N/2); endfor
Now, We are at second part. There is two condition. One is the value of N is even another is odd. Here the if statement will work if n is even and the else statement will work if the N is odd. We need to continue the index with previous i because here j can move as a fraction value. Although you can do this task in one loop by adding an if statement. But I think it is easier.
if mod(N,2)==0 for j=N/2+1:1:N-1 i = i + 1; w(i) = 2 - (i)/(N/2); endfor else for j=N/2+1:1:N i = i + 1; w(i) = 2 - (i)/(N/2); endfor endif
Plot Triangular window on second subplot. Set the title.
subplot(2,2,2); plot(n,w); title('Triangular Window');
Triangular Window Code
N = input('Enter the value of N: '); n = 0:1:N-1; for i=1:1:N/2+1 w(i) = (i-1)/(N/2); endfor if mod(N,2)==0 for j=N/2+1:1:N-1 i = i + 1; w(i) = 2 - (i)/(N/2); endfor else for j=N/2+1:1:N i = i + 1; w(i) = 2 - (i)/(N/2); endfor endif subplot(2,2,2); plot(n,w); title('Triangular Window');
The Hanning window is too much easy. It is like the rectangular window. You just put the formula above instead of 1 in the loop.
for i=1:N w(i) = 0.5 - 0.5*cos(2*pi*(i-1)/N); endfor
Plot the Hanning window on third subplot. Set the title.
subplot(2,2,3); plot(n,w); title('Hanning Window');
Hanning Window Code
N = input('Enter the value of N: '); n = 0:1:N-1; for i=1:N w(i) = 0.5 - 0.5*cos(2*pi*(i-1)/N); endfor subplot(2,2,3); plot(n,w); title('Hanning Window');
The Hamming window is also too much easy. It is like the rectangular window also. You just put the formula above instead of 1 in the loop.
for i=1:N w(i) = 0.54 - 0.46*cos(2*pi*(i-1)/N); endfor
Plot the Hamming window on fourth subplot. Set the title.
subplot(2,2,4); plot(n,w); title('Hamming Window');
Hamming Window Code
N = input('Enter the value of N: '); n = 0:1:N-1; for i=1:N w(i) = 0.54 - 0.46*cos(2*pi*(i-1)/N); endfor subplot(2,2,4); plot(n,w); title('Hamming Window');
FULL CODE
N = input('Enter the value of N: '); n = 0:1:N-1; for i=1:N w(i) = 1; endfor subplot(2,2,1); plot(n,w); title('Rectangal Window'); for i=1:1:N/2+1 w(i) = (i-1)/(N/2); endfor if mod(N,2)==0 for j=N/2+1:1:N-1 i = i + 1; w(i) = 2 - (i)/(N/2); endfor else for j=N/2+1:1:N i = i + 1; w(i) = 2 - (i)/(N/2); endfor endif subplot(2,2,2); plot(n,w); title('Triangular Window'); for i=1:N w(i) = 0.5 - 0.5*cos(2*pi*(i-1)/N); endfor subplot(2,2,3); plot(n,w); title('Hanning Window'); for i=1:N w(i) = 0.54 - 0.46*cos(2*pi*(i-1)/N); endfor subplot(2,2,4); plot(n,w); title('Hamming Window');
OUTPUT
For N=41 The output below is shown.

I think your experience with this tutorial is quite good. If you have to seek anything please ask in comment.
Thanks.