clear all; close all;
warning('off','MATLAB:dispatcher:InexactMatch')
% 1st Homework 'Using Window method'
% #1 For omega1 = pi / 2 and M = 10,

M=21;
n=-10:1:10;
wc=pi/2;
hd=ideal_lp(wc, M);

% (a) Draw w[n], W[omega] => dB scale  , h[n], H[omega] in case of rect window
figure('name', 'Rectangular Window @ M=10', 'Position', [50, 420, 550, 300], 'MenuBar', 'none')
w_rect=(rectwin(M));
h=hd.* w_rect'; 
[db,mag,pha,grd,w] = freqz_m(h,1);
[db2,mag,pha,grd,w] = freqz_m(w_rect,1);
subplot(1,2,1); stem(n,w_rect, 'r');title('Response'); hold on; axis([-(M-8) M-8 -0.2 1.1]); xlabel('n'); ylabel('w(n)');
subplot(1,2,1); stem( n,h);
subplot(1,2,2); plot(w/pi,db2, 'r');title('Magnitude Response in dB');grid ; xlabel('frequency in pi units'); ylabel('Decibels'); hold on;
subplot(1,2,2); plot(w/pi,db); h = legend('W[¥Ø]','H[¥Ø]',1); set(h,'Interpreter','none')

% (b) Draw w[n], W[omega] => dB scale  , h[n], H[omega] in case of tri window
figure('name', 'Triangular Window @ M=10', 'Position', [250, 370, 550, 300], 'MenuBar', 'none')
w_rect=(triang(M));
h=hd.* w_rect'; 
[db,mag,pha,grd,w] = freqz_m(h,1);
[db2,mag,pha,grd,w] = freqz_m(w_rect,1);
subplot(1,2,1); stem(n,w_rect, 'r');title('Response'); hold on; axis([-(M-8) M-8 -0.2 1.1]); xlabel('n'); ylabel('w(n)');
subplot(1,2,1); stem( n,h);
subplot(1,2,2); plot(w/pi,db2, 'r');title('Magnitude Response in dB');grid ; xlabel('frequency in pi units'); ylabel('Decibels'); hold on;
subplot(1,2,2); plot(w/pi,db); h = legend('W[¥Ø]','H[¥Ø]',1); set(h,'Interpreter','none')

% (c) Draw w[n], W[omega] => dB scale  , h[n], H[omega] in case of hann window
figure('name', 'Hanning Window @ M=10', 'Position', [450, 330, 550, 300], 'MenuBar', 'none')
w_rect=(hann(M));
h=hd.* w_rect'; 
[db,mag,pha,grd,w] = freqz_m(h,1);
[db2,mag,pha,grd,w] = freqz_m(w_rect,1);
subplot(1,2,1); stem(n,w_rect, 'r');title('Response'); hold on; axis([-(M-8) M-8 -0.2 1.1]); xlabel('n'); ylabel('w(n)');
subplot(1,2,1); stem( n,h);
subplot(1,2,2); plot(w/pi,db2, 'r');title('Magnitude Response in dB');grid ; xlabel('frequency in pi units'); ylabel('Decibels'); hold on;
subplot(1,2,2); plot(w/pi,db); h = legend('W[¥Ø]','H[¥Ø]',1); set(h,'Interpreter','none')

% (d) Draw w[n], W[omega] => dB scale  , h[n], H[omega] in case of hamming window
figure('name', 'Hamming Window @ M=10', 'Position', [650, 280, 550, 300], 'MenuBar', 'none')
w_rect=(hamming(M));
h=hd.* w_rect'; 
[db,mag,pha,grd,w] = freqz_m(h,1);
[db2,mag,pha,grd,w] = freqz_m(w_rect,1);
subplot(1,2,1); stem(n,w_rect, 'r');title('Response');  hold on; axis([-(M-8) M-8 -0.2 1.1]); xlabel('n'); ylabel('w(n)');
subplot(1,2,1); stem( n,h)
subplot(1,2,2); plot(w/pi,db2, 'r');title('Magnitude Response in dB');grid ; xlabel('frequency in pi units'); ylabel('Decibels'); hold on;
subplot(1,2,2); plot(w/pi,db); h = legend('W[¥Ø]','H[¥Ø]',1); set(h,'Interpreter','none')

% (e) Discuss the results

% #2 For omega1 = pi / 2 and M = 20,
M=41;
n=-20:1:20;
wc=pi/2;
hd=ideal_lp(wc, M);

