You are on page 1of 16

# ESE 482 DIGITAL SIGNAL PROCESSING - WASHINGTON UNIVERSITY IN SAINT LOUIS - FINAL PROJECT - DECEMBER 14, 2010

## Filtering and Signal-Averaging Algorithms for Raw ECG Signals

Matthias Chan
Abstract Interpreting data from raw ECG signals can be fairly complicated if the signal is not extracted with care. Updating the standards for lters that was established in 1975, we will design new linear phase lters that will extract the signal and reduce noise and baseline drift. We then explore the use of signal averaging to nd the Signal-Averaged ECG (SAECG) for signals from four different leads. We will discuss several design choices that we encountered and show the results of our algorithm. Index Terms Matched Filtering, SAECG, ECG, Baseline Drift, High Frequency Noise

of multiple waveforms over a nite period of time[3]. In averaging the waveforms over the length of a signal, we hope to nd different micropotentials that we might otherwise discard as noise. There are three distinct parts to this problem. First, we will try to remove the signal baseline and drift from the raw data. Second, we will lter out the high frequency noise. Lastly, we will nd the SAECG from the entire signal. There are many different considerations to be taken into account, but we will discuss them in the next section. III. P ROBLEM A PPROACH To solve this problem, we split the problem into three subproblems. A. Removing Baseline Drift To remove the baseline drift, we created a high pass lter to cutout some of the really low frequencies which are responsible for this drift. The standard for the cutoff frequency was .05 Hz , but if we use a linear phase lter, then we can increase the cutoff frequency to up to .67 Hz . To design this lter, rst I will use a variety of different lters and see what the effect of the lter is, as well as take into consideration things like runtime, and how good the signal looks. After choosing the type of lter, I will then tweak the different parameters that go into selecting the lter such as passband corner, stopband corner, passband and stopband attenuation. We looked at 5 different lters; 1 FIR (Finite Impulse Response) and 4 IIR (Innite Impulse Response) lters. The FIR lter (Kaiser) is a windowing method which nds a nearoptimal window to use in windowing. The Butterworth lter exhibits monotonic decreases in the passband and stopband. The Elliptic lter is equiripple in both the stopband and the passband. The Chebyshev Type I lter is equiripple in the passband and varies monotonically in the stopband. While the Chebyshev Type II lter is the reverse of the Type I lter[4]. The FIR lters are linear phase, but they introduce a time lag since the group delay is a constant. The IIR lters have nonlinear phase, but we can take the signal, lter it, and then time reverse the sequence and run it through the lter again. This process creates a zero phase lter, since we have the negative phase for each frequency, which makes the phase zero for all frequencies. We can also look at the Fourier transform of the ECG signal to see which frequencies are being cut out and attenuated. All those things will be taken into account and shown in section IV.

I. I NTRODUCTION Electrocardiogram (ECG) signals show the electrical activity of the heart from a lead that is connected externally to a patient[1]. The raw signal that is obtained needs to be ltered in order to produce a readable ECG. With the addition of ltering, we may also want to increase the resolution of the signal by averaging many waves over a length of time to form one ECG wave which we can read. Things that are important to note in an ECG are the placement and duration of the Pwave, the QRS complex, the T-wave, and the ST segment. As we will see in this project, different placement of leads can result in different looking ECG waves. According to the assigned project statement, the standards for ECG ltering were developed in 1975, however, there is a need to update these standards for newer lters. We will look into different lters and use a signal averaging algorithm to nd the SignalAveraged ECG (SAECG). Section II will give the problem statement. In section III we will look at the approach to the problem. We will then examine the ltering and signal averaging methods in section IV. Section V will discuss the results that we found. In section VI we draw some nal conclusions. Future work will be discussed in VII, and will be followed by acknowledgements and references. Appendix I shows extra gures for the lter design, and Appendix II gives some Matlab code that was used to test lter design. II. P ROBLEM F ORMULATION With a raw ECG signal, we get a noisy signal with a baseline drift (voltage not centered around 0V and increases or decreases over time). We desire to uncover the ECG signal behind this noise and drift. For example, in Figure 1 we can see the baseline drift of the four ECG signals. The common approach to this problem has been well documented [2], and standards for the ltering have been designated since 1975. However, some of the recent literature has suggested that in order to get an even higher resolution picture of the ECG waveforms, we can take the average

ESE 482 DIGITAL SIGNAL PROCESSING - WASHINGTON UNIVERSITY IN SAINT LOUIS - FINAL PROJECT - DECEMBER 14, 2010

Fig. 1.

