To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / PeakInterpolator.cpp @ 42:45b4401136f6

History | View | Annotate | Download (3.63 KB)

1 39:822cf7b8e070 Chris
/* -*- 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 40:8f56ef28b0b1 Chris
#include <iostream>
28
29 39:822cf7b8e070 Chris
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 42:45b4401136f6 Chris
PeakInterpolator::findPeakLocation(const double *data, int size)
44
{
45
    double maxval;
46
    int maxidx = 0;
47
    int i;
48
    for (i = 0; i < size; ++i) {
49
        if (i == 0 || data[i] > maxval) {
50
            maxval = data[i];
51
            maxidx = i;
52
        }
53
    }
54
    return findPeakLocation(data, size, maxidx);
55
}
56
57
double
58 39:822cf7b8e070 Chris
PeakInterpolator::findPeakLocation(const double *data, int size, int peakIndex)
59
{
60 42:45b4401136f6 Chris
//    std::cerr << "findPeakLocation: size " << size << ", peakIndex " << peakIndex << std::endl;
61 40:8f56ef28b0b1 Chris
62
    if (peakIndex < 1 || peakIndex > size - 2) {
63 42:45b4401136f6 Chris
//        std::cerr << "returning " << peakIndex << ", data too short" << std::endl;
64 39:822cf7b8e070 Chris
        return peakIndex;
65
    }
66
67
    double maxval = 0.0;
68
    double location = peakIndex;
69
70
    const int divisions = 10;
71
    double y[4];
72
73
    y[0] = data[peakIndex-1];
74
    y[1] = data[peakIndex];
75
    y[2] = data[peakIndex+1];
76 40:8f56ef28b0b1 Chris
    if (peakIndex < size - 2) {
77
        y[3] = data[peakIndex+2];
78
    } else {
79
        y[3] = y[2];
80
    }
81 42:45b4401136f6 Chris
//    std::cerr << "a y: " << y[0] << " " << y[1] << " " << y[2] << " " << y[3] << std::endl;
82 39:822cf7b8e070 Chris
    for (int i = 0; i < divisions; ++i) {
83
        double probe = double(i) / double(divisions);
84
        double value = cubicInterpolate(y, probe);
85 42:45b4401136f6 Chris
//        std::cerr << "probe = " << probe << ", value = " << value << " for location " << peakIndex + probe << std::endl;
86 39:822cf7b8e070 Chris
        if (value > maxval) {
87
            maxval = value;
88
            location = peakIndex + probe;
89
        }
90
    }
91
92
    y[3] = y[2];
93
    y[2] = y[1];
94
    y[1] = y[0];
95 40:8f56ef28b0b1 Chris
    if (peakIndex > 1) {
96
        y[0] = data[peakIndex-2];
97
    } else {
98
        y[0] = y[1];
99
    }
100 42:45b4401136f6 Chris
//    std::cerr << "b y: " << y[0] << " " << y[1] << " " << y[2] << " " << y[3] << std::endl;
101 39:822cf7b8e070 Chris
    for (int i = 0; i < divisions; ++i) {
102
        double probe = double(i) / double(divisions);
103
        double value = cubicInterpolate(y, probe);
104 42:45b4401136f6 Chris
//        std::cerr << "probe = " << probe << ", value = " << value << " for location " << peakIndex - 1 + probe << std::endl;
105 39:822cf7b8e070 Chris
        if (value > maxval) {
106
            maxval = value;
107
            location = peakIndex - 1 + probe;
108
        }
109
    }
110
111 42:45b4401136f6 Chris
//    std::cerr << "returning " << location << std::endl;
112 40:8f56ef28b0b1 Chris
113 39:822cf7b8e070 Chris
    return location;
114
}