create a firfilter

This commit is contained in:
Tikea TE 2025-04-16 15:21:00 +02:00
parent 50874a3e58
commit 0ec9cf1393
3 changed files with 61 additions and 46 deletions

37
firFilter.m Normal file
View File

@ -0,0 +1,37 @@
function signal_filtered = firFilter(N, cutoffFreq, signal, samplingFreq)
%{
function signal_filtered = firFilter(N, cutoffFreq, signal, samplingFreq)
Ex: signal_filtered = firFilter(30, [5 20], x, 200)
Task: Apply an FIR bandpass filter to a signal.
Inputs:
- N: filter order
- cutoffFreq: 2-element vector [low high] for bandpass cutoff (Hz)
- signal: input signal to be filtered
- samplingFreq: sampling frequency (Hz)
Output:
- signal_filtered: the filtered output signal
Author: Tikea TE
Date: 16/04/2025
%}
% Normalize the cutoff frequency
Wn = cutoffFreq / (samplingFreq / 2); % Normalize to Nyquist
% Design the FIR bandpass filter using Hamming window
b = fir1(N, Wn, 'bandpass', hamming(N+1));
a = 1; % FIR filter has only numerator
% Apply filter
signal_filtered = filter(b, a, signal);
% Plot frequency response
figure;
freqz(b, a, 1024, samplingFreq);
title('Frequency Response of FIR Filter');
grid on;
end

View File

@ -1,60 +1,39 @@
function [signal_filtered, Z, P] = iirFilter(N, cutoffFreq, signal, samplingFreq, filterType)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function [signal_filtered, Z, P] = iirFilter(N, cutoffFreq, signal, samplingFreq, filterType)
% ex.: [signal_filtered, Z, P] = iirFilter(6, [5 20], x, 200, 1)
function [signal_filtered, Z,P] = iirFilter(N, cutoffFreq, signal, samplingFreq, filterType)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function [Z, P] = iirFilter(N, cutoffFreq, signal, samplingFreq, filterType)
% ex.: [Z, P] =iirFilter(6, 10, x, 500, 1)
%
% Task:
% To create and apply an IIR filter (Butterworth or Chebyshev)
% Supports low-pass or band-pass depending on the cutoffFreq input.
% Task: To create and analyze an IIR low pass filter (Butterworth ror Chebychev)
%
% Inputs:
% -N: order of the filter
% -cutoffFreq: scalar (for low-pass) or 2-element vector [low high] for bandpass
% -signal: input signal to be filtered
% -samplingFreq: sampling frequency in Hz
% -filterType: 1 = Butterworth, 2 = Chebyshev type I
% -N: order of the filter
% -cutoffFreq: below this frequency, signal is not modified and above signal is attenuated
% -samplingFreq: sampling frequency (In Hz)
% -signal: signal to be filter
% -filterType: Butterworth if equal to 1 and Chebychev if equal to 2
%
% Outputs:
% -signal_filtered: filtered signal
% -Z: filter zeros
% -P: filter poles
% Outputs:
%
%
% Author: Tikea TE
% Date: 16/04/2025
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Author: Guillaume Gibert, guillaume.gibert@ecam.fr
% Date: 09/04/2025
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Design the filter
if filterType == 1
if length(cutoffFreq) == 1
[b, a] = butter(N, cutoffFreq / (samplingFreq / 2), 'low');
elseif length(cutoffFreq) == 2
[b, a] = butter(N, cutoffFreq / (samplingFreq / 2), 'bandpass');
else
error('cutoffFreq must be scalar or 2-element vector');
end
elseif filterType == 2
Rp = 10; % ripple for Chebyshev filter
if length(cutoffFreq) == 1
[b, a] = cheby1(N, Rp, cutoffFreq / (samplingFreq / 2), 'low');
elseif length(cutoffFreq) == 2
[b, a] = cheby1(N, Rp, cutoffFreq / (samplingFreq / 2), 'bandpass');
else
error('cutoffFreq must be scalar or 2-element vector');
end
if (filterType == 1)
[b, a] = butter(N, cutoffFreq/(samplingFreq/2));
elseif (filterType == 2)
Rp = 10; % bandpass ripple of Rp dB
[b, a] = cheby1(N, Rp, cutoffFreq/(samplingFreq/2));
else
error('Filter type must be 1 (Butterworth) or 2 (Chebyshev)');
disp('Filter type is incorrect!')
return
end
% Analyze zeros/poles
[Z, P] = zeroPole(a, b, 1);
% Show frequency response
figure;
freqz(b, a, N, samplingFreq);
title('Frequency response');
grid on;
% Apply filter to signal
signal_filtered = filter(b, a, signal);
end

5
main.m
View File

@ -23,8 +23,7 @@ plotRawSignal(X, Fs);
[f, power] = frequencySpectrum(X, Fs, 1); % set 1 to plot
% ======== Apply a bandpass filter ==============
[filteredSignal, Z, P] = iirFilter(6, [5 20], X, 200, 1); % Butterworth bandpass
[filteredSignal, Z, P] = iirFilter(10, [5 20], X, Fs, 1);
plotFilteredSignal(filteredSignal, Fs);
end