0% found this document useful (0 votes)
1 views

DSP File

The document is a lab practical file for B.Tech Semester IV students at Jaypee Institute of Information Technology, focusing on Digital Signal Processing. It includes various experiments and exercises related to discrete and continuous-time signals, MATLAB implementations for convolution, DFT, IDFT, z-transforms, and circular convolution. Each experiment is accompanied by objectives, MATLAB code, and post-lab exercises for practical understanding and application of the concepts.

Uploaded by

mkkashif2002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

DSP File

The document is a lab practical file for B.Tech Semester IV students at Jaypee Institute of Information Technology, focusing on Digital Signal Processing. It includes various experiments and exercises related to discrete and continuous-time signals, MATLAB implementations for convolution, DFT, IDFT, z-transforms, and circular convolution. Each experiment is accompanied by objectives, MATLAB code, and post-lab exercises for practical understanding and application of the concepts.

Uploaded by

mkkashif2002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

JAYPEE INSTITUTE OF INFORMATION

TECHNOLOGY, NOIDA
B. TECH SEMESTER IV

LAB Practical File

Digital Signal Processing


[ 15B17EC473 ]

Submitted by:
Name: Aman Verma
Enrollment No.: 9923102024
Batch: E16

Under the Supervision of:


Kapi Dev Tyagi,
Department of Electronics
JIIT Noida, Sector 128
Experiment-2

Objective: Generation of discrete time and continuous-time signal with different operation
on them.

POST LAB EXERCISE

Q1.Generate the waveform of following signals:


For CT signals, t = is -10 to 10 sec
Take appropriate value of sampling interval.

1.​ xa(t) = δ(t + 2)


2.​ xb(t) = δ(t + 2) - δ(t -2)
3.​ xc(t) = t u(t)
4.​ xd(t) = u (t) – u(t - 5)
5.​ xe(t) = r (t +1) – r (t) + r (t - 2)
6.​ xf(t) = exp(-t) sin(2πt) + exp(-t/2) cos(5 πt)

clc;
clear all;
close all;
t = -10:0.1:10;
xa = (t == -2);
x1 = (t == 2);
xb = xa - x1;
figure;
subplot(2,3,1)
plot(t, xa);
title('9923102033 xa(t)')
xlabel('Time')
ylabel('Amplitude');
subplot(2,3,2)
plot(t, xb);
title('9923102033 xb(t)')
xlabel('Time')
ylabel('Amplitude');
u = t >= 0;
xc = t .* u;
subplot(2,3,3);
plot(t, xc)
title('9923102033 xc(t)')
xlabel('Time')
ylabel('Amplitude');
u1 = (t >= 5);
xd = u - u1;
subplot(2,3,4);
plot(t, xd);
title('9923102033 xd(t)')
xlabel('Time')
ylabel('Amplitude');
r1 = t .* (t >= -1);
r2 = xc;
r3 = t .* (t >= 2);
xe = r1 - r2 + r3;
subplot(2,3,5);
plot(t, xe);
title('9923102033 xe(t)')
xlabel('Time')
ylabel('Amplitude');
t1 = -10:1:5;
xf = exp(-t1).*sin(2*pi*t1) + exp(-t1/2).*cos(5*pi*t1);
subplot(2,3,6);
plot(t1, xf);
title('9923102033 xf(t)')
xlabel('Time')
ylabel('Amplitude');
Q2. For DT signals, n = -5 to 5 samples
1.​ xa[n] = r[-n]
2.​ xb[n] = exp(n-1)
3.​ xc[n] = [n - 1] δ[n - 2]
4.​ xd[n] = u [n + 1] – 2u[ n] + u [n-1]
5.​ xe[n] = r [n + 2] – r [n - 2]
6.​ xf [n] = an cos(2 πn)

