You are on page 1of 10

JOMO KENYATTA UNIVERSITY

OF AGRICULTURE AND TECHNOLOGY


Academic Year 2023/2024

FOURTH YEAR 1ST SEMESTER LABWORK FOR THE DEGREE OF


BACHELOR OF SCIENCE IN ELECTRONICS AND COMPUTER ENGINEERING
EEE 2405: ANALOGUE FILTERS

MATLAB LAB WORK


DATE: 1ST AUGUST 2023

MEMBERS:
1. KELVIN NGATIA ENE212-0064/2019
2. LENNY KIBE ENE212-0066/2019
3. BRIAN LEMAYIAN ENE212-0076/2019
4. DAVID KINYANJUI ENE212-0080/2019
1. LAB 1
QUESTION 1
Matlab code
% Filter specifications
Wc = 40000; % Passband corner frequency in rad/s
Ws = 56000; % Stopband corner frequency in rad/s
Amax = 0.28029; % Maximum passband ripple in dB
Amin = 40; % Minimum stopband attenuation in dB

% Calculate the filter order


N = cheb1ord(Wc, Ws, Amax, Amin, 's');

% Design the Chebyshev Type I filter


[b, a] = cheby1(N, Amax, Wc, 's');

% Calculate poles and zeros of the filter


[Z, P, K] = tf2zp(b, a);

% Calculate Q-factor of the poles


Q = -abs(P)./(2*real(P));

% Frequency response analysis


omega = linspace(0, 1e5, 1000);
H = freqs(b, a, omega);

% Convert magnitude response to attenuation in dB


Att = 20*log10(abs(H));

% Calculate group delay of the filter


Tg = grpdelay(b, a, omega);

% Plot the magnitude response and group delay


lw = 2;
fs = 16;
fn = 'times';
figure;
plot(omega, Att);
grid on;
xlabel('Frequency (rad/s)');
ylabel('Attenuation (dB)');
title('Chebyshev Type I Filter Magnitude Response');
ylim([-Amin-10, Amax+5]);
set(gca, 'FontName', fn, 'FontSize', fs);
xticks([0:10000:100000]);

figure;
plot(omega, Tg);
grid on;
xlabel('Frequency (rad/s)');
ylabel('Group Delay');
title('Chebyshev Type I Filter Group Delay');
set(gca, 'FontName', fn, 'FontSize', fs);
xticks([0:10000:100000]);

% Plot the pole-zero locations


figure;
zplane(Z, P);
title('Pole-Zero Plot');
set(gca, 'FontName', fn, 'FontSize', fs);

output:

Verification:
QUESTION 2 – 6 marks
Code:
% Filter specifications
Amax = 0.01; % Maximum passband ripple in dB
Amin = 40; % Minimum stopband attenuation in dB
Wp = 2; % Passband corner frequency in rad/s
Ws = 3; % Stopband corner frequency in rad/s

% Filter design and frequency response analysis


[n_butter, Wn_butter] = buttord(Wp, Ws, Amax, Amin, 's');
[n_cheby1, Wp_cheby1] = cheb1ord(Wp, Ws, Amax, Amin, 's');
[n_cheby2, Wp_cheby2] = cheb2ord(Wp, Ws, Amax, Amin, 's');
[n_ellip, Wp_ellip] = ellipord(Wp, Ws, Amax, Amin, 's');

[b_butter, a_butter] = butter(n_butter, Wn_butter, 's');


[b_cheby1, a_cheby1] = cheby1(n_cheby1, Amax, Wp_cheby1, 's');
[b_cheby2, a_cheby2] = cheby2(n_cheby2, Amin, Wp_cheby2, 's');
[b_ellip, a_ellip] = ellip(n_ellip, Amax, Amin, Wp_ellip, 's');

omega = logspace(-1, 2, 1000);


[mag_butter, phase_butter] = freqs(b_butter, a_butter, omega);
[mag_cheby1, phase_cheby1] = freqs(b_cheby1, a_cheby1, omega);
[mag_cheby2, phase_cheby2] = freqs(b_cheby2, a_cheby2, omega);
[mag_ellip, phase_ellip] = freqs(b_ellip, a_ellip, omega);

