Experiment 8

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

EXPERIMENT 8

AIM:
Develop a MATLAB program to create a digital FIR filter using windowing techniques.
Window techniques
1) Hamming Window
2) Kaiser Window
Specifications
passband edge frequency: 1800 Hz,
stopband edge frequency: 2600 Hz
maximum passband ripple: 0.5 dB,
minimum stopband damping: 40 dB,
sampling rate: 8 kHz.
sample sinusoids at frequencies: a. f1=500 Hz b. f2=1800 Hz c. f3=2000 Hz d. f4=3200 H

THEORY:
The FIR filter design consists of two steps;

1) The filter order is determined, then the coefficients.

2) An approximate formula for the filter order N, developed by Kaiser:

Steps to design MATLAB Code:

1) Create sampled sinusoids at different frequencies.

2) Calculate samples for a 4-tone input signal

3) Design FIR Filter using any window.


4) Plot frequency response - both amp and phase response.

5) Apply this filter to the 4-tone test sequence.

IN LAB EXERCISE:
Q1) Design following filters using rectangular window and black man window with given
specifications
a) Low pass (Wp=0.5, ws=0.6, delp=0.1,dels=0.1,Fs=8KHz)
SOLUTION:
%Self Made Function
wp = 0.5;
ws = 0.6;
Wp = (wp+ws)/2;
delp = 0.1;
dels = 0.1;
Fs = 8000;
N = ceil((-20*log10(min(delp,dels)) - 7.95) / (14.36 * (ws- wp) /
(2*pi)));
% Design FIR filters using
custom and built-in window
functions
[b_blackman, a_blackman] =my_fir1(N, Wp, 'blackman');
[b_rect, a_rect] = my_fir1(N,Wp, 'rectangular');
% Plot frequency response
freqz(b_blackman,a_blackman);
hold on;
freqz(b_rect, a_rect);
legend('Blackman Window', 'Rectangular Window');
function [b, a] = my_fir1(N, Wn, window)
% FIR filter design using the window method with custom window
formula
if nargin < 3
window = 'rectangular'; % Default to rectangular window if not
specified
end

n = (0:N)'; % Column vector of indices

% Generate the window coefficients


switch lower(window)
case 'rectangular'
w = ones(N+1, 1); % Rectangular window
case 'blackman'
% Blackman window formula
alpha = 0.16;
a0 = (1 - alpha) / 2;
a1 = 0.5;
a2 = alpha / 2;
w = a0 - a1 * cos(2*pi*n/N) + a2 * cos(4*pi*n/N);
otherwise
error('Unsupported window type');
end

% Compute the filter coefficients


b = fir1(N, Wn, w);
a = 1; % FIR filter, so denominator is always 1
end
%In-built Function
wp = 0.5;
ws = 0.6;
Wp = (wp+ws)/2;
delp = 0.1;
dels = 0.1;
Fs = 8000;
N = ceil((-20*log10(min(delp, dels)) - 7.95) / (14.36 * (ws - wp) /
(2*pi)));
[b,a] = fir1(N, Wp, 'low', blackman(N+1));
[d,c] = fir1(N, Wp, 'low', rectwin(N+1));
figure;
freqz(b,a);
hold on;
freqz(d,c);
legend('Blackman Window', 'Rectangular Window');

(b) High pass ( Wp=0.5, ws=0.6, delp=0.1,dels=0.1, Fs=8KHz)


SOLUTION:
%Self Made Function
wp = 0.5;
ws = 0.6;
Wp = (wp+ws)/2;
delp = 0.1;
dels = 0.1;
Fs = 8000;
N = ceil((-20*log10(min(delp,
dels)) - 7.95) / (14.36 * (ws -
wp) / (2*pi)));
% Design FIR filter using
custom window function
[b_rect, a_rect] = my_fir1(N,
Wp, 'rectangular');
% Plot frequency response
freqz(b_rect, a_rect);
function [b, a] = my_fir1(N, Wn, window)
% FIR filter design using the window method with custom window
formula
if nargin < 3
window = 'rectangular'; % Default to rectangular window if not
specified
end

n = (0:N)'; % Column vector of indices

% Generate the window coefficients


switch lower(window)
case 'rectangular'
w = ones(N+1, 1); % Rectangular window
case 'blackman'
% Blackman window formula
alpha = 0.16;
a0 = (1 - alpha) / 2;
a1 = 0.5;
a2 = alpha / 2;
w = a0 - a1 * cos(2*pi*n/N) + a2 * cos(4*pi*n/N);
otherwise
error('Unsupported window type');
end

% Compute the filter coefficients


