PeakFinder.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. ////////////////////////////////////////////////////////////////////////////////
  2. ///
  3. /// The routine detects highest value on an array of values and calculates the
  4. /// precise peak location as a mass-center of the 'hump' around the peak value.
  5. ///
  6. /// Author : Copyright (c) Olli Parviainen
  7. /// Author e-mail : oparviai 'at' iki.fi
  8. /// SoundTouch WWW: http://www.surina.net/soundtouch
  9. ///
  10. ////////////////////////////////////////////////////////////////////////////////
  11. //
  12. // License :
  13. //
  14. // SoundTouch audio processing library
  15. // Copyright (c) Olli Parviainen
  16. //
  17. // This library is free software; you can redistribute it and/or
  18. // modify it under the terms of the GNU Lesser General Public
  19. // License as published by the Free Software Foundation; either
  20. // version 2.1 of the License, or (at your option) any later version.
  21. //
  22. // This library is distributed in the hope that it will be useful,
  23. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  25. // Lesser General Public License for more details.
  26. //
  27. // You should have received a copy of the GNU Lesser General Public
  28. // License along with this library; if not, write to the Free Software
  29. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  30. //
  31. ////////////////////////////////////////////////////////////////////////////////
  32. #ifndef _PeakFinder_H_
  33. #define _PeakFinder_H_
  34. namespace soundtouch
  35. {
  36. class PeakFinder
  37. {
  38. protected:
  39. /// Min, max allowed peak positions within the data vector
  40. int minPos, maxPos;
  41. /// Calculates the mass center between given vector items.
  42. double calcMassCenter(const float *data, ///< Data vector.
  43. int firstPos, ///< Index of first vector item belonging to the peak.
  44. int lastPos ///< Index of last vector item belonging to the peak.
  45. ) const;
  46. /// Finds the data vector index where the monotoniously decreasing signal crosses the
  47. /// given level.
  48. int findCrossingLevel(const float *data, ///< Data vector.
  49. float level, ///< Goal crossing level.
  50. int peakpos, ///< Peak position index within the data vector.
  51. int direction /// Direction where to proceed from the peak: 1 = right, -1 = left.
  52. ) const;
  53. // Finds real 'top' of a peak hump from neighnourhood of the given 'peakpos'.
  54. int findTop(const float *data, int peakpos) const;
  55. /// Finds the 'ground' level, i.e. smallest level between two neighbouring peaks, to right-
  56. /// or left-hand side of the given peak position.
  57. int findGround(const float *data, /// Data vector.
  58. int peakpos, /// Peak position index within the data vector.
  59. int direction /// Direction where to proceed from the peak: 1 = right, -1 = left.
  60. ) const;
  61. /// get exact center of peak near given position by calculating local mass of center
  62. double getPeakCenter(const float *data, int peakpos) const;
  63. public:
  64. /// Constructor.
  65. PeakFinder();
  66. /// Detect exact peak position of the data vector by finding the largest peak 'hump'
  67. /// and calculating the mass-center location of the peak hump.
  68. ///
  69. /// \return The location of the largest base harmonic peak hump.
  70. double detectPeak(const float *data, /// Data vector to be analyzed. The data vector has
  71. /// to be at least 'maxPos' items long.
  72. int minPos, ///< Min allowed peak location within the vector data.
  73. int maxPos ///< Max allowed peak location within the vector data.
  74. );
  75. };
  76. }
  77. #endif // _PeakFinder_H_