B. Filtering High Frequency Noise After ltering the low frequencies, we seek to reduce the high frequency noise. We will do this by designing a linear phase low pass lter. By doing this, we take an approach similar to what we did to design the high pass lter. The standard cutoff frequency was established at 100 Hz , but it will be shown that the standard cutoff frequency will smooth too much of the high frequencies, and some of the physiological features will be lost. We will increase the cutoff frequency to 150 Hz and examine how the different parameters will affect the ltered signal. We also treat the IIR lters like we did for the high pass lter, where we run the signal through the lter twice (time reversed the second time). Like the high pass lter, we will also look at the Fourier transform to see which frequencies will be affected. The implementation and gures will be shown in section IV. C. Signal Averaging and Template Matching According to [3], there are many different ways to do signal averaging. The paper gives three different ways to average signals: the double-level method, normalized integrals method and the matched ltering method. We use the matched ltering method to signal average. We will look at the algorithm that we chose in section IV. We chose matched ltering because it gave us the best tradeoff between simplicity and robustness in the model. IV. F ILTERING AND S IGNAL AVERAGING M ETHODS A. Removing Baseline Drift In order to remove the baseline signal from the original ECG signal, and to remove the baseline drift, we need to use a high pass lter. To decide which lter to use, I examined how different lters affected the different features of the ECG signal.

For example, I looked at the different ltered P-Waves in Figure 2, the QRS-Complex in Figure 3, the T-Wave in Figure 4, and the ST Peak in Figure 5 for Lead 1. The same analysis was done for each of the features for the other leads. The gures for those waves are shown in Figures 25-30 in Appendix I. From those gures, we were able to choose a lter type that looked like it had reasonable performance, and didnt affect the features of the ECG in an undesirable way.

Fig. 2. P Wave for Several Different Filters Applied to Lead 1 (a) Original Signal (b) Kaiser (c) Butterworth (d) Elliptic (e) Chebyshev Type I (f ) Chebyshev Type II

We then proceeded to ne tune each of the different specications. For the high pass lter, we used the Matlab code shown in Appendix II-B. The results of the high pass lter that we chose will be discussed in section VI.

B. Filtering High Frequency Noise To lter out the high frequency noise, we want to design a low pass lter that allows the low frequencies to pass through, while attenuating the high frequencies that are due to noise.

ESE 482 DIGITAL SIGNAL PROCESSING - WASHINGTON UNIVERSITY IN SAINT LOUIS - FINAL PROJECT - DECEMBER 14, 2010

Fig. 3. QRS Complex for Several Different Filters Applied to Lead 1 (a) Original Signal (b) Kaiser (c) Butterworth (d) Elliptic (e) Chebyshev Type I (f ) Chebyshev Type II

Fig. 5. ST Peak for Several Different Filters Applied to Lead 1 (a) Original Signal (b) Kaiser (c) Butterworth (d) Elliptic (e) Chebyshev Type I (f ) Chebyshev Type II

Fig. 6. Lead 1 ECG Filtered Signals After Different Low Pass Filters with Various Transition Region (a) Signal Filtered Through High Pass Filter (b) 20 Hz (c) 50 Hz (d) 80 Hz (e) 110 Hz (f ) 140 Hz (g ) 170 Hz (h) 200 Hz (i) 230 Hz Fig. 4. T Wave for Several Different Filters Applied to Lead 1 (a) Original Signal (b) Kaiser (c) Butterworth (d) Elliptic (e) Chebyshev Type I (f ) Chebyshev Type II

We also looked at the cutoff frequency (Figure 9), the amount of attenuation (Figure 10), and the error of the passband (Figure 11). Note that Figures 9, 10 and 11 are located in Appendix I. We then chose a lter that combines the strict nature of the parameters while not smoothing too much of the physiological features. C. Signal Averaging and Template Matching

The design of the low pass lter is much like the design of the high pass lter. We use the same type of lter that we used in removing the baseline drift for ease.

To design the lter, we used the Matlab code shown in Appendix II-C. We started with the cutoff frequency (150 Hz ) that was suggested by the problem statement given in class, we then relaxed and constrained the different lter parameters until we found a good looking signal.

The signal averaging algorithm that we used was a big piece of this project. As discussed in section III-C, there are three main ways to average signals[3]. We have decided to use the template matching method to detect our waveforms over the length of the signal. The algorithm we used to do the matched ltering for signal averaging is shown in Algorithm 1. Algorithm 1 Matched Filtering 1: Select template from waveform by specifying t1 and t2 2: Find cross correlation between signal and template 3: for each point in the signal do 4: Determine if cross correlation is a peak 5: end for 6: for each peak found do 7: Add signal waveform at peak (index) to the template 8: end for 9: Divide by number of peaks to nd the SAECG

We looked at the results of the signals after passed through the low pass lter after varying one of the parameters, and then we selected the value for the parameter that looked the best while being as strict as we could make it. For example, in Figure 6 we can see that if the transition region is too high (as in subplot (i)), the signal gets blurred and we nd too much smoothing.

ESE 482 DIGITAL SIGNAL PROCESSING - WASHINGTON UNIVERSITY IN SAINT LOUIS - FINAL PROJECT - DECEMBER 14, 2010