clc;
clear all;
close all;
n = -5:5;
ramp = n .* (n >= 0);
figure;
subplot(2,3,1)
stem(-n, ramp);
title('9923102033 xa(n)')
xlabel('Time')
ylabel('Amplitude');
xb = exp(n - 1);
subplot(2,3,2)
stem(n, xb);
title('9923102033 xb(n)')
xlabel('Time')
ylabel('Amplitude');
d = (n == 2);
xc = (n - 1) .* d;
subplot(2,3,3);
stem(n, xc)
title('9923102033 xc(n)')
xlabel('Time')
ylabel('Amplitude');
u1 = (n >= -1);
u2 = (n >= 0);
u3 = (n >= 1);
xd = u1 - (2 .* u2) + u3;
subplot(2,3,4);
stem(n, xd);
title('9923102033 xd(n)')
xlabel('Time')
ylabel('Amplitude');
xe = (n .* (n >= -2)) - (n .* (n >= 2));
subplot(2,3,5);
stem(n, xe);
title('9923102033 xe(n)')
xlabel('Time')
ylabel('Amplitude');
a = 0.5;
xf = (a .^ n) .* (cos(2 * pi * n));
subplot(2,3,6);
stem(n, xf);
title('9923102033 xf(n)')
xlabel('Time')
ylabel('Amplitude');

Q3. Sketch the waveform:

b. Using the result of part (a), compute the derivative of v(t), and sketch its waveform.

clc;
clear all;
close all;
syms t;
% Define the piecewise function
x = piecewise(t > 0 & t <=2, 20*exp(-2*t), ...
t > 2 & t <= 3, 10*(t-3), ...
t > 3 & t <= 5, -10*(t-5), ...
t > 5 & t <= 7, 10*(t-7));

% Compute the derivative of the piecewise function


dx = diff(x, t);
% Define the time values
t_vals = 0:0.01:7;
% Evaluate the original function and its derivative at the time values
x_vals = double(subs(x, t, t_vals));
dx_vals = double(subs(dx, t, t_vals(1:end-1))); % Exclude the last point for
derivative
% Plot the original waveform
subplot(2, 1, 1);
plot(t_vals, x_vals);
xlabel('Time (t)');
ylabel('x(t)');
title('Original v(t) 9923102033');
grid on;
% Plot the derivative of the waveform
subplot(2, 1, 2);
plot(t_vals(1:end-1), dx_vals);
xlabel('Time (t)');
ylabel('dx(t)/dt');
title('Derivative of v(t) 9923102033');
grid on;
Q4.
Sketch the waveform:

clc;
clear all;
close all;
syms n;
x = piecewise(n <= 0, 5, ...
n > 0 & n <= 4, 5 - 3*n, ...
n > 4 & n <= 8, -23 + n*n, ...
n > 8, 41);
n1 = -2:10;
x1 = double(subs(x, n, n1));
stem(n1, x1, 'filled');
xlabel('Time (n)');
ylabel('x[n]');
title('9923102033 g(n) Signal');
grid on;
Experiment-3

Objective: Write your own MATLAB function to implement linear convolution as an


operation to analyze discrete time LTI system.

Post-Lab Exercise

Consider two LTI systems with the unit sample responses h1[n]= u[n − 5] .
Let x[n] = δ[n].

a. Compute y[n] by first computing w[n] = x[n]* h1[n] and then computing
y[n] = w[n]* h2[n] ; that is y[n ] = [ x[n ] * h1 [n ]]*h2[n ].

b. Now, find y[n] by first convolving h1[n] and h2[n] to obtain g[n] = h1[n]* h2[n]and then
convolving x[n] with g[n] to obtain y[n] = x[n]*[h1[n]* h2[n]].

clc;
clear all;
close all;
n = 0:10;
u = double(n >= 0);
h1 = (-0.5).^n .* u;
h2 = u - double(n >=5);
x = double(n==0);
w = conv(x, h1);
y1 = conv(w, h2);
y1_n = 0:length(y1)-1;
g = conv(h1, h2);
y2 = conv(g,x);
y2_n = 0:length(y2)-1;
subplot(2,1,1)
stem(y1_n, y1);
xlabel("n")
ylabel("y[n]")
title("9923102033 y[n] by y[n] = w[n] * h2[n]")
subplot(2,1,2)
stem(y2_n, y2);
xlabel("n")
ylabel("y[n]")
title(" 9923102033 y[n] by y[n] = g[n] * x[n]")
Experiment-4

