You are on page 1of 22

Data acquisition

Question 1 Draw a block diagram to illustrate how the data was acquired. Be sure to include
important parameter values
The block diagram illustrating how the signal was acquired is shown in figure 1.

Figure 1 Block diagram illustrating how the signal was acquired


Question 2 If you examine the data, you will notice that the ECG data values have been
rounded to the nearest milivolt. Is this a result of the 16-bit quantization, or was additional
resolution lost after the quantization, explain.
This is because of the 16-bit quantization because the quantization round quantizer output xQ[n] to
kQ where the Q is the quantization step, and k is the integer closest to x[n]/Q. This is, we are going
from a continuous signal to a finite signal (that only takes discrete values).
Question 3 Consider the analog filter used in the data acquisition.
(a) Why was the signal filtered to exclude frequencies above 100Hz prior to sampling?
The signal was filtered to exclude frequencies above 100Hz because we wanted to avoid aliasing
due to signals above 100Hz. For example, to avoid high frequency noise above 100Hz.
Also, the sampling rate is 250Hz. Band limiting the signal at 100Hz, make us to over sample the
signal a little bit so that we are able to better reconstruct the signal (remember that a signal can be
reconstructed if it was sampled at least at the double of the highest frequency). With a sampling rate
of 250Hz we are still able to reconstruct signals of 125Hz.
Finally, because the bandwidth of the ECG signal is believed to be between 0.5Hz and 100Hz.
Some doctors argue that the really important frequencies are contained between 0.1 and 50Hz.
(b) Consider what would have happened if this analog filter had not being used. Assume that
the ECG signal is effectively band limited to 50Hz and that the only noise id due to the third
and sixth harmonics of 60Hz power noise (at 180Hz and 360Hz). Does sampling the unfiltered
analog ECG signal at 250Hz "filter out " the power line noise?
When sampling the signal at 250Hz, we are able to reconstruct signals of 125Hz. Even though the

signals of noise at 180Hz and 360Hz, these are still introducing noise to our signal. This is because
of the aliasing phenomenon. We eliminate most of the noise due to 180 and 360Hz, but the low
frequency components of these signals are still adding noise to our sampled signal (I am assuming
that the noisy signals at 180Hz and 360Hz are not pure sinusoidal as it normally is).

Signal Conditioning/Noise Reduction


The first thing I did was to find a typical 10 seconds segment of clean data, 10 seconds segment of
noisy data and examine the frequency content to have an idea of what low and high cutoff
frequencies I could use to eliminate as much noise as possible and still be able to conserve as much
energy as possible of the original signal. I got this segment from the file called janet.txt. The signals
and their frequency contents can be seen in figure 2.

Figure 2. Clean and noisy segment of 10 seconds of signal of the janet file
Question 4 Describe your bandpass filter. Including plots of your filter's impulse response and
frequency response (in dB versus Hz)
In this step, I experimented with almost all the MATLAB functions for FIR filter design.
I wanted to choose the filter that have the best transition band (slope) and sidelobe magnitude in its
frequency response.

Filter Function
fir1
fir2
firls
remez

Filter design method used


windowing method
Frequency Sampling method
The least squares design method
the Equiripple design method

The second step was to make a graph of all the frequency response of the filters in order to decide
which one was the most suitable for use in this application. The frequency response of the filters
can be seen in Figure 3 for low-frequency cutoff of 3Hz and a high-frequency cutoff of 30Hz.

Figure 3 Magnitude of the frequency response of all the filters designed


In my point of view, the filter that offers the best tradeoff between transition band slope (how
similar it is to the ideal filter) and the magnitude of the sidelobes magnitude is the fir1 filter. The
filter response of the fir1 filter can be seen in figure 4.

Figure 4. Magnitude of the frequency response of the filter designed using the Fir1 MATLAB
function

What were the desired specifications for the filter?


How did you decide on those specifications?
In order to select the desired specifications, I played with the low and high-cutoff frequencies of the
bandpass filter.
First, I tried setting the low-frequency cutoff (FL) to the following values: 1, 2, 3, 5 and 10Hz.
Frequencies under 2 Hz does not completely eliminate the baseline fluctuations. The filtered signal
using FL = 1Hz can be seen in Figure 5. Frequencies over 3Hz eliminate too much low frequencies
that the QRS signal starts deforming (distortioning). This can be seen in figure 6.

Figure 5. low-frequency cutoff of 1Hz

Figure 6. low-frequency cutoff of 10Hz

Secondly, I tried playing with the high-cutoff frequency(FH) using values of 80, 50, 40, 30 and
20Hz. My analysis showed that using frequencies over 40 Hz does not really eliminate the high
frequency noise. This effect can be seen in figure 7. Frequencies under 30 Hz deform (distortion)
the form of the QRS signal. This can be seen in figure 8.

Figure 7. high-frequency cutoff of 50Hz

Figure 8. high-frequency cutoff of 20Hz


Finally, I decided to use the following specifications that in my point of view, are the optimal ones
for this application.

The specifications of the filter are the following:


Filter order
Low cutoff frequency (FL)
High cutoff frequency (FH)

200
2.3Hz
30Hz

I chose these specifications because these cutoff frequencies eliminate the baseline fluctuations (low
frequencies) and at the same time does not eliminate too many high frequencies maintaining the
QRS signal sharp enough for clinical diagnosis. These cutoff frequencies also maintain most of the
energy in the signal. The filtered signal containing low frequency noise can be seen in figure 9 and
the filtered signal containing high frequency noise in figure 10.

Figure 9. Filtered signal originally (low frequency noise segment) FL= 3Hz and FH = 30Hz.

Figure 10. Filtered signal originally (segment with high frequency noise) FL = 3Hz, FH = 30Hz.

What filter design technique did you use?


Since I used the function fir1 to design the FIR filter, I used the windowing method for designing
filters.
How well does your actual filter meet the desired specifications as well as any tradeoffs you
encountered in the design process?
As I mentioned earlier, I tried to find a balance between the low and high cutoff frequencies were
able to eliminate as much noise as possible but without distortioning the original QRS signal. I also
used a tradeoff between the transition bandpass section of the filter (slope) and the sidelobes
magnitude of the frequency response of the filter so that the original QRS signal were not
distortioned too much.
Question 5. Describe the effect of your bandpass filter on both, the clean data and noisy data.
Include plots of the clean and noisy data in both the time and frequency domain before and
after filtering and make relevant comparisons.
The plot of the clean and noisy segments of the signal in the time is shown in figures 9 and 10. The
plots in the frequency domain can be seen in figure 11.

Figure 11. Plot of the time and frequency domain of the clean and noisy data of the signal.
Another way to appreciate the filtering operation is to plot the frequency contents of the noisy
signal after and before the filtering step in the same plot. This can be seen in figure 12.

Figure 12. Frequency contents of the noisy segment of the signal after and before the filtering step.
Question 6. What are the limitations of this bandpass filtering approach? (if it were possible
to implement an ideal filter with any desired specifications, would you expect to remove all of
the noise?)
If we were able to implement an ideal filter with any desired specifications, we could not be able to
remove all the noise. Consider noise that has low and high frequency components. Imagine that the
low frequency components are between 10 and 200Hz and we want to filter a signal between 0.1
and 100Hz. Using a filter between 1 and 100Hz is going to eliminate the high frequency
components of the noise, but not the low frequency components contained between 10 and 100Hz.
This is, we are only going to be able to eliminate the noise if we are willing to throw away our
signal. In other words, we are never going to completely eliminate all noise in a signal by filtering.

Ventricular Arrhythmia Detection


Objective: detect life threatening arrhythmias such as Ventricular fibrillation and ventricular
fluttering.
Question 7. Explain your choice of parameters (window length, window shape, and FFT
length) used with the spectrum function. What is the effective frequency resolution (in Hz) of
the spectral analysis that you performed?
The parameters that I used with the spectrum signal are the following:
Window length
Window Shape
FFT length

512
Hamming
512

First of all, I chose the hamming window because this shape of window is the one that gives the
best tradeoff between frequency and amplitude resolution.

Please remember that the frequency resolution is determined by the window length and shape, and
the amplitude resolution is primarily determined by the window shape.
I chose the FFT length to be the length of the window length because this is the minimum
meaningful length for spectrum analysis.
Increasing the FFT length will provide a more closely spaced frequency samples of the underlying
DTFT. In general, the DFT (FFT) provides more information about the underlying DTFT when we
increase the DFT (FFT) length.
I chose the window length accordingly to the following statements: The mainlobe is inversely
proportional to the window length. Also remember that a narrower mainlobe produces a better
frequency resolution. Thus, we conclude that a desirable characteristic of our window is to be long
enough but without exceeding the number of points of our signal .

The frequency resolution is the following:


4/M = F/Fs

F = 4(Fs)/M

where M is the length of the window Thus, we have:

F = 4(250)/512 = 1.95Hz

Question 8. How do the ventricular arrhythmia segments differ from the normal segments in
both, the time and frequency domain? (include relevant plots)

The first thing that I did in this step is to read the files n_422.bin and n_424.bin using the readecg
function. Once I had the data of both files in memory, I consulted the files atr_n422.txt and
atr_n424.txt (which have an expert evaluation of the QRS signal) so that I could make sure I was
going to get one segment of normal QRS signal and one of ventricular fibrillation/fluttering
segment for each file. I calculated the spectrum of each segment (10 seconds) and I plotted the
segment in the frequency and time domain. The plot of the normal and fibrillation/fluttering
segments in the time domain for file n_422 can be seen in figure13. The plot of the frequency
contents of the same segments for file n_422 is shown in figure 14. The plot of the normal and
fibrillation/fluttering segments for file n_424 can be seen in figure15. The frequency contents for
the same segments of file n_424 are shown in figure16.

Figure 13. Normal and fibrillation/fluttering segment for file n_422.bin

Figure 14. Frequency contents of Normal and fibrillation/fluttering segment for file n_422.bin

Figure 15. Normal and fibrillation/fluttering segment for file n_424.bin

Figure 16. Frequency contents of Normal and fibrillation/fluttering segment for file n_424.bin

After analyzing the "normal" and ventricular fibrillation/fluttering signals in the time and frequency
domain, I found the following characteristics in the signals
"Normal" segment
Before continuing, I would like to point out that I consider a Normal signal all the signals that do
not have ventricular fibrillation or ventricular fluttering. Having said that, the characteristics are the
following
Time domain
The signals could have noise(low or high frequencies) or tachycardia for example, but we are still
able to more or less distinguish the QRS signals. We are still able to say that what we are looking at
an electrocardiographic signal.
Frequency domain
The signal seems to have a more or less smooth decaying form. It has no peaks in the frequency
domain that could represent frequencies with high contain of energy. There are no drastic changes
in the power distribution of the signal.
Ventricular fibrillation/fluttering segment
Time domain
The signal look like being oscillating. It resembles a sinusoidal waveform. At the same time, the
signal sometimes seems to oscillate more with baseline fluctuations. We can see this in figure 17.

Figure 17. Ventricular arrhythmia which seems to oscillate with baseline fluctuations.

Frequency domain
The signal have a peak located between the frequencies of 3Hz and 6.5Hz. This peak is really
obvious, indicating that there exist high energy components there. It is really important to mention
that I was not able to find this peak in normal segments of the signals and that this is the basis of my
arrhythmia detector program.
This peak is consistent with the theory seen in class, because in class we saw that ventricular
fluttering occurs at frequencies of 3.333Hz and 5Hz (200-300 beats/min) Ventricular fibrillation
occurs at frequencies of 5Hz(sinusoid period of 200mS).
Question 9. Describe your approach to distinguish ventricular arrhythmias from normal
rhythms. First, present a quantitative description, explaining the general rationale for the
metric. Then give a more quantitative description of the metric, with attention to areas of
uncertainty of variability. Include a block diagram of flowchart of your system if
appropriate. What are the advantages and disadvantages of your metric? Under what
conditions do you expect it to perform well and under what conditions might it fail? Please
include the matlab code for your ventricular arrhythmia detection as an appendix to your lab
report
My approach to detecting the ventricular fibrillation/fluttering is based on the analysis of the signal
in the frequency domain and based on the main difference that I found between the normal and
fibrillation/fluttering segments.
Remembering the characteristics of the fibrillation/fluttering segments in the frequency domain we
have that the segments have a peak located between the frequencies of 3Hz and 6.5Hz. This peak is
really obvious, indicating that there exist high-energy components there. This peak can be found
using the spectrum function in matlab which calculates the Power Spectrum Density of the signal
(adding the calculation 10*logP).
Since we are interested in detecting the ventricular fibrillation/fluttering in real time, it is necessary
to chop the signal in segments for individual analysis. I decided to chop the original signal in
segments of 10 seconds. This means that at most, we are going to need to wait 10 seconds to detect
the ventricular fibrillation/fluttering in real time. I am assuming that a 10 second delay is not long
enough to cause the dead of a patient. An example of the chopped signal is shown in figure 18.

