# Is there a relatively easy way to detect likely real-time peaks in discrete-time data?

Let's say I have a set of data over time, t:

[0, 4, 6, 7, 7, 6, 4, 0]


It seems likely that this data would peak at t=3.5.

Is there a well-known algorithm for calculating this sort of peak?

• hey chaimp, i go to your website chaimpeck.com and it says "Malware detected". that's not very friendly. Nov 29 '15 at 19:57
• I have an old version of wordpress that I haven't updated in a while. That is probably why. I need to update that. Thanks for the heads-up Nov 29 '15 at 20:18
• what is the step size? Jan 19 '16 at 21:49

The most straight-forward solution would be to interpolate the discrete values around the maximum you're interested in, and compute the location of the maximum of the interpolating function. I'd start by using a quadratic or cubic polynomial. This is a simple example of using an interpolating quadratic polynomial.

• i'd use a quadratic. it's unlikely that you will have two adjacent samples at a peak that are precisely the same value (and even if you do, the quadratic interpolation still works fine). so pick the discrete peak $x[n]$ and the two samples adjacent to it: the quadratic estimation for the peak is at: $$p = n + \frac12 \frac{x[n+1]-x[n-1]}{2x[n]-x[n+1]-x[n-1]}$$ just to be clear: $x[n] \ge x[n-1]$ and $x[n] \ge x[n+1]$ so $n-\frac12 \le p \le n+\frac12$ . Nov 29 '15 at 18:06
• I like this solution, and thank you for posting the formula @robertbristow-johnson Is it trivial to expand this formula to more than two adjacent samples. And would doing so increase the accuracy of finding a real peak, or does it suffice to just use three samples? Note, I would like to do this on FFT data for the purpose of tuning an instrument accurately, so in a typical use case the peak is clear for a human to see on a graph (i.e. not a lot of noise and an obvious looking spike close to the expected frequency). Thank you for the fast response!!! Nov 29 '15 at 19:09
• "Is it trivial to expand this formula to more than two adjacent samples [?]" -- no, but why would you want to? -- "I would like to do this on FFT data for the purpose of tuning an instrument accurately," -- appears to be what we call "pitch detection". i wouldn't bother with the FFT. just do autocorrelation or AMDF and apply this same formula for peaks or valleys. the result is the period not frequency, but you can reciprocate that. Nov 29 '15 at 19:49
• Thank you. I am learning a lot from your comments. I found an article on autocorrelation and I intend to try that. Another idea that I had was to create a band-pass filter with a spike at the exact frequency that I want (i.e. 110Hz for an A-string) and then to calculate the sum of the amplitudes of the resulting FFT, which should tell me how close it is to the actual frequency. Does that sound feasible or is there some drawback I am not thinking of? Nov 29 '15 at 20:22
• yeah, but musical notes have harmonics. it's even possible for a musical tone to have energy at the harmonics and not much at the fundamental. then your BPF fails. if you want to have a bank of tuned filters, it makes more sense to have a bank of comb filters tuned to each note. but sweeping different lengths (or "lags") for autocorrelation (or AMDF or ASDF) is like sweeping a comb filter. if you're building a pitch detector, that's where i would suggest starting. Nov 29 '15 at 21:41