mag_butter = 20*log10(abs(mag_butter));
mag_cheby1 = 20*log10(abs(mag_cheby1));
mag_cheby2 = 20*log10(abs(mag_cheby2));
mag_ellip = 20*log10(abs(mag_ellip));
phase_butter = rad2deg(phase_butter);
phase_cheby1 = rad2deg(phase_cheby1);
phase_cheby2 = rad2deg(phase_cheby2);
phase_ellip = rad2deg(phase_ellip);
% Plot magnitude responses on a logarithmic scale
lw = 2;
fs = 16;
fn = 'times';

figure;
semilogx(omega, mag_butter, 'b', 'LineWidth', lw);
hold on;
semilogx(omega, mag_cheby1, 'r', 'LineWidth', lw);
semilogx(omega, mag_cheby2, 'g', 'LineWidth', lw);
semilogx(omega, mag_ellip, 'm', 'LineWidth', lw);
grid on;
xlabel('Frequency (rad/s)');
ylabel('Magnitude (dB)');
title('Comparison of LP Filters: Butterworth, Chebyshev I, Chebyshev II, and Elliptic');
legend('Butterworth', 'Chebyshev I', 'Chebyshev II', 'Elliptic', 'Location', 'southwest');
set(gca, 'FontName', fn, 'FontSize', fs);

% Plot pole-zero locations for each filter type

Output:

QUESTION 3 – 5 marks
Code:
% Given specifications
Amax = 0.28; % Maximum passband ripple (dB)
Amin = 60; % Minimum stopband attenuation (dB)
Fpass1 = 12; % First passband frequency (Hz)
Fstop1 = 25; % First stopband frequency (Hz)
Fstop2 = 32; % Second stopband frequency (Hz)
Fpass2 = 60; % Second passband frequency (Hz)

% Convert dB to linear scale for Amax and Amin


Ap = 10^(Amax/20);
As = 10^(Amin/20);

% Calculate the frequency ratios


Wpass1 = 2 * pi * Fpass1;
Wstop1 = 2 * pi * Fstop1;
Wstop2 = 2 * pi * Fstop2;
Wpass2 = 2 * pi * Fpass2;

% Estimate the filter order and cutoff frequencies using the buttord function
[n, Wn] = buttord([Wpass1, Wpass2], [Wstop1, Wstop2], Amax, Amin, 's');

% Design the Cauer (elliptic) filter


[b, a] = ellip(n, Ap, As, Wn, 's');

% Frequency response plot in the s-domain


frequencies = logspace(log10(Fpass1), log10(Fpass2), 1000);
s = 1j * frequencies;
H_s = freqs(b, a, s);

% Convert complex response to magnitude (dB)


magnitude_response_db = 20 * log10(abs(H_s));

% Plot the magnitude response in the s-domain


figure;
semilogx(frequencies, magnitude_response_db);
grid on;
title('Cauer (Elliptic) Filter Magnitude Response');
xlabel('Frequency (Hz)');
ylabel('Magnitude (dB)');
xlim([Fpass1, Fpass2]);
ylim([-Amin - 10, Amax + 10]);

% Compute poles and zeros


[z, p, k] = tf2zp(b, a);

% Plot the poles and zeros in the s-plane


figure;
plot(real(p), imag(p), 'rx', 'MarkerSize', 10);
hold on;
plot(real(z), imag(z), 'bo', 'MarkerSize', 8);
grid on;
title('Poles and Zeros of the Cauer (Elliptic) Filter');
xlabel('Real Axis');
ylabel('Imaginary Axis');
legend('Poles', 'Zeros');
% Adjust the figure positions and titles
pos1 = [100, 100, 800, 400];
pos2 = [100, 100, 800, 400];

set(gcf, 'Position', pos1);


set(gca, 'Position', [0.1 0.2 0.85 0.7]);

set(gcf, 'Position', pos2);


set(gca, 'Position', [0.1 0.2 0.85 0.7]);

sgtitle('Cauer (Elliptic) Filter Design Verification', 'FontSize', 16);

output:

2. LAB 2
QUESTION 1 – 15 marks
a.
% Filter specifications
Wc = 20000; % Passband corner frequency in rad/s
Ws = 62000; % Stopband corner frequency in rad/s
Amax = 0.5; % Maximum passband ripple in dB
Amin = 40; % Minimum stopband attenuation in dB

