DSPLIB User Guide
DSPLIB_cascadeBiquad.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * *
3  * module name :DSPLIB *
4  * *
5  * module descripton :Matrix Multiply Accelerator Library module for C7x+MMA *
6  * *
7  * Copyright (C) 2017-2018 Texas Instruments Incorporated - https://www.ti.com/ *
8  * ALL RIGHTS RESERVED *
9  * *
10  ******************************************************************************/
11 
24 
26 {
27  int32_t privBufSize = sizeof(DSPLIB_cascadeBiquad_PrivArgs);
28  return privBufSize;
29 }
30 
32  const DSPLIB_bufParams2D_t *bufParamsIn,
33  const DSPLIB_bufParams1D_t *bufParamsFilterCoeff,
34  const DSPLIB_bufParams2D_t *bufParamsFilterVar,
35  const DSPLIB_bufParams2D_t *bufParamsOut,
36  const DSPLIB_cascadeBiquad_InitArgs *pKerInitArgs)
37 {
39  // DSPLIB_bufParams1D_t bufParamsFilterCoeffLocal;
40  // DSPLIB_bufParams2D_t bufParamsInLocal, bufParamsOutLocal, bufParamsFilterVarLocal;
41 
42 #if DSPLIB_DEBUGPRINT
43  printf("Enter DSPLIB_cascadeBiquad_init_checkParams\n");
44 #endif
45  if (handle == NULL) {
46  status = DSPLIB_ERR_NULL_POINTER;
47  }
48 
49  if (status == DSPLIB_SUCCESS) {
50  if ((bufParamsIn->data_type != DSPLIB_INT16) && (bufParamsIn->data_type != DSPLIB_INT32) &&
51  (bufParamsIn->data_type != DSPLIB_INT8) && (bufParamsIn->data_type != DSPLIB_FLOAT32)) {
52  status = DSPLIB_ERR_INVALID_TYPE;
53  }
54  else if (bufParamsIn->data_type != bufParamsOut->data_type) {
55  status = DSPLIB_ERR_INVALID_TYPE;
56  }
57  else if (bufParamsIn->data_type != bufParamsFilterCoeff->data_type) {
58  status = DSPLIB_ERR_INVALID_TYPE;
59  }
60  else {
61  /* Nothing to do here */
62  }
63  }
64 
65  return status;
66 }
67 
69  const void *restrict pIn,
70  const void *restrict pFilterCoeff,
71  const void *restrict pFilterVar,
72  const void *restrict pOut)
73 {
74  DSPLIB_STATUS status;
75 
76 #if DSPLIB_DEBUGPRINT
77  printf("Enter DSPLIB_cascadeBiquad_exec_checkParams\n");
78 #endif
79  if ((pIn == NULL) || (pFilterCoeff == NULL) || (pFilterVar == NULL) || (pOut == NULL)) {
80  status = DSPLIB_ERR_NULL_POINTER;
81  }
82  else {
83  status = DSPLIB_SUCCESS;
84  }
85 
86  return status;
87 }
88 
89 template <typename dataType> static inline dataType fast_recip(dataType x)
90 {
91  typedef typename c7x::make_full_vector<dataType>::type vec;
92 
93  vec a = (vec) x;
94  vec f = __recip(a);
95  f = f * (2 - a * f);
96  f = f * (2 - a * f);
97  f = f * (2 - a * f);
98  vec out = f * (2 - a * f);
99  return out.s[0];
100 }
101 
103 {
104  int32_t size = 0;
105  int eleCount = 0;
106 
107  eleCount = 8;
108 
109  int32_t offset = ((numStages * 5 + eleCount) / eleCount) * eleCount * sizeof(DSPLIB_F32);
110  size = offset * 2;
111 
112  return size;
113 }
114 template <typename dataType>
115 dataType DSPLIB_cascadeBiquad_store_coefficients(uint32_t numStages, void *restrict pFilterCoeff)
116 {
117 
118 #if DSPLIB_DEBUGPRINT
119  printf("DSPLIB_DEBUGPRINT DSPLIB_cascadeBiquad_store_coefficients\n");
120 #endif
121 
122  int32_t loadCounter = 0;
123  int32_t storeCounter_0 = 0;
124  int32_t storeCounter_1 = 0;
125 
126  dataType *pFilterCoeffLocal = (dataType *) pFilterCoeff;
127 
128  float outGain = 1;
129 
130  typedef typename c7x::make_full_vector<dataType>::type vec;
131  int eleCount = c7x::element_count_of<vec>::value;
132  int32_t offset = ((numStages * 5 + eleCount) / eleCount) * eleCount;
133 
134 #if DSPLIB_DEBUGPRINT
135  printf("DSPLIB_DEBUGPRINT eleCount %d offset %d numStages %d\n", eleCount, offset, numStages);
136 #endif
137 
138  for (uint32_t coeffCount = 0; coeffCount < numStages * 5; coeffCount = coeffCount + 5) {
139  float coeff0 = pFilterCoeffLocal[loadCounter];
140  // std::cout << coeff0 << std::endl;
141  loadCounter++;
142  float b10 = coeff0; // pFilterCoeffLocal[0];
143  float r = fast_recip<dataType>(b10);
144  float b11 = pFilterCoeffLocal[loadCounter] * r;
145  loadCounter++;
146  float b12 = pFilterCoeffLocal[loadCounter] * r;
147  loadCounter++;
148  float a11 = -pFilterCoeffLocal[loadCounter];
149  loadCounter++;
150  float a12 = -pFilterCoeffLocal[loadCounter];
151  loadCounter++;
152  float b11_a11 = b11 + a11;
153  float b12_a12 = b12 + a12;
154  outGain = outGain * b10;
155 #if DSPLIB_DEBUGPRINT
156  printf("DSPLIB_DEBUGPRINT coeffCount %f b10 %f outGain %f\n", coeffCount, b10, outGain);
157 #endif
158 
159  pFilterCoeffLocal[storeCounter_0] = b11_a11;
160  storeCounter_0++;
161  pFilterCoeffLocal[storeCounter_0] = b12_a12;
162  storeCounter_0++;
163  pFilterCoeffLocal[offset + storeCounter_1] = a12;
164  storeCounter_1++;
165  pFilterCoeffLocal[offset + storeCounter_1] = a11;
166  storeCounter_1++;
167 #if DSPLIB_DEBUGPRINT
168  printf("DSPLIB_DEBUGPRINT a11 %f a12 %f b11 %f b12 %f b11_a11 %f b12_a12 %f \n", a11, a12, b11, b12, b11_a11,
169  b12_a12);
170 #endif
171  }
172 
173  return outGain;
174 }
175 
177  DSPLIB_bufParams2D_t *bufParamsIn,
178  DSPLIB_bufParams1D_t *bufParamsFilterCoeff,
179  DSPLIB_bufParams2D_t *bufParamsFilterVar,
180  DSPLIB_bufParams2D_t *bufParamsOut,
181  DSPLIB_cascadeBiquad_InitArgs *pKerInitArgs)
182 {
183  DSPLIB_STATUS status;
185 
186 #if DSPLIB_DEBUGPRINT
187  printf("DSPLIB_DEBUGPRINT Enter DSPLIB_cascadeBiquad_init\n");
188 #endif
189  pKerPrivArgs->initArgs = *pKerInitArgs;
190 
191  int32_t ele_size = 1;
192 
193  if (bufParamsIn->data_type == DSPLIB_FLOAT32) {
194  ele_size = 4;
195  }
196  else {
197  status = DSPLIB_ERR_INVALID_TYPE;
198  }
199 
200  pKerPrivArgs->dataBufferInPitch = bufParamsIn->stride_y / ele_size;
201  pKerPrivArgs->dataBufferOutPitch = bufParamsOut->stride_y / ele_size;
202  pKerPrivArgs->filterVarPitch = bufParamsFilterVar->stride_y / ele_size;
203  pKerPrivArgs->filterCoeff = bufParamsFilterCoeff->dim_x;
204 
205  if (pKerInitArgs->funcStyle == DSPLIB_FUNCTION_NATC) {
206  status = DSPLIB_cascadeBiquad_init_cn(handle, bufParamsIn, bufParamsFilterCoeff, bufParamsFilterVar, bufParamsOut,
207  pKerInitArgs);
208  if (bufParamsIn->data_type == DSPLIB_FLOAT32) {
210  }
211  else {
212  status = DSPLIB_ERR_INVALID_TYPE;
213  }
214  }
215  else {
216  if (bufParamsIn->data_type == DSPLIB_FLOAT32) {
217 #if DSPLIB_DEBUGPRINT
218  printf("DSPLIB_DEBUGPRINT bufParamsIn->data_type == DSPLIB_FLOAT32\n");
219 #endif
220  if (pKerPrivArgs->initArgs.numStages == 3) {
221 
222  status = DSPLIB_cascadeBiquad_init_ci<float>(handle, bufParamsIn, bufParamsFilterCoeff, bufParamsFilterVar,
223  bufParamsOut, pKerInitArgs);
225  }
226  else if (pKerPrivArgs->initArgs.numStages == 7) {
227  status = DSPLIB_cascadeBiquad7Stage_init_ci<float>(handle, bufParamsIn, bufParamsFilterCoeff,
228  bufParamsFilterVar, bufParamsOut, pKerInitArgs);
230  }
231  else {
233  }
234  }
235  else {
236  status = DSPLIB_ERR_INVALID_TYPE;
237  }
238  }
239 
240  return status;
241 }
242 
244  void *restrict pIn,
245  void *restrict pFilterCoeff,
246  void *restrict pFilterVar,
247  void *restrict pOut)
248 {
249  DSPLIB_STATUS status;
250 
251 #if DSPLIB_DEBUGPRINT
252  printf("DSPLIB_DEBUGPRINT Enter DSPLIB_cascadeBiquad_exec\n");
253 #endif
254 
256 
257  uint32_t numStages = pKerPrivArgs->initArgs.numStages;
258 
259  if (pKerPrivArgs->initArgs.funcStyle == DSPLIB_FUNCTION_OPTIMIZED && numStages == 7) {
260  pKerPrivArgs->outGain = DSPLIB_cascadeBiquad_store_coefficients<float>(numStages, pFilterCoeff);
261  }
262  else {
263  }
264 
265 #if DSPLIB_DEBUGPRINT
266  printf("DSPLIB_DEBUGPRINT pKerPrivArgs->outGain %f\n", pKerPrivArgs->outGain);
267 #endif
268  status = pKerPrivArgs->execute(handle, pIn, pFilterCoeff, pFilterVar, pOut);
269 
270  return status;
271 }
template DSPLIB_STATUS DSPLIB_cascadeBiquad7Stage_init_ci< float >(DSPLIB_kernelHandle handle, const DSPLIB_bufParams2D_t *bufParamsIn, const DSPLIB_bufParams1D_t *bufParamsFilterCoeff, const DSPLIB_bufParams2D_t *bufParamsFilterVar, const DSPLIB_bufParams2D_t *bufParamsOut, const DSPLIB_cascadeBiquad_InitArgs *pKerInitArgs)
template DSPLIB_STATUS DSPLIB_cascadeBiquad7Stage_exec_ci< float >(DSPLIB_kernelHandle handle, void *restrict pIn, void *restrict pFilterCoeff, void *restrict pFilterVar, void *restrict pOut)
static dataType fast_recip(dataType x)
dataType DSPLIB_cascadeBiquad_store_coefficients(uint32_t numStages, void *restrict pFilterCoeff)
template DSPLIB_STATUS DSPLIB_cascadeBiquad_init_ci< float >(DSPLIB_kernelHandle handle, const DSPLIB_bufParams2D_t *bufParamsIn, const DSPLIB_bufParams1D_t *bufParamsFilterCoeff, const DSPLIB_bufParams2D_t *bufParamsFilterVar, const DSPLIB_bufParams2D_t *bufParamsOut, const DSPLIB_cascadeBiquad_InitArgs *pKerInitArgs)
template DSPLIB_STATUS DSPLIB_cascadeBiquad_exec_ci< float >(DSPLIB_kernelHandle handle, void *restrict pIn, void *restrict pFilterCoeff, void *restrict pFilterVar, void *restrict pOut)
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
float DSPLIB_F32
Single precision floating point.
Definition: DSPLIB_types.h:148
void * DSPLIB_kernelHandle
Handle type for DSPLIB operations.
Definition: DSPLIB_types.h:172
@ DSPLIB_FUNCTION_OPTIMIZED
Definition: DSPLIB_types.h:177
@ DSPLIB_FUNCTION_NATC
Definition: DSPLIB_types.h:176
@ DSPLIB_INT32
@ DSPLIB_INT16
@ DSPLIB_FLOAT32
@ DSPLIB_INT8
@ DSPLIB_SUCCESS
Definition: DSPLIB_types.h:152
@ DSPLIB_ERR_NULL_POINTER
Definition: DSPLIB_types.h:157
@ DSPLIB_ERR_NOT_IMPLEMENTED
Definition: DSPLIB_types.h:158
@ DSPLIB_ERR_INVALID_TYPE
Definition: DSPLIB_types.h:155
int32_t DSPLIB_cascadeBiquad_getHandleSize(DSPLIB_cascadeBiquad_InitArgs *pKerInitArgs)
This is a query function to calculate the size of internal handle.
DSPLIB_STATUS DSPLIB_cascadeBiquad_exec(DSPLIB_kernelHandle handle, void *restrict pIn, void *restrict pFilterCoeff, void *restrict pFilterVar, void *restrict pOut)
This function is the main kernel compute function.
DSPLIB_STATUS DSPLIB_cascadeBiquad_exec_checkParams(DSPLIB_kernelHandle handle, const void *restrict pIn, const void *restrict pFilterCoeff, const void *restrict pFilterVar, const void *restrict pOut)
This function checks the validity of the parameters passed to DSPLIB_cascadeBiquad_exec function....
int32_t DSPLIB_cascadeBiquad_get_coefficientsBufferSize(uint32_t numStages)
This is a query function to calculate the sizes of coefficient buffer needed based on the number of s...
DSPLIB_STATUS DSPLIB_cascadeBiquad_init(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 should be called before the DSPLIB_cascadeBiquad_exec function is called....
DSPLIB_STATUS DSPLIB_cascadeBiquad_init_checkParams(DSPLIB_kernelHandle handle, const DSPLIB_bufParams2D_t *bufParamsIn, const DSPLIB_bufParams1D_t *bufParamsFilterCoeff, const DSPLIB_bufParams2D_t *bufParamsFilterVar, const DSPLIB_bufParams2D_t *bufParamsOut, const DSPLIB_cascadeBiquad_InitArgs *pKerInitArgs)
This function checks the validity of the parameters passed to DSPLIB_cascadeBiquad_init function....
A structure for a 1 dimensional buffer descriptor.
uint32_t dim_x
Width of buffer in X dimension in elements.
uint32_t data_type
Values are of type DSPLIB_data_type_e.
A structure for a 2 dimensional buffer descriptor.
uint32_t data_type
Values are of type DSPLIB_data_type_e.
int32_t stride_y
Stride in Y dimension in bytes.
Structure containing the parameters to initialize the kernel.
int8_t funcStyle
Variant of the function refer to DSPLIB_FUNCTION_STYLE
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 filterCoeff
number of filter coefficients for all stages
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...
float outGain
b10*b20*b30*... Gain a11, a12 coefficients
pFxnDSPLIB_cascadeBiquad_exec execute
Function pointer to point to the right execution variant between DSPLIB_cascadeBiquad_exec_cn and DSP...