# [R] Finding flat-topped "peaks" in simple data set

Carl Witthoft carl at witthoft.com
Thu Dec 23 20:29:12 CET 2010

```Hi.
First, you might have more success with turnpoints() in the pastecs package.
Next, consider an approach  which makes your peaks truly flattopped.
The sample dataset appears to show that the data on each side of your
peaks are down by at least 6 counts or so, so try running the phase
values thru a filter sort of like

{ R- pseudocode, so will need editing}
phase[i] <-  if (abs(phase[i+1]-phase[i])<6) phase[i+1] else phase[i]

Then any peak-finder will find only one peak there, and you can select
the center value if desired pretty easily.

Or, if necessary,  you could use instead the same if() condition but
replace phase[i] with NA instead of phase[i+1] .  Then there'd be a
single phase value remaining at each peak.

Carl

****<quote>*****
I have a new challenge. I often generate time-series data sets that look
like the one below, with a variable ("Phase") which has a series of
flat-topped peaks (sample data below with 5 "peaks"). I would like to
calculate the phase value for each peak. It would be great to calculate the
mean value for the peak (possibly using rollmean(), but given the low
variability within the broad peak even just picking one value from the peak
would be sufficient.
I have tried using peaks() from the library(simecol),

peaks(data\$Time,data\$phase, model="max"),

but because of the flat nature of the peaks and the fact that peaks() looks
for values with lower neighbors I am unable to find all the peaks
(especially the one between time 0.762-0.897). For all my data files the
maximum phase values will fall between 27 and 62 so adding a function which
removes the values below 27 is reasonable and a technique I have used to
eliminate some of the noise between the peaks.

```