bandpass filter in matlab

To implement a bandpass filter in MATLAB, you can use the designfilt function from the Signal Processing Toolbox to design a filter and then use the filter function to apply the filter to your signal. Here's an example code snippet that demonstrates the process:

main.m
% Define the filter parameters
fs = 1000;                   % Sampling rate (Hz)
fpass = [50 200];            % Passband frequencies (Hz)
fstop = [40 250];            % Stopband frequencies (Hz)
apass = 1;                   % Passband ripple (dB)
astop = 60;                  % Stopband attenuation (dB)

% Design the filter
d = designfilt('bandpassiir', 'FilterOrder', 4, ...
               'PassbandFrequency', fpass, ...
               'StopbandFrequency', fstop, ...
               'PassbandRipple', apass, ...
               'StopbandAttenuation', astop, ...
               'SampleRate', fs);

% Load the signal
load('ecg.mat', 'ecg');

% Apply the filter
filtered_signal = filter(d, ecg);

% Plot the original and filtered signals
t = (0:length(ecg)-1)/fs;
subplot(2,1,1)
plot(t, ecg)
xlabel('Time (s)')
ylabel('Amplitude')
title('Original ECG')
subplot(2,1,2)
plot(t, filtered_signal)
xlabel('Time (s)')
ylabel('Amplitude')
title('Filtered ECG')
953 chars
34 lines

In this example, we first define the desired passband and stopband frequencies, as well as the passband ripple and stopband attenuation. We then use the designfilt function to create a fourth-order IIR bandpass filter with these specifications. We load an example ECG signal and apply the filter using the filter function, before visualizing the original and filtered signals using the plot function.

gistlibby LogSnag