Demo entry 6661113

Matlab code

Submitted by anonymous on Nov 20, 2017 at 07:14
Language: Matlab. Code size: 4.8 kB.

```% Project 2 Code

% --- Part i ---

% Calculate ideal impulse response
% Hd(w) = 1 for |w| <= pi/6
hd = ideal_lp(pi/6, 25);

stem(hd);
title('Ideal Impulse Response - Lowpass filter');
xlabel('n'); ylabel('h_d(n)');
figure;

% Hd(w) = 1 for |w| <= pi/6 & pi/3 <= |w| <= pi
hd_band = ideal_lp(pi/6, 25) + ideal_lp(pi, 25) - ideal_lp(pi/3, 25);

stem(hd_band);
title('Ideal Impulse Response - Bandstop filter');
xlabel('n'); ylabel('h_d(n)');
figure;

% --- Part ii ---

% Hd(w) = 1 for |w| <= pi/6
% Calculate window and construct h(n)
% Rectangular window
w = ones(1, 25);
h_rect = hd.*w;
stem(h_rect);
title('Actual Impulse Response - Rectangular window');
xlabel('n'); ylabel('h(n)');
figure;

% Hamming window
w = hamming(25)';
h_hamm = hd.*w;
stem(h_hamm);
title('Actual Impulse Response - Hamming window');
xlabel('n'); ylabel('h(n)');
figure;

% Bartlett window
w = bartlett(25)';
h_bart = hd.*w;
stem(h_bart);
title('Actual Impulse Response - Bartlett window');
xlabel('n'); ylabel('h(n)');
figure;

% Hanning window
w = hann(25)';
h_hann = hd.*w;
stem(h_hann);
title('Actual Impulse Response - Hanning window');
xlabel('n'); ylabel('h(n)');
figure;

% Blackman window
w = blackman(25)';
h_black = hd.*w;
stem(h_black);
title('Actual Impulse Response - Blackman window');
xlabel('n'); ylabel('h(n)');
figure;

% Hd(w) = 1 for |w| <= pi/6 & pi/3 <= |w| <= pi
% Calculate window and construct h(n)
% Rectangular window
w = ones(1, 25);
h_rect_band = hd_band.*w;
stem(h_rect_band);
title('Actual Impulse Response - Rectangular window');
xlabel('n'); ylabel('h(n)');
figure;

% Hamming window
w = hamming(25)';
h_hamm_band = hd_band.*w;
stem(h_hamm_band);
title('Actual Impulse Response - Hamming window');
xlabel('n'); ylabel('h(n)');
figure;

% Bartlett window
w = bartlett(25)';
h_bart_band = hd_band.*w;
stem(h_bart_band);
title('Actual Impulse Response - Bartlett window');
xlabel('n'); ylabel('h(n)');
figure;

% Hanning window
w = hann(25)';
h_hann_band = hd_band.*w;
stem(h_hann_band);
title('Actual Impulse Response - Hanning window');
xlabel('n'); ylabel('h(n)');
figure;

% Blackman window
w = blackman(25)';
h_black_band = hd_band.*w;
stem(h_black_band);
title('Actual Impulse Response - Blackman window');
xlabel('n'); ylabel('h(n)');
figure;

% --- Part iii ---

% Hd(w) = 1 for |w| <= pi/6
% h_d(n)
subplot(3, 2, 1); stem(hd); grid
title('Ideal Impulse Response');
xlabel('n'); ylabel('h_d(n)');

% Rectangular window
[db,~,~,~,w] = freqz_m(h_rect, 1);
subplot(3, 2, 2); plot(w, db); grid
title('Frequency Response Magnitude - Rectangular window');

% Hamming window
[db,~,~,~,w] = freqz_m(h_hamm, 1);
subplot(3, 2, 3); plot(w, db); grid
title('Frequency Response Magnitude - Hamming window');

% Bartlett window
[db,~,~,~,w] = freqz_m(h_bart, 1);
subplot(3, 2, 4); plot(w, db); grid
title('Frequency Response Magnitude - Bartlett window');

% Hanning window
[db,~,~,~,w] = freqz_m(h_hann, 1);
subplot(3, 2, 5); plot(w, db); grid
title('Frequency Response Magnitude - Hanning window');

% Blackman window
[db,~,~,~,w] = freqz_m(h_black, 1);
subplot(3, 2, 6); plot(w, db); grid
title('Frequency Response Magnitude - Blackman window');
figure;

% Hd(w) = 1 for |w| <= pi/6 & pi/3 <= |w| <= pi
% h_d(n)
subplot(3, 2, 1); stem(hd_band); grid
title('Ideal Impulse Response');
xlabel('n'); ylabel('h_d(n)');

% Rectangular window
[db,~,~,~,w] = freqz_m(h_rect_band, 1);
subplot(3, 2, 2); plot(w, db); grid
title('Frequency Response Magnitude - Rectangular window');

% Hamming window
[db,~,~,~,w] = freqz_m(h_hamm_band, 1);
subplot(3, 2, 3); plot(w, db); grid
title('Frequency Response Magnitude - Hamming window');

% Bartlett window
[db,~,~,~,w] = freqz_m(h_bart_band, 1);
subplot(3, 2, 4); plot(w, db); grid
title('Frequency Response Magnitude - Bartlett window');

% Hanning window
[db,~,~,~,w] = freqz_m(h_hann_band, 1);
subplot(3, 2, 5); plot(w, db); grid
title('Frequency Response Magnitude - Hanning window');