% Source and load resistance values


Rs = 1000;
RL = 1000;

% Choose the ladder configuration: 1 for a T ladder and 0 for a ? ladder


Ladder = 1;

% Calculate the filter order


N = BW_ORDER(Wc, Ws, Amax, Amin);

% Choose a specific filter order (5th-order in this case)


Norder = 5;

% Design the ladder network for the Butterworth filter


[L, C, K] = BW_LADDER(Wc, Ws, Amax, Amin, Norder, Rs, RL, Ladder);

% Define transmission line parameters


Z0 = [];
T = 1;

% Define the frequency range for analysis


omega = linspace(0, 2*Ws, 201); % Using linspace to create a frequency vector

% Calculate the transfer function H


H = LADDER_2_H(Norder, Z0, L, C, Rs, RL, K, omega, T);

% Normalize the attenuation to 0 dB


Att = MAG_2_ATT(2*H);

% Create a subplot for plotting


subplot('position', [0.08 0.4 0.90 0.5]);

% Plot the magnitude response (Att)


PLOT_ATTENUATION_S(omega, Att);

% Plot the low-pass filter specifications


% Amin = 0 => No stopband spec
PLOT_LP_SPEC_S(Wc, Ws, Amax, Amin);

% Set axis limits for better visualization


axis([0, 2*Ws, 0, 1.5*Amin]);
% Display the calculated values of L and C
% NOTE: Assuming L and C are row vectors, not transposed
disp('L:');
disp(L);
disp('C:');
disp(C);

output:
We get :
T ladder
L1 = 2.503946e-02 C1 = 0
L2 = 0 C2 = 6.55542e-08
L3 = 8.102939e-02 C3 = 0
L4 = 0 C4 = 6.55542e-08
L5 = 2.503946e-02 C5 = 0

π ladder:
L1 = 0 C1 = 2.503946e-08
L2 = 6.55542e-02 C2 = 0
L3 = 0 C3 = 8.102939e-08
L4 = 6.55542e-02 C4 = 0
L5 = 0 C5 = 2.503946e-08
b.
% Given specifications
Wc = 22000; % Cutoff frequency in Hz
Ws = 28000; % Stopband edge frequency in Hz
r = 0.5; % Desired attenuation ripple
Amax = -10*log10(1-r^2); % Maximum passband ripple in dB
Amin = 40; % Minimum stopband attenuation in dB
Rs = 1000; % Source impedance in ohms
RL = 1000; % Load impedance in ohms

% Calculate filter order (N) based on the specifications


N = CA_ORDER(Wc, Ws, Amax, Amin);

% Calculate poles and zeros of the analog prototype filter


[G, Z, R_ZEROS, P, Wsnew] = A_POLES(Wc, Ws, Amax, Amin, N);

% Select filter parameters


Norder = 5; % We select a 5th-order filter
Ladder = 0; % 1 for a T ladder and 0 for a ? ladder

% Design the filter using the chosen parameters


[L, C, Rs, RL, Wo, KI] = CA_LADDER(G, Z, R_ZEROS, P, Wc, Ws, Rs, RL, Ladder);

Z0 = []; % Used only for transmission lines


T = 0; % Transmission line parameter
% Frequency range for plotting
omega = [0:Ws/200:2*Ws];

% Calculate the filter's frequency response


H = LADDER_2_H(Norder, Z0, L, C, Rs, RL, K, omega, T);

% Normalize the attenuation to 0 dB


Att = MAG_2_ATT(2*H);

% Plot the results


subplot('position', [0.08 0.4 0.90 0.5]);
PLOT_ATTENUATION_S(omega, Anorm);
PLOT_LP_SPEC_S(Wc, Ws, Amax, Amin);
axis([0, 2*Ws, 0, 1.5*Amin]);

L' % Transpose of L
C' % Transpose of C
Wo' % Transpose of Wo

Output:
C1 = 9.076098e-08
L2 = 3.763656-02 C2 = 1.839124-08 w02=3.800927e+04
C3 = 10.17367-08
L4 = 2.464374e-02 C4 = 5.490668-08 w04 = 2.718530e+04
C5 = 6.953684-08

You might also like