create a firfilter
This commit is contained in:
parent
50874a3e58
commit
0ec9cf1393
|
|
@ -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
|
||||||
63
iirFilter.m
63
iirFilter.m
|
|
@ -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)
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
% function [signal_filtered, Z, P] = iirFilter(N, cutoffFreq, signal, samplingFreq, filterType)
|
% function [Z, P] = iirFilter(N, cutoffFreq, signal, samplingFreq, filterType)
|
||||||
% ex.: [signal_filtered, Z, P] = iirFilter(6, [5 20], x, 200, 1)
|
% ex.: [Z, P] =iirFilter(6, 10, x, 500, 1)
|
||||||
%
|
%
|
||||||
% Task:
|
% Task: To create and analyze an IIR low pass filter (Butterworth ror Chebychev)
|
||||||
% To create and apply an IIR filter (Butterworth or Chebyshev)
|
|
||||||
% Supports low-pass or band-pass depending on the cutoffFreq input.
|
|
||||||
%
|
%
|
||||||
% Inputs:
|
% Inputs:
|
||||||
% -N: order of the filter
|
% -N: order of the filter
|
||||||
% -cutoffFreq: scalar (for low-pass) or 2-element vector [low high] for bandpass
|
% -cutoffFreq: below this frequency, signal is not modified and above signal is attenuated
|
||||||
% -signal: input signal to be filtered
|
% -samplingFreq: sampling frequency (In Hz)
|
||||||
% -samplingFreq: sampling frequency in Hz
|
% -signal: signal to be filter
|
||||||
% -filterType: 1 = Butterworth, 2 = Chebyshev type I
|
% -filterType: Butterworth if equal to 1 and Chebychev if equal to 2
|
||||||
%
|
%
|
||||||
% Outputs:
|
% Outputs:
|
||||||
% -signal_filtered: filtered signal
|
|
||||||
% -Z: filter zeros
|
|
||||||
% -P: filter poles
|
|
||||||
%
|
%
|
||||||
% 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 filterType == 1
|
[b, a] = butter(N, cutoffFreq/(samplingFreq/2));
|
||||||
if length(cutoffFreq) == 1
|
elseif (filterType == 2)
|
||||||
[b, a] = butter(N, cutoffFreq / (samplingFreq / 2), 'low');
|
Rp = 10; % bandpass ripple of Rp dB
|
||||||
elseif length(cutoffFreq) == 2
|
[b, a] = cheby1(N, Rp, cutoffFreq/(samplingFreq/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
|
|
||||||
else
|
else
|
||||||
error('Filter type must be 1 (Butterworth) or 2 (Chebyshev)');
|
disp('Filter type is incorrect!')
|
||||||
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
% Analyze zeros/poles
|
|
||||||
[Z, P] = zeroPole(a, b, 1);
|
[Z, P] = zeroPole(a, b, 1);
|
||||||
|
|
||||||
% Show frequency response
|
|
||||||
figure;
|
figure;
|
||||||
freqz(b, a, N, samplingFreq);
|
freqz(b, a, N, samplingFreq);
|
||||||
title('Frequency response');
|
title('Frequency response');
|
||||||
grid on;
|
grid on;
|
||||||
|
|
||||||
% Apply filter to signal
|
|
||||||
signal_filtered = filter(b, a, signal);
|
signal_filtered = filter(b, a, signal);
|
||||||
|
|
||||||
end
|
|
||||||
|
|
|
||||||
5
main.m
5
main.m
|
|
@ -23,8 +23,7 @@ plotRawSignal(X, Fs);
|
||||||
[f, power] = frequencySpectrum(X, Fs, 1); % set 1 to plot
|
[f, power] = frequencySpectrum(X, Fs, 1); % set 1 to plot
|
||||||
|
|
||||||
% ======== Apply a bandpass filter ==============
|
% ======== 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
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue