src/mathutils.{c,h}: add fvec_quadratic_peak_pos, a fixed replacement for fvec_quadint
authorPaul Brossier <piem@piem.org>
Mon, 8 Apr 2013 15:19:10 +0000 (10:19 -0500)
committerPaul Brossier <piem@piem.org>
Mon, 8 Apr 2013 15:19:10 +0000 (10:19 -0500)
src/mathutils.c
src/mathutils.h

index b0a958b..7d2424e 100644 (file)
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org>
+  Copyright (C) 2003-2013 Paul Brossier <piem@aubio.org>
 
   This file is part of aubio.
 
@@ -379,6 +379,18 @@ smpl_t fvec_quadint (fvec_t * x, uint_t pos) {
   return pos + 0.5 * (s2 - s0 ) / (s2 - 2.* s1 + s0);
 }
 
+smpl_t fvec_quadratic_peak_pos (fvec_t * x, uint_t pos) {
+  smpl_t s0, s1, s2;
+  uint_t x0 = (pos < 1) ? pos : pos - 1;
+  uint_t x2 = (pos + 1 < x->length) ? pos + 1 : pos;
+  if (x0 == pos) return (x->data[pos] <= x->data[x2]) ? pos : x2;
+  if (x2 == pos) return (x->data[pos] <= x->data[x0]) ? pos : x0;
+  s0 = x->data[x0];
+  s1 = x->data[pos];
+  s2 = x->data[x2];
+  return pos + 0.5 * (s0 - s2 ) / (s0 - 2.* s1 + s2);
+}
+
 uint_t fvec_peakpick(fvec_t * onset, uint_t pos) {
   uint_t tmp=0;
   tmp = (onset->data[pos] > onset->data[pos-1]
index 01142dd..6d6c8fb 100644 (file)
@@ -235,6 +235,21 @@ smpl_t fvec_median (fvec_t * v);
 /** finds exact peak index by quadratic interpolation*/
 smpl_t fvec_quadint (fvec_t * x, uint_t pos);
 
+/** finds exact peak index by quadratic interpolation
+
+  See [Quadratic Interpolation of Spectral
+  Peaks](https://ccrma.stanford.edu/~jos/sasp/Quadratic_Peak_Interpolation.html),
+  by Julius O. Smith III
+
+  \f$ p_{frac} = \frac{1}{2} \frac {x[p-1] - x[p+1]} {x[p-1] - 2 x[p] + x[p+1]} \in [ -.5, .5] \f$
+
+  \param x vector to get the interpolated peak position from
+  \param p index of the peak in vector `x`
+  \return \f$ p + p_{frac} \f$ exact peak position of interpolated maximum or minimum
+
+*/
+smpl_t fvec_quadratic_peak_pos (fvec_t * x, uint_t p);
+
 /** Quadratic interpolation using Lagrange polynomial.
  
   Inspired from ``Comparison of interpolation algorithms in real-time sound