PeakFinder.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. ////////////////////////////////////////////////////////////////////////////////
  2. ///
  3. /// Peak detection routine.
  4. ///
  5. /// The routine detects highest value on an array of values and calculates the
  6. /// precise peak location as a mass-center of the 'hump' around the peak value.
  7. ///
  8. /// Author : Copyright (c) Olli Parviainen
  9. /// Author e-mail : oparviai 'at' iki.fi
  10. /// SoundTouch WWW: http://www.surina.net/soundtouch
  11. ///
  12. ////////////////////////////////////////////////////////////////////////////////
  13. //
  14. // License :
  15. //
  16. // SoundTouch audio processing library
  17. // Copyright (c) Olli Parviainen
  18. //
  19. // This library is free software; you can redistribute it and/or
  20. // modify it under the terms of the GNU Lesser General Public
  21. // License as published by the Free Software Foundation; either
  22. // version 2.1 of the License, or (at your option) any later version.
  23. //
  24. // This library is distributed in the hope that it will be useful,
  25. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  27. // Lesser General Public License for more details.
  28. //
  29. // You should have received a copy of the GNU Lesser General Public
  30. // License along with this library; if not, write to the Free Software
  31. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  32. //
  33. ////////////////////////////////////////////////////////////////////////////////
  34. #include <math.h>
  35. #include <assert.h>
  36. #include "PeakFinder.h"
  37. using namespace soundtouch;
  38. #define max(x, y) (((x) > (y)) ? (x) : (y))
  39. PeakFinder::PeakFinder()
  40. {
  41. minPos = maxPos = 0;
  42. }
  43. // Finds real 'top' of a peak hump from neighnourhood of the given 'peakpos'.
  44. int PeakFinder::findTop(const float *data, int peakpos) const
  45. {
  46. int i;
  47. int start, end;
  48. float refvalue;
  49. refvalue = data[peakpos];
  50. // seek within ±10 points
  51. start = peakpos - 10;
  52. if (start < minPos) start = minPos;
  53. end = peakpos + 10;
  54. if (end > maxPos) end = maxPos;
  55. for (i = start; i <= end; i ++)
  56. {
  57. if (data[i] > refvalue)
  58. {
  59. peakpos = i;
  60. refvalue = data[i];
  61. }
  62. }
  63. // failure if max value is at edges of seek range => it's not peak, it's at slope.
  64. if ((peakpos == start) || (peakpos == end)) return 0;
  65. return peakpos;
  66. }
  67. // Finds 'ground level' of a peak hump by starting from 'peakpos' and proceeding
  68. // to direction defined by 'direction' until next 'hump' after minimum value will
  69. // begin
  70. int PeakFinder::findGround(const float *data, int peakpos, int direction) const
  71. {
  72. int lowpos;
  73. int pos;
  74. int climb_count;
  75. float refvalue;
  76. float delta;
  77. climb_count = 0;
  78. refvalue = data[peakpos];
  79. lowpos = peakpos;
  80. pos = peakpos;
  81. while ((pos > minPos+1) && (pos < maxPos-1))
  82. {
  83. int prevpos;
  84. prevpos = pos;
  85. pos += direction;
  86. // calculate derivate
  87. delta = data[pos] - data[prevpos];
  88. if (delta <= 0)
  89. {
  90. // going downhill, ok
  91. if (climb_count)
  92. {
  93. climb_count --; // decrease climb count
  94. }
  95. // check if new minimum found
  96. if (data[pos] < refvalue)
  97. {
  98. // new minimum found
  99. lowpos = pos;
  100. refvalue = data[pos];
  101. }
  102. }
  103. else
  104. {
  105. // going uphill, increase climbing counter
  106. climb_count ++;
  107. if (climb_count > 5) break; // we've been climbing too long => it's next uphill => quit
  108. }
  109. }
  110. return lowpos;
  111. }
  112. // Find offset where the value crosses the given level, when starting from 'peakpos' and
  113. // proceeds to direction defined in 'direction'
  114. int PeakFinder::findCrossingLevel(const float *data, float level, int peakpos, int direction) const
  115. {
  116. float peaklevel;
  117. int pos;
  118. peaklevel = data[peakpos];
  119. assert(peaklevel >= level);
  120. pos = peakpos;
  121. while ((pos >= minPos) && (pos + direction < maxPos))
  122. {
  123. if (data[pos + direction] < level) return pos; // crossing found
  124. pos += direction;
  125. }
  126. return -1; // not found
  127. }
  128. // Calculates the center of mass location of 'data' array items between 'firstPos' and 'lastPos'
  129. double PeakFinder::calcMassCenter(const float *data, int firstPos, int lastPos) const
  130. {
  131. int i;
  132. float sum;
  133. float wsum;
  134. sum = 0;
  135. wsum = 0;
  136. for (i = firstPos; i <= lastPos; i ++)
  137. {
  138. sum += (float)i * data[i];
  139. wsum += data[i];
  140. }
  141. if (wsum < 1e-6) return 0;
  142. return sum / wsum;
  143. }
  144. /// get exact center of peak near given position by calculating local mass of center
  145. double PeakFinder::getPeakCenter(const float *data, int peakpos) const
  146. {
  147. float peakLevel; // peak level
  148. int crosspos1, crosspos2; // position where the peak 'hump' crosses cutting level
  149. float cutLevel; // cutting value
  150. float groundLevel; // ground level of the peak
  151. int gp1, gp2; // bottom positions of the peak 'hump'
  152. // find ground positions.
  153. gp1 = findGround(data, peakpos, -1);
  154. gp2 = findGround(data, peakpos, 1);
  155. peakLevel = data[peakpos];
  156. if (gp1 == gp2)
  157. {
  158. // avoid rounding errors when all are equal
  159. assert(gp1 == peakpos);
  160. cutLevel = groundLevel = peakLevel;
  161. } else {
  162. // get average of the ground levels
  163. groundLevel = 0.5f * (data[gp1] + data[gp2]);
  164. // calculate 70%-level of the peak
  165. cutLevel = 0.70f * peakLevel + 0.30f * groundLevel;
  166. }
  167. // find mid-level crossings
  168. crosspos1 = findCrossingLevel(data, cutLevel, peakpos, -1);
  169. crosspos2 = findCrossingLevel(data, cutLevel, peakpos, 1);
  170. if ((crosspos1 < 0) || (crosspos2 < 0)) return 0; // no crossing, no peak..
  171. // calculate mass center of the peak surroundings
  172. return calcMassCenter(data, crosspos1, crosspos2);
  173. }
  174. double PeakFinder::detectPeak(const float *data, int aminPos, int amaxPos)
  175. {
  176. int i;
  177. int peakpos; // position of peak level
  178. double highPeak, peak;
  179. this->minPos = aminPos;
  180. this->maxPos = amaxPos;
  181. // find absolute peak
  182. peakpos = minPos;
  183. peak = data[minPos];
  184. for (i = minPos + 1; i < maxPos; i ++)
  185. {
  186. if (data[i] > peak)
  187. {
  188. peak = data[i];
  189. peakpos = i;
  190. }
  191. }
  192. // Calculate exact location of the highest peak mass center
  193. highPeak = getPeakCenter(data, peakpos);
  194. peak = highPeak;
  195. // Now check if the highest peak were in fact harmonic of the true base beat peak
  196. // - sometimes the highest peak can be Nth harmonic of the true base peak yet
  197. // just a slightly higher than the true base
  198. for (i = 1; i < 3; i ++)
  199. {
  200. double peaktmp, harmonic;
  201. int i1,i2;
  202. harmonic = (double)pow(2.0, i);
  203. peakpos = (int)(highPeak / harmonic + 0.5f);
  204. if (peakpos < minPos) break;
  205. peakpos = findTop(data, peakpos); // seek true local maximum index
  206. if (peakpos == 0) continue; // no local max here
  207. // calculate mass-center of possible harmonic peak
  208. peaktmp = getPeakCenter(data, peakpos);
  209. // accept harmonic peak if
  210. // (a) it is found
  211. // (b) is within ±4% of the expected harmonic interval
  212. // (c) has at least half x-corr value of the max. peak
  213. double diff = harmonic * peaktmp / highPeak;
  214. if ((diff < 0.96) || (diff > 1.04)) continue; // peak too afar from expected
  215. // now compare to highest detected peak
  216. i1 = (int)(highPeak + 0.5);
  217. i2 = (int)(peaktmp + 0.5);
  218. if (data[i2] >= 0.4*data[i1])
  219. {
  220. // The harmonic is at least half as high primary peak,
  221. // thus use the harmonic peak instead
  222. peak = peaktmp;
  223. }
  224. }
  225. return peak;
  226. }