Author Topic: Real time plot ecg in Matlab (Pan-Tompkins algorithm)  (Read 9780 times)

peppenaso

  • Newbie
  • *
  • Posts: 1
    • View Profile
Real time plot ecg in Matlab (Pan-Tompkins algorithm)
« on: November 13, 2013, 12:09:55 PM »
hello to everyone!
I would like to know how to implement the plotting of an ECG in real time.
the matlab code is below:

clear all
close all
x1 = load ( ' . txt ');
fs = 200; % Sampling rate
N = length ( x1) ; % Signal length)
t = [ 0 : N- 1] / fs ; % time index
t1 = input ( ' enter the time of detection of the ECG in seconds :');
Figures ( 1 )
subplot ( 2,1,1 )
plot ( t , x1 )
xlabel ( 'second' ), ylabel ( ' Volts ' ) title (' ECG Signal Input ' )
subplot ( 2,1,2 )
plot ( t ( 200:600 ) , x1 ( 200:600 ) )
xlabel ( 'second' ), ylabel ( ' Volts ' ) title (' Input ECG Signal 1-3 second' )
XLIM ( [1 3] )
CANCELLATION % DC DRIFT AND NORMALIZATION
x1 = x1 - mean ( x1) ;
x1 = x1 / max ( abs ( x1) ) ;
Figures ( 2 )
subplot ( 2,1,1 )
plot ( t , x1 )
xlabel ( 'second' ), ylabel ( ' Volts ' ) title (' ECG Signal after cancellation DC drift and normalization ' )
subplot ( 2,1,2 )
plot ( t ( 200:600 ) , x1 ( 200:600 ) )
xlabel ( 'second' ), ylabel ( ' Volts ' ) title (' ECG Signal 1-3 second' )
XLIM ( [1 3] )
% LOW PASS FILTERING
% LPF ( 1 - z ^ -6 ) ^ 2 / ( 1 - z ^ -1) ^ 2
[b a] = cheby2 ( 8,0.001,0.08 ) ;
h_LP = filter (b, a , [1 zeros ( 1,12 ) ] ) ;
x2 = conv ( x1, h_LP ) ;
x2 = x2 (6 + [1: N] ) ;
x2 = x2 / max ( abs ( x2) ) ;
subplot ( 2,1,1 )
plot ( [0 : length ( x2) -1] / fs , x2)
xlabel ( 'second' ), ylabel ( ' Volts ' ) title (' ECG Signal after LPF ' )
XLIM ( [ 0 max (t) ] )
subplot ( 2,1,2 )
plot ( t ( 200:600 ) , x2 ( 200:600 ) )
xlabel ( 'second' ), ylabel ( ' Volts ' ) title (' ECG Signal 1-3 second' )
XLIM ( [1 3] )
 % HIGH PASS FILTERING
% HPF = Allpass - ( Lowpass ) = z ^ -16 - [ ( 1 - z ^ -32 ) / ( 1 - z ^ -1) ]
[b a] = cheby1 (4.10 E- 10 0.03 ' high ');
h_HP = filter (b, a , [1 zeros (1,32 ) ] ) ;
x3 = conv ( x2, h_HP ) ;
x3 = x3 (16 + [1 : N] ) ;
x3 = x3 / max ( abs ( x3) ) ;
Figures ( 4 )
subplot ( 2,1,1 )
plot ( [0 : length ( x3) -1] / fs , x3 )
xlabel ( 'second' ), ylabel ( ' Volts ' ) title (' ECG Signal after HPF ' )
XLIM ( [ 0 max (t) ] )
subplot ( 2,1,2 )
plot ( t ( 200:600 ) , x3 ( 200:600 ) )
xlabel ( 'second' ), ylabel ( ' Volts ' ) title (' ECG Signal 1-3 second' )
XLIM ( [1 3] )
% DERIVATIVE FILTER
% Make impulse response
h = [-1 -2 0 2 1] / 8;
x4 = conv ( x3, h ) ;
x4 = x4 (2 + [1: N] ) ;
x4 = x4 / max ( abs ( x4 ) ) ;
Figures ( 5 )
subplot ( 2,1,1 )
plot ( [0 : length ( x4 ) -1 ] / fs , x4 )
xlabel ( 'second' ), ylabel ( ' Volts ' ) title (' ECG Signal after Derivative ' )
subplot ( 2,1,2 )
plot ( t ( 200:600 ) , x4 ( 200:600 ) )
xlabel ( 'second' ), ylabel ( ' Volts ' ) title (' ECG Signal 1-3 second' )
XLIM ( [1 3] )
 SQUARING %
