To generate and analyze a real-time ECG signal using MATLAB, introduce noise, and apply digital filtering techniques to remove unwanted interference, thereby improving the quality of the signal.
1 Computer system with MATLAB software installed
2 ECG data file ()
3 MATLAB script for ECG signal processing
The real-time signal used in this experiment is the Electrocardiogram (ECG) signal. It represents the electrical activity of the human heart over time.
An ECG signal typically consists of repeating P, QRS, and T waves, which are vital for diagnosing heart conditions. The signal is analog in nature but is converted to digital form for processing in MATLAB.
-
Loading the ECG Signal: The ECG data is first loaded into MATLAB using the load command. This data represents the amplitude of the heart signal sampled at a specific frequency.
-
Noise Reduction (Averaging Filter): A simple moving average filter is applied to smooth the ECG waveform and reduce small fluctuations.
-
Noise Addition (50 Hz Power Line Interference): Artificial 50 Hz sinusoidal noise is added to simulate the common electrical interference seen in biomedical devices.
-
Frequency Analysis (FFT): Fast Fourier Transform (FFT) is used to convert the ECG signal from the time domain to the frequency domain, where noise components are easily identified.
5.Filtering in Frequency Domain: A notch filter is implemented to remove the 50 Hz noise component. The signal is then reconstructed using the inverse FFT (IFFT).
- Comparison: The clean ECG signal, noisy ECG signal, and filtered ECG signal are plotted to visualize the effect of filtering.
clear all; close all; clc;
%fs=400;
%%[a fs]=wavread('new.wav');
load ecgdata.dat -ascii;
a=ecgdata;
T=1;fs=1000;
N=length(a); % no of samples per second
ts=1/fs;
t=(0:N-1)*ts; % time axis definition 0:ts:N-1
plot(t,a);
% averaging filter
out=zeros(1,N);
out(1)=a(1);
out(N)=a(N);
for i=2:N-1
out(i)=(a(i-1)+a(i)+a(i+1))/3; % 1 X 3 averaging filter
end
plot(t,out);
% noise effect
n=0.25*sin(2*pi*50*t); % Noise
outn=out+n; % noise signal is added
%f1=20;f2=50;f3=80;
%y=sin(2*pi*f1*t)+sin(2*pi*f2*t)+sin(2*pi*f3*t);
L=2^nextpow2(N);%2^n > no of samples
yf=fft(out,L);
xf=fft(outn,L);
f=fs*(0:L-1)/L;% frequency axis definition (0: L-1)/L is normalization
subplot(321), plot(t,out); % original signal
subplot(322), plot(f,abs(yf)); % spectrum of original signal
set(gca,'xlim',[0 0.5*fs],'ylim',[0 max(abs(yf))]);
subplot(323), plot(t,outn); % original signal with 50hz noise
subplot(324), plot(f,abs(xf));% spectrum wth 50Hz noise
set(gca,'xlim',[0 0.5*fs],'ylim',[0 max(abs(xf))]);
H=ones(1,L); % defining the filter with all ones
l1=floor(50*L/fs); %bin number for 50 hz (take min ie 26.5 will take 26)
l2=L-l1+1; % complex conjugate
%l2=5000*L/fs; %high frequency
for i=1:L
if ((i==l1)&&(i==l1-1)&&(i==l2)&&(i==l2-1)&&(i==l1+1)&&(i==l2+1)) % bin no corresponding to f=50 and conjugatesare made 1
H(i)=0;
end
end
G=xf.*H;
G1=real(ifft(G));
N1=length(G1); % no of samples per second
t1=(0:N1-1)*ts;
subplot(325), plot(t1,G1); % recovered signal
subplot(326), plot(f,abs(G1)); %recovered signal spectrum
set(gca,'xlim',[0 0.5*fs],'ylim',[0 max(abs(G1))]);% spectrum after removing 50 hz noise
% truncate the axis to half
% gca get current axes and set fs as half
After running the MATLAB code, the following results are observed through subplots:
Subplot Description
(3,2,1) Original filtered ECG signal (clean waveform)
(3,2,2) Spectrum of clean ECG signal
(3,2,3) ECG signal with added 50 Hz noise
(3,2,4) Spectrum showing 50 Hz interference
(3,2,5) Filtered ECG signal after removing 50 Hz noise
(3,2,6) Spectrum after filtering (noise removed)
The experiment demonstrates how MATLAB can be used to perform real-time ECG signal analysis and filtering. By applying digital processing techniques, unwanted noise was removed, and the quality of the ECG signal was enhanced for accurate medical interpretation.