The implementation of this algorithm was done in Matlab, and is shown in Appendix II-D. We rst allowed the user to identify a template from the signal using the ginput Matlab function. If the user doesnt specify a template, we have a default template region for the signal. After nding the template, we used the xcorr Matlab function to compute the cross correlation. Finding the peaks that resulted in a repetition of a waveform that matched (with minimum error) our template waveform, we found the indices for each of the waveforms. We then proceeded to iterate through the indices and average the resulting signals together. We found that this algorithm worked fairly well, and we were pleased with the resulting SAECGs. We will discuss the results in the following section. V. R ESULTS A. Removing Baseline Drift We saw many things when we were designing our lters. In designing the high pass lter, we were concerned with a few things. We are concerned with the amount of drift that we can eliminated, the amplitude of the voltages (not being attenuated), and also that the physiological features are not eliminated. When we looked at the different type of lters and how they affected the signals, we can see from Figure 7, that the FIR lter (Kaiser) introduces a time lag from the group delay of the lter. Since the IIR lters are zero-phase (from ltering twice), there is no time lag since the group delay is zero. From the ve different lters, we decided to use the Chebyshev Type II lter since the IIR lters all looked like they had pretty fair performance. Similar preliminary analysis was done on Figures 15-17 in Appendix I. From Figures 2, 3, 4, and 5 we can see that the different lters have different effects on the various physiological features. The other gures corresponding to the other leads can be found in Appendix I.

TABLE I F ILTER O RDER AND RUNTIME FOR 5 D IFFERENT F ILTERS Filter Type Kaiser Butterworth Elliptic Chebyshev Type I Chebyshev Type II Order 4572 4 3 3 3 Runtime 4.146 0.956 0.585 0.574 0.569

IIR lters. Other than that, the IIR lters all ran quite quickly (+/ runtime variability). We chose the Chebyshev Type II lter because it isnt as common as the Chebyshev Type I lter[5], and we wanted to see the performance of that lter. The Chebyshev Type II lter has no ripple in the passband, but is equiripple in the stopband. From Figure 8, we can see 8 different cases. We found that if the transition width is too small (as in subplot (c)) the signal does not lter as we want it to. We also found that if the cutoff frequency (c ) is too high, the end of the signal gets attenuated a lot more (as in subplot (e)). If the stopband attenuation (Rs ) is not high enough, we do not eliminate enug of the baseline drift and the signal is not centered around 0V (as in subplot (g )). We can see that if the stopband attenuation is too high, we also do not have the desired results. We therefore ended up using a lter with the specications of c = 0.67, transition width = 0.6, Rp = 0.5, and Rs = 40.

Fig. 8. Lead 1 ECG Baseline Signal After Applying Different Filters (a) Original Signal (b) Normal Filter (c) Low Transition Width (d) Lower Cutoff Frequency (e) High Transition Width (f ) High Ripple in Passband (g ) Low Attenuation (h) Low Ripple in Passband (i) Low Ripple in Passband and High Attenuation

Fig. 7. Lead 1 SAECG with Various Different Filter Types (a) Original Signal (b) Kaiser (c) Butterworth (d) Elliptic (e) Chebyshev Type I (f ) Chebyshev Type II

B. Filtering High Frequency Noise We used a Chebyshev Type II lter for the low pass lter for the same reasons that we chose for the high pass lter. We found that this lter works well, and achieves what we wanted to do. We can see from Figures 6, 9, 10 and 11 that the same type of comparisons as for the high pass lter are valid.

We also took into account the lter order and runtime for the algorithm with each lter type used. The results are summarized in Table I. Note that the FIR lter (Kaiser) has a much greater order, and that the Butterworth lter also had a higher runtime since it had a higher order than the other

ESE 482 DIGITAL SIGNAL PROCESSING - WASHINGTON UNIVERSITY IN SAINT LOUIS - FINAL PROJECT - DECEMBER 14, 2010

each lter and the signals at different magnitudes. We can see that the low frequencies that account for very high responses (and the baseline drift) are cut out by the high pass lter, and the high frequency noise will be cutout from the low pass lter. Similar plots were created for Leads 2, 3, and 4 in Figures 18, 19, and 20 respectively.

Fig. 9. Lead 1 ECG Filtered Signals After Different Low Pass Filters with Various Cutoff Frequencies (a) Signal Filtered Through High Pass Filter (b) 150 Hz (c) 100 Hz (d) 115 Hz (e) 125 Hz (f ) 130 Hz (g ) 135 Hz (h) 140 Hz (i) 145 Hz

Fig. 12. Frequency Domain Representation of (a) ECG Signal for Lead 1 with Magnitude 107 (b) ECG Signal for Lead 1 with Magnitude 104 (c) High Pass Filter (d) Low Pass Filter

C. Signal Averaging and Template Matching Using the two lters that were designed in the previous two subsections (V-A, V-B), we then proceeded to the signal averaging step. For this step, we used the template matching algorithm described in subsection IV-C, we can see that our signal averaging algorithm works. Figure 13 shows the cross correlation function. We can see the peaks of the cross correlation on subplot (b), and the indices that correspond to those peaks on subplot (c).

