DSPLIB User Guide
DSPLIB_cascadeBiquad_cn.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 **+--------------------------------------------------------------------------+**
3 **| **** |**
4 **| **** |**
5 **| ******o*** |**
6 **| ********_///_**** |**
7 **| ***** /_//_/ **** |**
8 **| ** ** (__/ **** |**
9 **| ********* |**
10 **| **** |**
11 **| *** |**
12 **| |**
13 **| Copyright (c) 2016 Texas Instruments Incorporated |**
14 **| ALL RIGHTS RESERVED |**
15 **| |**
16 **| Permission to use, copy, modify, or distribute this software, |**
17 **| whether in part or in whole, for any purpose is forbidden without |**
18 **| a signed licensing agreement and NDA from Texas Instruments |**
19 **| Incorporated (TI). |**
20 **| |**
21 **| TI makes no representation or warranties with respect to the |**
22 **| performance of this computer program, and specifically disclaims |**
23 **| any responsibility for any damages, special or consequential, |**
24 **| connected with the use of this program. |**
25 **| |**
26 **+--------------------------------------------------------------------------+**
27 *******************************************************************************/
28 
30 
32  DSPLIB_bufParams2D_t *bufParamsIn,
33  DSPLIB_bufParams1D_t *bufParamsFilterCoeff,
34  DSPLIB_bufParams2D_t *bufParamsFilterVar,
35  DSPLIB_bufParams2D_t *bufParamsOut,
36  DSPLIB_cascadeBiquad_InitArgs *pKerInitArgs)
37 {
39  return status;
40 }
41 
42 template <typename dataType>
44  void *restrict pIn,
45  void *restrict pFilterCoeff,
46  void *restrict pFilterVar,
47  void *restrict pOut)
48 {
50 
51 #if DSPLIB_DEBUGPRINT
52  printf("Enter DSPLIB_cascadeBiquad_exec_cn\n");
53 #endif
54 
55  dataType *pInLocal;
56  dataType *pFilterCoeffLocal = (dataType *) pFilterCoeff;
57  dataType *pFilterVarLocal = (dataType *) pFilterVar;
58  dataType *pOutLocal;
60 
61  uint32_t dataBufferInPitch = pKerPrivArgs->dataBufferInPitch;
62  uint32_t dataBufferOutPitch = pKerPrivArgs->dataBufferOutPitch;
63  uint32_t filterVarPitch = pKerPrivArgs->filterVarPitch;
64 
65  uint32_t dataSize = pKerPrivArgs->initArgs.dataSize;
66  uint32_t numStages = pKerPrivArgs->initArgs.numStages;
67  uint32_t numChannels = pKerPrivArgs->initArgs.numChannels;
68 
69 #if DSPLIB_DEBUGPRINT
70  printf("Enter pInLocal %p pFilterCoeffLocal %p pFilterVarLocal %p pOut %p filterVarPitch %d\n", pIn,
71  pFilterCoeffLocal, pFilterVarLocal, pOut, filterVarPitch);
72 #endif
73 
74  dataType b11, b12, a11, a12; /* Cascade-1 coeffs */
75  dataType b21, b22, a21, a22; /* Cascade-2 coeffs */
76  dataType b31, b32, a31, a32; /* Cascade-3 coeffs */
77  dataType b41, b42, a41, a42; /* Cascade-4 coeffs */
78  dataType b51, b52, a51, a52; /* Cascade-5 coeffs */
79  dataType b61, b62, a61, a62; /* Cascade-6 coeffs */
80  dataType b71, b72, a71, a72; /* Cascade-7 coeffs */
81  dataType b10, b20, b30, b40, b50, b60, b70;
82  dataType vd0, vd1, vd2, vd3, vd4, vd5, vd6, vd7, vd8, vd9, vd10, vd11, vd12, vd13;
83 
84  float coeff0 = pFilterCoeffLocal[0];
85  b10 = coeff0; // pFilterCoeffLocal[0];
86  dataType r = (dataType) (1 / coeff0);
87  b11 = pFilterCoeffLocal[1] * r;
88  b12 = pFilterCoeffLocal[2] * r;
89  a11 = -pFilterCoeffLocal[3];
90  a12 = -pFilterCoeffLocal[4];
91 
92  if (numStages >= 2) {
93  b20 = pFilterCoeffLocal[5];
94  r = (dataType) (1 / b20);
95  b21 = pFilterCoeffLocal[6] * r;
96  b22 = pFilterCoeffLocal[7] * r;
97  a21 = -pFilterCoeffLocal[8];
98  a22 = -pFilterCoeffLocal[9];
99  }
100  else {
101  // if second stage is unused, create coefficients of unity transfer function
102  b20 = 1.0;
103  b21 = 0;
104  b22 = 0;
105  a21 = 0;
106  a22 = 0;
107  }
108 
109  if (numStages >= 3) {
110  b30 = pFilterCoeffLocal[10];
111  r = (dataType) (1 / b30);
112  b31 = pFilterCoeffLocal[11] * r;
113  b32 = pFilterCoeffLocal[12] * r;
114  a31 = -pFilterCoeffLocal[13];
115  a32 = -pFilterCoeffLocal[14];
116  }
117  else {
118  // if third stage is unused, create coefficients of unity transfer function
119  b30 = 1.0;
120  b31 = 0;
121  b32 = 0;
122  a31 = 0;
123  a32 = 0;
124  }
125 
126  if (numStages >= 4) {
127  b40 = pFilterCoeffLocal[15];
128  r = (dataType) (1 / b40);
129  b41 = pFilterCoeffLocal[16] * r;
130  b42 = pFilterCoeffLocal[17] * r;
131  a41 = -pFilterCoeffLocal[18];
132  a42 = -pFilterCoeffLocal[19];
133  }
134  else {
135  // if third stage is unused, create coefficients of unity transfer function
136  b40 = 1.0;
137  b41 = 0;
138  b42 = 0;
139  a41 = 0;
140  a42 = 0;
141  }
142 
143  if (numStages >= 5) {
144  b50 = pFilterCoeffLocal[20];
145  r = (dataType) (1 / b50);
146  b51 = pFilterCoeffLocal[21] * r;
147  b52 = pFilterCoeffLocal[22] * r;
148  a51 = -pFilterCoeffLocal[23];
149  a52 = -pFilterCoeffLocal[24];
150  }
151  else {
152  // if third stage is unused, create coefficients of unity transfer function
153  b50 = 1.0;
154  b51 = 0;
155  b52 = 0;
156  a51 = 0;
157  a52 = 0;
158  }
159 
160  if (numStages >= 6) {
161  b60 = pFilterCoeffLocal[25];
162  r = (dataType) (1 / b60);
163  b61 = pFilterCoeffLocal[26] * r;
164  b62 = pFilterCoeffLocal[27] * r;
165  a61 = -pFilterCoeffLocal[28];
166  a62 = -pFilterCoeffLocal[29];
167  }
168  else {
169  // if third stage is unused, create coefficients of unity transfer function
170  b60 = 1.0;
171  b61 = 0;
172  b62 = 0;
173  a61 = 0;
174  a62 = 0;
175  }
176 
177  if (numStages >= 7) {
178  b70 = pFilterCoeffLocal[30];
179  r = (dataType) (1 / b70);
180  b71 = pFilterCoeffLocal[31] * r;
181  b72 = pFilterCoeffLocal[32] * r;
182  a71 = -pFilterCoeffLocal[33];
183  a72 = -pFilterCoeffLocal[34];
184  }
185  else {
186  // if third stage is unused, create coefficients of unity transfer function
187  b70 = 1.0;
188  b71 = 0;
189  b72 = 0;
190  a71 = 0;
191  a72 = 0;
192  }
193  /* Get the filter states into corresponding regs */
194 
195  dataType outGain = b10 * b20 * b30 * b40 * b50 * b60 * b70; // Final output gain rescales based on b0 term
196  dataType input1, output01, output02, output03, output04, output05, output06, output07;
197  dataType t0, t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13; /* Filter temp regs */
198  dataType output;
199 
200 #if DSPLIB_DEBUGPRINT
201  printf("DSPLIB_DEBUGPRINT a11 %f a12 %f b11 %f b12 %f b21 %f b22 %f b31 %f b32 %f\n", a11, a12, b11, b12, b21, b22,
202  b31, b32);
203  printf("DSPLIB_DEBUGPRINT a41 %f a42 %f b41 %f b42 %f b51 %f b52 %f b61 %f b62 %f\n", a41, a42, b41, b42, b51, b52,
204  b61, b62);
205  printf("DSPLIB_DEBUGPRINT b10 %f b20 %f b30 %f b40 %f b50 %f b60 %f b70 %f\n", b10, b20, b30, b40, b50, b60, b70);
206  printf("DSPLIB_DEBUGPRINT outGain %f\n", outGain);
207 #endif
208 
209  for (uint32_t numCh = 0; numCh < numChannels; numCh++) {
210  pInLocal = (dataType *) pIn + numCh;
211  pOutLocal = (dataType *) pOut + numCh;
212  vd0 = pFilterVarLocal[0];
213  vd1 = pFilterVarLocal[filterVarPitch];
214  if (numStages >= 2) {
215  vd2 = pFilterVarLocal[2 * filterVarPitch];
216  vd3 = pFilterVarLocal[3 * filterVarPitch];
217  }
218  else {
219  vd2 = 0;
220  vd3 = 0;
221  }
222  if (numStages >= 3) {
223  vd4 = pFilterVarLocal[4 * filterVarPitch];
224  vd5 = pFilterVarLocal[5 * filterVarPitch];
225  }
226  else {
227  vd4 = 0;
228  vd5 = 0;
229  }
230  if (numStages >= 4) {
231  vd6 = pFilterVarLocal[6 * filterVarPitch];
232  vd7 = pFilterVarLocal[7 * filterVarPitch];
233  }
234  else {
235  vd6 = 0;
236  vd7 = 0;
237  }
238  if (numStages >= 5) {
239  vd8 = pFilterVarLocal[8 * filterVarPitch];
240  vd9 = pFilterVarLocal[9 * filterVarPitch];
241  }
242  else {
243  vd8 = 0;
244  vd9 = 0;
245  }
246  if (numStages >= 6) {
247  vd10 = pFilterVarLocal[10 * filterVarPitch];
248  vd11 = pFilterVarLocal[11 * filterVarPitch];
249  }
250  else {
251  vd10 = 0;
252  vd11 = 0;
253  }
254  if (numStages >= 7) {
255  vd12 = pFilterVarLocal[12 * filterVarPitch];
256  vd13 = pFilterVarLocal[13 * filterVarPitch];
257  }
258  else {
259  vd12 = 0;
260  vd13 = 0;
261  }
262 
263 #if DSPLIB_DEBUGPRINT
264  printf("DSPLIB_DEBUGPRINT numCh %d: pFilterVarLocal %p vd0 %f vd1 %f vd2 %f vd3 %f vd4 %f vd5 %f\n", numCh,
265  pFilterVarLocal, vd0, vd1, vd2, vd3, vd4, vd5);
266  printf("DSPLIB_DEBUGPRINT vd6 %f vd7 %f vd8 %f vd9 %f vd10 %f vd11 %f vd12 %f vd13 %f\n", vd6, vd7, vd8, vd9,
267  vd10, vd11, vd12, vd13);
268 #endif
269 
270  for (uint32_t samp = 0; samp < dataSize; samp++) {
271  input1 = *(pInLocal); //, *in2++);
272  pInLocal = (dataType *) pInLocal + dataBufferInPitch;
273  // Channel 1 stage 1
274 
275  output01 = input1 + vd0;
276  t1 = (((b11 + a11) * input1) + vd1);
277  t0 = (a12 * vd0);
278  vd0 = ((a11 * vd0) + t1);
279  vd1 = (((b12 + a12) * input1) + t0);
280 
281  // Channel 1 stage 2
282  output02 = (output01 + vd2);
283  t3 = (((b21 + a21) * output01) + vd3); //(b21 + a21) * output1 + d3 ;
284  t2 = (a22 * vd2);
285  vd2 = ((a21 * vd2) + t3);
286  vd3 = (((b22 + a22) * output01) + t2);
287 
288  // Channel 1 stage 3
289  output03 = (output02 + vd4);
290  t5 = (((b31 + a31) * output02) + vd5);
291  t4 = (a32 * vd4);
292  vd4 = ((a31 * vd4) + t5);
293  vd5 = (((b32 + a32) * output02) + t4);
294 
295  // Channel 1 stage 4
296  output04 = output03 + vd6;
297  t7 = (((b41 + a41) * output03) + vd7);
298  t6 = (a42 * vd6);
299  vd6 = ((a41 * vd6) + t7);
300  vd7 = (((b42 + a42) * output03) + t6);
301 
302  // Channel 1 stage 5
303  output05 = (output04 + vd8);
304  t9 = (((b51 + a51) * output04) + vd9); //(b21 + a21) * output1 + d3 ;
305  t8 = (a52 * vd8);
306  vd8 = ((a51 * vd8) + t9);
307  vd9 = (((b52 + a52) * output04) + t8);
308 
309  // Channel 1 stage 6
310  output06 = (output05 + vd10);
311  t11 = (((b61 + a61) * output05) + vd11);
312  t10 = (a62 * vd10);
313  vd10 = ((a61 * vd10) + t11);
314  vd11 = (((b62 + a62) * output05) + t10);
315 
316  // Channel 1 stage 7
317  output07 = (output06 + vd12);
318  t13 = (((b71 + a71) * output06) + vd13);
319  t12 = (a72 * vd12);
320  vd12 = ((a71 * vd12) + t13);
321  vd13 = (((b72 + a72) * output06) + t12);
322 
323 #if DSPLIB_DEBUGPRINT
324 // if (samp == 0) {
325 // printf("DSPLIB_DEBUGPRINT samp %d: vd0 %f vd1 %f vd2 %f vd3 %f vd4 %f vd5 %f\n", samp, vd0, vd1, vd2, vd3,
326 // vd4, vd5); printf("DSPLIB_DEBUGPRINT samp %d: t1 %f t0 %f t3 %f t2 %f t5 %f t4 %f\n", samp, t1, t0, t3, t2,
327 // t5, t4);
328 // }
329 #endif
330 
331  output = output07 * outGain;
332 
333  // Output samples per channel
334  *(dataType *) pOutLocal = output;
335  pOutLocal = (dataType *) pOutLocal + dataBufferOutPitch;
336 #if DSPLIB_DEBUGPRINT
337  // if (samp == 0)
338  printf("DSPLIB_DEBUGPRINT numCh %d samp %d input1 %f output01 %f output02 %f output03 %f output07 %f output "
339  "%f outGain %f pInLocal %p pOutLocal %p pOutLocal %f dataBufferOutPitch %d\n",
340  numCh, samp, input1, output01, output02, output03, output07, output, outGain, pInLocal, pOutLocal,
341  *(dataType *) pOutLocal, dataBufferOutPitch);
342 #endif
343  }
344 
345  *(pFilterVarLocal) = vd0;
346  *(pFilterVarLocal + filterVarPitch) = vd1;
347  if (numStages >= 2) {
348  *(pFilterVarLocal + 2 * filterVarPitch) = vd2;
349  *(pFilterVarLocal + 3 * filterVarPitch) = vd3;
350  }
351  if (numStages >= 3) {
352  *(pFilterVarLocal + 4 * filterVarPitch) = vd4;
353  *(pFilterVarLocal + 5 * filterVarPitch) = vd5;
354  }
355  if (numStages >= 4) {
356  *(pFilterVarLocal + 6 * filterVarPitch) = vd6;
357  *(pFilterVarLocal + 7 * filterVarPitch) = vd7;
358  }
359  if (numStages >= 5) {
360  *(pFilterVarLocal + 8 * filterVarPitch) = vd8;
361  *(pFilterVarLocal + 9 * filterVarPitch) = vd9;
362  }
363  if (numStages >= 6) {
364  *(pFilterVarLocal + 10 * filterVarPitch) = vd10;
365  *(pFilterVarLocal + 11 * filterVarPitch) = vd11;
366  }
367  if (numStages >= 7) {
368  *(pFilterVarLocal + 12 * filterVarPitch) = vd12;
369  *(pFilterVarLocal + 13 * filterVarPitch) = vd13;
370  }
371  pFilterVarLocal++;
372  }
373  return (status);
374 }
375 
376 // explicit instantiation for the different data type versions
378  void *restrict pIn,
379  void *restrict pFilterCoeff,
380  void *restrict pFilterVar,
381  void *restrict pOut);
DSPLIB_STATUS DSPLIB_cascadeBiquad_exec_cn(DSPLIB_kernelHandle handle, void *restrict pIn, void *restrict pFilterCoeff, void *restrict pFilterVar, void *restrict pOut)
This function is the main execution function for the natural C implementation of the kernel....
template DSPLIB_STATUS DSPLIB_cascadeBiquad_exec_cn< float >(DSPLIB_kernelHandle handle, void *restrict pIn, void *restrict pFilterCoeff, void *restrict pFilterVar, void *restrict pOut)
DSPLIB_STATUS DSPLIB_cascadeBiquad_init_cn(DSPLIB_kernelHandle handle, DSPLIB_bufParams2D_t *bufParamsIn, DSPLIB_bufParams1D_t *bufParamsFilterCoeff, DSPLIB_bufParams2D_t *bufParamsFilterVar, DSPLIB_bufParams2D_t *bufParamsOut, DSPLIB_cascadeBiquad_InitArgs *pKerInitArgs)
This function is the initialization function for the natural C implementation of the kernel....
Header file for kernel's internal use. For the kernel's interface, please see DSPLIB_cascadeBiquad.
DSPLIB_STATUS_NAME
The enumeration of all status codes.
Definition: DSPLIB_types.h:151
void * DSPLIB_kernelHandle
Handle type for DSPLIB operations.
Definition: DSPLIB_types.h:172
@ DSPLIB_SUCCESS
Definition: DSPLIB_types.h:152
A structure for a 1 dimensional buffer descriptor.
A structure for a 2 dimensional buffer descriptor.
Structure containing the parameters to initialize the kernel.
uint32_t dataSize
Size of input data
uint32_t numChannels
number of channels
uint32_t numStages
Size of batch in terms of number of channels of input data
Structure that is reserved for internal use by the kernel.
uint32_t filterVarPitch
Pitch of filter Variable buffer for different channels DSPLIB_cascadeBiquad_init that will be retriev...
uint32_t dataBufferOutPitch
Pitch of output buffer for different batches DSPLIB_cascadeBiquad_init that will be retrieved and use...
DSPLIB_cascadeBiquad_InitArgs initArgs
Structure holding initialization parameters
uint32_t dataBufferInPitch
Pitch of input buffer for different batches DSPLIB_cascadeBiquad_init that will be retrieved and used...