ECG simulation using MATLAB
Implementation in MATLAB:
Code:
Save the below file as complete.m
x=0.01:0.01:2;
default=input('Press 1 if u want default ecg signal else press 2:\n');
if(default==1)
li=30/72;
a_pwav=0.25;
d_pwav=0.09;
t_pwav=0.16;
a_qwav=0.025;
d_qwav=0.066;
t_qwav=0.166;
a_qrswav=1.6;
d_qrswav=0.11;
a_swav=0.25;
d_swav=0.066;
t_swav=0.09;
a_twav=0.35;
d_twav=0.142;
t_twav=0.2;
a_uwav=0.035;
d_uwav=0.0476;
t_uwav=0.433;
else
rate=input('\n\nenter the heart beat rate :');
li=30/rate;
%p wave specifications
fprintf('\n\np wave specifications\n');
d=input('Enter 1 for default specification else press 2: \n');
if(d==1)
a_pwav=0.25;
d_pwav=0.09;
t_pwav=0.16;
else
a_pwav=input('amplitude = ');
d_pwav=input('duration = ');
t_pwav=input('p-r interval = ');
d=0;
end
%q wave specifications
fprintf('\n\nq wave specifications\n');
d=input('Enter 1 for default specification else press 2: \n');
if(d==1)
a_qwav=0.025;
d_qwav=0.066;
t_qwav=0.166;
else
a_qwav=input('amplitude = ');
d_qwav=input('duration = ');
t_qwav=0.1;
d=0;
end
%qrs wave specifications
fprintf('\n\nqrs wave specifications\n');
d=input('Enter 1 for default specification else press 2: \n');
if(d==1)
a_qrswav=1.6;
d_qrswav=0.11;
else
a_qrswav=input('amplitude = ');
d_qrswav=input('duration = ');
d=0;
end
%s wave specifications
fprintf('\n\ns wave specifications\n');
d=input('Enter 1 for default specification else press 2: \n');
if(d==1)
a_swav=0.25;
d_swav=0.066;
t_swav=0.125;
else
a_swav=input('amplitude = ');
d_swav=input('duration = ');
t_swav=0.125;
d=0;
end
%t wave specifications
fprintf('\n\nt wave specifications\n');
d=input('Enter 1 for default specification else press 2: \n');
if(d==1)
a_twav=0.35;
d_twav=0.142;
t_twav=0.18;
else
a_twav=input('amplitude = ');
d_twav=input('duration = ');
t_twav=input('s-t interval = ');
d=0;
end
%u wave specifications
fprintf('\n\nu wave specifications\n');
d=input('Enter 1 for default specification else press 2: \n');
if(d==1)
a_uwav=0.035;
d_uwav=0.0476;
t_uwav=0.433;
else
a_uwav=input('amplitude = ');
d_uwav=input('duration = ');
t_uwav=0.433;
d=0;
end
end
pwav=p_wav(x,a_pwav,d_pwav,t_pwav,li);
%qwav output
qwav=q_wav(x,a_qwav,d_qwav,t_qwav,li);
%qrswav output
qrswav=qrs_wav(x,a_qrswav,d_qrswav,li);
%swav output
swav=s_wav(x,a_swav,d_swav,t_swav,li);
%twav output
twav=t_wav(x,a_twav,d_twav,t_twav,li);
%uwav output
uwav=u_wav(x,a_uwav,d_uwav,t_uwav,li);
%ecg output
ecg=pwav+qrswav+twav+swav+qwav+uwav;
figure(1)
plot(x,ecg);
Save the below file as p_wav.m
function [pwav]=p_wav(x)
l=1;
a=0.25
x=x+(1/1.8);
b=3;
n=100;
p1=1/l
p2=0
for i = 1:n
harm1=(((sin((pi/(2*b))*(b-(2*i))))/(b-
(2*i))+(sin((pi/(2*b))*(b+(2*i))))/(b+(2*i)))*(2/pi))*cos((i*pi*x)/l);
p2=p2+harm1
end
pwav1=p1+p2;
pwav=a*pwav1;
Save the below file as q_wav.m
function [qwav]=q_wav(x)
l=1;
x=x+l/6
a=0.025;
b=15;
n=100;
q1=(a/(2*b))*(2-b);
q2=0
for i = 1:n
harm5=(((2*b*a)/(i*i*pi*pi))*(1-cos((i*pi)/b)))*cos((i*pi*x)/l);
q2=q2+harm5;
end
qwav=-1*(q1+q2);
Save the below file as qrs_wav.m
function [qrswav]=qrs_wav(x)
l=1;
a=1;
b=5;
n=100;
qrs1=(a/(2*b))*(2-b);
qrs2=0
for i = 1:n
harm=(((2*b*a)/(i*i*pi*pi))*(1-cos((i*pi)/b)))*cos((i*pi*x)/l);
qrs2=qrs2+harm;
end
qrswav=qrs1+qrs2;
Save the below file as s_wav.m
function [swav]=s_wav(x)
l=1;
x=x-l/6
a=0.25;
b=15;
n=100;
s1=(a/(2*b))*(2-b);
s2=0
for i = 1:n
harm3=(((2*b*a)/(i*i*pi*pi))*(1-cos((i*pi)/b)))*cos((i*pi*x)/l);
s2=s2+harm3;
end
swav=-1*(s1+s2);
Save the below file as t_wav.m
function [twav]=t_wav(x)
l=1;
a=0.35
x=x-(1/1.8);
b=7;
n=20;
t1=1/l
t2=0
for i = 1:n
harm2=(((sin((pi/(2*b))*(b-(2*i))))/(b-
(2*i))+(sin((pi/(2*b))*(b+(2*i))))/(b+(2*i)))*(2/pi))*cos((i*pi*x)/l);
t2=t2+harm2
end
twav1=t1+t2;
twav=a*twav1;
Save the below file as u_wav.m
function [uwav]=u_wav(x)
l=1;
a=0.03;
x=x-(1/1.1);
b=21;
n=100;
u1=1/l;
u2=0;
for i = 1:n
harm4=(((sin((pi/(2*b))*(b-(2*i))))/(b-
(2*i))+(sin((pi/(2*b))*(b+(2*i))))/(b+(2*i)))*(2/pi))*cos((i*pi*x)/l);
u2=u2+harm4;
end
uwav1=u1+u2;
uwav=a*uwav1;
precautions:
• All the files have to be saved in the same folder
• Save the files in the names mentioned above the code
• While entering the specification, give the amplitude in mV and duration in
seconds
Output waveform:
Default Specification
• Heart beat :72
• Amplitude:
P wave 25mV
R wave 1.60mV
Q wave 0.025mV
T wave 0.35mV
• Duration:
P-R interval 0.16s
S-T interval 0.18s
P interval 0.09s
QRS interval 0.11s
Not all the default values are specified here. They can be obtained from the code of
the simulator from the file complete.m. The user can enter their desired values of
specifications too. Other concepts of the code are simple and are self explanatory.
Code:
Save the below file as complete.m
x=0.01:0.01:2;
default=input('Press 1 if u want default ecg signal else press 2:\n');
if(default==1)
li=30/72;
a_pwav=0.25;
d_pwav=0.09;
t_pwav=0.16;
a_qwav=0.025;
d_qwav=0.066;
t_qwav=0.166;
a_qrswav=1.6;
d_qrswav=0.11;
a_swav=0.25;
d_swav=0.066;
t_swav=0.09;
a_twav=0.35;
d_twav=0.142;
t_twav=0.2;
a_uwav=0.035;
d_uwav=0.0476;
t_uwav=0.433;
else
rate=input('\n\nenter the heart beat rate :');
li=30/rate;
%p wave specifications
fprintf('\n\np wave specifications\n');
d=input('Enter 1 for default specification else press 2: \n');
if(d==1)
a_pwav=0.25;
d_pwav=0.09;
t_pwav=0.16;
else
a_pwav=input('amplitude = ');
d_pwav=input('duration = ');
t_pwav=input('p-r interval = ');
d=0;
end
%q wave specifications
fprintf('\n\nq wave specifications\n');
d=input('Enter 1 for default specification else press 2: \n');
if(d==1)
a_qwav=0.025;
d_qwav=0.066;
t_qwav=0.166;
else
a_qwav=input('amplitude = ');
d_qwav=input('duration = ');
t_qwav=0.1;
d=0;
end
%qrs wave specifications
fprintf('\n\nqrs wave specifications\n');
d=input('Enter 1 for default specification else press 2: \n');
if(d==1)
a_qrswav=1.6;
d_qrswav=0.11;
else
a_qrswav=input('amplitude = ');
d_qrswav=input('duration = ');
d=0;
end
%s wave specifications
fprintf('\n\ns wave specifications\n');
d=input('Enter 1 for default specification else press 2: \n');
if(d==1)
a_swav=0.25;
d_swav=0.066;
t_swav=0.125;
else
a_swav=input('amplitude = ');
d_swav=input('duration = ');
t_swav=0.125;
d=0;
end
%t wave specifications
fprintf('\n\nt wave specifications\n');
d=input('Enter 1 for default specification else press 2: \n');
if(d==1)
a_twav=0.35;
d_twav=0.142;
t_twav=0.18;
else
a_twav=input('amplitude = ');
d_twav=input('duration = ');
t_twav=input('s-t interval = ');
d=0;
end
%u wave specifications
fprintf('\n\nu wave specifications\n');
d=input('Enter 1 for default specification else press 2: \n');
if(d==1)
a_uwav=0.035;
d_uwav=0.0476;
t_uwav=0.433;
else
a_uwav=input('amplitude = ');
d_uwav=input('duration = ');
t_uwav=0.433;
d=0;
end
end
pwav=p_wav(x,a_pwav,d_pwav,t_pwav,li);
%qwav output
qwav=q_wav(x,a_qwav,d_qwav,t_qwav,li);
%qrswav output
qrswav=qrs_wav(x,a_qrswav,d_qrswav,li);
%swav output
swav=s_wav(x,a_swav,d_swav,t_swav,li);
%twav output
twav=t_wav(x,a_twav,d_twav,t_twav,li);
%uwav output
uwav=u_wav(x,a_uwav,d_uwav,t_uwav,li);
%ecg output
ecg=pwav+qrswav+twav+swav+qwav+uwav;
figure(1)
plot(x,ecg);
Save the below file as p_wav.m
function [pwav]=p_wav(x)
l=1;
a=0.25
x=x+(1/1.8);
b=3;
n=100;
p1=1/l
p2=0
for i = 1:n
harm1=(((sin((pi/(2*b))*(b-(2*i))))/(b-
(2*i))+(sin((pi/(2*b))*(b+(2*i))))/(b+(2*i)))*(2/pi))*cos((i*pi*x)/l);
p2=p2+harm1
end
pwav1=p1+p2;
pwav=a*pwav1;
Save the below file as q_wav.m
function [qwav]=q_wav(x)
l=1;
x=x+l/6
a=0.025;
b=15;
n=100;
q1=(a/(2*b))*(2-b);
q2=0
for i = 1:n
harm5=(((2*b*a)/(i*i*pi*pi))*(1-cos((i*pi)/b)))*cos((i*pi*x)/l);
q2=q2+harm5;
end
qwav=-1*(q1+q2);
Save the below file as qrs_wav.m
function [qrswav]=qrs_wav(x)
l=1;
a=1;
b=5;
n=100;
qrs1=(a/(2*b))*(2-b);
qrs2=0
for i = 1:n
harm=(((2*b*a)/(i*i*pi*pi))*(1-cos((i*pi)/b)))*cos((i*pi*x)/l);
qrs2=qrs2+harm;
end
qrswav=qrs1+qrs2;
Save the below file as s_wav.m
function [swav]=s_wav(x)
l=1;
x=x-l/6
a=0.25;
b=15;
n=100;
s1=(a/(2*b))*(2-b);
s2=0
for i = 1:n
harm3=(((2*b*a)/(i*i*pi*pi))*(1-cos((i*pi)/b)))*cos((i*pi*x)/l);
s2=s2+harm3;
end
swav=-1*(s1+s2);
Save the below file as t_wav.m
function [twav]=t_wav(x)
l=1;
a=0.35
x=x-(1/1.8);
b=7;
n=20;
t1=1/l
t2=0
for i = 1:n
harm2=(((sin((pi/(2*b))*(b-(2*i))))/(b-
(2*i))+(sin((pi/(2*b))*(b+(2*i))))/(b+(2*i)))*(2/pi))*cos((i*pi*x)/l);
t2=t2+harm2
end
twav1=t1+t2;
twav=a*twav1;
Save the below file as u_wav.m
function [uwav]=u_wav(x)
l=1;
a=0.03;
x=x-(1/1.1);
b=21;
n=100;
u1=1/l;
u2=0;
for i = 1:n
harm4=(((sin((pi/(2*b))*(b-(2*i))))/(b-
(2*i))+(sin((pi/(2*b))*(b+(2*i))))/(b+(2*i)))*(2/pi))*cos((i*pi*x)/l);
u2=u2+harm4;
end
uwav1=u1+u2;
uwav=a*uwav1;
precautions:
• All the files have to be saved in the same folder
• Save the files in the names mentioned above the code
• While entering the specification, give the amplitude in mV and duration in
seconds
Output waveform:
Default Specification
• Heart beat :72
• Amplitude:
P wave 25mV
R wave 1.60mV
Q wave 0.025mV
T wave 0.35mV
• Duration:
P-R interval 0.16s
S-T interval 0.18s
P interval 0.09s
QRS interval 0.11s
Not all the default values are specified here. They can be obtained from the code of
the simulator from the file complete.m. The user can enter their desired values of
specifications too. Other concepts of the code are simple and are self explanatory.
No comments