Fig. 10. Lead 1 ECG Filtered Signals After Different Low Pass Filters with Various Attenuation (a) Signal Filtered Through High Pass Filter (b) 1 dB (c) 5 dB (d) 10 dB (e) 15 dB (f ) 20 dB (g ) 40 dB (h) 60 dB (i) 80 dB

Fig. 11. Lead 1 ECG Filtered Signals After Different Low Pass Filters with Various Error (a) Signal Filtered Through High Pass Filter (b) .1 dB (c) .2 dB (d) .3 dB (e) .4 dB (f ) .5 dB (g ) 1 dB (h) 2 dB (i) 5 dB

## Matched ltering (a) Filtered Signal (b) Cross Correlation (c)

Some observations that can be drawn are that for a low frequency (c 100), the signal gets smoothed too much, and the physiological features get distorted. We can also see that the higher the attenuation (Rs ), the more the signal gets smoothed (less sharp features). However, if the attenuation is too low, then we exhibit too much noise. As the transition region becomes too large, we also get some distortion of the signal (from smoothing and also attenuation). Looking at all the gures, we settled on lter parameters of c = 150, transition width = 20, Rp = 0.5, and Rs = 40. To check to see if our lters did what we expected them to do, we looked at the frequency domain representation of the signals and the lter. Figure 12 shows the frequency plot of

In Figure 14, we can see the Signal-Averaged ECGs, and that the waveforms that we expected to see (from the template chosen) are all accurate and displayed. The SAECG signals look like they recover most of the physiological features that are in the original ECG signal. Therefore, we accept our lters, and our signal averaging algorithm as good. VI. C ONCLUSION We have seen that our algorithm works from the previous section. We have gone through various lter design techniques and have examined the different effects of the lter types and parameters. We have also seen that the matched ltering signal

ESE 482 DIGITAL SIGNAL PROCESSING - WASHINGTON UNIVERSITY IN SAINT LOUIS - FINAL PROJECT - DECEMBER 14, 2010

Fig. 14.

averaging technique works. In looking at the signal averaged ECG, we have seen that the various physiological features are still intact after ltering, and in fact, we can now read important features from the SAECG that we might not have been able to interpret clearly from the raw ECG data. VII. F UTURE W ORK We have only explored a small portion of the many different lters and signal averaging methods. However, in the future, we could implement several other signal averaging algorithms such as the Double-Value and Integral methods. We could also look closer at how the different lter types work, and maybe look at how to use a FIR lter (if we can speed up the process). ACKNOWLEDGEMENTS Id like to thank Professor Jason Trobaugh for teaching me this course. I would not have known how to even approach this problem, or design a lter without this class. I would also like to acknowledge Jess Stigile with whom many conversations about digital signal processing took place throughout the semester. She made going deeper into the material fun and interesting when it would have been easy to settle for the basics. R EFERENCES
[1] Electrocardiography, Wikipedia, Dec 2010. [2] Guyton and Hall, Textbook of Medical Physiology, 10th ed., 2000, ch. 11: The Normal Electrocardiogram, pp. 114119.

[3] R. Jane, H. Rix, P. Caminal, and P. Laguna, Alignment methods for averaging of high-resolution cardiac signals: A comparitive study of performance, IEEE Transactions on Biomedical Engineering, vol. 38, no. 6, pp. 571579, June 1991. [4] A. V. Oppenheim and R. W. Shafer, Discrete-Time Signal Processing. Prentice Hall, 2010. [5] Chebyshev lter, Wikipedia, Nov 2010.

ESE 482 DIGITAL SIGNAL PROCESSING - WASHINGTON UNIVERSITY IN SAINT LOUIS - FINAL PROJECT - DECEMBER 14, 2010

A PPENDIX I F IGURES

Fig. 18. Frequency Domain Representation of (a) ECG Signal for Lead 2 with Magnitude 107 (b) ECG Signal for Lead 2 with Magnitude 104 (c) High Pass Filter (d) Low Pass Filter

Fig. 15. Lead 2 SAECG with Various Different Filter Types (a) Original Signal (b) Kaiser (c) Butterworth (d) Elliptic (e) Chebyshev Type I (f ) Chebyshev Type II

Fig. 19. Frequency Domain Representation of (a) ECG Signal for Lead 3 with Magnitude 107 (b) ECG Signal for Lead 3 with Magnitude 104 (c) High Pass Filter (d) Low Pass Filter

Fig. 16. Lead 3 SAECG with Various Different Filter Types (a) Original Signal (b) Kaiser (c) Butterworth (d) Elliptic (e) Chebyshev Type I (f ) Chebyshev Type II Fig. 20. Frequency Domain Representation of (a) ECG Signal for Lead 4 with Magnitude 107 (b) ECG Signal for Lead 4 with Magnitude 104 (c) High Pass Filter (d) Low Pass Filter

Fig. 17. Lead 4 SAECG with Various Different Filter Types (a) Original Signal (b) Kaiser (c) Butterworth (d) Elliptic (e) Chebyshev Type I (f ) Chebyshev Type II

Fig. 21. P Wave for Several Different Filters Applied to Lead 2 (a) Original Signal (b) Kaiser (c) Butterworth (d) Elliptic (e) Chebyshev Type I (f ) Chebyshev Type II

ESE 482 DIGITAL SIGNAL PROCESSING - WASHINGTON UNIVERSITY IN SAINT LOUIS - FINAL PROJECT - DECEMBER 14, 2010

Fig. 22. QRS Complex for Several Different Filters Applied to Lead 2 (a) Original Signal (b) Kaiser (c) Butterworth (d) Elliptic (e) Chebyshev Type I (f ) Chebyshev Type II

Fig. 26. QRS Complex for Several Different Filters Applied to Lead 3 (a) Original Signal (b) Kaiser (c) Butterworth (d) Elliptic (e) Chebyshev Type I (f ) Chebyshev Type II

Fig. 23. T Wave for Several Different Filters Applied to Lead 2 (a) Original Signal (b) Kaiser (c) Butterworth (d) Elliptic (e) Chebyshev Type I (f ) Chebyshev Type II

Fig. 27. T Wave for Several Different Filters Applied to Lead 3 (a) Original Signal (b) Kaiser (c) Butterworth (d) Elliptic (e) Chebyshev Type I (f ) Chebyshev Type II

Fig. 24. ST Peak for Several Different Filters Applied to Lead 2 (a) Original Signal (b) Kaiser (c) Butterworth (d) Elliptic (e) Chebyshev Type I (f ) Chebyshev Type II

Fig. 28. ST Peak for Several Different Filters Applied to Lead 3 (a) Original Signal (b) Kaiser (c) Butterworth (d) Elliptic (e) Chebyshev Type I (f ) Chebyshev Type II

Fig. 25. P Wave for Several Different Filters Applied to Lead 3 (a) Original Signal (b) Kaiser (c) Butterworth (d) Elliptic (e) Chebyshev Type I (f ) Chebyshev Type II

Fig. 29. QRS Complex for Several Different Filters Applied to Lead 4 (a) Original Signal (b) Kaiser (c) Butterworth (d) Elliptic (e) Chebyshev Type I (f ) Chebyshev Type II

ESE 482 DIGITAL SIGNAL PROCESSING - WASHINGTON UNIVERSITY IN SAINT LOUIS - FINAL PROJECT - DECEMBER 14, 2010

Fig. 30. T Wave for Several Different Filters Applied to Lead 4 (a) Original Signal (b) Kaiser (c) Butterworth (d) Elliptic (e) Chebyshev Type I (f ) Chebyshev Type II

ESE 482 DIGITAL SIGNAL PROCESSING - WASHINGTON UNIVERSITY IN SAINT LOUIS - FINAL PROJECT - DECEMBER 14, 2010

10

## A PPENDIX II M ATLAB C ODE

Note that all plotting functions were truncated to save space. If you want to see the entire code, please email the author at mchan@ese.wustl.edu.

## A. Matched Filter Signal Averaging Algorithm

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

%% Detect Matched Signals % Find cross correltion between signal and the chosen template w = xcorr(z1,z1(t1:t2)); % Truncate cross correlation to get rid of zero entries w = w(length(z1):2*length(z1)-1); u = zeros(1,length(w)); % Define threshhold to get rid of erroneous "peaks" thresh = .7*max(w(10000:15000)); % If group delay is not zero, then take into account the group delay for he % signal if ceil(gd1(1)+gd2(1)) < 2 start = 2; else start = ceil(gd1(1)+gd2(1)); end % Find all peaks by finding points above threshhold set above and testing % if point is higher than point before and after it for i = start:length(w)-1 if w(i) > thresh && w(i) > w(i-1) && w(i) > w(i+1) u(i) = 1; end end %% Average Waveforms Throughout Each Signal % Calculate the start of each matched waveform index = find(u==1); % Create template interval int = t2-t1+1; % Average Signal for each index of matched waveform (minus some erroneous % terms at beginning and end) ave1 = zeros(1,int); ave2 = zeros(1,int); ave3 = zeros(1,int); ave4 = zeros(1,int); counter = 0; for i = 3:length(index)-3 if index(i)+int<=204800 ave1 = ave1 + z1(index(i):index(i)+int-1); ave2 = ave2 + z2(index(i):index(i)+int-1); ave3 = ave3 + z3(index(i):index(i)+int-1); ave4 = ave4 + z4(index(i):index(i)+int-1); counter = counter + 1; end end ave1 = ave1/counter; ave2 = ave2/counter; ave3 = ave3/counter; ave4 = ave4/counter;

