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

Project3

Uploaded by

shawlintory0051
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Project3

Uploaded by

shawlintory0051
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Name ID Project NO

Syeda Shawlin Monjur 40257281 03


1. Report: Design and Analysis of High-Pass Filters

Objective

The primary objective of this project is to design a 6th-order Chebyshev Type I high-pass
filter and analyze its performance based on given specifications. The project also involves
comparing the designed filter with Chebyshev filters of di?erent orders and an equivalent
Butterworth high-pass filter. Finally, the behavior of the designed filter is studied under
di?erent input signals to evaluate its practical performance.

Introduction

High-pass filters are widely used in signal processing to block low-frequency components
while allowing high-frequency components to pass through. The Chebyshev Type I filter is
known for its sharp transition between the passband and stopband, achieved at the cost of
ripples in the passband. Conversely, the Butterworth filter provides a smooth and ripple-
free response but with a less steep transition.

This project focuses on:

1. Designing a 6th-order Chebyshev Type I high-pass filter that meets specific


passband and stopband requirements.

2. Verifying the -3 dB cuto? frequency and its alignment with the filter specifications.

3. Studying the impact of filter order by comparing Chebyshev filters of orders 4, 6, and
8.

4. Comparing the designed Chebyshev filter with a 6th-order Butterworth filter.

5. Evaluating the response of the designed Chebyshev filter under di?erent input
signals.

Theory

1. Chebyshev Type I Filters:


o Characterized by ripples in the passband but no ripples in the stopband.

o Achieves a sharper transition between the passband and stopband


compared to Butterworth filters of the same order.

o The sharpness of the roll-o? and the amount of ripple are determined by the
filter design specifications, including passband ripple and stopband
attenuation.

2. Butterworth Filters:

o Designed to have a maximally flat passband with no ripples.

o Provides a smoother but less steep transition compared to Chebyshev filters.

3. Key Parameters:

o CutoR Frequency: The frequency at which the filter's gain decreases by 3 dB


relative to the passband gain.

o Filter Order: Determines the steepness of the transition between the


passband and stopband. Higher orders result in sharper roll-o? but increase
computational complexity and introduce phase nonlinearity.

4. MATLAB Tools:

o The cheby1 and butter functions were used to design the filters.

o The freqz function was used to compute the frequency and phase responses.

o Linear interpolation was employed to accurately identify the -3 dB cuto?


frequency.

MATLAB Code

The following MATLAB code was used to design and analyze the filters:

1. Chebyshev Type I Filter Design: A 6th-order Chebyshev Type I high-pass filter was
designed using cheby1. The magnitude and phase responses were plotted.

% Specifications
N = 6; % Filter order
Rp = 1; % Passband ripple in dB
Wp = 0.7 * pi; % Passband frequency
[b_cheby6, a_cheby6] = cheby1(N, Rp, Wp/pi, 'high');
[H_cheby6, w] = freqz(b_cheby6, a_cheby6, 16384);
% Plotting
subplot(2, 1, 1);
plot(w / pi, 20 * log10(abs(H_cheby6)), 'b');
title('Magnitude Response');
xlabel('Normalized Frequency (\omega / \pi)');
ylabel('Magnitude (dB)');
grid on;
subplot(2, 1, 2);
plot(w / pi, angle(H_cheby6), 'b');
title('Phase Response');
xlabel('Normalized Frequency (\omega / \pi)');
ylabel('Phase (radians)');
grid on;

Identification of the -3 dB CutoR: A zoomed-in magnitude response was plotted, and the
cuto? frequency was identified by interpolation.

% Zoomed-in plot for cuto? identification


plot(w / pi, 20 * log10(abs(H_cheby6)), 'b');
hold on;
yline(-3, '--r'); % -3 dB line

Comparison of Filters with DiRerent Orders: Filters of orders 4, 6, and 8 were designed,
and their magnitude responses were compared.

orders = [4, 6, 8];


for i = 1:length(orders)
[b, a] = cheby1(orders(i), Rp, Wp/pi, 'high');
[H, w] = freqz(b, a, 16384);
plot(w / pi, 20 * log10(abs(H)));
end

Comparison with a Butterworth Filter: A 6th-order Butterworth high-pass filter was


designed and compared to the Chebyshev filter.

[b_butter6, a_butter6] = butter(N, Wp/pi, 'high');


[H_butter6, w] = freqz(b_butter6, a_butter6, 16384);

Output for Given Inputs: The filter's output response to sinusoidal and step inputs was
analyzed.
% Test inputs
x1 = sin(pi/8 * n);
y1 = filter(b_cheby6, a_cheby6, x1);

Results and Analysis

1. Magnitude and Phase Response of the 6th-Order Chebyshev Type I Filter

• The magnitude response exhibits a sharp roll-o? from the stopband to the
passband, with ripples in the passband as specified (-1 dB). The stopband
attenuation exceeds the requirement of -40 dB.

• The phase response is non-linear, especially near the cuto? frequency, which is
typical for Chebyshev filters.
Figure: 01

2. -3 dB CutoR Frequency Identification

• The cuto? frequency was determined to be: fc = 2.1803 rad/sample.

• This closely aligns with the specified passband frequency of 0.7π ≈ 2.1990.7,
confirming the filter's design accuracy.

Figure: 02
3. Comparison of Chebyshev Filters with Orders 4, 6, and 8

• Order 4:

o Exhibits a more gradual transition between the stopband and passband.

o Suitable for applications where computational e?iciency is prioritized over


sharpness.

• Order 6:

o Provides a sharper cuto? compared to the 4th-order filter.

o Balances computational complexity and roll-o? steepness.

• Order 8:

o Features the sharpest roll-o?, closely approximating an ideal filter.

o Requires more computational resources and introduces greater phase


nonlinearity.
Figure: 03

4. Comparison with a 6th-Order Butterworth Filter

• The Butterworth filter was designed with the same cuto? frequency as the 6th-order
Chebyshev filter.

• Magnitude Response:

o The Butterworth filter has a smooth passband with no ripples, unlike the
Chebyshev filter.
o The roll-o? of the Butterworth filter is more gradual, making it less selective
than the Chebyshev filter.

Figure: 04

• Phase Response:

o The Butterworth filter exhibits a more linear phase response, making it


suitable for applications requiring minimal phase distortion.
Figure: 05

• Summary of DiRerences:

Characteristic Chebyshev Type I Butterworth


Passband Ripple Present (-1 dB max) None
Roll-O9 Sharpness Sharper Gradual
Phase Response Non-linear More linear

5. Response to DiRerent Input Signals

The 6th-order Chebyshev filter was tested with three input signals:

• Input 1 (x1[n]=sin(π/8⋅n):
o This frequency lies in the passband, resulting in minimal attenuation.

• Input 2 (x2[n]=sin(3π/4⋅n):

o This frequency lies in the stopband, leading to significant attenuation.

• Input 3 (x3[n]=u[n]:

o The DC component is removed, and the output shows transient behavior as


expected for a high-pass filter.

Figure: 06
Conclusion

The project successfully demonstrated the design and analysis of Chebyshev Type I and
Butterworth high-pass filters. Key findings include:

1. The 6th-order Chebyshev filter met the design specifications with a -3 dB cuto?
frequency of 2.1803 rad/sample.

2. Increasing the filter order enhances the sharpness of the cuto? but introduces
phase nonlinearity and increases computational complexity.

3. The Butterworth filter o?ers a smooth response with no passband ripple but a less
steep roll-o? compared to the Chebyshev filter.

4. The output responses confirmed the filter's ability to attenuate low-frequency


components and pass high-frequency signals e?ectively.

This project highlights the trade-o?s in high-pass filter design and provides valuable
insights into the practical applications of Chebyshev and Butterworth filters in signal
processing.

APPENDIX
MATLAB CODE

% Filter Specifications
Fs = 1; % Sampling frequency (1 Hz for 1-second sampling period)
Wp = 0.7 * pi; % Passband frequency in radians/sample
Ws = 0.3 * pi; % Stopband frequency in radians/sample
Rp = 1; % Passband ripple in dB
Rs = 40; % Stopband attenuation in dB
N = 6; % Filter order for Chebyshev Type I (Part I)

%% Part I: Design the 6th-Order Chebyshev Type I High-Pass Filter


% Design the Chebyshev Type I high-pass filter
[b_cheby6, a_cheby6] = cheby1(N, Rp, Wp/pi, 'high');

% Frequency response of the Chebyshev Type I high-pass filter


[H_cheby6, w] = freqz(b_cheby6, a_cheby6, 16384); % Increased resolution to 16384 points

% Plotting the Magnitude and Phase Response


figure;
subplot(2, 1, 1);
plot(w / pi, 20 * log10(abs(H_cheby6)), 'b', 'LineWidth', 1.2);
hold on;
yline(-3, '--r', 'LineWidth', 2, 'DisplayName', '-3 dB Cutoff'); % Thicker line at -3 dB
title('Magnitude Response (6th Order Chebyshev Type I High-Pass Filter)');
xlabel('Normalized Frequency (\omega / \pi)');
ylabel('Magnitude (dB)');
grid on;
legend show;

subplot(2, 1, 2);
plot(w / pi, angle(H_cheby6), 'b', 'LineWidth', 1.2);
title('Phase Response (6th Order Chebyshev Type I High-Pass Filter)');
xlabel('Normalized Frequency (\omega / \pi)');
ylabel('Phase (radians)');
grid on;

%% Part II: Manual Identification and Interpolation for -3 dB Cutoff


figure;
plot(w / pi, 20 * log10(abs(H_cheby6)), 'b', 'LineWidth', 1.2);
hold on;
yline(-3, '--r', 'LineWidth', 2, 'DisplayName', '-3 dB Cutoff');
title('Zoomed-In Magnitude Response (Manual Identification of Cutoff)');
xlabel('Normalized Frequency (\omega / \pi)');
ylabel('Magnitude (dB)');
xlim([0.6, 0.8]); % Zoom in to expected cutoff region
ylim([-10, 5]); % Focus on range near -3 dB
grid on;
legend show;

% Prompt user to click at the approximate -3 dB crossing point


disp('Please click near the point where the curve crosses the -3 dB line.');
[cross_freq, ~] = ginput(1); % User selects point on plot

% Find the two points surrounding the user's click for interpolation
[~, idx] = min(abs(w / pi - cross_freq)); % Closest index to user's click

% Perform a more accurate linear interpolation


if idx > 1 && idx < length(w)
% Get neighboring points around the identified index
x1 = w(idx - 1) / pi;
y1 = 20 * log10(abs(H_cheby6(idx - 1)));
x2 = w(idx) / pi;
y2 = 20 * log10(abs(H_cheby6(idx)));

% Perform linear interpolation


cutoff_frequency = x1 + ((-3 - y1) * (x2 - x1)) / (y2 - y1);
disp(['Estimated -3dB Cutoff Frequency: ', num2str(cutoff_frequency * pi), ' rad/sample']);
else
disp('Unable to interpolate accurately, please click closer to the cutoff region.');
end

%% Part III: Design Filters of Orders 4, 6, and 8 and Compare Responses


orders = [4, 6, 8];
figure;
hold on;
colors = ['r', 'b', 'g'];
for i = 1:length(orders)
[b, a] = cheby1(orders(i), Rp, Wp/pi, 'high');
[H, w] = freqz(b, a, 16384);
plot(w / pi, 20 * log10(abs(H)), colors(i), 'DisplayName', [num2str(orders(i)), 'th Order']);
end
title('Magnitude Response Comparison (Chebyshev Type I Filters: Orders 4, 6, 8)');
xlabel('Normalized Frequency (\omega / \pi)');
ylabel('Magnitude (dB)');
grid on;
legend show;
hold off;

%% Part IV: Discussion of Filter Order


% The effects of changing the filter order are visualized in the previous plot.
% - Higher order filters result in a sharper transition from passband to stopband.
% - Passband ripple remains the same, as specified by Rp.
% - The phase response becomes more nonlinear with increasing order.

%% Part V: Design a 6th-Order Butterworth High-Pass Filter


% The Butterworth filter is designed to have the same cutoff frequency as the Chebyshev filter.
[b_butter6, a_butter6] = butter(N, Wp/pi, 'high');

% Frequency response of the Butterworth filter


[H_butter6, w] = freqz(b_butter6, a_butter6, 16384);
% Compare the magnitude responses of the Butterworth and Chebyshev filters
figure;
plot(w / pi, 20 * log10(abs(H_cheby6)), 'b', 'DisplayName', 'Chebyshev Type I');
hold on;
plot(w / pi, 20 * log10(abs(H_butter6)), 'r', 'DisplayName', 'Butterworth');
title('Magnitude Response Comparison (6th Order Chebyshev vs Butterworth High-Pass)');
xlabel('Normalized Frequency (\omega / \pi)');
ylabel('Magnitude (dB)');
grid on;
legend show;
hold off;

% Compare the phase responses of the Butterworth and Chebyshev filters


figure;
plot(w / pi, angle(H_cheby6), 'b', 'DisplayName', 'Chebyshev Type I');
hold on;
plot(w / pi, angle(H_butter6), 'r', 'DisplayName', 'Butterworth');
title('Phase Response Comparison (6th Order Chebyshev vs Butterworth High-Pass)');
xlabel('Normalized Frequency (\omega / \pi)');
ylabel('Phase (radians)');
grid on;
legend show;
hold off;

%% Discussion on Chebyshev vs Butterworth Filter Characteristics


% - Chebyshev Type I filters have ripples in the passband, while Butterworth filters are flat.
% - Chebyshev filters typically have a sharper cutoff compared to Butterworth filters for the same
order.
% - Butterworth filters have a more linear phase response compared to Chebyshev Type I filters.

%% Part VI: Plot the Output for Given Inputs


n = 0:100; % Define the time index for 0 <= n <= 100
x1 = sin(pi/8 * n);
x2 = sin(3*pi/4 * n);
x3 = ones(1, length(n)); % Step function u[n]

% Compute the output responses using the filter function


y1_cheby6 = filter(b_cheby6, a_cheby6, x1);
y2_cheby6 = filter(b_cheby6, a_cheby6, x2);
y3_cheby6 = filter(b_cheby6, a_cheby6, x3);

figure;
subplot(3, 1, 1);
plot(n, y1_cheby6);
title('Output Response to x_1[n] = sin(\pi/8 \cdot n) (Chebyshev Type I)');
xlabel('n');
ylabel('y_1[n]');
grid on;

subplot(3, 1, 2);
plot(n, y2_cheby6);
title('Output Response to x_2[n] = sin(3\pi/4 \cdot n) (Chebyshev Type I)');
xlabel('n');
ylabel('y_2[n]');
grid on;

subplot(3, 1, 3);
plot(n, y3_cheby6);
title('Output Response to x_3[n] = u[n] (Chebyshev Type I)');
xlabel('n');
ylabel('y_3[n]');
grid on;

%% Part VII: Discuss the System Behavior with Different Inputs


% - For x1[n] = sin(pi/8 * n), the system response shows an oscillatory output with attenuation.
% - For x2[n] = sin(3*pi/4 * n), the response demonstrates significant attenuation, indicating that
this frequency
% is in the stopband of the high-pass filter.
% - For x3[n] = u[n], the output settles to a steady-state value after an initial transient,
consistent with a high-pass filter
% blocking the DC component.
2. Report: FIR Bandpass Filter Design Using Windowing Techniques

Introduction

Digital filters are vital tools in signal processing applications, such as noise removal,
frequency selection, and signal enhancement. This report focuses on the design of an FIR
bandpass filter using the Kaiser window method and compares its performance against
other windowing techniques, namely Hamming, Hanning, and Blackman windows. The
design adheres to the following specifications:

• Passband frequencies: π/4\pi/4π/4 to π/2\pi/2π/2 radians/sample (125 Hz to 250


Hz for a sampling frequency of 1000 Hz),

• Stopband attenuation: At least 50 dB,

• Sampling frequency: 1000 Hz.

The report addresses the filter design process, evaluates the frequency and time-domain
characteristics, and explores the trade-o?s between di?erent windowing techniques to
answer all parts of the problem.

Background

FIR (Finite Impulse Response) filters are inherently stable and exhibit a linear phase
response, making them ideal for applications requiring precise frequency-domain
performance. Designing an FIR filter involves approximating an ideal frequency response
(e.g., a bandpass filter) with a finite-length impulse response.

Theory of FIR Filter Design

1. Ideal Bandpass Filter:

o The ideal bandpass filter has a frequency response with unity gain in the
passband and zero gain in the stopband. However, its impulse response is
infinite in length and non-implementable.

2. Truncation and Gibbs Phenomenon:

o Truncating the impulse response to a finite length introduces ripples in the


frequency response due to the Gibbs phenomenon.
3. Window Functions:

o A window function tapers the truncated impulse response smoothly to zero


at both ends, reducing the Gibbs phenomenon and shaping the filter's
frequency response.

o The trade-o? lies between:

Main Lobe Width: Determines the sharpness of the transition between


the passband and stopband.

Side Lobe Levels: Represent stopband ripple or attenuation.

Windowing Techniques

1. Kaiser Window:

o The Kaiser window allows a tunable trade-o? between main lobe width and
side lobe attenuation via the beta parameter.

o The kaiserord function in MATLAB determines the required filter order and
beta value based on stopband attenuation and transition width.

2. Hamming and Hanning Windows:

o Fixed-width windows o?ering moderate main lobe width and stopband


attenuation.

o Hamming is slightly better than Hanning in terms of stopband attenuation.

3. Blackman Window:

o A window with the lowest side lobe levels, achieving excellent stopband
attenuation but with a wider main lobe, resulting in less sharp transitions.

MATLAB Code

The MATLAB code provided implements the FIR bandpass filter design using the Kaiser
window and compares it to filters designed with the Hamming, Hanning, and Blackman
windows.

Kaiser Window Filter Design: The kaiserord function estimates the required filter order and
the Kaiser beta parameter. The fir1 function designs the FIR filter.
% Specifications
fs = 1000; % Sampling frequency
passband_freq = [pi/4 pi/2]; % Passband frequencies in radians/sample
stopband_atten = 50; % Stopband attenuation in dB

% Normalize frequencies
passband_freq_hz = passband_freq * fs / (2 * pi); % Convert to Hz
f_p = passband_freq_hz / (fs / 2); % Normalize to 0-1 scale

% Define stopband frequencies


stopband_freq_hz = [f_p(1) - 0.05, f_p(2) + 0.05] * (fs / 2);
f_s = stopband_freq_hz / (fs / 2);

% Frequency and amplitude specifications


freqs = [f_s(1) f_p(1) f_p(2) f_s(2)];
amps = [0 1 0]; % Desired amplitudes
devs = [0.01 0.05 0.01]; % Deviations

% Kaiser Window Design


[N_kaiser, Wn, beta] = kaiserord(freqs, amps, devs);
N_kaiser = ceil(N_kaiser);
b_kaiser = fir1(N_kaiser, Wn, kaiser(N_kaiser+1, beta));

Filter Design Using Other Windows: Filters were redesigned using the Hamming,
Hanning, and Blackman windows with the same filter order.

% Redesign with Hamming, Hanning, and Blackman Windows


b_hamming = fir1(N_kaiser, Wn, hamming(N_kaiser+1));
b_hanning = fir1(N_kaiser, Wn, hanning(N_kaiser+1));
b_blackman = fir1(N_kaiser, Wn, blackman(N_kaiser+1));

Frequency and Impulse Response Plots:The freqz function generates magnitude


responses, and stem plots the impulse responses.
% Magnitude Response Comparison
[H_kaiser, W] = freqz(b_kaiser, 1, 1024, fs);
[H_hamming, ~] = freqz(b_hamming, 1, 1024, fs);
[H_hanning, ~] = freqz(b_hanning, 1, 1024, fs);
[H_blackman, ~] = freqz(b_blackman, 1, 1024, fs);

% Plot impulse responses


subplot(4,1,1); stem(b_kaiser); title('Impulse Response - Kaiser');
subplot(4,1,2); stem(b_hamming); title('Impulse Response - Hamming');
subplot(4,1,3); stem(b_hanning); title('Impulse Response - Hanning');
subplot(4,1,4); stem(b_blackman); title('Impulse Response - Blackman');

Results

Part I: Design an FIR Bandpass Filter Using the Kaiser Window

The Kaiser window method was used to design the filter. The MATLAB function kaiserord
estimated the minimum filter order, ensuring the stopband attenuation of 50 dB. Below,
Figure 1 shows the magnitude response of the FIR bandpass filter designed using the
Kaiser window. The passband (125–250 Hz) is preserved, and the stopband attenuation
exceeds the required 50 dB.

Part II: Determine the Filter Order

The Kaiser window filter order was calculated using:

• Stopband attenuation: 50 dB,

• Transition width: Defined by the normalized stopband and passband frequencies.

The calculated filter order was N = 36.

Part III: Plot the Magnitude Response


Figure 1: Magnitude response of FIR bandpass filter designed using the Kaiser window.
The passband is within 125–250 Hz, and the stopband attenuation exceeds 50 dB.

Part IV: Redesign Using Hamming, Hanning, and Blackman Windows

The FIR bandpass filter was redesigned using Hamming, Hanning, and Blackman windows
with the same filter order (N=36N = 36N=36) for a fair comparison. Each filter’s magnitude
response is compared in Figure 2.
Part V: Plot the Magnitude Responses for Comparison

Figure 2 : Comparison of magnitude responses for FIR bandpass filters designed using
Kaiser, Hamming, Hanning, and Blackman windows. The trade-oRs in stopband
attenuation and transition width are evident.

Part VI: Compare the Frequency Response Characteristics

• Passband Behavior: All filters maintain a flat passband within 125–250 Hz.
• Stopband Behavior: Blackman achieves the best stopband attenuation, followed
by Kaiser. Hamming and Hanning have higher side lobes, reducing their attenuation.

• Transition Width: Blackman has the widest transition band, while Hamming and
Hanning o?er sharper transitions.

Part VII: Plot the Impulse Responses

• The Kaiser window provides a balanced impulse response with minimal oscillations.

• The Blackman window results in a longer decay due to its wider main lobe.

• The Hamming and Hanning windows show more pronounced oscillations due to
higher side lobe levels.
Figure 3: Impulse responses of FIR bandpass filters designed with diRerent window
functions. The Kaiser window shows minimal oscillations, while the Blackman
window exhibits smoother decay.

Discussion

VI. Frequency Response Characteristics Comparison:

• Passband: All filters achieve a flat passband within the specified range of 125 Hz to
250 Hz.
• Stopband: Blackman provides the best attenuation, followed by Kaiser, Hamming,
and Hanning.
• Transition Width:Blackman has the widest transition band, while Hamming and
Hanning o?er sharper transitions.

VIII. Relationship Between Window Function, Main Lobe Width, and Side Lobe
Levels:

• Main Lobe Width: A wider main lobe (e.g., Blackman) results in smoother
transitions but compromises sharpness. A narrower main lobe (e.g., Kaiser,
Hamming) ensures sharp transitions but may lead to higher side lobes.
• Side Lobe Levels: Lower side lobe levels reduce stopband ripple. Blackman excels
in this regard, followed by Kaiser.
• Impulse Response: Higher side lobes correlate with more oscillations in the
impulse response.

IX. ERect of Window on Frequency Response:

• Stopband Attenuation: Blackman achieves the highest stopband attenuation due


to its low side lobes. Kaiser o?ers tunable attenuation via the beta parameter.
Hamming and Hanning show higher side lobe levels, reducing attenuation.
• Transition Width: Blackman sacrifices sharpness for better attenuation. Kaiser
balances transition width and stopband attenuation. Hamming and Hanning o?er
sharper transitions but with more stopband ripple.

Conclusion

Filter Order: The Kaiser window method accurately determines the filter order required for
the specified stopband attenuation.

Frequency Response: Blackman provides the best stopband attenuation, while Hamming
and Hanning excel in sharp transitions. Kaiser balances these characteristics, making it
versatile.

Impulse Response: Kaiser o?ers a balanced response, while Blackman decays more
smoothly.

Recommendations:
• Use Kaiser for flexible designs.
• Use Blackman for applications requiring minimal leakage.
• Use Hamming/Hanning for sharp transitions.

This study highlights the trade-o?s in FIR filter design and demonstrates the impact of
window choice on frequency and time-domain performance.

APPENDIX
MATLAB CODE

% Specifications
fs = 1000; % Sampling frequency (Hz)
passband_freq = [pi/4 pi/2]; % Passband frequencies (radians/sample)
stopband_atten = 50; % Stopband attenuation (dB)

% Convert passband frequencies to normalized frequencies (0 to 1 scale)


passband_freq_hz = passband_freq * fs / (2 * pi); % Convert to Hz
f_p = passband_freq_hz / (fs / 2); % Normalize (0 to 1 scale)

% Define stopband frequencies slightly outside passband


stopband_freq_hz = [f_p(1) - 0.05, f_p(2) + 0.05] * (fs / 2); % in Hz
f_s = stopband_freq_hz / (fs / 2); % Normalize

% Combine frequencies for Kaiser design


freqs = [f_s(1) f_p(1) f_p(2) f_s(2)];
amps = [0 1 0]; % Desired amplitudes (0 in stopband, 1 in passband)
devs = [0.01 0.05 0.01]; % Maximum deviations in each band

% Part I & II: Kaiser Window Filter Design


[N_kaiser, Wn, beta] = kaiserord(freqs, amps, devs); % Corrected frequencies
N_kaiser = ceil(N_kaiser); % Ensure the order is an integer
b_kaiser = fir1(N_kaiser, Wn, kaiser(N_kaiser+1, beta)); % FIR bandpass filter

% Plot magnitude response of Kaiser Window Filter


figure;
freqz(b_kaiser, 1, 1024, fs);
title('Magnitude Response of Kaiser Window FIR Bandpass Filter');
xlabel('Frequency (Hz)');
ylabel('Magnitude (dB)');
grid on;

% Redesign with Hamming, Hanning, and Blackman Windows


b_hamming = fir1(N_kaiser, Wn, hamming(N_kaiser+1));
b_hanning = fir1(N_kaiser, Wn, hanning(N_kaiser+1));
b_blackman = fir1(N_kaiser, Wn, blackman(N_kaiser+1));

% Plot magnitude response for comparison


figure;
hold on;
[H_kaiser, W] = freqz(b_kaiser, 1, 1024, fs);
[H_hamming, ~] = freqz(b_hamming, 1, 1024, fs);
[H_hanning, ~] = freqz(b_hanning, 1, 1024, fs);
[H_blackman, ~] = freqz(b_blackman, 1, 1024, fs);

plot(W, 20*log10(abs(H_kaiser)), 'b', 'DisplayName', 'Kaiser');


plot(W, 20*log10(abs(H_hamming)), 'r', 'DisplayName', 'Hamming');
plot(W, 20*log10(abs(H_hanning)), 'g', 'DisplayName', 'Hanning');
plot(W, 20*log10(abs(H_blackman)), 'm', 'DisplayName', 'Blackman');

title('Magnitude Response Comparison of Different Window Functions');


xlabel('Frequency (Hz)');
ylabel('Magnitude (dB)');
legend;
grid on;
% Plot impulse responses for each filter
figure;
subplot(4, 1, 1);
stem(b_kaiser);
title('Impulse Response - Kaiser Window');
xlabel('n'); ylabel('Amplitude');

subplot(4, 1, 2);
stem(b_hamming);
title('Impulse Response - Hamming Window');
xlabel('n'); ylabel('Amplitude');

subplot(4, 1, 3);
stem(b_hanning);
title('Impulse Response - Hanning Window');
xlabel('n'); ylabel('Amplitude');

subplot(4, 1, 4);
stem(b_blackman);
title('Impulse Response - Blackman Window');
xlabel('n'); ylabel('Amplitude');
grid on;

% Discuss the main lobe width and side lobe levels in the report to be provided later.

You might also like