b = fir1(N, Wn, w); % Remove 'high' filter type
a = 1; % FIR filter, so denominator is always 1
end
%In-built Function
wp = 0.5;
ws = 0.6;
Wp = (wp+ws)/2;
delp = 0.1;
dels = 0.1;
Fs = 8000;
N = ceil((-20*log10(min(delp, dels)) - 7.95) / (14.36 * (ws - wp) /
(2*pi)));
[b,a] = fir1(N, Wp, 'high');
figure;
freqz(b,a);

c) Band pass (wp1=0.3, wp2=0.6,ws1=0.2, ws2=0.7, Fs=8KHz)


SOLUTION:
%Self Made Function
wp1 = 0.4; % Lower passband
frequency
wp2 = 0.6; % Upper passband
frequency
ws1 = 0.3; % Lower stopband
frequency
ws2 = 0.7; % Upper stopband
frequency
delp = 0.1;
dels = 0.1;
Fs = 8000;
Wp = [wp1 wp2] * (2 / Fs);
Ws = [ws1 ws2] * (2 / Fs);
% Calculate filter order
delta_w = min(abs(Wp - Ws)); %
Transition width
N = ceil((-
20*log10(min(delp,dels)) - 13) /
(14.6 * delta_w));
% Design bandpass filter using custom window function
[b_blackman, a_blackman] = my_fir1(N, [wp1 wp2], 'bandpass',
'blackman');
[b_rect, a_rect] = my_fir1(N, [wp1 wp2], 'bandpass', 'rectangular');
% Plot frequency response
figure;
freqz(b_blackman, a_blackman, 512, Fs);
hold on;
freqz(b_rect, a_rect, 512, Fs);
legend('Blackman Window', 'Rectangular Window');
function [b, a] = my_fir1(N, Wn, type, window)
% FIR filter design using the window method with custom window
formula
if nargin < 4
window = 'rectangular'; % Default to rectangular window if not
specified
end

n = (0:N)'; % Column vector of indices

% Generate the window coefficients


switch lower(window)
case 'rectangular'
w = ones(N+1, 1); % Rectangular window
case 'blackman'
% Blackman window formula
alpha = 0.16;
a0 = (1 - alpha) / 2;
a1 = 0.5;
a2 = alpha / 2;
w = a0 - a1 * cos(2*pi*n/N) + a2 * cos(4*pi*n/N);
otherwise
error('Unsupported window type');
end

% Compute the filter coefficients


b = fir1(N, Wn, type, w);
a = 1; % FIR filter, so denominator is always 1
end
%In Built Function
wp1 = 0.4; % Lower passband frequency
wp2 = 0.6; % Upper passband frequency
ws1 = 0.3; % Lower stopband frequency
ws2 = 0.7; % Upper stopband frequency
delp = 0.1;
dels = 0.1;
Fs = 8000;
Wp = [wp1 wp2] * (2 / Fs);
Ws = [ws1 ws2] * (2 / Fs);
% Calculate filter order
delta_w = min(abs(Wp - Ws)); % Transition width
N = ceil((-20*log10(min(delp,dels)) - 13) / (14.6 * delta_w));
% Design bandpass filter
[b, a] = fir1(N, [wp1 wp2], 'bandpass', blackman(N+1));
[d, c] = fir1(N, [wp1 wp2], 'bandpass', rectwin(N+1));
% Plot frequency response
figure;
freqz(b, a, 512, Fs);
hold on;
freqz(d, c, 512, Fs);
legend('Blackman Window', 'Rectangular Window');

d) Band stop filter(wp1=0.2, wp2=0.7,ws1=0.3, ws2=0.6, Fs=8KHz)


SOLUTION:
%Self Made function
wp1 = 0.2; % Lower passband
frequency
wp2 = 0.7; % Upper passband
frequency
ws1 = 0.3; % Lower stopband
frequency
ws2 = 0.6; % Upper stopband
frequency
delp = 0.1;
dels = 0.1;
Fs = 8000;
Wp = [wp1 wp2] * (2 / Fs);
Ws = [ws1 ws2] * (2 / Fs);
% Calculate filter order
delta_w = min(abs(Wp - Ws)); % Transition width
N = ceil((-20*log10(min(delp,dels)) - 13) / (14.6 * delta_w));
% Design bandstop filter using custom window function
[b_blackman, a_blackman] = my_fir1(N+1, [ws1 ws2], 'stop',
'blackman');
[b_rect, a_rect] = my_fir1(N+1, [ws1 ws2], 'stop', 'rectangular');
% Plot frequency response
figure;
freqz(b_blackman, a_blackman, 512, Fs);
hold on;
freqz(b_rect, a_rect, 512, Fs);
legend('Blackman Window', 'Rectangular Window');
function [b, a] = my_fir1(N, Wn, type, window)
% FIR filter design using the window method with custom window
formula
if nargin < 4
window = 'rectangular'; % Default to rectangular window if not
specified
end

