2008-02-10 17:24:28 +01:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
///
|
|
|
|
|
/// Peak detection routine.
|
|
|
|
|
///
|
|
|
|
|
/// The routine detects highest value on an array of values and calculates the
|
|
|
|
|
/// precise peak location as a mass-center of the 'hump' around the peak value.
|
|
|
|
|
///
|
|
|
|
|
/// Author : Copyright (c) Olli Parviainen
|
|
|
|
|
/// Author e-mail : oparviai 'at' iki.fi
|
|
|
|
|
/// SoundTouch WWW: http://www.surina.net/soundtouch
|
|
|
|
|
///
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// License :
|
|
|
|
|
//
|
|
|
|
|
// SoundTouch audio processing library
|
|
|
|
|
// Copyright (c) Olli Parviainen
|
|
|
|
|
//
|
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
|
// version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
//
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
|
|
#include "PeakFinder.h"
|
|
|
|
|
|
2008-12-25 13:20:01 +01:00
|
|
|
|
using namespace soundtouch;
|
|
|
|
|
|
2008-12-13 19:43:55 +01:00
|
|
|
|
#define max(x, y) (((x) > (y)) ? (x) : (y))
|
|
|
|
|
|
2008-02-10 17:24:28 +01:00
|
|
|
|
|
|
|
|
|
PeakFinder::PeakFinder()
|
|
|
|
|
{
|
2009-02-21 17:00:14 +01:00
|
|
|
|
minPos = maxPos = 0;
|
2008-02-10 17:24:28 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-12-30 21:33:46 +01:00
|
|
|
|
// Finds real 'top' of a peak hump from neighnourhood of the given 'peakpos'.
|
|
|
|
|
int PeakFinder::findTop(const float *data, int peakpos) const
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
int start, end;
|
|
|
|
|
float refvalue;
|
|
|
|
|
|
|
|
|
|
refvalue = data[peakpos];
|
|
|
|
|
|
2018-08-12 19:51:24 +02:00
|
|
|
|
// seek within <20>10 points
|
2011-12-30 21:33:46 +01:00
|
|
|
|
start = peakpos - 10;
|
|
|
|
|
if (start < minPos) start = minPos;
|
|
|
|
|
end = peakpos + 10;
|
|
|
|
|
if (end > maxPos) end = maxPos;
|
|
|
|
|
|
|
|
|
|
for (i = start; i <= end; i ++)
|
|
|
|
|
{
|
|
|
|
|
if (data[i] > refvalue)
|
|
|
|
|
{
|
|
|
|
|
peakpos = i;
|
|
|
|
|
refvalue = data[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// failure if max value is at edges of seek range => it's not peak, it's at slope.
|
|
|
|
|
if ((peakpos == start) || (peakpos == end)) return 0;
|
|
|
|
|
|
|
|
|
|
return peakpos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-02-10 17:24:28 +01:00
|
|
|
|
// Finds 'ground level' of a peak hump by starting from 'peakpos' and proceeding
|
|
|
|
|
// to direction defined by 'direction' until next 'hump' after minimum value will
|
|
|
|
|
// begin
|
|
|
|
|
int PeakFinder::findGround(const float *data, int peakpos, int direction) const
|
|
|
|
|
{
|
|
|
|
|
int lowpos;
|
|
|
|
|
int pos;
|
|
|
|
|
int climb_count;
|
2011-12-30 21:33:46 +01:00
|
|
|
|
float refvalue;
|
2008-02-10 17:24:28 +01:00
|
|
|
|
float delta;
|
|
|
|
|
|
|
|
|
|
climb_count = 0;
|
|
|
|
|
refvalue = data[peakpos];
|
|
|
|
|
lowpos = peakpos;
|
|
|
|
|
|
|
|
|
|
pos = peakpos;
|
|
|
|
|
|
2012-08-30 21:45:25 +02:00
|
|
|
|
while ((pos > minPos+1) && (pos < maxPos-1))
|
2008-02-10 17:24:28 +01:00
|
|
|
|
{
|
|
|
|
|
int prevpos;
|
|
|
|
|
|
|
|
|
|
prevpos = pos;
|
|
|
|
|
pos += direction;
|
|
|
|
|
|
|
|
|
|
// calculate derivate
|
|
|
|
|
delta = data[pos] - data[prevpos];
|
|
|
|
|
if (delta <= 0)
|
|
|
|
|
{
|
|
|
|
|
// going downhill, ok
|
|
|
|
|
if (climb_count)
|
|
|
|
|
{
|
|
|
|
|
climb_count --; // decrease climb count
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check if new minimum found
|
|
|
|
|
if (data[pos] < refvalue)
|
|
|
|
|
{
|
|
|
|
|
// new minimum found
|
|
|
|
|
lowpos = pos;
|
|
|
|
|
refvalue = data[pos];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// going uphill, increase climbing counter
|
|
|
|
|
climb_count ++;
|
2012-08-30 21:45:25 +02:00
|
|
|
|
if (climb_count > 5) break; // we've been climbing too long => it's next uphill => quit
|
2008-02-10 17:24:28 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return lowpos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Find offset where the value crosses the given level, when starting from 'peakpos' and
|
|
|
|
|
// proceeds to direction defined in 'direction'
|
|
|
|
|
int PeakFinder::findCrossingLevel(const float *data, float level, int peakpos, int direction) const
|
|
|
|
|
{
|
|
|
|
|
float peaklevel;
|
|
|
|
|
int pos;
|
|
|
|
|
|
|
|
|
|
peaklevel = data[peakpos];
|
|
|
|
|
assert(peaklevel >= level);
|
|
|
|
|
pos = peakpos;
|
|
|
|
|
while ((pos >= minPos) && (pos < maxPos))
|
|
|
|
|
{
|
|
|
|
|
if (data[pos + direction] < level) return pos; // crossing found
|
|
|
|
|
pos += direction;
|
|
|
|
|
}
|
|
|
|
|
return -1; // not found
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Calculates the center of mass location of 'data' array items between 'firstPos' and 'lastPos'
|
2008-12-13 19:43:55 +01:00
|
|
|
|
double PeakFinder::calcMassCenter(const float *data, int firstPos, int lastPos) const
|
2008-02-10 17:24:28 +01:00
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
float sum;
|
|
|
|
|
float wsum;
|
|
|
|
|
|
|
|
|
|
sum = 0;
|
|
|
|
|
wsum = 0;
|
|
|
|
|
for (i = firstPos; i <= lastPos; i ++)
|
|
|
|
|
{
|
|
|
|
|
sum += (float)i * data[i];
|
|
|
|
|
wsum += data[i];
|
|
|
|
|
}
|
2009-02-21 17:00:14 +01:00
|
|
|
|
|
|
|
|
|
if (wsum < 1e-6) return 0;
|
2008-02-10 17:24:28 +01:00
|
|
|
|
return sum / wsum;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-12-13 19:43:55 +01:00
|
|
|
|
/// get exact center of peak near given position by calculating local mass of center
|
2009-02-21 17:00:14 +01:00
|
|
|
|
double PeakFinder::getPeakCenter(const float *data, int peakpos) const
|
2008-12-13 19:43:55 +01:00
|
|
|
|
{
|
2008-02-10 17:24:28 +01:00
|
|
|
|
float peakLevel; // peak level
|
|
|
|
|
int crosspos1, crosspos2; // position where the peak 'hump' crosses cutting level
|
|
|
|
|
float cutLevel; // cutting value
|
|
|
|
|
float groundLevel; // ground level of the peak
|
|
|
|
|
int gp1, gp2; // bottom positions of the peak 'hump'
|
|
|
|
|
|
|
|
|
|
// find ground positions.
|
|
|
|
|
gp1 = findGround(data, peakpos, -1);
|
|
|
|
|
gp2 = findGround(data, peakpos, 1);
|
|
|
|
|
|
2008-12-13 19:43:55 +01:00
|
|
|
|
peakLevel = data[peakpos];
|
2008-02-10 17:24:28 +01:00
|
|
|
|
|
2015-05-18 17:22:02 +02:00
|
|
|
|
if (gp1 == gp2)
|
|
|
|
|
{
|
|
|
|
|
// avoid rounding errors when all are equal
|
|
|
|
|
assert(gp1 == peakpos);
|
|
|
|
|
cutLevel = groundLevel = peakLevel;
|
|
|
|
|
} else {
|
|
|
|
|
// get average of the ground levels
|
|
|
|
|
groundLevel = 0.5f * (data[gp1] + data[gp2]);
|
|
|
|
|
|
|
|
|
|
// calculate 70%-level of the peak
|
|
|
|
|
cutLevel = 0.70f * peakLevel + 0.30f * groundLevel;
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-10 17:24:28 +01:00
|
|
|
|
// find mid-level crossings
|
|
|
|
|
crosspos1 = findCrossingLevel(data, cutLevel, peakpos, -1);
|
|
|
|
|
crosspos2 = findCrossingLevel(data, cutLevel, peakpos, 1);
|
|
|
|
|
|
|
|
|
|
if ((crosspos1 < 0) || (crosspos2 < 0)) return 0; // no crossing, no peak..
|
|
|
|
|
|
|
|
|
|
// calculate mass center of the peak surroundings
|
|
|
|
|
return calcMassCenter(data, crosspos1, crosspos2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-02-21 17:00:14 +01:00
|
|
|
|
double PeakFinder::detectPeak(const float *data, int aminPos, int amaxPos)
|
2008-12-13 19:43:55 +01:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
int peakpos; // position of peak level
|
|
|
|
|
double highPeak, peak;
|
|
|
|
|
|
2009-02-21 17:00:14 +01:00
|
|
|
|
this->minPos = aminPos;
|
|
|
|
|
this->maxPos = amaxPos;
|
2008-12-13 19:43:55 +01:00
|
|
|
|
|
|
|
|
|
// find absolute peak
|
|
|
|
|
peakpos = minPos;
|
|
|
|
|
peak = data[minPos];
|
|
|
|
|
for (i = minPos + 1; i < maxPos; i ++)
|
|
|
|
|
{
|
|
|
|
|
if (data[i] > peak)
|
|
|
|
|
{
|
|
|
|
|
peak = data[i];
|
|
|
|
|
peakpos = i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Calculate exact location of the highest peak mass center
|
|
|
|
|
highPeak = getPeakCenter(data, peakpos);
|
|
|
|
|
peak = highPeak;
|
|
|
|
|
|
|
|
|
|
// Now check if the highest peak were in fact harmonic of the true base beat peak
|
|
|
|
|
// - sometimes the highest peak can be Nth harmonic of the true base peak yet
|
|
|
|
|
// just a slightly higher than the true base
|
2011-12-30 21:33:46 +01:00
|
|
|
|
|
2018-05-10 19:23:39 +02:00
|
|
|
|
for (i = 1; i < 3; i ++)
|
2008-12-13 19:43:55 +01:00
|
|
|
|
{
|
2011-12-30 21:33:46 +01:00
|
|
|
|
double peaktmp, harmonic;
|
2008-12-13 19:43:55 +01:00
|
|
|
|
int i1,i2;
|
|
|
|
|
|
2018-05-10 19:23:39 +02:00
|
|
|
|
harmonic = (double)pow(2.0, i);
|
2011-12-30 21:33:46 +01:00
|
|
|
|
peakpos = (int)(highPeak / harmonic + 0.5f);
|
2008-12-13 19:43:55 +01:00
|
|
|
|
if (peakpos < minPos) break;
|
2011-12-30 21:33:46 +01:00
|
|
|
|
peakpos = findTop(data, peakpos); // seek true local maximum index
|
|
|
|
|
if (peakpos == 0) continue; // no local max here
|
2008-12-13 19:43:55 +01:00
|
|
|
|
|
2011-12-30 21:33:46 +01:00
|
|
|
|
// calculate mass-center of possible harmonic peak
|
2008-12-13 19:43:55 +01:00
|
|
|
|
peaktmp = getPeakCenter(data, peakpos);
|
|
|
|
|
|
2011-12-30 21:33:46 +01:00
|
|
|
|
// accept harmonic peak if
|
|
|
|
|
// (a) it is found
|
2018-08-12 19:51:24 +02:00
|
|
|
|
// (b) is within <20>4% of the expected harmonic interval
|
2011-12-30 21:33:46 +01:00
|
|
|
|
// (c) has at least half x-corr value of the max. peak
|
|
|
|
|
|
|
|
|
|
double diff = harmonic * peaktmp / highPeak;
|
|
|
|
|
if ((diff < 0.96) || (diff > 1.04)) continue; // peak too afar from expected
|
|
|
|
|
|
2008-12-13 19:43:55 +01:00
|
|
|
|
// now compare to highest detected peak
|
|
|
|
|
i1 = (int)(highPeak + 0.5);
|
|
|
|
|
i2 = (int)(peaktmp + 0.5);
|
2012-08-30 21:45:25 +02:00
|
|
|
|
if (data[i2] >= 0.4*data[i1])
|
2008-12-13 19:43:55 +01:00
|
|
|
|
{
|
2011-12-30 21:33:46 +01:00
|
|
|
|
// The harmonic is at least half as high primary peak,
|
|
|
|
|
// thus use the harmonic peak instead
|
2008-12-13 19:43:55 +01:00
|
|
|
|
peak = peaktmp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return peak;
|
|
|
|
|
}
|