I am trying to filter out an ECG signal using the eighth order butterworth filter method. I am using a bandstop filter. Here is the MATLAB Code: clear; Data = csvread('ecg_HF_noise.csv',1,0); signal = Data(:,3)/100000; N = length(signal); FreqS = 94.3; % Sampling frequency nyqFreq = FreqS/2; % Nyquist frequency fft_signal = abs(fft(signal)); figure(1); % Plot single-sided magnitude spectrum (normalized) x1 = 0:1/(N/2 -1):1; y1 = fft_signal(1:N/2); p1 = line(x1,y1); grid on; ax1 = gca; % current axes axis([0 1 0 1]); xlabel('Normalised frequency [\pi rads/sample]'); ylabel('Magnitude'); [b a] = butter(8, [0.49 0.55], 'stop'); H = freqz(b,a,N/2); hold on; x2 = linspace(0,nyqFreq,500); y2 = abs(H); ax1_pos = ax1.Position; ax2 = axes('Position',ax1_pos,'XAxisLocation','top','YAxisLocation','right','Color','none'); ax2.XLabel.String = 'Frequency [Hz]'; p2 = line(x2,y2,'Parent',ax2,'Color','r'); % Plot frequency response title('Magnitude spectrum of signal and frequency response of filter'); axis([0 nyqFreq 0 1]); legend([p1 p2],'magnitude spectrum','frequency response'); x_filtered = filter(b,a,signal); hold off; This is the resulting frequency response of the filter and the magnitude spectrum of the signal: An here the filtered signal vs. the noisy signal: Figure 2: I don't understand why by rejecting the ca. 24Hz peak, the signal is filtered. What about the noisy part between 0 to 0.4 and the mysterious large peak at the beginning? -When I apply the filter over that region the signal is not filtered. How can that be explained? I had my PC adapter (50Hz) near the sensor during the time of capture of the data. What might that peak at the beginning with the large amplitude be? The unfiltered signal at the beginning of figure 3 looks normal though.
John Michell answered .
2025-11-20
FreqS = 94.3; % Sampling frequency fcuts = [0.5 1.0 45 46]; % Frequency Vector mags = [0 1 0]; % Magnitude (Defines Passbands & Stopbands) devs = [0.05 0.01 0.05]; % Allowable Deviations [n,Wn,beta,ftype] = kaiserord(fcuts,mags,devs,FreqS); n = n + rem(n,2); hh = fir1(n,Wn,ftype,kaiser(n+1,beta),'scale'); figure freqz(hh, 1, 2^14, FreqS)