util.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. dsp/util.h
  3. Copyright 2002-4 Tim Goetze <tim@quitte.de>
  4. http://quitte.de/dsp/
  5. Common math utility functions.
  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_UTIL_H_
  22. #define _DSP_UTIL_H_
  23. namespace DSP {
  24. inline int next_power_of_2 (int n)
  25. {
  26. assert (n <= 0x40000000);
  27. int m = 1;
  28. while (m < n)
  29. m <<= 1;
  30. return m;
  31. }
  32. inline bool
  33. isprime (int v)
  34. {
  35. if (v <= 3)
  36. return true;
  37. if (!(v & 1))
  38. return false;
  39. for (int i = 3; i < (int) sqrt (v) + 1; i += 2)
  40. if ((v % i) == 0)
  41. return false;
  42. return true;
  43. }
  44. inline double
  45. db2lin (double db)
  46. {
  47. return pow (10., db * .05);
  48. }
  49. inline double
  50. lin2db (double lin)
  51. {
  52. return 20. * log10 (lin);
  53. }
  54. } /* namespace DSP */
  55. #endif /* _DSP_UTIL_H_ */