0
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ hey chaimp, i go to your website chaimpeck.com and it says "Malware detected". that's not very friendly. $\endgroup$ – robert bristow-johnson Nov 29 '15 at 19:57
  • $\begingroup$ 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 $\endgroup$ – chaimp Nov 29 '15 at 20:18
  • $\begingroup$ what is the step size? $\endgroup$ – CroCo Jan 19 '16 at 21:49
2
$\begingroup$

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.

|improve this answer|||||
$\endgroup$
  • 1
    $\begingroup$ 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$ . $\endgroup$ – robert bristow-johnson Nov 29 '15 at 18:06
  • $\begingroup$ 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!!! $\endgroup$ – chaimp Nov 29 '15 at 19:09
  • $\begingroup$ "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. $\endgroup$ – robert bristow-johnson Nov 29 '15 at 19:49
  • $\begingroup$ 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? $\endgroup$ – chaimp Nov 29 '15 at 20:22
  • 2
    $\begingroup$ 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. $\endgroup$ – robert bristow-johnson Nov 29 '15 at 21:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.