resample16.h 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * FILE: resample16.h
  3. *
  4. * The configuration constants below govern
  5. * the number of bits in the input sample and filter coefficients, the
  6. * number of bits to the right of the binary-point for fixed-point math, etc.
  7. *
  8. */
  9. /* Conversion constants */
  10. #define Nhc 8
  11. #define Na 7
  12. #define Np (Nhc+Na)
  13. #define Npc (1<<Nhc)
  14. #define Amask ((1<<Na)-1)
  15. #define Pmask ((1<<Np)-1)
  16. #define Nh 16
  17. #define Nb 16
  18. #define Nhxn 14
  19. #define Nhg (Nh-Nhxn)
  20. #define NLpScl 13
  21. /* Description of constants:
  22. *
  23. * Npc - is the number of look-up values available for the lowpass filter
  24. * between the beginning of its impulse response and the "cutoff time"
  25. * of the filter. The cutoff time is defined as the reciprocal of the
  26. * lowpass-filter cut off frequence in Hz. For example, if the
  27. * lowpass filter were a sinc function, Npc would be the index of the
  28. * impulse-response lookup-table corresponding to the first zero-
  29. * crossing of the sinc function. (The inverse first zero-crossing
  30. * time of a sinc function equals its nominal cutoff frequency in Hz.)
  31. * Npc must be a power of 2 due to the details of the current
  32. * implementation. The default value of 512 is sufficiently high that
  33. * using linear interpolation to fill in between the table entries
  34. * gives approximately 16-bit accuracy in filter coefficients.
  35. *
  36. * Nhc - is log base 2 of Npc.
  37. *
  38. * Na - is the number of bits devoted to linear interpolation of the
  39. * filter coefficients.
  40. *
  41. * Np - is Na + Nhc, the number of bits to the right of the binary point
  42. * in the integer "time" variable. To the left of the point, it indexes
  43. * the input array (X), and to the right, it is interpreted as a number
  44. * between 0 and 1 sample of the input X. Np must be less than 16 in
  45. * this implementation.
  46. *
  47. * Nh - is the number of bits in the filter coefficients. The sum of Nh and
  48. * the number of bits in the input data (typically 16) cannot exceed 32.
  49. * Thus Nh should be 16. The largest filter coefficient should nearly
  50. * fill 16 bits (32767).
  51. *
  52. * Nb - is the number of bits in the input data. The sum of Nb and Nh cannot
  53. * exceed 32.
  54. *
  55. * Nhxn - is the number of bits to right shift after multiplying each input
  56. * sample times a filter coefficient. It can be as great as Nh and as
  57. * small as 0. Nhxn = Nh-2 gives 2 guard bits in the multiply-add
  58. * accumulation. If Nhxn=0, the accumulation will soon overflow 32 bits.
  59. *
  60. * Nhg - is the number of guard bits in mpy-add accumulation (equal to Nh-Nhxn)
  61. *
  62. * NLpScl - is the number of bits allocated to the unity-gain normalization
  63. * factor. The output of the lowpass filter is multiplied by LpScl and
  64. * then right-shifted NLpScl bits. To avoid overflow, we must have
  65. * Nb+Nhg+NLpScl < 32.
  66. */
  67. typedef char BOOL;
  68. typedef short HWORD;
  69. typedef unsigned short UHWORD;
  70. typedef int WORD;
  71. typedef unsigned int UWORD;
  72. struct resample16_s;
  73. typedef struct {
  74. UHWORD LpScl; /* Unity-gain scale factor */
  75. UHWORD Nwing; /* Filter table size */
  76. UHWORD Nmult; /* Filter length for up-conversions */
  77. const HWORD *Imp; /* Filter coefficients */
  78. const HWORD *ImpD; /* ImpD[n] = Imp[n+1]-Imp[n] */
  79. } resample16_filter_t;
  80. typedef enum { RESAMPLE16_BASIC, RESAMPLE16_LOW, RESAMPLE16_MED } resample16_filter_e;
  81. WORD resample16(struct resample16_s *r, HWORD X[], int inCount, HWORD Y[]);
  82. struct resample16_s* resample16_create(float factor, resample16_filter_e filter, resample16_filter_t *custom, BOOL interp);
  83. void resample16_delete(struct resample16_s *r);
  84. void resample16_flush(struct resample16_s *r);