Advertisement
Guest User

Untitled

a guest
Oct 17th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. // Welch, Wright, & Morrow,
  2. // Real-time Digital Signal Processing, 2017
  3.  
  4. ///////////////////////////////////////////////////////////////////////
  5. // Filename: ISRs.c
  6. //
  7. // Synopsis: Interrupt service routine for codec data transmit/receive
  8. //
  9. ///////////////////////////////////////////////////////////////////////
  10.  
  11. #include "DSP_Config.h"
  12.  
  13. // Data is received as 2 16-bit words (left/right) packed into one
  14. // 32-bit word. The union allows the data to be accessed as a single
  15. // entity when transferring to and from the serial port, but still be
  16. // able to manipulate the left and right channels independently.
  17.  
  18. #define LEFT 0
  19. #define RIGHT 1
  20.  
  21. volatile union {
  22. Uint32 UINT;
  23. Int16 Channel[2];
  24. } CodecDataIn, CodecDataOut;
  25.  
  26.  
  27. /* add any global variables here */
  28. /*
  29. #define N 6 // IIR filter order
  30.  
  31. float B[N+1] = {0.1085, 0, -0.3255, 0, 0.3255, 0, -0.1085}; // numerator coefficients
  32. float A[N+1] = {1.0, -1.1355, 0.9443, -0.6378, 0.5444, -0.1737, 0.0459}; // denominator coefficients
  33. float x[N+1] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; // input value (buffered)
  34. float y[N+1] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; // output values (buffered)
  35.  
  36.  
  37. void iir_df_one(void){
  38. int i = 0;
  39. y[0] = 0.0;
  40. float sum = 0.0;
  41. for(i = 0; i < N + 1; i++){
  42. sum += (B[i] * x[i]) - (A[i] * y[i]);
  43. }
  44. y[0] = sum;
  45. int j = 0;
  46. for(j = N - 1; j > - 1; j--){
  47. y[j + 1] = y[j];
  48. x[j + 1] = x[j];
  49. }
  50.  
  51. }
  52. */
  53.  
  54. #define N 2 // IIR filter order
  55. #define M 3 // number of biquad
  56. float B[M][N+1] = {{1.0, 0.0, -1.0},{1.0, 0.0, -1.0},{1.0, 0.0, -1.0}}; // numerator coefficients
  57. float A[M][N+1] = {{1.0, -1.2803, 0.6573},{1.0, 0.5139, 0.5305},{1.0, -0.3692, 0.1317}}; // denominator coefficients
  58. float G[M+1] = {0.4999, 0.4999, 0.4342, 1.0}; // scale factors
  59. float x[M][N+1] = {{0.0, 0.0, 0.0},{0.0, 0.0, 0.0},{0.0, 0.0, 0.0}}; // input value (buffered)
  60. float y[M][N+1] = {{0.0, 0.0, 0.0},{0.0, 0.0, 0.0},{0.0, 0.0, 0.0}}; // output values (buffered)
  61.  
  62. void biquad(int i){
  63. y[i][0] = B[i][0]*x[i][0] + B[i][1]*x[i][1] + B[i][2]*x[i][2] - A[i][1]*y[i][1]- A[i][2]*y[i][2];
  64. int j = 0;
  65. for(j = M - 1; j > 0; j--){
  66. y[i][j] = y[i][j - 1];
  67. x[i][j] = x[i][j - 1];
  68. }
  69. y[i][0] = G[i + 1] * y[i][0];
  70. }
  71.  
  72.  
  73. interrupt void Codec_ISR()
  74. ///////////////////////////////////////////////////////////////////////
  75. // Purpose: Codec interface interrupt service routine
  76. //
  77. // Input: None
  78. //
  79. // Returns: Nothing
  80. //
  81. // Calls: CheckForOverrun, ReadCodecData, WriteCodecData
  82. //
  83. // Notes: None
  84. ///////////////////////////////////////////////////////////////////////
  85. {
  86. /* add any local variables here */
  87.  
  88.  
  89. if(CheckForOverrun()) // overrun error occurred (i.e. halted DSP)
  90. return; // so serial port is reset to recover
  91.  
  92. CodecDataIn.UINT = ReadCodecData(); // get input data samples
  93.  
  94. /* I added my IIR filter routine here */
  95. //x[0] = CodecDataIn.Channel[RIGHT]; // current input value
  96. y[0][0] = G[0]*x[0][0];
  97. x[0][0] = CodecDataIn.Channel[RIGHT]/32000.0; // current input value
  98. int i = 0;
  99. for (i=0;i<M;i++){
  100. biquad(i); // implement the i_th biquad
  101. if(i < M - 1) x[i + 1][0] = y[i][0];
  102. }
  103.  
  104. // iir_df_one(); // setup for the next input
  105.  
  106. //CodecDataOut.Channel[LEFT] = y[0]; // setup the LEFT value
  107. CodecDataOut.Channel[LEFT] = y[M-1][0]*32000.0; // setup the LEFT value
  108. CodecDataOut.Channel[RIGHT] = y[M-1][0]*32000.0; // setup the LEFT value
  109.  
  110. // CodecDataOut.Channel[RIGHT] = y[0]; // setup the LEFT value
  111. /* end of my IIR filter routine */
  112.  
  113. WriteCodecData(CodecDataOut.UINT); // send output data to port
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement