303Lab3 Peaks

Embed Size (px)

Citation preview

  • 8/4/2019 303Lab3 Peaks

    1/4

    BIOEN 303 Bioengineering Signal Processing Winter 2010

    Print date: 1/19/2010 1 Instructor: C. Neils

    Laboratory Exercise 3: Peak detection

    Meets: January 19-22

    Due: January 26-29

    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 usefulto create computer routines that find those features automatically. A biomedical device

    might use such a routine to detect abnormal waveforms for further analysis by aspecialist, 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 featuresmathematically. 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 peaksheight 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-zeroslope. 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 theoffset b before we analyze the signal.

    Some features have complicated shapes that are difficult to describe mathematically. Ifsuch 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 ina 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.

    Figure 1. Simulated arterial blood pressure signal with circles marking

    the peak pressures. The peaks were found by a MATLAB script.

    0 500 1000 1500 2000 2500-2

    -1

    0

    1

    2

  • 8/4/2019 303Lab3 Peaks

    2/4

    BIOEN 303 Bioengineering Signal Processing Winter 2010

    Print date: 1/19/2010 2 Instructor: C. Neils

    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 theprocessing speed of our system.

    Familiarity with some of the conditional and search functions in MATLAB will make yourprogramming 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

    A = [10 20 30 40 30 20 10 0 10 20 30 40 30 20]

    (A>25) = [ 0 0 1 1 1 0 0 0 0 0 1 1 1 0]

    find(A>25) = [3 4 5 11 12 13]

    find((A>25),1) = [3]

    max(A) = 40

    max(A)==A = [ 0 0 0 1 0 0 0 0 0 0 0 1 0 0]

    find(max(A)==A) = [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 Commands

    Find a single peak find(max(signal)==signal);

    Find multiple peaksthat are all above the same

    threshold

    1. Establish threshold value2. Create search intervals with logical statement:

    signal > threshold

    3. Find the single peak w/in each interval

    4. If necessary, throw out intervals that are too

    short

    5. Iterate through intervals using while...end

    until you run out of signal

    Establish a threshold from userinput

    For keyboard input: [x y]=input()

    For mouse input: [x y]=ginput()Establish a thresholdautomatically (mean is constant)

    Use a proportion:threshold = mean()+k*(max()-min())

    Establish a thresholdautomatically (mean is variable)

    A slowly changing mean is low-frequency noise.

    Apply high-pass filter (Labs 6-7) or

    Subtract mean every S samples or

    Use adaptable threshold or search for local

    maxima and minima

  • 8/4/2019 303Lab3 Peaks

    3/4

    BIOEN 303 Bioengineering Signal Processing Winter 2010

    Print date: 1/19/2010 3 Instructor: C. Neils

    Ignore subsequent peaksUse blanking period (jump ahead in signal)

    --Requires an estimate of peak frequency1

    Find a peak in a noisy signal(i.e., find only the large peaks

    and not every little spike)

    Except in extreme cases, random noise does not

    cause the interval-search strategy to find extrapeaks. Tall, sharp peaks (such as the R-wave) are

    especially easy to find even in noise.

    However, noise added to rounded peaks can makethe 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.

    Find an interval with a specific

    shape (cross-correlation)

    1. Use while loop to iterate through the signal

    2. Calculate correlation coefficient at each location

    to get a new signal with peaks where thecorrelation is best

    3. Apply another peak detection scheme to findthese locations.

    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 ismeaningless but the script is a starting point for the exercises below. Note that you can

    plot circles with the commandplot(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. thepositive 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 N

    2array 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:

    function [indexColumn valueColumn] = peakfinder(signal)

    d. Create a script that calls your function to find the peaks, and then plots thesignal with a circle at each detected peak.

    3) Pulse oximetry simulation

    a. In a new script (or a new cell in your old script), create a time axis that matchesthe length and sampling frequency of the ECG. Using it, create and plot a sum of

    two sinusoids as a model of pulse oximetry data:a(t) = *sin(2f1t) + sin(2f2t). Start with f1 = 1 Hz (the heart rate) and

    f2 = .2 Hz (low-frequency noise).

    b. Write a function that detects the peaks of the faster oscillations (the heart beat)and returns an array with the location (index) and height (voltage) of each one.Note that this situation is more challenging than the ECG because the mean value

    of each cycle changes over time.

    1 If the signal is known to be periodic, then Fourier analysis can be used to estimate the averagespacing over a series of peaks. However, the Fourier spectrum does not help us identify individualpeaks.

  • 8/4/2019 303Lab3 Peaks

    4/4

    BIOEN 303 Bioengineering Signal Processing Winter 2010

    Print date: 1/19/2010 4 Instructor: C. Neils

    Choose one of the following two exercises:

    4) Peak detection in a noisy signal

    a. Make a noisy version of the electrocardiogram by adding 10% white noise:

    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 yourfunction 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 experimentwith 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 thefigures (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 ofdata 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. Howmight 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 thatthere are often large differences between consecutive peaks (or consecutive troughs).