Objective: Write your own MATLAB function to compute DFT (Discrete Fourier
Transform) and IDFT (Inverse Discrete Fourier Transform) for the spectral analysis
of signals.

Post Lab Exercise:

Q 1. Find the DFT-transform of the following signals x(n)={1,2,3,2}

clc;
clear all;
close all;
% Define the input signal
% isko change kia h tut ke questions ke liye
x = [1, 1, 0, 0];
% Compute the DFT
X = fft(x);
% Display the result
disp('9923102033 DFT of x(n):');
disp(X);
Q 2. Find the IDFT of X(K)={4,2,0,4}

% Define the input DFT


X = [4, 2, 0, 4];
% Compute the IDFT
x = ifft(X);
% Display the result
disp('9923102033 IDFT of X(k):');
disp(x);

Q3. Find the DFT-transform of the following signals x(n)={1,1,0,0,0,0,0,0}

x = [1, 1, 0, 0, 0, 0, 0, 0];
x_k = fft(x);
disp("9923102033 FFT of x(n) is: ")
disp(x_k)

Q 4. Find the IDFT of X(K)={1,0,1,0}

x = [6, -1+j, 0, -1-j];


x_n = ifft(x);
disp(" 9923102033 IDFT of X(k) is : ")
disp(x_n)
Experiment - 5

Objective: Compute z- transform and inverse z transform of a discrete time signals and
systems. Plot pole-zero map of the same using symbolic tool box.

Post Lab Exercise

Q1. Determine whether or not all the roots of the following polynomial are inside the
unit circle:

% Coefficients of the polynomial


coeffs = [1, 0.6, 0.5, 0.4, 0.3, 0.2];
% Calculate the roots of the polynomial
roots_poly = roots(coeffs);
% Check if all roots are within the unit circle
if all(abs(roots_poly) < 1)
disp('All roots are inside the unit circle.');
else
disp('Not all roots are inside the unit circle.');
end

Q2.
Find the residues of the following polynomial

% Define the function


syms z;
x = z^3 / ((z - 0.5)*(z - 0.75)*(z - 1));
% Define the poles
poles = [0.5, 0.75, 1];
% Calculate residues at each pole
residues = zeros(size(poles));
for i = 1:length(poles)
residue = limit((z - poles(i)) * x, z, poles(i)); % Residue formula
residues(i) = residue;
end
disp('Residues at each pole:');
disp(residues);
Experiment-6

Objective: Write your own MATLAB function “mycirconv‟ to compute circular convolution
of two sequences.

In lab Exercise

Write your own MATLAB function „mycirconv‟ to compute circular convolution of two
sequences.

Define the custom circular convolution function


function y = mycirconv(x, h)

L = max(length(x), length(h));
x = [x, zeros(1, L - length(x))];
h = [h, zeros(1, L - length(h))];
y = zeros(1, L);
for n = 1:L
for m = 1:L
k = mod(n - m, L) + 1;
y(n) = y(n) + x(m) * h(k);
end
end
end

Post lab Exercise

Q 1. Find circular convolution of the following signals x(n)={1,2,1,2},


h(n)={4,3,2,1}
Q 2. Find circular convolution of the following signals x(n)={1,2,0,1},
h(n)={2,2,1,1}
Q3. Find circular convolution of the following signals x(n)={1,-1,1,1},
h(n)={1,2,3,4}
Q4. Find circular convolution of the following signals x(n)={1,3,4,5},
h(n)={2,1,4,1}

disp('Q1: Circular convolution for x(n) = [1, 2, 1, 2] and h(n) = [4, 3, 2,


1]');
y1 = mycirconv([1, 2, 1, 2], [4, 3, 2, 1]);
disp(y1);
disp('Q2: Circular convolution for x(n) = [1, 2, 0, 1] and h(n) = [2, 2, 1,
1]');
y2 = mycirconv([1, 2, 0, 1], [2, 2, 1, 1]);
disp(y2);
disp('Q3: Circular convolution for x(n) = [1, -1, 1, 1] and h(n) = [1, 2, 3,
4]');
y3 = mycirconv([1, -1, 1, 1], [1, 2, 3, 4]);
disp(y3);
disp('Q4: Circular convolution for x(n) = [1, 3, 4, 5] and h(n) = [2, 1, 4,
1]');
y4 = mycirconv([1, 3, 4, 5], [2, 1, 4, 1]);
disp(y4);

Q 5. Find linear convolution of the following signals using circular convolution


x(n)={1,2,4}, h(n)={2,5}
Q 6. Find linear convolution of the following signals using circular convolution x(n)={5,3},
h(n)={2,4,6}

function y = mycirconv(x, h)
L = max(length(x), length(h));
x = [x, zeros(1, L - length(x))];
h = [h, zeros(1, L - length(h))];
y = zeros(1, L);
for n = 1:L
for m = 1:L

k = mod(n - m, L) + 1;
y(n) = y(n) + x(m) * h(k);
end
end
end
disp('Q5: Linear convolution for x(n) = [1, 2, 4] and h(n) = [2, 5] using
circular convolution');
x1 = [1, 2, 4];
h1 = [2, 5];
N1 = length(x1) + length(h1) - 1;
x1_padded = [x1, zeros(1, N1 - length(x1))];
h1_padded = [h1, zeros(1, N1 - length(h1))];
y5 = mycirconv(x1_padded, h1_padded);
disp(y5);
disp('Q6: Linear convolution for x(n) = [5, 3] and h(n) = [2, 4, 6] using
circular convolution');
x2 = [5, 3];
h2 = [2, 4, 6];
N2 = length(x2) + length(h2) - 1;
x2_padded = [x2, zeros(1, N2 - length(x2))];
h2_padded = [h2, zeros(1, N2 - length(h2))];
y6 = mycirconv(x2_padded, h2_padded);
disp(y6);

Experiment-7

Objective: Computation of DFT using FFT algorithms


Objective: Develop radix-2 butterfly FFT (Decimation in Time) algorithm for the computation
of N-point DFT

Post lab Exercise

Fs = 8000;
N = 240;
t = (0:N-1) / Fs;
x1 = 5 * cos(2 * pi * 500 * t);
x2 = 5 * cos(2 * pi * 1200 * t + 0.25 * pi);
x3 = 5 * cos(2 * pi * 1800 * t + 0.5 * pi);
x = x1 + x2 + x3;
figure;
plot(t, x);
title('Sum of Sinusoids');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
figure;
subplot(3,1,1);
plot(t, x1);
title('Sinusoid x1(t)');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
subplot(3,1,2);
plot(t, x2);
title('Sinusoid x2(t)');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
subplot(3,1,3);
plot(t, x3);
title('Sinusoid x3(t)');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;

2. Write a program to compute and plot the amplitude spectrum of the signal
data points you have determined based on FFT. Comment on your results.
with the number of x(n)

% Parameters
Fs = 8000;
N = 240;
t = (0:N-1) / Fs;
% Define the signal x(t)
x1 = 5 * cos(2 * pi * 500 * t);
x2 = 5 * cos(2 * pi * 1200 * t + 0.25 * pi);
x3 = 5 * cos(2 * pi * 1800 * t + 0.5 * pi);
x = x1 + x2 + x3;
% Compute FFT
X_fft = fft(x);
X_magnitude = abs(X_fft) / N;
f = (0:N-1) * (Fs / N);
% Plot amplitude spectrum
figure;
stem(f, X_magnitude, 'filled');
title('Amplitude Spectrum of x(t)');
xlabel('Frequency (Hz)');
ylabel('Amplitude');
grid on;
X_half = X_magnitude(1:N/2+1);
f_half = f(1:N/2+1);
% Plot positive amplitude spectrum
figure;
stem(f_half, X_half, 'filled');
title('Positive Amplitude Spectrum of x(t)');
xlabel('Frequency (Hz)');
ylabel('Amplitude');
grid on;
Experiment-8

Objective: Write MATLAB program to design digital FIR filter employing windowing
technique.
Window techniques
1) Hamming Window
2) Kaiser Window

clc;
clear;
close all;

