To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / PeakInterpolator.cpp @ 40:8f56ef28b0b1
History | View | Annotate | Download (3.3 KB)
| 1 |
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
|---|---|
| 2 |
/*
|
| 3 |
This file is Copyright (c) 2012 Chris Cannam
|
| 4 |
|
| 5 |
Permission is hereby granted, free of charge, to any person
|
| 6 |
obtaining a copy of this software and associated documentation
|
| 7 |
files (the "Software"), to deal in the Software without
|
| 8 |
restriction, including without limitation the rights to use, copy,
|
| 9 |
modify, merge, publish, distribute, sublicense, and/or sell copies
|
| 10 |
of the Software, and to permit persons to whom the Software is
|
| 11 |
furnished to do so, subject to the following conditions:
|
| 12 |
|
| 13 |
The above copyright notice and this permission notice shall be
|
| 14 |
included in all copies or substantial portions of the Software.
|
| 15 |
|
| 16 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
| 17 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
| 18 |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
| 19 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
|
| 20 |
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
| 21 |
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
| 22 |
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
| 23 |
*/
|
| 24 |
|
| 25 |
#include "PeakInterpolator.h" |
| 26 |
|
| 27 |
#include <iostream> |
| 28 |
|
| 29 |
static double cubicInterpolate(const double y[4], double x) |
| 30 |
{
|
| 31 |
double a0 = y[3] - y[2] - y[0] + y[1]; |
| 32 |
double a1 = y[0] - y[1] - a0; |
| 33 |
double a2 = y[2] - y[0]; |
| 34 |
double a3 = y[1]; |
| 35 |
return
|
| 36 |
a0 * x * x * x + |
| 37 |
a1 * x * x + |
| 38 |
a2 * x + |
| 39 |
a3; |
| 40 |
} |
| 41 |
|
| 42 |
double
|
| 43 |
PeakInterpolator::findPeakLocation(const double *data, int size, int peakIndex) |
| 44 |
{
|
| 45 |
std::cerr << "findPeakLocation: size " << size << ", peakIndex " << peakIndex << std::endl; |
| 46 |
|
| 47 |
if (peakIndex < 1 || peakIndex > size - 2) { |
| 48 |
std::cerr << "returning " << peakIndex << ", data too short" << std::endl; |
| 49 |
return peakIndex;
|
| 50 |
} |
| 51 |
|
| 52 |
double maxval = 0.0; |
| 53 |
double location = peakIndex;
|
| 54 |
|
| 55 |
const int divisions = 10; |
| 56 |
double y[4]; |
| 57 |
|
| 58 |
y[0] = data[peakIndex-1]; |
| 59 |
y[1] = data[peakIndex];
|
| 60 |
y[2] = data[peakIndex+1]; |
| 61 |
if (peakIndex < size - 2) { |
| 62 |
y[3] = data[peakIndex+2]; |
| 63 |
} else {
|
| 64 |
y[3] = y[2]; |
| 65 |
} |
| 66 |
std::cerr << "a y: " << y[0] << " " << y[1] << " " << y[2] << " " << y[3] << std::endl; |
| 67 |
for (int i = 0; i < divisions; ++i) { |
| 68 |
double probe = double(i) / double(divisions); |
| 69 |
double value = cubicInterpolate(y, probe);
|
| 70 |
std::cerr << "probe = " << probe << ", value = " << value << " for location " << peakIndex + probe << std::endl; |
| 71 |
if (value > maxval) {
|
| 72 |
maxval = value; |
| 73 |
location = peakIndex + probe; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
y[3] = y[2]; |
| 78 |
y[2] = y[1]; |
| 79 |
y[1] = y[0]; |
| 80 |
if (peakIndex > 1) { |
| 81 |
y[0] = data[peakIndex-2]; |
| 82 |
} else {
|
| 83 |
y[0] = y[1]; |
| 84 |
} |
| 85 |
std::cerr << "b y: " << y[0] << " " << y[1] << " " << y[2] << " " << y[3] << std::endl; |
| 86 |
for (int i = 0; i < divisions; ++i) { |
| 87 |
double probe = double(i) / double(divisions); |
| 88 |
double value = cubicInterpolate(y, probe);
|
| 89 |
std::cerr << "probe = " << probe << ", value = " << value << " for location " << peakIndex - 1 + probe << std::endl; |
| 90 |
if (value > maxval) {
|
| 91 |
maxval = value; |
| 92 |
location = peakIndex - 1 + probe;
|
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
std::cerr << "returning " << location << std::endl;
|
| 97 |
|
| 98 |
return location;
|
| 99 |
} |
| 100 |
|