annotate PeakInterpolator.cpp @ 43:53260757fc9f

Switch from cubic to much simpler & faster parabolic interpolator
author Chris Cannam
date Fri, 20 Jul 2012 22:06:15 +0100
parents 45b4401136f6
children c5cd88de5809
rev   line source
Chris@39 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@39 2 /*
Chris@39 3 This file is Copyright (c) 2012 Chris Cannam
Chris@39 4
Chris@39 5 Permission is hereby granted, free of charge, to any person
Chris@39 6 obtaining a copy of this software and associated documentation
Chris@39 7 files (the "Software"), to deal in the Software without
Chris@39 8 restriction, including without limitation the rights to use, copy,
Chris@39 9 modify, merge, publish, distribute, sublicense, and/or sell copies
Chris@39 10 of the Software, and to permit persons to whom the Software is
Chris@39 11 furnished to do so, subject to the following conditions:
Chris@39 12
Chris@39 13 The above copyright notice and this permission notice shall be
Chris@39 14 included in all copies or substantial portions of the Software.
Chris@39 15
Chris@39 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Chris@39 17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Chris@39 18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Chris@39 19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
Chris@39 20 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
Chris@39 21 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
Chris@39 22 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Chris@39 23 */
Chris@39 24
Chris@39 25 #include "PeakInterpolator.h"
Chris@39 26
Chris@40 27 #include <iostream>
Chris@40 28
Chris@39 29 static double cubicInterpolate(const double y[4], double x)
Chris@39 30 {
Chris@39 31 double a0 = y[3] - y[2] - y[0] + y[1];
Chris@39 32 double a1 = y[0] - y[1] - a0;
Chris@39 33 double a2 = y[2] - y[0];
Chris@39 34 double a3 = y[1];
Chris@39 35 return
Chris@39 36 a0 * x * x * x +
Chris@39 37 a1 * x * x +
Chris@39 38 a2 * x +
Chris@39 39 a3;
Chris@39 40 }
Chris@39 41
Chris@39 42 double
Chris@42 43 PeakInterpolator::findPeakLocation(const double *data, int size)
Chris@42 44 {
Chris@42 45 double maxval;
Chris@42 46 int maxidx = 0;
Chris@42 47 int i;
Chris@42 48 for (i = 0; i < size; ++i) {
Chris@42 49 if (i == 0 || data[i] > maxval) {
Chris@42 50 maxval = data[i];
Chris@42 51 maxidx = i;
Chris@42 52 }
Chris@42 53 }
Chris@42 54 return findPeakLocation(data, size, maxidx);
Chris@42 55 }
Chris@42 56
Chris@42 57 double
Chris@39 58 PeakInterpolator::findPeakLocation(const double *data, int size, int peakIndex)
Chris@39 59 {
Chris@43 60 // after jos,
Chris@43 61 // https://ccrma.stanford.edu/~jos/sasp/Quadratic_Interpolation_Spectral_Peaks.html
Chris@40 62
Chris@40 63 if (peakIndex < 1 || peakIndex > size - 2) {
Chris@39 64 return peakIndex;
Chris@39 65 }
Chris@39 66
Chris@43 67 double alpha = data[peakIndex-1];
Chris@43 68 double beta = data[peakIndex];
Chris@43 69 double gamma = data[peakIndex+1];
Chris@39 70
Chris@43 71 double denom = (alpha - 2*beta + gamma);
Chris@39 72
Chris@43 73 if (denom == 0) {
Chris@43 74 // flat
Chris@43 75 return peakIndex;
Chris@39 76 }
Chris@39 77
Chris@43 78 double p = ((alpha - gamma) / denom) / 2.0;
Chris@39 79
Chris@43 80 return double(peakIndex) + p;
Chris@39 81 }
Chris@39 82