%% 1. Specifications
Fs = 8000; % Sampling rate in Hz
f_pass = 1800; % Passband edge frequency in Hz
f_stop = 2600; % Stopband edge frequency in Hz
Rp = 0.5; % Passband ripple in dB
As = 40; % Minimum stopband attenuation in dB
%% 2. Normalized frequencies
wp = f_pass / (Fs/2); % Normalized passband edge (0 to 1)
ws = f_stop / (Fs/2); % Normalized stopband edge (0 to 1)
wn = (f_pass + f_stop) / (2 * (Fs/2)); % Cutoff frequency for FIR design
%% 3. Convert ripple values to linear scale
delta_p = 1 - 10^(-Rp/20); % Linear passband ripple
delta_s = 10^(-As/20); % Linear stopband ripple
delta = min(delta_p, delta_s); % Use min for formula
%% 4. Kaiser formula for filter order (from image)
d_omega = ws - wp;
N_kaiser = ceil( (-20*log10(delta) - 7.95) / (14.36 * d_omega / (2*pi)) );
% Make filter order odd (for symmetry, linear phase)
if mod(N_kaiser, 2) == 0
N_kaiser = N_kaiser + 1;
end
%% 5. Design FIR Filters using Hamming and Kaiser windows
b_hamming = fir1(N_kaiser - 1, wn, hamming(N_kaiser));
% Kaiser beta calculation based on attenuation
if As > 50
beta = 0.1102 * (As - 8.7);
elseif As >= 21
beta = 0.5842 * (As - 21)^0.4 + 0.07886 * (As - 21);
else
beta = 0;
end
b_kaiser = fir1(N_kaiser - 1, wn, kaiser(N_kaiser, beta));
%% 6. Frequency response plots
figure;
freqz(b_hamming, 1, 1024, Fs);
title('Frequency Response - Hamming Window');
figure;
freqz(b_kaiser, 1, 1024, Fs);
title('Frequency Response - Kaiser Window');
%% 7. Generate 4-tone input signal
t = 0:1/Fs:0.01; % Short time vector for clarity
f1 = 500;
f2 = 1800;
f3 = 2000;
f4 = 3200;
x = sin(2*pi*f1*t) + sin(2*pi*f2*t) + sin(2*pi*f3*t) + sin(2*pi*f4*t);
%% 8. Filter the input signal
y_hamming = filter(b_hamming, 1, x);
y_kaiser = filter(b_kaiser, 1, x);
%% 9. Time-domain plots
figure;
subplot(3,1,1);
plot(t, x);
title('Original 4-Tone Signal');
xlabel('Time (s)'); ylabel('Amplitude');
subplot(3,1,2);
plot(t, y_hamming);
title('Filtered Signal - Hamming Window');
xlabel('Time (s)'); ylabel('Amplitude');
subplot(3,1,3);
plot(t, y_kaiser);
title('Filtered Signal - Kaiser Window');
xlabel('Time (s)'); ylabel('Amplitude');
%% 10. Frequency-domain comparison
NFFT = 1024;
f = linspace(0, Fs, NFFT);
X = abs(fft(x, NFFT));
Yh = abs(fft(y_hamming, NFFT));
Yk = abs(fft(y_kaiser, NFFT));

figure;
plot(f, X, '--k', f, Yh, 'b', 'LineWidth', 1.2);
legend('Original', 'Filtered - Hamming');
title('Frequency Spectrum (Hamming)');
xlabel('Frequency (Hz)'); ylabel('Magnitude');
figure;
plot(f, X, '--k', f, Yk, 'r', 'LineWidth', 1.2);
legend('Original', 'Filtered - Kaiser');
title('Frequency Spectrum (Kaiser)');
xlabel('Frequency (Hz)'); ylabel('Magnitude');
Experiment-9

Objective: Write MATLAB program to design IIR digital filter for a given specification using
bilinear transformation and impulse invariant method.

EXERCISE:

1) Using MATLAB determine the lowest order of a digital IIR highpass filter of all
four types. The specifications are as follows: sampling rate of 3,500 Hz,
passband edge frequency of 1,050 Hz, stopband edge frequency of 600 Hz, passband
ripple of 1 dB, and a minimum stopband attenuation of 50 dB. Plot the frequency
response of the above IIR filter for bilinear and impulse invariant transforms. Comment
on your results.

