MATLAB Implementation of Bandpass Filter with Code Example
- Login to Download
- 1 Credits
Resource Overview
MATLAB code implementation for designing and applying a bandpass filter using Butterworth filter design method, complete with signal generation, filtering demonstration, and visualization.
Detailed Documentation
Below is an example of MATLAB code for implementing a bandpass filter:
% Design bandpass filter parameters
fs = 1000; % Sampling frequency (Hz)
f1 = 50; % Lower passband edge frequency
f2 = 200; % Upper passband edge frequency
f3 = 250; % Lower stopband edge frequency
f4 = 300; % Upper stopband edge frequency
rp = 1; % Maximum passband ripple (dB)
rs = 60; % Minimum stopband attenuation (dB)
% Filter design using Butterworth method
% buttord calculates the minimum filter order and cutoff frequencies
[n, wn] = buttord([f1 f2]/(fs/2), [f3 f4]/(fs/2), rp, rs);
% butter function creates the filter coefficients for the specified order
[b, a] = butter(n, wn, 'bandpass');
% Generate test input signal
% Creates a time vector from 0 to 1 second with sampling interval 1/fs
t = 0:1/fs:1;
% Creates a mixed signal containing 100Hz and 300Hz components plus noise
x = sin(2*pi*100*t) + sin(2*pi*300*t) + randn(size(t));
% Apply the designed filter to the input signal
% filter function processes the signal using the obtained coefficients
y = filter(b, a, x);
% Plot results for comparison
subplot(2,1,1);
plot(t, x);
title('Original Signal');
xlabel('Time (s)');
ylabel('Amplitude');
subplot(2,1,2);
plot(t, y);
title('Filtered Signal');
xlabel('Time (s)');
ylabel('Amplitude');
This code demonstrates how to design and apply a bandpass filter using MATLAB. Bandpass filters are used to preserve signals within a specified frequency range while attenuating signals outside this range. In this example, we utilize the Butterworth filter design method to create a bandpass filter and apply it to an input signal. By plotting both the original and filtered signals, we can observe the filter's effect on signal components.
Note that you can adjust the parameters and frequency ranges in the code to suit your specific application requirements. The Butterworth filter provides maximally flat frequency response in the passband, making it suitable for applications where phase linearity is important.
- Login to Download
- 1 Credits