It is important to note that when I chop the signal, I chop it in overlapping segments. For example,
the first segment goes from 0 to 10 Sec, the second from 5 to 15, the third from 10 to 20 and so
forth. The idea in the overlapping segments is the following:
Imagine that for some reason, the frequency components of the ventricular fluttering/fibrillation
were located precisely between the first and second segment. Then, since the frequency components
were going to be spitted between segment one and segment two, the arrhythmia detector could not
be able to detect the arrhythmia in this part of the signal. Overlapping the segments by half of length
of the segment (5sec), we warrantee that we are always going to be able to analyze the frequency

components of all the signal. Figure 19 is illustrating the overlapping chopping of the signal.

Figure 18. Graph showing the step of splitting the signal in chunks of 10 Seconds Overlapping one
another by half of the period (5 Sec). Just for the first 3 chunks of the total signal.

Figure 19. Illustration of the overlapping of the signal using windows to chop the signal
Once I have chopped the signal in 10 second segments, I use the spectrum function to calculate the
power spectrum density of the signal. Since my detector is based on the existence of the peak in the
frequency domain located between 3 and 6.5 Hz, my detector measures how peaky is the peak
calculating a ration between to areas. In order to do this, the following steps are necessary:
1)._search for the maximum amplitude value between 2.3Hz and 8 Hz (where the peak is more
probable to exist if the segment has ventricular fibrillation/fluttering).
2)._calculate the area under peak-epsilon and peak+epsilon. Epsilon is an increment founded by

trial and error. In my detector, this epsilon has a value of 1.5 Hz. We are going to call this area
area1.
To exemplify this, imagine that the peak was found at F=4Hz. Then, with an epsilon equal to
1.5Hz, we are going to calculate the area between 2.5 and 5.5Hz. In order to calculate the area under
this interval, we need to calculate the integral between this interval. Since the data is discrete, the
integral is just the summation of all the values between this interval.
3)._We calculate the area under the interval 2.3Hz and 8Hz. This interval is the original interval at
which we searched for the maximum value (the peak). We are going to call this area area2.
4)._Once we have the two areas, we calculate the ratio area1/area2. This ratio is always going to be
less than one and is going to give us an idea of how peaky the peak is.
5)._Finally, we can find a threshold by trial and error analyzing and plotting our segmented signals
(60 plots per signal) which gives the best results in detecting the arrhythmias.

Steps 1, 2 and 3 can be seen in the sketch of figure 20.

Figure 20. Graphical example of the calculations of the areas under the peak of the ventricular
fibrillation/fluttering in the frequency domain.

The overall steps that my arrhythmia detector calculates are shown in the block diagram shown in
figure 21.

Figure 21. Example of the analysis of the peak in a normal segment of the file n_422.bin
Now that I have explained how my arrhythmia detector works, I am going to show how it analyzes
the segments of the signals by showing analysis for two segments for the N_422.bin and N_424.bin
files. The exaples can be seen in figures 21,22,23 and 24.

Figure 21. Example of the arrhythmia detection in a normal segment of the file n_422.bin. Note that

the value of the ratio is 0.59 (high because is a segment with tachycardia)

Figure 22. Example of the arrhythmia detection in a segment with ventricular fibrillation of the file
n_422.bin. Note that the value of the ratio is 0.66

Figure 23. Example of the arrhythmia detection in a normal segment of the file n_424.bin. Note that

The value of the ratio is 0.44.

Figure 24 Example of arrhythmia detection in a segment with ventricular fibrillation of the file
n_424.bin. Note that the value of the ratio is 0.65

Nothe: the asterisc (*) in figures 21, 22, 23 and 24 means the area1 region (peak-epsilon, peak
and peak+epsilon). The green circles means area2 (from 2.3 to 8Hz)
Now that we have seen figures 21, 22, 23, and 24 we can set up the threshold for the ration in my
arrhythmia detector. From all the experiments that I ran plotting the frequency and time domain for
all the segments from signals stored in files n_422.bin and n_424.bin, my foundings were the
following:

Type of signal/segment
Normal signal
Ventricular fibrillation/fluttering
Tachycardia

Treshold for ratio


0.30 to 0.59
0.61 to 0.75
0.53-0.54

Then, since I am interested in only detecting Ventricular fibrillation/fluttering, I set the treshold of
ratio to 0.61.

Results of the detection


