I'm working on a project involving digital filters and DFT. I want to use sampled ECG data to determine the heart rate using power spectral density. While filtering the ECG samples (samples are from this database), I noticed a significant transient when applying an IIR high-pass Butterworth filter. Here's the code and the output:
clear;
clc;
fc = 360;
T = 1/fc;
ecg_samples = load('105m (0).mat').val;
N = length(ecg_samples);
times = (0:N-1)*T;
ecg_samples = (ecg_samples-ecg_samples(1))./(max(ecg_samples)-ecg_samples(1));
fstopband_hhp = 0.5;
fpass_hhp = 0.6;
hhp_butter = designfilt("highpassiir", ...
PassbandFrequency = fpass_hhp, ...
StopbandFrequency = fstopband_hhp, ...
PassbandRipple = 1, ...
StopbandAttenuation = 60, ...
SampleRate = fc, ...
DesignMethod = "butter");
hhp_elliptic = designfilt("highpassiir", ...
PassbandFrequency = fpass_hhp, ...
StopbandFrequency = fstopband_hhp, ...
PassbandRipple = 1, ...
StopbandAttenuation = 60, ...
SampleRate = fc, ...
DesignMethod = "ellip");
ecg_samples_filtered1 = filtfilt(hhp_butter,ecg_samples);
ecg_samples_filtered2 = filtfilt(hhp_elliptic,ecg_samples);
subplot(3,1,1);
plot(times,ecg_samples);
xlabel("time");
ylabel("amplitude");
title("before filtering");
subplot(3,1,2);
plot(times,ecg_samples_filtered1);
xlabel("time");
ylabel("amplitude");
title("butterworth filter");
subplot(3,1,3);
plot(times,ecg_samples_filtered2);
xlabel("time");
ylabel("amplitude");
title("elliptic filter");
As you can see the transient is not present using an elliptic filter with the same specs.
Why does this huge transient appear only when using a Butterworth filter with these specific specs? If I change the filter cutoff frequencies the Butterworth filter doesn't produce the transient, e.g. changing from fpass_hhp = 0.6;
to fpass_hhp = 0.7;
filtord(hhp_butter)
I get 42. What's a SOS ? $\endgroup$[num,den] = tf(hhp_butter); [sos,g] = tf2sos(num,den);
$\endgroup$zp2sos
,[z,p,k] = zpk(hhp_butter); [sos,g] = zp2sos(z,p,k);
and then you can useecg_samples_filtered1 = filtfilt(sos,g,ecg_samples);
$\endgroup$