ESE 482 DIGITAL SIGNAL PROCESSING - WASHINGTON UNIVERSITY IN SAINT LOUIS - FINAL PROJECT - DECEMBER 14, 2010

11

## B. High Pass Filter Design

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 72 73

% % % % % % % % % % % % % % % % % % % % % %

filterspecshighpass - Calculates the signal for different parameters of the Chebyshev Type II High Pass Filter. Syntax: Inputs: none Outputs: none Other m-files required: filterdesign Subfunctions: none MAT-files required: ECGSignals.mat See also: filtersignals, filterspecslowpass, filterdesign, filterdesign, ChanMatthias_FinalProject Author: Matthias Chan Washington University in Saint Louis email: mchan@ese.wustl.edu Website: http://research.engineering.wustl.edu/mchan/ December; Last revision: 14-December-2010 filterspecshighpass

%------------- BEGIN CODE -------------%% Filters used: % Kaiser, Butter, Elliptic, Cheby1, Cheby2 close all; clear all; clc; load ECGSignals.mat; screen_size = get(0, 'ScreenSize'); %% Different parameters % Cutoff Frequency w_c = [.67 .67 .4 .75 .67 .67 .67 .67]; % Transition Region delta = [.3 .2 .3 .74 .3 .3 .3 .3]; % Passband Error Rp = [.5 .5 .5 .5 1 .5 .15 .15]; % Stopband Attenuation Rs = [40 40 40 40 40 10 40 50]; % Preset lengths of variables w1 = w_c-delta; w2 = w_c+delta; y1 = zeros(8,2131); temp = zeros(8,204800); int = zeros(1,8); y2 = y1; y3 = y1; y4 = y1; y1hp = temp; y2hp = temp; y3hp = temp; y4hp = temp; y1lp = temp; y2lp = temp; y3lp = temp; y4lp = temp; % Find filtered signals for i = 1:length(w_c) tic; [y1(i,:), y2(i,:), y3(i,:), y4(i,:), int(i), y1hp(i,:), y2hp(i,:),... y3hp(i,:), y4hp(i,:), y1lp(i,:), y2lp(i,:), y3lp(i,:), y4lp(i,:)] = ... filterdesign(5,[w1(i) w2(i)],Rp(i),Rs(i)); toc; end; %% Lead 1 % Elapsed time % Elapsed time % Elapsed time % Elapsed time % Elapsed time % Elapsed time % Elapsed time % Elapsed time %-------------

is 0.568595 is 0.641623 is 0.558458 is 0.556059 is 0.564087 is 0.643838 is 0.567224 is 0.593634 END OF CODE

## seconds. seconds. seconds. seconds. seconds. seconds. seconds. seconds. --------------

ESE 482 DIGITAL SIGNAL PROCESSING - WASHINGTON UNIVERSITY IN SAINT LOUIS - FINAL PROJECT - DECEMBER 14, 2010

12

## C. Low Pass Filter Design

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

% % % % % % % % % % % % % % % % % % % % % %

filterspecslowpass - Calculates the signal for different parameters of the Chebyshev Type II Low Pass Filter. Syntax: Inputs: none Outputs: none Other m-files required: filterdesign Subfunctions: none MAT-files required: ECGSignals.mat See also: filtersignals, filterspecshighpass, filterdesign, filterdesign, ChanMatthias_FinalProject Author: Matthias Chan Washington University in Saint Louis email: mchan@ese.wustl.edu Website: http://research.engineering.wustl.edu/mchan/ December; Last revision: 14-December-2010 filterspecslowpass

%------------- BEGIN CODE -------------% Kaiser, Butter, Elliptic, Cheby1, Cheby2 close all; clear all; clc; load ECGSignals.mat; screen_size = get(0, 'ScreenSize'); % Define parameters for the High Pass Filter from filterspecshighpass.m w_c = .67; delta = .3; Rp = .5; Rs = 40; w1 = w_c-delta; w2 = w_c+delta; %% Vary parameters for the Low Pass Filter % Cutoff Frequency w_c2 = [150 150 150 150 150 150 150 150]; % Transition Region delta2 = [10 10 10 10 10 10 10 10]; % Passband Error Rp2 = [.5 .5 .5 .5 .5 .5 .5 .5]; % Stopband Attenuation Rs2 = [1 5 10 15 20 40 60 80]; % Define frequencies w3 = w_c2-delta2; w4 = w_c2+delta2; % Predefine variable lengths y1 = zeros(8,2131); temp = zeros(8,204800); int = zeros(1,8); y2 = y1; y3 = y1; y4 = y1; y1hp = temp; y2hp = temp; y3hp = temp; y4hp = temp; y1lp = temp; y2lp = temp; y3lp = temp; y4lp = temp; % Find filtered signals from the high and low pass filters for i = 1:length(w_c2) tic; [y1(i,:), y2(i,:), y3(i,:), y4(i,:), int(i), y1hp(i,:), y2hp(i,:),... y3hp(i,:), y4hp(i,:), y1lp(i,:), y2lp(i,:), y3lp(i,:), y4lp(i,:)] = ... filterdesign(5,[w1 w2],Rp,Rs,[w3(i) w4(i)],Rp2(i),Rs2(i)); toc; end; %------------- END OF CODE --------------