Question 10. How well does your detector perform? Include plots and/or other figures and
tables to represent the output of your system compared with the expert classification. Does
your detector produce false alarms? Missed detections. Or both? Under what conditions is
your detector more prone to errors?
First, I would like to point the advantages of using the ration between area1 and area2 as a metric
for detecting Ventricular fibrillation/fluttering. The first and most important characteristic is that the
detector is able to detect the Ventricular fibrillation/fluttering even in the first segment of our signal.
Another approaches for detecting arrhythmias involve assuming that the first segment of signal is
normal, and then uses this first segment of the signal to set the a treshold. This approach does not
allow to detect the arrhythmia in the first segment and also it never detects the arrhythmia if the first
segment of the signal already contains arrhythmia (the electrodes were connected to a patient
suffering from arrhythmia since the beginning). This is not a problem at all for my detector, since
the metric used for the detection the comparison between to areas within the same segment of the
signal being analyzed.
Now, I show the results of running my arrhythmia detector for the files n_422, n_424, n_426 and
429. After this, a discussion about the problems of my detector is done.
The output of the detector is a matrix called alarm which list the following information:
First Row
Second Row
Third Row

Shows 1 (the alarm is on )


The sample number of the beginning of the segment
The sample number of the last sample of the segment

file n_422.bin Episode of ventricular fibrillation


alarm =
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1

10001
11251
12501
13751
15001
16251
17501
18751
20001
21251
22501
23751
25001
26251
27501
28751
30001
31251
32501
33751
35001

12500
13750
15000
16250
17500
18750
20000
21250
22500
23750
25000
26250
27500
28750
30000
31250
32500
33750
35000
36250
37500

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1

36251
37501
38751
40001
41251
42501
48751
50001
52501
53751
55001
56251
57501
58751
62501
63751
65001
66251
67501
68751
70001
71251
72501

38750
40000
41250
42500
43750
45000
51250
52500
55000
56250
57500
58750
60000
61250
65000
66250
67500
68750
70000
71250
72500
73750
75000

file n_424 Episode of ventricular fibrillation


alarm =
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1

1251
2501
3751
17501
26251
27501
28751
30001
31251
32501
33751
35001
36251
37501
38751
40001
41251
42501
43751
45001
46251
47501
48751
50001

3750
5000
6250
20000
28750
30000
31250
32500
33750
35000
36250
37500
38750
40000
41250
42500
43750
45000
46250
47500
48750
50000
51250
52500

file n_426 Ventricular fibrillation and low frequency noise


alarm =
1
1
1
1
1
1
1
1
1

25001
26251
27501
40001
41251
48751
61251
63751
70001

27500
28750
30000
42500
43750
51250
63750
66250
72500

72501

75000

File n_429 Ventricular flutter (2 episodes) and ventricular tachycardia


alarm =
1
1
1

27501
62501
63751

30000
65000
66250

File n_421 Normal sinus Normal sinus rhythm with noise


alarm =
1
1

5001
58751

7500
61250

Comparing the results with the atr_n4XX.txt files which are diagnosis of the signals contained in
the files n_4XX.bin done by experts, I can conclude that my alarm detector is not often triggered by
noise. We can see this from the results of running the detector in the file n_421.bin which is just the
normal sinus signal with noise. We can see that in this file, my detector only turned on the alarm
two times.
We can also see from the results of running the detector in the file n_422, that the detector is often
triggered by the tachycardia. This can be explained looking at the thresholds found by trial and error
in which the ratio value for the tachycardia is really close to the threshold for the arrhythmias.
Fortunately, the detector almost never missed detections and when it missed detections, the alarm
turned on at least one time in the correct segment (segment tagged as arrhythmia by the experts).
Question 11. If you had more time to work in this problem, how would you attempt to
improve the detector?
If I had more time to work in this problem, I would check that when calculating the areas, the range
of area2 is dynamically relocated, so that the area1 is never touching the limits of area2. I will also
add a matching filter. I am sure that using a matching filter could improve the arrhythmia detection.
I would improve the detector so that it could also detect tachycardias. This would be more or less
easy, since I observed ratio values of 0.53-0.54 when the tachycardia is present. I would not only
analyze the signal using the matlab spectrum function, but also, a filterbank. Then, I would compare
both approaches to see which one detect better the arrythmia or probably use the filterbank to
improve the arrhythmia detector.

Question 12. What is the most important thing that you learned from the lab exercise?
I learned a very interesting application of signal processing, and the most important thing, an

application that can save lifes. I learned a lot from matlab, signal processing, the physiology of the
hearth and characteristics of the QRS signal.
Question 13. What did you like/dislike the most about this lab exercise?
At the beginning, I was not familiar with matlab. It took me a lot of time and effort to get familiar
with it. Most of the time spent in this lab was looking at the matlab help and looking how the
commands work in the internet