How to convert 1D-ECG signal into 2D-ECG image

12 views (last 30 days)
Sanjay Kumar
Sanjay Kumar on 17 Aug 2020
Answered: Prasanna on 5 Nov 2024 at 6:46
I have to convert 1D- ECG signal into 2D- ECG signal. How to covert it using Pan Thomkins Algorithm?

Answers (1)

Prasanna
Prasanna on 5 Nov 2024 at 6:46
Hi Sanjay,
The Pan-Tompkins algorithm is mainly used for QRS detection, and while it doesn’t inherently convert 1D signals to 2D, it can be part of a pre-processing step before conversion. To convert 1D signal to 2D, you can get 2 spectral pictures of time or frequency. Therefore, the ‘reshape’ function can be used to convert from a vector to a matrix and the Pan-Tompkins algorithm can just be used for preprocessing.
To implement the Pan-Tompkins algorithm, refer the following steps:
  • Remove the baseline wander and high-frequency noise of the signal by bandpass filtering
  • Differentiate the signal to emphasize slopes of the QRS complex
  • Square the signal to enhance the QRS peaks
  • Smoothen the signal to obtain a waveform suitable for thresholding
  • Detect QRS complexes by thresholding.
  • After detecting QRS complexes, you can convert the signal to a 2D format. One common approach is using a spectrogram or reshaping the signal into a matrix.
A sample MATLAB code to perform the above steps is as following assuming a sample random ECG signal.
% Generate a random ECG signal
fs = 360; % Sampling frequency
t = 0:1/fs:10;
% Simulate a random ECG-like signal using a sine wave with added noise
ecg_signal = 0.5 * sin(2 * pi * 1 * t) + 0.1 * randn(size(t)); % 1 Hz sine wave + noise
% Preprocess the ECG signal using Pan-Tompkins Algorithm
% Bandpass filter
[b, a] = butter(1, [5 15] / (fs / 2), 'bandpass');
ecg_filtered = filtfilt(b, a, ecg_signal);
% Differentiate
ecg_diff = diff(ecg_filtered);
% Square
ecg_squared = ecg_diff .^ 2;
% Moving window integration
window_size = round(0.15 * fs);
ecg_integrated = movmean(ecg_squared, window_size);
% Thresholding to detect QRS complexes
threshold = max(ecg_integrated) * 0.6;
qrs_peaks = ecg_integrated > threshold;
% converting it to a 2D representation by spectrogram/reshaping
% Spectrogram
figure;
spectrogram(ecg_signal, 128, 120, 128, fs, 'yaxis');
title('ECG Spectrogram');
% Reshape into a 2D matrix
segment_length = 256; % Define segment length
num_segments = floor(length(ecg_signal) / segment_length);
ecg_2d = reshape(ecg_signal(1:num_segments*segment_length), segment_length, num_segments);
The above code outputs the following:
This approach uses QRS detection with a method to visualize the ECG in 2D, which can be useful for pattern recognition or feature extraction tasks. However, the Pan-Thompkins algorithm is just used here for pre-processing and not for converting the ECG signal into 2D explicitly. You can further adjust the corresponding parameters for specific ECG data. For more information on the algorithm and functions used, refer the following resources:
Hope this helps!

Categories

Find more on Denoising and Compression in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!