clc;
clear;
close all;
% Specifications
Fs = 3500;
fp = 1050;
fs = 600;
Rp = 1; % dB
Rs = 50; % dB
% Normalize the frequencies (digital freq. from 0 to 1)
wp = 2*fp/Fs;
ws = 2*fs/Fs;
% Pre-warping for bilinear transform
Wp_warp = tan(pi*wp/2);
Ws_warp = tan(pi*ws/2);
% ================================
% Bilinear Transform-Based Filters
% ================================
fprintf('\n--- Bilinear Transform Filters ---\n');
% Butterworth
[N_butt, Wn_butt] = buttord(wp, ws, Rp, Rs);
[b_butt, a_butt] = butter(N_butt, Wn_butt, 'high');
% Chebyshev Type I
[N_cheb1, Wn_cheb1] = cheb1ord(wp, ws, Rp, Rs);
[b_cheb1, a_cheb1] = cheby1(N_cheb1, Rp, Wn_cheb1, 'high');
% Chebyshev Type II
[N_cheb2, Wn_cheb2] = cheb2ord(wp, ws, Rp, Rs);
[b_cheb2, a_cheb2] = cheby2(N_cheb2, Rs, Wn_cheb2, 'high');
% Elliptic
[N_ellip, Wn_ellip] = ellipord(wp, ws, Rp, Rs);
[b_ellip, a_ellip] = ellip(N_ellip, Rp, Rs, Wn_ellip, 'high');
% Display orders
fprintf('Butterworth Order: %d\n', N_butt);
fprintf('Chebyshev I Order: %d\n', N_cheb1);
fprintf('Chebyshev II Order: %d\n', N_cheb2);
fprintf('Elliptic Order: %d\n', N_ellip);
% Plot frequency responses
figure;
freqz(b_butt, a_butt, 1024, Fs); title('Butterworth (Bilinear)');
figure;
freqz(b_cheb1, a_cheb1, 1024, Fs); title('Chebyshev I (Bilinear)');
figure;
freqz(b_cheb2, a_cheb2, 1024, Fs); title('Chebyshev II (Bilinear)');
figure;
freqz(b_ellip, a_ellip, 1024, Fs); title('Elliptic (Bilinear)');
% ================================
% Impulse Invariant Transform (IIT)
% ================================
fprintf('\n--- Impulse Invariant Transform Filters ---\n');
% Analog specs for IIT (in rad/sec)
Wp_a = 2*pi*fp;
Ws_a = 2*pi*fs;
% Butterworth
[N_a_butt, Wn_a_butt] = buttord(Wp_a, Ws_a, Rp, Rs, 's');
[ba_butt, aa_butt] = butter(N_a_butt, Wn_a_butt, 'high', 's');
[bd_butt, ad_butt] = impinvar(ba_butt, aa_butt, Fs);
figure;
freqz(bd_butt, ad_butt, 1024, Fs); title('Butterworth (Impulse Invariant)');
% Note: Chebyshev and elliptic designs may not work well with IIT
% due to aliasing and complexity of analog prototypes.

Comments on Results:

1.​ Elliptic filter gives the lowest order due to its equiripple design in both passband
and stopband.
2.​ Butterworth has the highest order because it has a maximally flat response and no
ripples.
3.​ Impulse Invariant Transform may suffer from aliasing and is not ideal for highpass
filters—best suited for lowpass.
4.​ Bilinear Transform avoids aliasing by frequency warping and is the preferred
method for digital IIR filter design.
2) Using MATLAB determine the lowest order of a digital IIR bandstop filter of all
four types. The specifications are as follows: sampling rate of 12 kHz, passband edge
frequencies at 2.1 kHz and 4.5 kHz, stopband edge frequencies at 2.7 kHz and 3.9
kHz, passband ripple of 0.6 dB, and a minimum stopband attenuation of 45 dB.
Plot the frequency response of the above IIR filter for bilinear and impulse invariant
transforms. Comment on your results.