% (a) Draw w[n], W[omega] => dB scale  , h[n], H[omega] in case of rect window
figure('name', 'Rectangular Window @ M=20', 'Position', [50, 300, 550, 300], 'MenuBar', 'none')
w_rect=(rectwin(M));
h=hd.* w_rect'; 
[db,mag,pha,grd,w] = freqz_m(h,1);
[db2,mag,pha,grd,w] = freqz_m(w_rect,1);
subplot(1,2,1); stem(n,w_rect, 'r');title('Response'); hold on; axis([-(M-18) M-18 -0.2 1.1]); xlabel('n'); ylabel('w(n)');
subplot(1,2,1); stem( n,h);
subplot(1,2,2); plot(w/pi,db2, 'r');title('Magnitude Response in dB');grid ; xlabel('frequency in pi units'); ylabel('Decibels'); hold on;
subplot(1,2,2); plot(w/pi,db); h = legend('W[¥Ø]','H[¥Ø]',1); set(h,'Interpreter','none')

% (b) Draw w[n], W[omega] => dB scale  , h[n], H[omega] in case of tri window
figure('name', 'Triangular Window @ M=20', 'Position', [250, 250, 550, 300], 'MenuBar', 'none')
w_rect=(triang(M));
h=hd.* w_rect'; 
[db,mag,pha,grd,w] = freqz_m(h,1);
[db2,mag,pha,grd,w] = freqz_m(w_rect,1);
subplot(1,2,1); stem(n,w_rect, 'r');title('Response'); hold on; axis([-(M-18) M-18 -0.2 1.1]); xlabel('n'); ylabel('w(n)');
subplot(1,2,1); stem( n,h);
subplot(1,2,2); plot(w/pi,db2, 'r');title('Magnitude Response in dB');grid ; xlabel('frequency in pi units'); ylabel('Decibels'); hold on;
subplot(1,2,2); plot(w/pi,db); h = legend('W[¥Ø]','H[¥Ø]',1); set(h,'Interpreter','none')

% (c) Draw w[n], W[omega] => dB scale  , h[n], H[omega] in case of hann window
figure('name', 'Hanning Window @ M=20', 'Position', [450, 200, 550, 300], 'MenuBar', 'none')
w_rect=(hann(M));
h=hd.* w_rect'; 
[db,mag,pha,grd,w] = freqz_m(h,1);
[db2,mag,pha,grd,w] = freqz_m(w_rect,1);
subplot(1,2,1); stem(n,w_rect, 'r');title('Response'); hold on; axis([-(M-18) M-18 -0.2 1.1]); xlabel('n'); ylabel('w(n)');
subplot(1,2,1); stem(n,h);
subplot(1,2,2); plot(w/pi,db2, 'r');title('Magnitude Response in dB');grid ; xlabel('frequency in pi units'); ylabel('Decibels'); hold on;
subplot(1,2,2); plot(w/pi,db); h = legend('W[¥Ø]','H[¥Ø]',1); set(h,'Interpreter','none')

% (d) Draw w[n], W[omega] => dB scale  , h[n], H[omega] in case of hamming window
figure('name', 'Hamming Window @ M=20', 'Position', [650, 150, 550, 300], 'MenuBar', 'none')
w_rect=(hamming(M));
h=hd.* w_rect'; 

[db2,mag,pha,grd,w] = freqz_m(w_rect,1);
[db,mag,pha,grd,w] = freqz_m(h,1);
subplot(1,2,1); stem(n,w_rect, 'r');title('Response');  hold on; axis([-(M-18) M-18 -0.2 1.1]); xlabel('n'); ylabel('w(n)');
subplot(1,2,1); stem( n,h)
subplot(1,2,2); plot(w/pi,db2, 'r');title('Magnitude Response in dB');grid ; xlabel('frequency in pi units'); ylabel('Decibels'); hold on;
subplot(1,2,2); plot(w/pi,db); h = legend('W[¥Ø]','H[¥Ø]',1); set(h,'Interpreter','none')
% (e) Discuss the results

% #3 For input x[n]=cos(pi/6*n)+cos(pi/2*n),
% n=0, 1, ... , 23
M=24;
N=0:M-1;
input_n= cos((pi/6)*N)+cos((pi/2)*N);
% draw X(omega) and Y(omega) where Y(omega) = X(omega)H(omega)
[db1,mag1,pha,grd,w] = freqz_m(input_n,1);
[db2,mag2,pha,grd,w] = freqz_m(hamming(M),1);
mag3 = mag1 .* mag;
figure('name', 'cos((pi/6)*N)+cos((pi/2)*N) & n=0~23', 'Position', [300, 50, 600, 250], 'MenuBar', 'none')
plot(mag3); hold all; plot (mag); plot (mag1); hold off; figure(gcf)
h = legend('Y[¥Ø]','X[¥Ø]','H[¥Ø]',0); set(h,'Interpreter','none'); axis([0 500 0 24]);