libcm is a C development framework with an emphasis on audio signal processing applications.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmMath.h 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //| Copyright: (C) 2009-2020 Kevin Larke <contact AT larke DOT org>
  2. //| License: GNU GPL version 3.0 or above. See the accompanying LICENSE file.
  3. #ifndef cmMath_h
  4. #define cmMath_h
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. //( { file_desc:"Math utility functions" kw:[math] }
  9. double cmX80ToDouble( unsigned char s[10] );
  10. void cmDoubleToX80( double v, unsigned char s[10] );
  11. bool cmIsPowerOfTwo( unsigned i );
  12. unsigned cmNextPowerOfTwo( unsigned i );
  13. unsigned cmNearPowerOfTwo( unsigned i );
  14. bool cmIsOddU( unsigned v );
  15. bool cmIsEvenU( unsigned v );
  16. unsigned cmNextOddU( unsigned v );
  17. unsigned cmPrevOddU( unsigned v );
  18. unsigned cmNextEvenU( unsigned v );
  19. unsigned cmPrevEvenU( unsigned v );
  20. /// Increment or decrement 'idx' by 'delta' always wrapping the result into the range
  21. /// 0 to (maxN-1).
  22. /// 'idx': initial value
  23. /// 'delta': incremental amount
  24. /// 'maxN' - 1 : maximum return value.
  25. unsigned cmModIncr(int idx, int delta, int maxN );
  26. // modified bessel function of first kind, order 0
  27. // ref: orfandis appendix B io.m
  28. double cmBessel0( double x );
  29. //=================================================================
  30. // The following elliptic-related function approximations come from
  31. // Parks & Burrus, Digital Filter Design, Appendix program 9, pp. 317-326
  32. // which in turn draws directly on other sources
  33. // calculate complete elliptic integral (quarter period) K
  34. // given *complimentary* modulus kc
  35. cmReal_t cmEllipK( cmReal_t kc );
  36. // calculate elliptic modulus k
  37. // given ratio of complete elliptic integrals r = K/K'
  38. // (solves the "degree equation" for fixed N = K*K1'/K'K1)
  39. cmReal_t cmEllipDeg( cmReal_t r );
  40. // calculate arc elliptic tangent u (elliptic integral of the 1st kind)
  41. // given argument x = sc(u,k) and *complimentary* modulus kc
  42. cmReal_t cmEllipArcSc( cmReal_t x, cmReal_t kc );
  43. // calculate Jacobi elliptic functions sn, cn, and dn
  44. // given argument u and *complimentary* modulus kc
  45. cmRC_t cmEllipJ( cmReal_t u, cmReal_t kc, cmReal_t* sn, cmReal_t* cn, cmReal_t* dn );
  46. //=================================================================
  47. // bilinear transform
  48. // z = (2*sr + s)/(2*sr - s)
  49. cmRC_t cmBlt( unsigned n, cmReal_t sr, cmReal_t* rp, cmReal_t* ip );
  50. //=================================================================
  51. // Pitch conversion
  52. unsigned cmHzToMidi( double hz );
  53. float cmMidiToHz( unsigned midi );
  54. //=================================================================
  55. // Floating point byte swapping
  56. unsigned cmFfSwapFloatToUInt( float v );
  57. float cmFfSwapUIntToFloat( unsigned v );
  58. unsigned long long cmFfSwapDoubleToULLong( double v );
  59. double cmFfSwapULLongToDouble( unsigned long long v );
  60. //=================================================================
  61. int cmRandInt( int min, int max );
  62. unsigned cmRandUInt( unsigned min, unsigned max );
  63. float cmRandFloat( float min, float max );
  64. double cmRandDouble( double min, double max );
  65. //=================================================================
  66. bool cmIsCloseD( double x0, double x1, double eps );
  67. bool cmIsCloseF( float x0, float x1, double eps );
  68. bool cmIsCloseI( int x0, int x1, double eps );
  69. bool cmIsCloseU( unsigned x0, unsigned x1, double eps );
  70. //=================================================================
  71. // Run a length 'lfsrN' linear feedback shift register (LFSR) for 'yN' iterations to
  72. // produce a length 'yN' bit string in yV[yN].
  73. // 'lfsrN' count of bits in the shift register range: 2<= lfsrN <= 32.
  74. // 'tapMask' is a bit mask which gives the tap indexes positions for the LFSR.
  75. // The least significant bit corresponds to the maximum delay tap position.
  76. // The min tap position is therefore denoted by the tap mask bit location 1 << (lfsrN-1).
  77. // A minimum of two taps must exist.
  78. // 'seed' sets the initial delay state.
  79. // 'yV[yN]' is the the output vector
  80. // 'yN' is count of elements in yV.
  81. // The function resturn kOkAtRC on success or kInvalidArgsRCRC if any arguments are invalid.
  82. // /sa cmLFSR_Test.
  83. void cmLFSR( unsigned lfsrN, unsigned tapMask, unsigned seed, unsigned* yV, unsigned yN );
  84. // Example and test code for cmLFSR()
  85. bool cmLFSR_Test();
  86. // Generate a set of 'goldN' Gold codes using the Maximum Length Sequences (MLS) generated
  87. // by a length 'lfsrN' linear feedback shift register.
  88. // 'err' is an error object to be set if the the function fails.
  89. // 'lfsrN' is the length of the Linear Feedback Shift Registers (LFSR) used to generate the MLS.
  90. // 'poly_coeff0' tap mask for the first LFSR.
  91. // 'coeff1' tap mask the the second LFSR.
  92. // 'goldN' is the count of Gold codes to generate.
  93. // 'yM[mlsN', goldN] is a column major output matrix where each column contains a Gold code.
  94. // 'mlsN' is the length of the maximum length sequence for each Gold code which can be
  95. // calculated as mlsN = (1 << a->lfsrN) - 1.
  96. // Note that values of 'lfsrN' and the 'poly_coeffx' must be carefully selected such that
  97. // they will produce a MLS. For example to generate a MLS with length 31 set 'lfsrN' to 5 and
  98. // then select poly_coeff from two different elements of the set {0x12 0x14 0x17 0x1B 0x1D 0x1E}.
  99. // See http://www.ece.cmu.edu/~koopman/lfsr/index.html for a complete set of MSL polynomial
  100. // coefficients for given LFSR lengths.
  101. // Returns false if insufficient balanced pairs exist.
  102. bool cmGenGoldCodes( unsigned lfsrN, unsigned poly_coeff0, unsigned poly_coeff1, unsigned goldN, int* yM, unsigned mlsN );
  103. //)
  104. #ifdef __cplusplus
  105. }
  106. #endif
  107. #endif