RMS.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. dsp/RMS.h
  3. Copyright 2004 Tim Goetze <tim@quitte.de>
  4. http://quitte.de/dsp/
  5. root-mean-square accumulator.
  6. */
  7. /*
  8. This program is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU General Public License
  10. as published by the Free Software Foundation; either version 2
  11. of the License, or (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  19. 02111-1307, USA or point your web browser to http://www.gnu.org.
  20. */
  21. #ifndef _DSP_RMS_H_
  22. #define _DSP_RMS_H_
  23. namespace DSP {
  24. class RMS
  25. {
  26. protected:
  27. sample_t buffer[64];
  28. int write;
  29. public:
  30. double sum;
  31. RMS()
  32. {
  33. write = 0;
  34. reset();
  35. }
  36. void reset()
  37. {
  38. sum = 0.;
  39. memset (buffer, 0, sizeof (buffer));
  40. }
  41. /* caution: pass in the *squared* sample value */
  42. void store (sample_t x)
  43. {
  44. sum -= buffer[write];
  45. sum += (buffer[write] = x);
  46. write = (write + 1) & 63;
  47. }
  48. sample_t process (sample_t x)
  49. {
  50. store (x);
  51. return rms();
  52. }
  53. sample_t rms()
  54. {
  55. /* fabs it before sqrt, just in case ... */
  56. return sqrt (fabs (sum) / 64);
  57. }
  58. };
  59. } /* namespace DSP */
  60. #endif /* _DSP_RMS_H_ */