n = (0:N)'; % Column vector of indices

% Generate the window coefficients


switch lower(window)
case 'rectangular'
w = ones(N+1, 1); % Rectangular window
case 'blackman'
% Blackman window formula
alpha = 0.16;
a0 = (1 - alpha) / 2;
a1 = 0.5;
a2 = alpha / 2;
w = a0 - a1 * cos(2*pi*n/N) + a2 * cos(4*pi*n/N);
otherwise
error('Unsupported window type');
end

% Compute the filter coefficients


b = fir1(N, Wn, type, w);
a = 1; % FIR filter, so denominator is always 1
end
%In built function
wp1 = 0.2; % Lower passband frequency
wp2 = 0.7; % Upper passband frequency
ws1 = 0.3; % Lower stopband frequency
ws2 = 0.6; % Upper stopband frequency
delp = 0.1;
dels = 0.1;
Fs = 8000;
Wp = [wp1 wp2] * (2 / Fs);
Ws = [ws1 ws2] * (2 / Fs);
% Calculate filter order
delta_w = min(abs(Wp - Ws)); % Transition width
N = ceil((-20*log10(min(delp,dels)) - 13) / (14.6 * delta_w));
% Design bandstop filter with Blackman window
[b, a] = fir1(N+1, [ws1 ws2], 'stop', blackman(N+2)); % Increase
order and
window length by 1
% Design bandstop filter with Rectangular window
[d, c] = fir1(N+1, [ws1 ws2], 'stop', rectwin(N+2)); % Increase
order and
window length by 1
% Plot frequency response
figure;
freqz(b, a, 512, Fs);
hold on;
freqz(d, c, 512, Fs);
legend('Blackman Window', 'Rectangular Window');

POST LAB EXERCISE:


Q1) Design following filters using Kaiser window and Hamming window with given
specifications
a) Low pass (Wp=0.5, ws=0.6, delp=0.1,dels=0.1, Fs=8KHz)
SOLUTION:
%Self Made function
% Given specifications
Wp = 0.5;
Ws = 0.6;
delp = 0.1;
dels = 0.1;
Fs = 8000; % Hz
% Determine the filter order
N = kaiserord([Wp Ws], [1 0], [delp
dels], Fs);
% Design the filter using Kaiser window
[b_kaiser, a_kaiser] = my_fir1(N, Wp,
'low', 'kaiser', delp);
% Visualize the frequency response
freqz(b_kaiser, a_kaiser, 512, Fs);
function [b, a] = my_fir1(N, Wn, type,
window, params)
% FIR filter design using the window method with custom window
formula
if nargin < 5
params = []; % Initialize params if not provided
end

n = (0:N)'; % Column vector of indices

% Generate the window coefficients


switch lower(window)
case 'kaiser'
if isempty(params)
error('Kaiser window parameters are required.');
end
w = kaiser(N+1, params); % Generate Kaiser window
otherwise
error('Unsupported window type');
end

% Compute the filter coefficients


b = fir1(N, Wn, type, w);
a = 1; % FIR filter, so denominator is always 1
end
%In built function
Wp = 0.5;
Ws = 0.6;
delp = 0.1;
dels = 0.1;
Fs = 8000; % Hz
% Determine the filter order
N = kaiserord([Wp Ws], [1 0], [delp dels], Fs);
% Design the filter using Kaiser window
b = fir1(N, Wp, 'low', kaiser(N+1, delp));
% Visualize the frequency response
freqz(b, 1, 512, Fs);
(b) High pass( Wp=0.5, ws=0.6, delp=0.1,dels=0.1, , Fs=8KHz)
SOLUTION:
% High pass filter specifications
Wp_high = 0.5;
Ws_high = 0.6;
delp_high = 0.1;
dels_high = 0.1;
Fs_high = 8000;
% Calculate filter order
delta_w_high = min(abs(Wp_high - Ws_high)); % Transition width
N_high = ceil((-20*log10(min(delp_high,dels_high)) - 13) / (14.6 *
delta_w_high));
% Increment filter order by 1 for an odd-length symmetric FIR filter
N_high = N_high + 1;
% Kaiser window parameter (beta)
beta = 3.5; % Adjust beta as needed for desired stopband attenuation
% Design high pass filter using Kaiser window
[b_kaiser_high, a_kaiser_high] = my_fir1(N_high, Wp_high, 'high',
'kaiser',
beta);
% Plot frequency response of the Kaiser high-pass filter
figure;
freqz(b_kaiser_high, a_kaiser_high, 512, Fs_high);
title('Frequency Response of Kaiser High-Pass Filter');
xlabel('Frequency (Hz)');
ylabel('Magnitude (dB)');
grid on;
function [b, a] = my_fir1(N, Wn,
type, window, beta)
% FIR filter design using the
window method with custom window
formula
if nargin < 4
window = 'hamming'; % Default
to Hamming window if not specified
end

