How to do TKEO based Peak Detector in ECG In Simulink Realtime?

31 views (last 30 days)
I have been trying to do ECG peak detection using TKEO in matlab and has done the same in matlab and has got results
Heres the code for it
% Passband frequency range in Hz
[b, a] = butter(4, fc/(fs/2), 'bandpass');
ecg_filtered = filter(b, a, ecg);
% Apply TKEO to enhance R-peaks
tkeo = ecg_filtered(2:end-1).^2 - ecg_filtered(1:end-2).*ecg_filtered(3:end);
% Apply moving average filter to smooth the TKEO signal
win_size = round(fs*0.15); % Window size in samples (0.15 seconds)
tkeo_smooth = movmean(tkeo, win_size);
% Find R-peak locations
[r_peaks_tkeo, r_locs_tkeo] = findpeaks(tkeo_smooth, 'MinPeakDistance', 50,'MinPeakHeight',mean(tkeo_smooth)); % 0.2 seconds between R-peaks
r_locs_tkeo_updated = r_locs_tkeo+3;
figure;
plot(t, ecg_filtered);
xlim([0 10])
title('Filtered ECG')
xlabel('Time(Sec)')
ylabel('Amplitude')
But Now I would like to do the same realtime using simulink and arduino.
I have done the filtering using Butterworth filter part in Simulink using Digital filter design block.
But I dont know how to implement the TKEO Block so that I would get R peaks.
Kindly Help Me Out
Thank You in Advance

Answers (1)

Omega
Omega on 12 Sep 2023
Hi Krishnapriya,
I understand that you are looking for assistance in translating your MATLAB code for ECG peak detection using TKEO into a real-time Simulink model that can be used with an Arduino for continuous ECG data processing.
To implement the TKEO algorithm, you can create a custom “MATLAB Function block” Here’s a step-by-step breakdown of how to implement the TKEO algorithm in Simulink:
  • Create a MATLAB Function block.
  • In the MATLAB Function block, define the algorithm following your MATLAB code. Here's an example implementation of the TKEO algorithm in Simulink's MATLAB Function block:
function tkeo_output = myTKEO(ecg_filtered)
% Apply TKEO to enhance R-peaks
tkeo = ecg_filtered(2:end-1).^2 - ecg_filtered(1:end-2) .* ecg_filtered(3:end);
% Return the TKEO signal
tkeo_output = tkeo;
end
  • Connect the input (filtered ECG signal) to the MATLAB Function block.
  • Connect the output of the MATLAB Function block to the subsequent blocks in your model.
R-Peak Detection:
To detect R-peaks in the TKEO signal, you can use a “Switch block” as a threshold or a custom “MATLAB Function block”. The key idea is to set a threshold and detect peaks in the TKEO signal above that threshold.
To know more you can refer to the following documentation links:
I hope this helps!

Categories

Find more on Applications in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!