clc;
clear;
close all;
% Specifications
Fs = 12000; % Sampling frequency
fp = [2100 4500]; % Passband edge frequencies (Hz)
fsb = [2700 3900]; % Stopband edge frequencies (Hz)
Rp = 0.6; % Passband ripple (dB)
Rs = 45; % Stopband attenuation (dB)
% Normalize frequencies for digital design (0 to 1 scale)
wp = fp / (Fs/2);
ws = fsb / (Fs/2);
%% ========================
% Bilinear Transform Design
%% ========================
fprintf('\n--- Bilinear Transform Filters ---\n');
% Butterworth
[N_butt, Wn_butt] = buttord(wp, ws, Rp, Rs);
[b_butt, a_butt] = butter(N_butt, Wn_butt, 'stop');
% Chebyshev I
[N_cheb1, Wn_cheb1] = cheb1ord(wp, ws, Rp, Rs);
[b_cheb1, a_cheb1] = cheby1(N_cheb1, Rp, Wn_cheb1, 'stop');
% Chebyshev II
[N_cheb2, Wn_cheb2] = cheb2ord(wp, ws, Rp, Rs);
[b_cheb2, a_cheb2] = cheby2(N_cheb2, Rs, Wn_cheb2, 'stop');
% Elliptic
[N_ellip, Wn_ellip] = ellipord(wp, ws, Rp, Rs);
[b_ellip, a_ellip] = ellip(N_ellip, Rp, Rs, Wn_ellip, 'stop');
% Display Orders
fprintf('Butterworth Order: %d\n', N_butt);
fprintf('Chebyshev I Order: %d\n', N_cheb1);
fprintf('Chebyshev II Order: %d\n', N_cheb2);
fprintf('Elliptic Order: %d\n', N_ellip);
% Plot Frequency Responses
figure;
freqz(b_butt, a_butt, 1024, Fs); title('Butterworth (Bilinear)');
figure;
freqz(b_cheb1, a_cheb1, 1024, Fs); title('Chebyshev I (Bilinear)');
figure;
freqz(b_cheb2, a_cheb2, 1024, Fs); title('Chebyshev II (Bilinear)');
figure;
freqz(b_ellip, a_ellip, 1024, Fs); title('Elliptic (Bilinear)');
%% ================================
% Impulse Invariant Transform (IIT)
%% ================================
fprintf('\n--- Impulse Invariant Transform (Butterworth Only) ---\n');
% Convert edge frequencies to analog (rad/s)
Wp_analog = 2*pi*fp;
Ws_analog = 2*pi*fsb;
% Butterworth analog design
[N_a, Wn_a] = buttord(Wp_analog, Ws_analog, Rp, Rs, 's');
[ba, aa] = butter(N_a, Wn_a, 'stop', 's');
% Apply impulse invariant transformation
[bd_iit, ad_iit] = impinvar(ba, aa, Fs);
figure;
freqz(bd_iit, ad_iit, 1024, Fs);
title('Butterworth (Impulse Invariant)');

Comments on Results
Filter Type Characteristics Order

Butterworth Smooth response, no ripple, large order Highest

Chebyshev I Ripple in passband, lower order Lower

Chebyshev II Ripple in stopband, better stopband Lower


control

Elliptic Ripple in both bands, lowest order Smallest

Impulse Invariant Causes aliasing (not ideal for bandstop) Used for
comparison
Experiment 10

Objectives: Write MATLAB program for realization of digital IIR filter using direct form-I & II,
cascade and parallel method.

Part A: FIR Filter Implementation


FIR system coefficients are given
below Numerator coefficients: b = [0.2, 0.3, 0.5, .2];
1. Write a MATLAB program to implement the filter using the direct-form I to replace the
MATLAB routine “filter()”.
2. Generate the sum of the sinusoids for duration of one second achieved from:
x(t) 5 cos[2 (500)t] 5 cos[2 (1200)t 0.25 ] 5 cos[2 (1800)t 0.5 ]
using the sampling rate of 8 kHz.
3. Perform filtering, plot. Compare the output with routine “ filter( ) ”.

Part B: IIR Filter Implementation Using Direct-form I