x5 = x4 . ^ 2 ;
x5 = x5 / max ( abs (x5) ) ;
figures ( 6 )
subplot ( 2,1,1 )
plot ( [0 : length ( x5 ) -1 ] / fs , x5 )
xlabel ( 'second' ), ylabel ( ' Volts ' ) title (' ECG Signal Squarting ' )
subplot ( 2,1,2 )
plot ( t ( 200:600 ) , x5 ( 200:600 ) )
xlabel ( 'second' ), ylabel ( ' Volts ' ) title (' ECG Signal 1-3 second' )
XLIM ( [1 3] )
 % MOVING WINDOW INTEGRATION
% Make impulse response
h = ones ( 1 , 31 ) / 31 ;
Delay = 15; % Delay in samples
x6 = conv ( x5, h ) ;
x6 = x6 (15 + [1: N] ) ;
x6 = x6 / max ( abs (x6) ) ;
Figures ( 7 )
subplot ( 2,1,1 )
plot ( [0 : length ( x6 ) -1 ] / fs , x6 )
xlabel ( 'second' ), ylabel ( ' Volts ' ) title (' ECG Signal after Averaging ' )
subplot ( 2,1,2 )
plot ( t ( 200:600 ) , x6 ( 200:600 ) )
xlabel ( 'second' ), ylabel ( ' Volts ' ) title (' ECG Signal 1-3 second' )
XLIM ( [1 3] )
 FIND POINTS % QRS
max_h = max (x6) ;
thresh = mean ( x6 ) ;
poss_reg = (x6 > thresh * max_h ) ' ;
figures (8 )
subplot ( 2,1,1 )
hold on
plot ( t ( 200:600 ) , x1 ( 200:600 ) / max ( x1) )
box on
xlabel ( 'second' ), ylabel ( ' Integrated ' )
XLIM ( [1 3] )
subplot ( 2,1,2 )
plot ( t ( 200:600 ) , x6 ( 200:600 ) / max (x6) )
xlabel ( 'second' ), ylabel ( ' Integrated ' )
XLIM ( [1 3] )
left = find ( diff ( [0 poss_reg ] ) == 1) ;
right = find ( diff ( [ poss_reg 0]) == -1) ;
beat_count = 0;
for i = [ 1: length (left ) ]
    [ R_value ( i) R_loc ( i) ] = max ( x1 (left ( i) : right ( i) ) ) ;
    R_loc (i) = R_loc ( i) -1 + left ( i) ; % add offset

    [ Q_value ( i) Q_loc ( i) ] = min ( x1 (left ( i) : R_loc ( i) ) ) ;
    Q_loc (i) = Q_loc ( i) -1 + left ( i) ; % add offset

    [ S_Value (s) S_loc (s)] = min ( x1 (left (s) : right (i ))) ;
    S_loc (i) = S_loc ( i) -1 + left ( i) ; % add offset
beat_count beat_count = +1;
end
% There is no selective wave
Q_loc = Q_loc ( find ( Q_loc ~ = 0) ) ;
R_loc = R_loc ( find ( R_loc ~ = 0) ) ;
S_loc = S_loc ( find ( S_loc ~ = 0) ) ;
Figures (9 )
subplot ( 2,1,1 )
title (' ECG Signal with R- points ');
plot ( t , x1 , t ( R_loc ) , R_value , ' r ^ ' , t ( S_loc ) , s_Value , ' * ' , t ( Q_loc ) , Q_value , ' o') ;
legend ( ' ECG ' , ' R ' , ' S ' , ' Q ');
subplot ( 2,1,2 )
plot ( t ( R_loc ) , R_value , ' r ^ ' , t ( S_loc ) , s_Value , ' * ' , t ( Q_loc ) , Q_value , ' o') ;
XLIM ( [1 3] )
% Calculate distance peaks RR in ms
figures ( 10 )
peakInterval = diff ( R_loc ) ;
plot ( peakInterval ) ;
xlabel ( ' beats ' ), ylabel ( ' distance in time in sec * 10 ^ -3 ' ) grid on ;
title (' distance curve peaks R ' )
% Calculate number of beats per minute
beats_per_minutes =

What I should do is plot the ECG in a time window of 2 sec ( an indication of the points representing the QRS complex ), then go to the next window of 2 sec and so on ...
thanks

 

Sitemap 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71