ESE 482 DIGITAL SIGNAL PROCESSING - WASHINGTON UNIVERSITY IN SAINT LOUIS - FINAL PROJECT - DECEMBER 14, 2010

13

D. Filter Design

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 72 73

%------------- BEGIN CODE -------------load('ECGSignals.mat') screen_size = get(0, 'ScreenSize'); %% Plot of Original Signals % Define the time sequence for 100 seconds sampled at 2048 Hz n = 1/2048:1/2048:100; %% High Pass Filter design % For all filters, the output is [h1,w1] for frequency response, and the % group delay given as [gd1,w1gd] if filternum==1 % Kaiser design fsamp = 2048; delta = .5; fcuts = [.67-delta .67+delta]; mags = [0 1]; devs = [0.01 0.075]; [nhp,Wnhp,beta,ftype] = kaiserord(fcuts,mags,devs,fsamp); b1 = fir1(nhp,Wnhp,ftype,kaiser(nhp+1,beta),'noscale'); a1 = 1;

ESE 482 DIGITAL SIGNAL PROCESSING - WASHINGTON UNIVERSITY IN SAINT LOUIS - FINAL PROJECT - DECEMBER 14, 2010

14

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150

[h1,w1] = freqz(b1,a1); [gd1filt,w1gd] = grpdelay(b1,a1); gd1 = gd1filt; iir1 = 0; elseif filternum==2 % Butterworth design fsamp = 2048; delta = .5; fcuts = [.67-delta .67+delta]; [nhp,Wnhp] = buttord(2*fcuts(2)/fsamp,2*fcuts(1)/fsamp,-db(.99),-db(.01)); [b1,a1] = butter(nhp,Wnhp,'high'); [h1,w1] = freqz(b1,a1); [gd1filt,w1gd] = grpdelay(b1,a1); gd1 = zeros(1,length(gd1filt)); iir1 = 1; elseif filternum==3 % Elliptic design fsamp = 2048; delta = .5; fcuts = [.67-delta .67+delta]; [nhp,Wnhp] = ellipord(2*fcuts(2)/fsamp,2*fcuts(1)/fsamp,-db(.99),-db(.01)); [b1,a1] = ellip(nhp,-db(.99),-db(.01),Wnhp,'high'); [h1,w1] = freqz(b1,a1); [gd1filt,w1gd] = grpdelay(b1,a1); gd1 = zeros(1,length(gd1filt)); iir1 = 1; elseif filternum==4 % Chebyshev Type 1 design fsamp = 2048; delta = .5; fcuts = [.67-delta .67+delta]; [nhp,Wnhp] = cheb1ord(2*fcuts(2)/fsamp,2*fcuts(1)/fsamp,-db(.99),-db(.01)); [b1,a1] = cheby1(nhp,-db(.99),Wnhp,'high'); [h1,w1] = freqz(b1,a1); [gd1filt,w1gd] = grpdelay(b1,a1); gd1 = zeros(1,length(gd1filt)); iir1 = 1; elseif filternum==5 % Chebyshev Type 2 design fsamp = 2048; delta = .5; if nargin > 1 fcuts = cutoff; Rp = error; Rs = attenuation; else fcuts = [.67-delta .67+delta]; Rp = .05; Rs = 40; end; [nhp,Wnhp] = cheb2ord(2*fcuts(2)/fsamp,2*fcuts(1)/fsamp,Rp,Rs); [b1,a1] = cheby2(nhp,Rs,Wnhp,'high'); [h1,w1] = freqz(b1,a1); [gd1filt,w1gd] = grpdelay(b1,a1); gd1 = zeros(1,length(gd1filt)); iir1 = 1; end %% y1 y2 y3 y4 if Signals with High Pass Filter Applied = filter(b1,a1,ECGSignals(1,(end:-1:1))); = filter(b1,a1,ECGSignals(2,(end:-1:1))); = filter(b1,a1,ECGSignals(3,(end:-1:1))); = filter(b1,a1,ECGSignals(4,(end:-1:1))); iir1 == 1 y1 = filter(b1,a1,y1(end:-1:1)); y2 = filter(b1,a1,y2(end:-1:1)); y3 = filter(b1,a1,y3(end:-1:1)); y4 = filter(b1,a1,y4(end:-1:1)); end; %% Low Pass Filter Design % For all filters, the output is [h2,w2] for frequency response, and the % group delay given as [gd2,w2gd] if filternum==1 % Kaiser design fsamp = 2048; delta = 25;

ESE 482 DIGITAL SIGNAL PROCESSING - WASHINGTON UNIVERSITY IN SAINT LOUIS - FINAL PROJECT - DECEMBER 14, 2010

