obtaining heart rate from ECG signal

조회 수: 40 (최근 30일)
Daniel
Daniel 2024년 5월 9일
댓글: Daniel 2024년 5월 14일
Hello,
I need to obtain heart rate values from an ECG signal. While searching for information, I discovered that I need to (more or less) get the RR interval and then use the time difference between samples to calculate the instantaneous HR.
I have a database acquired in the local hospital, but the raw signal is full of noise and invalid samples but I dont know how to remove both.
Attached you can find of the raw ECG. It is sampled at 1kHz, stored in int16 data type and the measured units are milivolts.
fl = fopen("ficheroAnalogico.dat");
A = fread(fl, inf, 'int16');
fclose(fl);
figure(1)
subplot(3,1,1);
plot(A);
title('raw');
subplot(3,1,2);
plot(A_4000);
title('zoom');
I use this short code to read the data and plot it, but, as you can see in the capute, the data is full of noise and invalid samples.
The second plot show the first 4000 samples, where you can see the R peaks, but I need to filter and remove invalid samples before calculating HR. Can you please help me? I know I'm asking for too much, but I haven't programmed in more than 10 years, so my knowlegde is rusted and I'm really stucked.
Thanks you in advance and sorry for my english.
Daniel
  댓글 수: 2
Mario Malic
Mario Malic 2024년 5월 12일
Amazing, put this as an answer @Diego please.
Diego Caro
Diego Caro 2024년 5월 12일
Done!

댓글을 달려면 로그인하십시오.

채택된 답변

Diego Caro
Diego Caro 2024년 5월 12일
이동: Image Analyst 2024년 5월 12일
If you only want to estimate the heart rate, my suggestion is that you take the derivative of the ecg signal, so that R peaks increase their amplitude considerably.
Getting the raw signal:
fl = fopen("ficheroAnalogico.dat");
A = fread(fl,inf,'int16');
fs = 1e3; % Setting Sample Frequency
fclose(fl);
A = A - mean(A); % Removing offset
n = length(A);
t = (1:n)/fs;
figure
plot(t,A)
title('Raw data')
xlabel('Time (s)')
ylabel('Amplitude (mV)')
Now getting the derivative:
A_der = A;
for k = 2:length(A)-1
A_der(k) = (A(k+1)-A(k))./(1/fs);
end
figure
plot(t,A_der)
title('Derivative of raw data')
xlabel('Time (s)')
ylabel('Amplitude (mV)')
If you zoom into the figure above (R peaks show consistently as negative spikes):
Then you could use findpeaks to get RR intervals:
[peaks,tpeaks] = findpeaks(-A_der,'MinPeakHeight',1.5,'MinPeakDistance',0.25*fs);
hold on
plot(t(tpeaks),-peaks,'xr')
hold off
Zooming in to verify proper detection of negative peaks:
xlim([0 60])
Finally, computing the heart rate from the average RR interval (AVNN)
NN = 0;
j = 1;
peak_count = length(peaks);
NNs = zeros(1,peak_count-1);
for i = 1:peak_count-1
NNi = tpeaks(j+1) - tpeaks(i);
NNs(i) = NNi;
NN = NN + NNi;
j = j + 1;
end
AVNN = NN/(fs*length(peaks));
BPM = 60/AVNN
BPM =
152.4790
  댓글 수: 3
Diego Caro
Diego Caro 2024년 5월 13일
In fact, the instantaneous BPM you obtained is correct for the signal provided. If you look at whats happening with the raw signal at the moment the derivative goes flat, you see the raw signal is also flat.
If you want to obtain a figure like HR2, I'd suggest using a moving average filter with movmean, to smooth out the spike at the end of the plot you obtained.
BPM2 = movmean(B,5); %try different values for k
tt = max(t)*(0:length(B)-1)/length(B);
figure
plot(tt,BPM2)
title('Smooth HR')
xlabel('Time (s)')
ylabel('BPM')
ylim([0 200])
Daniel
Daniel 2024년 5월 14일
I will try the movmean function to get better results.
Once again, thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 ECG / EKG에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by