n = (0:N)'; % Column vector of


indices

% Generate the window


coefficients
switch lower(window)
case 'hamming'
% Hamming window formula
w = 0.54 - 0.46 * cos(2*pi*n/N);
case 'kaiser'
if nargin < 5
error('Kaiser window requires beta parameter.');
end
% Generate Kaiser window with length N+1
w = kaiser(N+1, beta);
otherwise
error('Unsupported window type');
end

% Compute the filter coefficients


b = fir1(N, Wn, type, w);
a = 1; % FIR filter, so denominator is always 1
end

c) Band pass (wp1=0.3, wp2=0.6,ws1=0.2, ws2=0.7, Fs=8KHz)


SOLUTION:
%Self made function
wp1 = 0.3;
wp2 = 0.6;
ws1 = 0.2;
ws2 = 0.7;
% Determine the filter order
N = kaiserord([ws1 wp1 wp2 ws2], [0 1 0], [dels delp dels], Fs);
% Design the filter using Kaiser window
[b_kaiser, a_kaiser] = my_fir1(N, [wp1 wp2], 'bandpass', 'kaiser',
delp);
% Visualize the frequency response
freqz(b_kaiser, a_kaiser, 512, Fs);
function [b, a] = my_fir1(N, Wn,
type, window, params)
% FIR filter design using the
window method with custom window
formula
if nargin < 5
params = []; % Initialize
params if not provided
end

n = (0:N)'; % Column vector of


indices

% Generate the window


coefficients
switch lower(window)
case 'kaiser'
if isempty(params)
error('Kaiser window parameters are required.');
end
w = kaiser(N+1, params); % Generate Kaiser window
otherwise
error('Unsupported window type');
end

% Compute the filter coefficients


b = fir1(N, Wn, type, w);
a = 1; % FIR filter, so denominator is always 1
end
%In built function
wp1 = 0.3;
wp2 = 0.6;
ws1 = 0.2;
ws2 = 0.7;
% Determine the filter order
N = kaiserord([ws1 wp1 wp2 ws2], [0 1 0], [dels delp dels], Fs);
% Design the filter using Kaiser window
b = fir1(N, [wp1 wp2], 'bandpass', kaiser(N+1, delp));
% Visualize the frequency response
freqz(b, 1, 512, Fs);

d) Band stop filter(wp1=0.2, wp2=0.7,ws1=0.3, ws2=0.6, Fs=8KHz)


SOLUTION:
% Band stop filter specifications
wp1_bs = 0.2;
wp2_bs = 0.7;
ws1_bs = 0.3;
ws2_bs = 0.6;
Fs_bs = 8000;
% Calculate filter order
delta_w_bs = min(abs([wp1_bs -
ws1_bs, wp2_bs - ws2_bs])); %
Transition width
N_bs = ceil((-20*log10(0.1) -
13) / (14.6 * delta_w_bs));
% Increment filter order by 1
for an odd-length symmetric FIR
filter
N_bs = N_bs + 1;
% Kaiser window parameter
(beta)
beta_bs = 3.5; % Adjust beta as
needed for desired stopband
attenuation
% Design band stop filter using Kaiser window
[b_kaiser_bs, a_kaiser_bs] = my_fir1(N_bs, [wp1_bs wp2_bs], 'stop',
'kaiser',
beta_bs);
% Plot frequency response of the Kaiser band stop filter
figure;
freqz(b_kaiser_bs, a_kaiser_bs, 512, Fs_bs);
title('Frequency Response of Kaiser Band Stop Filter');
xlabel('Frequency (Hz)');
ylabel('Magnitude (dB)');
grid on;
function [b, a] = my_fir1(N, Wn, type, window, beta)
% FIR filter design using the window method with custom window
formula
if nargin < 4
window = 'hamming'; % Default to Hamming window if not specified
end

n = (0:N)'; % Column vector of indices

% Generate the window coefficients


switch lower(window)
case 'hamming'
% Hamming window formula
w = 0.54 - 0.46 * cos(2*pi*n/N);
case 'kaiser'
if nargin < 5
error('Kaiser window requires beta parameter.');
end
% Generate Kaiser window with length N+1
w = kaiser(N+1, beta);
otherwise
error('Unsupported window type');
end

% Compute the filter coefficients


b = fir1(N, Wn, type, w);
a = 1; % FIR filter, so denominator is always 1
end
RESULT:
Filters were successfully developed using the different windowing techniques and were also
verified using the in-built functions.

You might also like