15

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227

fcuts = [150-delta 150+delta]; mags = [1 0]; devs = [0.05 0.01]; [nlp,Wnlp,beta,ftype] = kaiserord(fcuts,mags,devs,fsamp); b2 = fir1(nlp,Wnlp,ftype,kaiser(nlp+1,beta),'noscale'); [h2,w2] = freqz(b2); [gd2filt,w2gd] = grpdelay(b2); gd2 = gd2filt; a2 = 1; iir2 = 0; elseif filternum==2 % Butterworth design fsamp = 2048; delta = 25; fcuts = [150-delta 150+delta]; [nlp,Wnlp] = buttord(2*fcuts(1)/fsamp,2*fcuts(2)/fsamp,-db(.99),-db(.01)); [b2,a2] = butter(nlp,Wnlp,'low'); [h2,w2] = freqz(b2,a2); [gd2filt,w2gd] = grpdelay(b2,a2); gd2 = zeros(1,length(gd2filt)); iir2 = 1; elseif filternum==3 % Elliptic design fsamp = 2048; delta = 25; fcuts = [150-delta 150+delta]; [nlp,Wnlp] = ellipord(2*fcuts(1)/fsamp,2*fcuts(2)/fsamp,-db(.99),-db(.01)); [b2,a2] = ellip(nlp,-db(.99),-db(.01),Wnlp,'low'); [h2,w2] = freqz(b2,a2); [gd2filt,w2gd] = grpdelay(b2,a2); gd2 = zeros(1,length(gd2filt)); iir2 = 1; elseif filternum==4 % Chebyshev Type 1 design fsamp = 2048; delta = 25; fcuts = [150-delta 150+delta]; [nlp,Wnlp] = cheb1ord(2*fcuts(1)/fsamp,2*fcuts(2)/fsamp,-db(.99),-db(.01)); [b2,a2] = cheby1(nlp,-db(.99),Wnlp,'low'); [h2,w2] = freqz(b2,a2); [gd2filt,w2gd] = grpdelay(b2,a2); gd2 = zeros(1,length(gd2filt)); iir2 = 1; elseif filternum==5 % Chebyshev Type 2 design fsamp = 2048; delta = 25; if nargin > 4 fcuts = cutoff2; Rp = error2; Rs = attenuation2; else fcuts = [150-delta 150+delta]; Rp = .05; Rs = 40; end; [nlp,Wnlp] = cheb2ord(2*fcuts(1)/fsamp,2*fcuts(2)/fsamp,Rp,Rs); [b2,a2] = cheby2(nlp,Rs,Wnlp,'low'); [h2,w2] = freqz(b2,a2); [gd2filt,w2gd] = grpdelay(b2,a2); gd2 = zeros(1,length(gd2filt)); iir2 = 1; end %% z1 z2 z3 z4 if Signals with Low Pass Filter Applied = filter(b2,a2,y1(end:-1:1)); = filter(b2,a2,y2(end:-1:1)); = filter(b2,a2,y3(end:-1:1)); = filter(b2,a2,y4(end:-1:1)); iir2 == 1 z1 = filter(b2,a2,z1(end:-1:1)); z2 = filter(b2,a2,z2(end:-1:1)); z3 = filter(b2,a2,z3(end:-1:1)); z4 = filter(b2,a2,z4(end:-1:1));

## end %% Lead 1 Signal for Choosing Template

ESE 482 DIGITAL SIGNAL PROCESSING - WASHINGTON UNIVERSITY IN SAINT LOUIS - FINAL PROJECT - DECEMBER 14, 2010

16

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269

t1 = 102240; t2 = 104370; %% Detect Matched Signals w = xcorr(z1,z1(t1:t2)); w = w(length(z1):2*length(z1)-1); u = zeros(1,length(w)); thresh = .7*max(w(10000:15000)); if ceil(gd1(1)+gd2(1)) < 2 start = 2; else start = ceil(gd1(1)+gd2(1)); end for i = start:length(w)-1 if w(i) > thresh && w(i) > w(i-1) && w(i) > w(i+1) u(i) = 1; end end %% Average Waveforms Throughout Each Signal index = find(u==1); int = t2-t1+1; % Combined Signal ave1 = zeros(1,int); ave2 = zeros(1,int); ave3 = zeros(1,int); ave4 = zeros(1,int); counter = 0; for i = 3:length(index)-3 if index(i)+int<=204800 ave1 = ave1 + z1(index(i):index(i)+int-1); ave2 = ave2 + z2(index(i):index(i)+int-1); ave3 = ave3 + z3(index(i):index(i)+int-1); ave4 = ave4 + z4(index(i):index(i)+int-1); counter = counter + 1; end end ave1 = ave1/counter; ave2 = ave2/counter; ave3 = ave3/counter; ave4 = ave4/counter; sig1 = ave1; sig2 = ave2; sig3 = ave3; sig4 = ave4; %------------- END OF CODE --------------