Advertisement
Guest User

sin_aprox.h

a guest
May 9th, 2013
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.75 KB | None | 0 0
  1. #pragma once
  2.  
  3. //
  4. #define uint32 unsigned int
  5. #define sint32   signed int
  6. #define uint16 unsigned short
  7. #define sint16   signed short
  8. #define uint8  unsigned char
  9.  
  10. // 16 bit sin tables
  11. extern uint16 sin_16_512[];
  12. extern uint16 sin_16_256[];
  13. extern uint16 sin_16_128[];
  14.  
  15. // 8 bit sin tables
  16. extern uint8  sin_8_512 [];
  17. extern uint8  sin_8_256 [];
  18. extern uint8  sin_8_128 [];
  19.  
  20. //
  21. #define fpsin( X ) sin_aprox_16bit( X )
  22. #define fpcos( X ) cos_aprox_16bit( X )
  23.  
  24.  
  25. // integer sin aproximation
  26. // input  range [-0x80, +0x80] wrapped if exeeds
  27. // output tange [-0x80, +0x80]
  28. inline sint32 sin_aprox_8bit( sint32 x )
  29. {
  30.     // wrap into range
  31.     x = ((x+0x80) & 0xFF) - 0x80;
  32.     // ax = absolute value
  33.     sint32 ax = (x > 0) ? x : -x;
  34.     return ((x << 2) - (((x << 2) * ax ) >> 7));
  35. }
  36.  
  37. // integer sin aproximation
  38. // input  range [-0x80, +0x80] wrapped if exeeds
  39. // output tange [-0x80, +0x80]
  40. inline sint32 cos_aprox_8bit( sint32 x )
  41. {
  42.     // wrap into range
  43.     x += 0x40;
  44.     x = ((x+0x80) & 0xFF) - 0x80;
  45.     // ax = absolute value
  46.     sint32 ax = (x > 0) ? x : -x;
  47.     return ((x << 2) - (((x << 2) * ax ) >> 7));
  48. }
  49.  
  50. // integer sin aproximation
  51. // input  range [-0x8000, +0x8000] wrapped if exeeds
  52. // output tange [-0x8000, +0x8000]
  53. inline sint32 sin_aprox_16bit( sint32 x )
  54. {
  55.     // wrap into range
  56.     x = ((x+0x8000) & 0xFFFF) - 0x8000;
  57.     // ax = absolute value
  58.     sint32 ax = (x > 0) ? x : -x;
  59.     sint32 y  = (x - ((x * (ax >> 2) ) >> 13));
  60.     return y << 2;
  61. }
  62.  
  63. // integer cos aproximation
  64. // input  range [-0x8000, +0x8000] wrapped if exeeds
  65. // output tange [-0x8000, +0x8000]
  66. inline sint32 cos_aprox_16bit( sint32 x )
  67. {
  68.     // wrap into range
  69.     x += 0x4000;
  70.     x  = ((x+0x8000) & 0xFFFF) - 0x8000;
  71.     // ax = absolute value
  72.     sint32 ax = (x > 0) ? x : -x;
  73.     sint32 y  = (x - ((x * (ax >> 2) ) >> 13));
  74.     return y << 2;
  75. }
  76.  
  77. // fixed point, unsigned, quantised, 16 bit 512 sample sin table lookup
  78. // input  is a 16 bit value unsigned
  79. // output is a 16 bit value unsigned
  80. inline uint16 qi_sin_u_16_512( uint32 index )
  81. {
  82.     // bound and shift index
  83.     index = (index & 0xFFFF) >> 7;
  84.     // index the table
  85.     return sin_16_512[ index ];
  86. }
  87.  
  88. // fixed point, signed, quantised, 16 bit, 512 sample sin table lookup
  89. inline sint16 qi_sin_s_16_512( uint32 index )
  90. {
  91.     // bound and shift index
  92.     index = (index & 0xFFFF) >> 7;
  93.     // index the table
  94.     return (sint32)(sin_16_512[ index ]) - 0x8000;
  95. }
  96.  
  97. // fixed point, unsigned quantised, 8 bit 128 sample sin table lookup
  98. // input  is a 16 bit value unsigned
  99. // output is a 16 bit value unsigned
  100. inline uint16 qi_sin_u_08_128( uint32 index )
  101. {
  102.     // bound and shift index
  103.     index = (index & 0xFFFF) >> 9;
  104.     // index table and shift
  105.     return sin_8_128[ index ] << 8;
  106. }
  107.  
  108. // fixed point, unsigned, linear interpolation, 8 bit 128 sample sin table lookup
  109. // input  is a 16 bit value unsigned
  110. // output is a 16 bit value unsigned
  111. inline uint16 li_sin_u_08_128( uint32 index )
  112. {
  113.     //
  114.     uint8 i1 =  (index>>9)    & 0x7F;
  115.     uint8 i2 = ((index>>9)+1) & 0x7F;
  116.     uint8 f  =  (index>>1)    & 0xFF;
  117.     //
  118.     i1 = sin_8_128[ i1 ];
  119.     i2 = sin_8_128[ i2 ];
  120.     //
  121.     return ( ((uint32)i1) * (0x100-f) + ((uint32)i2 * f) );
  122. }
  123.  
  124. // fixed point, unsigned, linear interpolation, 8 bit 128 sample cos table lookup
  125. // input  is a 16 bit value unsigned
  126. // output is a 16 bit value unsigned
  127. inline uint16 li_cos_u_08_128( uint32 index )
  128. {
  129.     // adjust into next quadrant
  130.     index += 0x4000;
  131.     //
  132.     uint8 i1 =  (index>>9)    & 0x7F;
  133.     uint8 i2 = ((index>>9)+1) & 0x7F;
  134.     uint8 f  =  (index>>1)    & 0xFF;
  135.     //
  136.     i1 = sin_8_128[ i1 ];
  137.     i2 = sin_8_128[ i2 ];
  138.     //
  139.     return ( ((uint32)i1) * (0x100-f) + ((uint32)i2 * f) );
  140. }
  141.  
  142.  
  143. // fixed point, unsigned, linear interpolation, 8 bit 128 sample sin table lookup
  144. // input  is a 16 bit value unsigned
  145. // output is a 16 bit value unsigned
  146. inline sint16 li_sin_s_08_128( uint32 index )
  147. {
  148.     // find indices and fraction
  149.     uint8 i1 =  (index>>9)    & 0x7F;
  150.     uint8 i2 = ((index>>9)+1) & 0x7F;
  151.     uint8 f  =  (index>>1)    & 0xFF;
  152.     // index the array
  153.     i1 = sin_8_128[ i1 ];
  154.     i2 = sin_8_128[ i2 ];
  155.     // linear interpolate value
  156.     sint32 v = ( ((uint32)i1) * (0x100-f) + ((uint32)i2 * f) );
  157.     // adjust into signed region
  158.     return v - 0x8000;
  159. }
  160.  
  161. // fixed point, unsigned, linear interpolation, 8 bit 128 sample cos table lookup
  162. // input  is a 16 bit value unsigned
  163. // output is a 16 bit value unsigned
  164. inline sint16 li_cos_s_08_128( uint32 index )
  165. {
  166.     // adjust into next quadrant
  167.     index += 0x4000;
  168.     // find indices and fraction
  169.     uint8 i1 =  (index>>9)    & 0x7F;
  170.     uint8 i2 = ((index>>9)+1) & 0x7F;
  171.     uint8 f  =  (index>>1)    & 0xFF;
  172.     // index the array
  173.     i1 = sin_8_128[ i1 ];
  174.     i2 = sin_8_128[ i2 ];
  175.     // linear interpolate value
  176.     sint32 v = ( ((uint32)i1) * (0x100-f) + ((uint32)i2 * f) );
  177.     // adjust into signed region
  178.     return v - 0x8000;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement