Need help calculating BPM from ECG

37 views (last 30 days)
Usama Zia
Usama Zia on 25 Jul 2021
Answered: Pavan Guntha on 28 Jul 2021
NEED HELP IMMEDIATELY please. THANK YOU!!!
I performed the Valsalva maneuver and I have to show the changes it causes in heart rate.
I have filtered the raw ECG signal and found the R peaks but Im having trouble calculating Heart Rate BPM. I have put my code at the bottom. Please help me identify what I did wrong and what changes must be made.
%% Finding heart rate
clear;clc;
load('Mydata.mat');
rawdata = b1;%loading data from Labscribe File
ecgraw = rawdata(:,4);%extracting Filtered ECG Data
timeraw = rawdata(:,1);%extracting time points
figure(1)
plot(timeraw,ecgraw)
xlim([5 84])
%Implementing Filters
%low-pass filter
Wn = 0.5; %Wn=cuttoff frequency, high cutoff frequency of 5 Hz
n=300; %order of filter
lowpass = fir1(n,Wn,'low'); %implementing a 300th order FIR low pass filter
filterlowpass = filtfilt(lowpass,1,ecgraw);
%high-pass filter
Wn = 0.0067; %Wn=cuttoff frequency, low cutoff frequency of 0.67 Hz
n=300;
highpass = fir1(n,Wn,'high'); %implementing a 300th order FIR high pass filter
filterhighpass = filtfilt(highpass,1,filterlowpass);
figure(2) %plotting filtered ECG signal
plot(timeraw,filterhighpass);
xlim([5 84]);
xlabel('Time (secs)');ylabel('ECG (mV)');
title('Filtered ECG Signal');
figure(3) %plotting filtered ECG signal
plot(timeraw,filterhighpass);
xlim([5 84]);
xlabel('Time (secs)');ylabel('ECG (mV)');
title('Filtered ECG Signal');
hold on
[ecgpeaks,time] = findpeaks(filterhighpass,timeraw,'MinPeakProminence',0.14,'MinPeakDistance',0.3);
%identifying the R wave peaks of the QRS complex, MinPeakHeight and
%MinPeakDistance can be changed to identify clear peaks
%plot heart rate as a function of time
plot(time,ecgpeaks,'r*');
xlabel('Time (secs)');ylabel('ECG (mV)');
title('Peaks of Filtered ECG Signal');
xlim([5 84])
hold off
%% ( This is where the probelm starts )
%
totalnumberofpeaks = numel(ecgpeaks) %count the total number of peaks found within the signal
peaknumber = [1:1:totalnumberofpeaks]'; %create a column vector of the peak numbers going from 1 to the total number of peaks. Create row vector then transpose
timeinsecs = time; %convert time into seconds
HeartrateBPM = (peaknumber./timeinsecs).*60; %calculate heart rate in bpm, where heart rate = (number of peaks at a given time/given time)x60
figure(4) %plot heart rate as a function of time
plot(timeinsecs,HeartrateBPM);
xlabel('Time (secs)');ylabel('HR (bpm)');
title('Heart Rate');
  2 Comments
Scott MacKenzie
Scott MacKenzie on 25 Jul 2021
It might help if you also post Mydata.mat.

Sign in to comment.

Answers (1)

Pavan Guntha
Pavan Guntha on 28 Jul 2021
Hi Usama,
I understand that the issue is related to the Heart rate computation in BPM. As per the code attached, the 'HeartrateBPM' computes the heart rate by considering all the R-peaks occured until current time. So, the instantaneous effect of R-peaks on the heart rate isn't captured. For calculating instantaneous heart rate you could have a look at the following code snippet:
totalnumberofpeaks = numel(ecgpeaks) %count the total number of peaks found within the signal
peaknumber = [1:1:totalnumberofpeaks]'; %create a column vector of the peak numbers going from 1 to the total number of peaks. Create row vector then transpose
timeinsecs = [time(1); diff(time)]; %calculating time difference b/w consecutive peaks in seconds
HeartrateBPM = 60./time_new; %calculating instantaneous heart rate (in BPM)
You could look at the documentation of diff function for more details.
Hope this helps!

Categories

Find more on ECG / EKG in Help Center and File Exchange

Tags

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!