Laboratory Exercise 3: Peak Detection
Laboratory Exercise 3: Peak Detection
Winter 2010
Objectives:
Explore techniques for detecting peaks in digitized signals Practice conditional operations in MATLAB
Prelab reading
None
Introduction
Many biological signals can be represented as sequences of line segments, curves and peaks. It is relatively easy for us to pick these features out by eye, but it is often useful to create computer routines that find those features automatically. A biomedical device might use such a routine to detect abnormal waveforms for further analysis by a specialist, or to compress a bioelectrical signal into an essential set of descriptive values so that a long time course of data can be stored or transmitted more efficiently. Before we write a feature-detection routine, we need to define the features mathematically. Some examples: A peak is the highest point that is separated from any other peak by a lateral distance of at least x and a vertical drop of at least y. A peak can be described by its location x, its height y, and its sharpness (which can be defined as the ratio of the peaks height to its width or area). A flat segment has a vertical variation that does not exceed y. Once detected, it can usually be expressed as a starting location and a length x. A ramp is a segment that is similar to a flat segment except that it has a non-zero slope. It can be defined as a segment in which |y(n) mx(n) b| < a. The slope m and the length of the ramp are of interest, but we might not know the slope or the offset b before we analyze the signal. Some features have complicated shapes that are difficult to describe mathematically. If such a feature always has the same size (width is most important) then cross-correlation can be used to find the places where the signal best matches a template of the feature. In todays lab we will create functions that detect the R-wave in an ECG and the peaks in a synthetic signal that is similar to pulse oximetry data. To show that we have detected the peaks, we will plot circles over them, as shown in Figure 1.
2 1 0 -1 -2
0 500 1000 1500 2000 2500 Figure 1. Simulated arterial blood pressure signal with circles marking the peak pressures. The peaks were found by a MATLAB script.
Instructor: C. Neils
Winter 2010
Programming strategies
Because the shape and meaning of peaks in a bioelectric signal depend on the signals source, there is no one best peak-detection algorithm. Our choice of method might depend on how much we know about the signals, how much noise is present, and the processing speed of our system. Familiarity with some of the conditional and search functions in MATLAB will make your programming and program execution go more quickly. As practice, define some sequence such as A below, then try out the commands in the following table. Statement Output
= = = = = = =
[10 20 30 40 30 20 10 0 10 20 30 40 30 20] [ 0 0 1 1 1 0 0 0 0 0 1 1 1 0]
[3 4 5 11 12 13] [3] 40 [ 0 0 0 1 0 0 0 0 0 0 0 1 0 0]
[4 12]
When you set out to make a new peak-finding program it helps to think about the signals shape and the kinds of noise you will encounter. A sketch usually helps too. A few commands and procedures are suggested in the table below to get you started. The signal processing toolbox also has a function findpeaks() that you might want to explore, especially to find its limitations. Task Find a single peak
1. 2.
Commands
find(max(signal)==signal);
Establish threshold value Create search intervals with logical statement:
Find multiple peaks that are all above the same threshold
Find the single peak w/in each interval If necessary, throw out intervals that are too short Iterate through intervals using while...end until you run out of signal
Establish a threshold from user input Establish a threshold automatically (mean is constant)
For keyboard input: [x y]=input() For mouse input: [x y]=ginput() Use a proportion:
threshold = mean()+k*(max()-min())
Apply high-pass filter (Labs 6-7) or Subtract mean every S samples or Use adaptable threshold or search for local maxima and minima
A slowly changing mean is low-frequency noise. Establish a threshold automatically (mean is variable)
Instructor: C. Neils
Winter 2010
Use blanking period (jump ahead in signal) --Requires an estimate of peak frequency1 Except in extreme cases, random noise does not cause the interval-search strategy to find extra peaks. Tall, sharp peaks (such as the R-wave) are especially easy to find even in noise. However, noise added to rounded peaks can make the algorithm find the wrong location. In this case it is best to filter the signal before analyzing it. Low-pass filters such as a running average will be covered in Lab 6.
1.
Find a peak in a noisy signal (i.e., find only the large peaks and not every little spike)
Use while loop to iterate through the signal Calculate correlation coefficient at each location to get a new signal with peaks where the correlation is best Apply another peak detection scheme to find these locations.
2.
3.
Lab exercises
1) Write a script that generates two series of random numbers, and plots the first series as a line and the second series as circles on the same pair of axes. This plot itself is meaningless but the script is a starting point for the exercises below. Note that you can plot circles with the command plot(x,y,o). 2) Peak detection in an electrocardiogram a. Follow the Laboratory data files link on the lab web page to find the ECG signal bsl52e00.ii. The .ii suffix indicates that the data is from lead II, i.e. the positive electrode is on the right arm and the negative electrode is on the left leg. b. Plot the signal. Its 2500 points represent 10 seconds sampled at 250 Hz. c. Write a function that detects the R-waves in the ECG signal, and returns an N2 array or two N1 arrays that contains the location (index) and height (voltage) for each of the N peaks. You might start with something like this:
If the signal is known to be periodic, then Fourier analysis can be used to estimate the average spacing over a series of peaks. However, the Fourier spectrum does not help us identify individual peaks.
Print date: 1/19/2010
Instructor: C. Neils
Winter 2010
signal + (max(signal)min(signal))*rand(1,length(signal))/10.
b. Test your peak-detection function on the modified signal. What is the result? c. Optional: If adding noise degraded the peak-detection capability, modify your function so that it is less affected by the random noise. 5) Rapid heart rate a. Add 5% white noise to the simulated pulse oximetry signal. b. Increase the heart rate f1 in this noisy signal, using steps of 0.2 Hz. For each step, run your peak-detection algorithm. At what speed does the function start producing errors?
Lab Report
Introduction Three or four sentences to explain what you set out to accomplish, and why. Provide special background information, if necessary. Methods - Describe your peak detection strategy with equations but not code. The code should be included in an appendix. Results Please provide one plot of the ECG with the circles on the peaks, one plot of the pulse oximetry trace with the circles on the peaks, and one plot from the experiment with the noisy ECG signal. A plot of the noisy pulse oximetry signal is optional. Discuss your findings, and include answers to the two questions below. General tips for results: A single paragraph is usually enough text to introduce the figures (or each set of figures). Present data in either a graph or a table; if you think both are needed, put the table in an appendix. When you can, combine multiple sets of data in a single table to facilitate comparison. Label all figures and tables with descriptive captions (e.g., Figure 1. Plot of ECG signals exhibiting (A) intermittent bradycardia and (B) sustained tachycardia, sampled at 250 Hz). Questions: 1. With some ECG lead pairs and heart orientations, the R-wave is inverted (pointing downward). How would you modify your R-wave detection algorithm to determine the orientation of the R-wave and then detect it? (Just strategy, not code). 2. You have detected the peaks in the pulse oximetry signal, but the blood oxygenation depends on the height of each peak above the troughs, not above the baseline. How might you modify your function so that it returns the information we want? Your answer does not need to include MATLAB code, but it should account for the fact that there are often large differences between consecutive peaks (or consecutive troughs).
Instructor: C. Neils