# How can I replace the high pass filter with a Bandpass filter? (New to matlab)

27 views (last 30 days)
Salinda Nandasena Talapitiya Rallage on 23 Sep 2020 at 11:46
Commented: Star Strider on 23 Sep 2020 at 14:02
fontSize = 22;
K = csvread("HeartBeatSample.csv") %path to the sample file
Time = K(1:45465,1);
Voltage= K(1:45465,2); %/amplitude
plot(Time,Voltage) % Plotting the virtual data that given
J = fir1(1000,1/1000*2,'high'); % Smoothing using high pass filter
Voltage_filter = filter(J,1,Voltage);
plot(Voltage_filter)
Findsq = Voltage_filter.^2;
plot(Findsq);
last = 0;
Point = 0;
pulse = zeros(length(Findsq),1);
for L = 1:length(Findsq)
p = 0;
if (Findsq(L) > 0.8)
if (Point == 0)
if (last > 0)
t = L - last;
p = 1000/t*60;
end
last = L;
end
Point = 100;
else
if (Point > 0)
Point = Point - 1;
end
end
pulse(L)=p;
end
plot(pulse); % plotting the pulse graph
grid on;
title('Pulse', 'FontSize', fontSize, 'Interpreter', 'none');
%xlabel('Numbers Of Data');
ylabel('BPM')
title('BPM Graph');
y = pulse;
x = 1 : length(y);
% There are a large number of zeros. Let's set those to nan to ignore them
% Get rid of those.
% Plot them in red.
hold on;
plot(x, y, 'r-', 'LineWidth', 2);
grid on;
% Find the min and put a line there.
darkGreen = [0, 0.7, 0];
yline(min(y), 'Color', darkGreen, 'LineWidth', 2);
caption = sprintf('Pulse. Min = %f, max = %f', min(y), max(y));
title(caption, 'FontSize', fontSize, 'Interpreter', 'none');
yPrior = rand(1); % Initialize
for L = 1 : length(y)
if ~isnan(y(L)) && y(L) ~= yPrior
if y(L) < 80
fprintf("You're in Normal condition at L = %d with a pulse of %f.\n", x(L), y(L));
elseif y(L) <= 90
fprintf("You're in Stressed condition at L = %d with a pulse of %f.\n", x(L), y(L));
else
fprintf("You're in Critical condition at L = %d with a pulse of %f.\n", x(L), y(L));
end
end
yPrior = y(L);
end
g = gcf;
g.WindowState = 'maximized'
MATLAB Version 9.8 (R2020a)
Control System Toolbox Version 10.8 (R2020a)
DSP System Toolbox Version 9.10 (R2020a)
Data Acquisition Toolbox Version 4.1 (R2020a)
Signal Processing Toolbox Version 8.4 (R2020a)
Statistics and Machine Learning Toolbox Version 11.7 (R2020a)
Symbolic Math Toolbox Version 8.5 (R2020a)

Star Strider on 23 Sep 2020 at 12:25
If you supply the two passband frequencies between 0 and 1 as a vector (frequencies in Hz normalised by the Nyquist frequency), fir1 will automatically design a bandpass filter.
A filter order of 1000 is likely overkill, and will be slow. I would choose a shorter filter (likely less than 200), and use the filtfilt function to do the actual filtering. Use the freqz function to see the filter Bode plot to be certain that ithe filter is doing what you want it to do.
For any EKG, a lower passband of 1 Hz and an upper passband of 100 Hz will pass all the necessary frequencies while eliminating baseline drift and high-frequency noise. In a healthy, normal EKG, an upper passband of 45 Hz will work.

Show 1 older comment
Star Strider on 23 Sep 2020 at 12:43
Prototype code:
Fs = 256; % Sampling Frequency (Use The Value That Applies To Your Signal)
Fn = Fs/2; % Nyquist Frequency
J = fir1(42, [1 100]/Fn); % Filter Design
figure
freqz(J, 1, 2^16, Fs) % Bode Plot
Voltage_filter = filtfilt(J, 1, Voltage); % Filter Signal
figure
plot(Time, Voltage_filter) % Plot Filtered Signal
grid
I use a 42-order filter here. Higher-order filters have steeper transition regions (steeper cutoffs), however are slower and less efficient. Use the freqz plot to help guide your choice of filter parameters.
Salinda Nandasena Talapitiya Rallage on 23 Sep 2020 at 13:56
Thank you. This improved the the time significally. For some reason using high pass filter does not give the required output.
Star Strider on 23 Sep 2020 at 14:02
As always, my pleasure!
With a cutoff frequency of , the highpass filter would pass essentially everything.