annotate PeakInterpolator.cpp @ 40:8f56ef28b0b1

Minor fixes
author Chris Cannam
date Fri, 20 Jul 2012 10:46:40 +0100
parents 822cf7b8e070
children 45b4401136f6
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@39 43 PeakInterpolator::findPeakLocation(const double *data, int size, int peakIndex)
Chris@39 44 {
Chris@40 45 std::cerr << "findPeakLocation: size " << size << ", peakIndex " << peakIndex << std::endl;
Chris@40 46
Chris@40 47 if (peakIndex < 1 || peakIndex > size - 2) {
Chris@40 48 std::cerr << "returning " << peakIndex << ", data too short" << std::endl;
Chris@39 49 return peakIndex;
Chris@39 50 }
Chris@39 51
Chris@39 52 double maxval = 0.0;
Chris@39 53 double location = peakIndex;
Chris@39 54
Chris@39 55 const int divisions = 10;
Chris@39 56 double y[4];
Chris@39 57
Chris@39 58 y[0] = data[peakIndex-1];
Chris@39 59 y[1] = data[peakIndex];
Chris@39 60 y[2] = data[peakIndex+1];
Chris@40 61 if (peakIndex < size - 2) {
Chris@40 62 y[3] = data[peakIndex+2];
Chris@40 63 } else {
Chris@40 64 y[3] = y[2];
Chris@40 65 }
Chris@40 66 std::cerr << "a y: " << y[0] << " " << y[1] << " " << y[2] << " " << y[3] << std::endl;
Chris@39 67 for (int i = 0; i < divisions; ++i) {
Chris@39 68 double probe = double(i) / double(divisions);
Chris@39 69 double value = cubicInterpolate(y, probe);
Chris@40 70 std::cerr << "probe = " << probe << ", value = " << value << " for location " << peakIndex + probe << std::endl;
Chris@39 71 if (value > maxval) {
Chris@39 72 maxval = value;
Chris@39 73 location = peakIndex + probe;
Chris@39 74 }
Chris@39 75 }
Chris@39 76
Chris@39 77 y[3] = y[2];
Chris@39 78 y[2] = y[1];
Chris@39 79 y[1] = y[0];
Chris@40 80 if (peakIndex > 1) {
Chris@40 81 y[0] = data[peakIndex-2];
Chris@40 82 } else {
Chris@40 83 y[0] = y[1];
Chris@40 84 }
Chris@40 85 std::cerr << "b y: " << y[0] << " " << y[1] << " " << y[2] << " " << y[3] << std::endl;
Chris@39 86 for (int i = 0; i < divisions; ++i) {
Chris@39 87 double probe = double(i) / double(divisions);
Chris@39 88 double value = cubicInterpolate(y, probe);
Chris@40 89 std::cerr << "probe = " << probe << ", value = " << value << " for location " << peakIndex - 1 + probe << std::endl;
Chris@39 90 if (value > maxval) {
Chris@39 91 maxval = value;
Chris@39 92 location = peakIndex - 1 + probe;
Chris@39 93 }
Chris@39 94 }
Chris@39 95
Chris@40 96 std::cerr << "returning " << location << std::endl;
Chris@40 97
Chris@39 98 return location;
Chris@39 99 }
Chris@39 100