IIR system coefficients are given
below:
Numerator coefficients: bBP = [0.0201
0.0000
Denominator coefficients: aBP = [1.0000 -2.1192
0.6414 ]
-0.0402 0.0000 0.0201 ]
2.6952 -1.6924
1. Write a MATLAB program to implement the filter using the direct form I to replace the
MATLAB routine “filter( ) ”.
2. Perform filtering for the input generated in Part A (2), plot,
Part C: IIR Filter Implementation Using Direct-form II
IIR system 2:
Numerator coefficients: bHP =[ 0.0938 -0.3753
0.5630 -0.3753
0.0938 ]
Denominator coefficients: aHP =[ 1.0000
0.0784 0.8267 0.2597 0.1960 ]
1. Write a MATLAB program to implement the filter using the direct-form II to replace the
MATLAB routine “filter( ) ”.

2. Perform filtering the input generated in Part A (2),


Organize your plots and program list.

% filter_realization_all.m
% FIR and IIR Filter Realization (Direct Form-I & II) without using filter()
clear; clc; close all;
%% Input Signal Generation
Fs = 8000; % Sampling frequency
t = 0:1/Fs:1-1/Fs; % 1 second duration
x = 5*cos(2*pi*500*t) + ...
5*cos(2*pi*1200*t + 0.25) + ...
5*cos(2*pi*1800*t + 0.5);
%% === PART A: FIR Filter using Direct Form-I ===
b_fir = [0.2, 0.3, 0.5, 0.2];
% FIR Direct Form I Implementation
y_fir_custom = fir_direct_form_I(b_fir, x);
y_fir_builtin = filter(b_fir, 1, x);
% Plot
figure;
subplot(2,1,1); plot(y_fir_custom); title('FIR Direct Form-I (Custom)');
subplot(2,1,2); plot(y_fir_builtin); title('FIR Output using filter()');
%% === PART B: IIR Filter using Direct Form-I ===
bBP = [0.0201 0 -0.0402 0 0.0201];
aBP = [1 -2.1192 2.6952 -1.6924 0.6414];
y_iir_df1_custom = iir_direct_form_I(bBP, aBP, x);
y_iir_df1_builtin = filter(bBP, aBP, x);
% Plot
figure;
subplot(2,1,1); plot(y_iir_df1_custom); title('IIR Direct Form-I (Custom)');
subplot(2,1,2); plot(y_iir_df1_builtin); title('IIR Output using filter()');
%% === PART C: IIR Filter using Direct Form-II ===
bHP = [0.0938 -0.3753 0.5630 -0.3753 0.0938];
aHP = [1.0000 0.0784 0.8267 0.2597 0.1960];
y_iir_df2_custom = iir_direct_form_II(bHP, aHP, x);
y_iir_df2_builtin = filter(bHP, aHP, x);
% Plot
figure;
subplot(2,1,1); plot(y_iir_df2_custom); title('IIR Direct Form-II (Custom)');
subplot(2,1,2); plot(y_iir_df2_builtin); title('IIR Output using filter()');
%% ========== Function Definitions ==========
function y = fir_direct_form_I(b, x)
N = length(b);
x = [zeros(1, N-1), x]; % Pad input
y = zeros(1, length(x) - N + 1);
for n = N:length(x)
for k = 1:N
y(n - N + 1) = y(n - N + 1) + b(k) * x(n - k + 1);
end
end
end
function y = iir_direct_form_I(b, a, x)
N = length(b);
M = length(a);
x = [zeros(1, N-1), x];
y = zeros(1, length(x));
for n = max(N, M):length(x)
for k = 1:N
y(n) = y(n) + b(k) * x(n - k + 1);
end
for k = 2:M
y(n) = y(n) - a(k) * y(n - k + 1);
end
end
y = y(max(N, M):end); % remove padding
end
function y = iir_direct_form_II(b, a, x)
N = max(length(b), length(a));
x = [x zeros(1, N-1)];
y = zeros(1, length(x));
w = zeros(1, N);
for n = 1:length(x)
w(1) = x(n) - sum(a(2:end) .* w(2:end));
y(n) = sum(b .* w);
w(2:end) = w(1:end-1);
end
end

You might also like