Skip to content

Peak & Trough Detection and Filtering

Sambit Paul edited this page Dec 2, 2023 · 10 revisions

In the context of Peak Detection, a peak or local maximum is defined as any sample whose two direct neighbours have a smaller amplitude and a trough or local minimum is defined as any sample whose two direct neighbours have a larger amplitude. Peak detection is often used for identifying certain events which cause undulations in the signal which are recognised as peaks.

In this library, we are able to identify peaks and filter them based on these properties:

  • Peak Height: The point in the signal corresponding to the peak.
  • Plateau Size: The number of samples for which a peak spreads. "Flat peaks" tend to spread for more than 1 sample point.
  • Peak Distance: Number of samples between a peak and the peak to its right.
  • Peak Sharpness: Height difference between the peak and its neighbouring samples to its left and right.
  • Peak Prominence: The prominence of a peak measures how much a peak stands out from the surrounding baseline of the signal.
  • Peak Width: The width of a peak in samples at a relative distance to the peak’s height and prominence.

This library also allows computation of these properties for specific peaks.

The examples provided here use this signal:
double[] data = UtilMethods.splitByIndex(UtilMethods.electrocardiogram(), 3200, 4200);
Smooth sObj = new Smooth(data, 15, "rectangular");
double[] ecgSignal = sObj.smoothSignal("same");

signal

Peak & Trough Detection

The first step of peak detection is to identify all the peaks in the signal. Filtering can only be done on the identified peaks.

Code
FindPeak fp = new FindPeak(this.highResSignal);

Peak out = fp.detectPeaks();
int[] peaks = out.getPeaks();

Peak out2 = fp.detectTroughs();
int[] troughs = out2.getPeaks();

peaks

Peak Properties & Filtering

peaks

Peak Height

GET HEIGHT
double[] outHeight = out.getHeights();
HEIGHT CALCULATION CODE
double[] height = out.findPeakHeights(peaks);
HEIGHT FILTERING CODE
int[] filteredPeaks1 = out.filterByHeight(0.02, 1.0);

Plateau Size

GET PLATEAU SIZE
int[] outPlateau = out.getPlateauSize();
PLATEAU SIZE CALCULATION CODE
int[] outPlateau = out.findPlateauSize(peaks);
PLATEAU SIZE FILTERING CODE
int[] outFilteredPS1 = out.filterByPlateauSize(2.0, 4.0);

Peak Distance

GET DISTANCE
int[] outDistance = out.getPeakDistance();
DISTANCE CALCULATION CODE
int[] distance = out.findPeakDistance(peaks);
DISTANCE FILTERING CODE
int[] outFilteredDistance1 = out.filterByPeakDistance(12);

Peak Sharpness

GET SHARPNESS
double[][] outSharpness = out.getPeakSharpness();
SHARPNESS CALCULATION CODE
double[][] sharpness = out.findPeakSharpness(peaks);
SHARPNESS FILTERING CODE
int[] filteredSharp1 = out.filterBySharpness(0.002, 0.005);

Peak Prominence

GET PROMINENCE
double[][] outPromData = out.getProminenceData(); // To get the prominence, left base and right base
double[] outProminence = out.getProminence();
PROMINENCE CALCULATION CODE
double[][] promData = out.findPeakProminence(peaks); // To get the prominence, left base and right base
double[] prominence = promData[0];
PROMINENCE FILTERING CODE
int[] filteredProm1 = out.filterByProminence(0.2, 0.6);

Peak Width

GET WIDTH
double[][] outWidthData = out.getWidthData(); // To get the width, left base and right base at a relative height of 0.5
double[] outWidth = out.getWidth();
WIDTH CALCULATION CODE
double[][] widthData = out.findPeakWidth(peaks, 0.5); // To get the width, left base and right base at a relative height of 0.5
double[] width = widthData[0];
WIDTH FILTERING CODE
int[] filteredWidth1 = out.filterByWidth(5.0, 20.0);
Clone this wiki locally