filter_so.h
Go to the documentation of this file.
1 //#############################################################################
2 //
3 // FILE: filter_so.h
4 //
5 // TITLE: C28x InstaSPIN filter library, second-order
6 //
7 //#############################################################################
8 // $TI Release: MotorControl SDK v1.00.00.00 $
9 // $Release Date: Mon Mar 11 18:37:40 CDT 2019 $
10 // $Copyright:
11 // Copyright (C) 2017-2018 Texas Instruments Incorporated - http://www.ti.com/
12 //
13 // Redistribution and use in source and binary forms, with or without
14 // modification, are permitted provided that the following conditions
15 // are met:
16 //
17 // Redistributions of source code must retain the above copyright
18 // notice, this list of conditions and the following disclaimer.
19 //
20 // Redistributions in binary form must reproduce the above copyright
21 // notice, this list of conditions and the following disclaimer in the
22 // documentation and/or other materials provided with the
23 // distribution.
24 //
25 // Neither the name of Texas Instruments Incorporated nor the names of
26 // its contributors may be used to endorse or promote products derived
27 // from this software without specific prior written permission.
28 //
29 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 // $
41 //#############################################################################
42 
43 #ifndef FILTER_SO_H
44 #define FILTER_SO_H
45 
46 
47 //*****************************************************************************
48 //
49 // If building with a C++ compiler, make all of the definitions in this header
50 // have a C binding.
51 //
52 //*****************************************************************************
53 #ifdef __cplusplus
54 extern "C"
55 {
56 #endif
57 
58 //*****************************************************************************
59 //
63 //
64 //*****************************************************************************
65 
66 #include "types.h"
67 
68 
69 //*****************************************************************************
70 //
72 //
73 //*****************************************************************************
74 typedef struct _FILTER_SO_Obj_
75 {
86 
87 //*****************************************************************************
88 //
90 //
91 //*****************************************************************************
93 
94 //*****************************************************************************
95 //
101 //
102 //*****************************************************************************
103 static inline float32_t
105 {
106  FILTER_SO_Obj *obj = (FILTER_SO_Obj *)handle;
107 
108  return(obj->a1);
109 } // end of FILTER_SO_get_a1() function
110 
111 //*****************************************************************************
112 //
118 //
119 //*****************************************************************************
120 static inline float32_t
122 {
123  FILTER_SO_Obj *obj = (FILTER_SO_Obj *)handle;
124 
125  return(obj->a2);
126 } // end of FILTER_SO_get_a2() function
127 
128 //*****************************************************************************
129 //
135 //
136 //*****************************************************************************
137 static inline float32_t
139 {
140  FILTER_SO_Obj *obj = (FILTER_SO_Obj *)handle;
141 
142  return(obj->b0);
143 } // end of FILTER_SO_get_b0() function
144 
145 //*****************************************************************************
146 //
152 //
153 //*****************************************************************************
154 static inline float32_t
156 {
157  FILTER_SO_Obj *obj = (FILTER_SO_Obj *)handle;
158 
159  return(obj->b1);
160 } // end of FILTER_SO_get_b1() function
161 
162 //*****************************************************************************
163 //
169 //
170 //*****************************************************************************
171 static inline float32_t
173 {
174  FILTER_SO_Obj *obj = (FILTER_SO_Obj *)handle;
175 
176  return(obj->b2);
177 } // end of FILTER_SO_get_b2() function
178 
179 //*****************************************************************************
180 //
186 //
187 //*****************************************************************************
188 static inline float32_t
190 {
191  FILTER_SO_Obj *obj = (FILTER_SO_Obj *)handle;
192 
193  return(obj->x1);
194 } // end of FILTER_SO_get_x1() function
195 
196 //*****************************************************************************
197 //
203 //
204 //*****************************************************************************
205 static inline float32_t
207 {
208  FILTER_SO_Obj *obj = (FILTER_SO_Obj *)handle;
209 
210  return(obj->x2);
211 } // end of FILTER_SO_get_x2() function
212 
213 //*****************************************************************************
214 //
220 //
221 //*****************************************************************************
222 static inline float32_t
224 {
225  FILTER_SO_Obj *obj = (FILTER_SO_Obj *)handle;
226 
227  return(obj->y1);
228 } // end of FILTER_SO_get_y1() function
229 
230 //*****************************************************************************
231 //
237 //
238 //*****************************************************************************
239 static inline float32_t
241 {
242  FILTER_SO_Obj *obj = (FILTER_SO_Obj *)handle;
243 
244  return(obj->y2);
245 } // end of FILTER_SO_get_y2() function
246 
247 //*****************************************************************************
248 //
260 //
261 //*****************************************************************************
262 extern void
264 
265 //*****************************************************************************
266 //
284 //
285 //*****************************************************************************
286 extern void
288  float32_t *px2, float32_t *py1, float32_t *py2);
289 
290 //*****************************************************************************
291 //
306 //
307 //*****************************************************************************
308 extern void
310  float32_t *pb2);
311 
312 //*****************************************************************************
313 //
323 //
324 //*****************************************************************************
325 extern FILTER_SO_Handle
326 FILTER_SO_init(void *pMemory, const size_t numBytes);
327 //*****************************************************************************
328 //
337 //
338 //*****************************************************************************
339 #ifdef __TMS320C28XX_CLA__
340 #pragma FUNC_ALWAYS_INLINE(FILTER_SO_run)
341 #endif // __TMS320C28XX_CLA__
342 
343 static inline float32_t
344 FILTER_SO_run(FILTER_SO_Handle handle, const float32_t inputValue)
345 {
346  FILTER_SO_Obj *obj = (FILTER_SO_Obj *)handle;
347 
348  float32_t a1 = obj->a1;
349  float32_t a2 = obj->a2;
350  float32_t b0 = obj->b0;
351  float32_t b1 = obj->b1;
352  float32_t b2 = obj->b2;
353  float32_t x1 = obj->x1;
354  float32_t x2 = obj->x2;
355  float32_t y1 = obj->y1;
356  float32_t y2 = obj->y2;
357 
358  //
359  // Compute the output
360  //
361  float32_t y0 = (b0 * inputValue) + (b1 * x1) + (b2 * x2) - (a1 * y1) -
362  (a2 * y2);
363 
364  //
365  // Store values for next time
366  //
367  obj->x1 = inputValue;
368  obj->x2 = x1;
369  obj->y1 = y0;
370  obj->y2 = y1;
371 
372  return(y0);
373 } // end of FILTER_SO_run() function
374 
375 //*****************************************************************************
376 //
385 //
386 //*****************************************************************************
387 static inline float32_t
389 {
390  FILTER_SO_Obj *obj = (FILTER_SO_Obj *)handle;
391 
392  float32_t a1 = obj->a1;
393  float32_t a2 = obj->a2;
394  float32_t b0 = obj->b0;
395  float32_t y1 = obj->y1;
396  float32_t y2 = obj->y2;
397 
398  //
399  // Compute the output
400  //
401  float32_t y0 = (b0 * inputValue) - (a1 * y1) - (a2 * y2);
402 
403  //
404  // Store values for next time
405  //
406  obj->y1 = y0;
407  obj->y2 = y1;
408 
409  return(y0);
410 } // end of FILTER_SO_run_form_0() function
411 
412 //*****************************************************************************
413 //
422 //
423 //*****************************************************************************
424 static inline float32_t
426 {
427  FILTER_SO_Obj *obj = (FILTER_SO_Obj *)handle;
428 
429  float32_t a1 = obj->a1;
430  float32_t a2 = obj->a2;
431  float32_t b0 = obj->b0;
432  float32_t b1 = obj->b1;
433  float32_t x1 = obj->x1;
434  float32_t y1 = obj->y1;
435  float32_t y2 = obj->y2;
436 
437  //
438  // Compute the output
439  //
440  float32_t y0 = (b0 * inputValue) + (b1 * x1) - (a1 * y1) - (a2 * y2);
441 
442  //
443  // Store values for next time
444  //
445  obj->x1 = inputValue;
446  obj->y1 = y0;
447  obj->y2 = y1;
448 
449  return(y0);
450 } // end of FILTER_SO_run_form_1() function
451 
452 //*****************************************************************************
453 //
461 //
462 //*****************************************************************************
463 static inline void
465 {
466  FILTER_SO_Obj *obj = (FILTER_SO_Obj *)handle;
467 
468  obj->a1 = a1;
469 
470  return;
471 } // end of FILTER_SO_set_a1() function
472 
473 //*****************************************************************************
474 //
482 //
483 //*****************************************************************************
484 static inline void
486 {
487  FILTER_SO_Obj *obj = (FILTER_SO_Obj *)handle;
488 
489  obj->a2 = a2;
490 
491  return;
492 } // end of FILTER_SO_set_a2() function
493 
494 //*****************************************************************************
495 //
503 //
504 //*****************************************************************************
505 static inline void
507 {
508  FILTER_SO_Obj *obj = (FILTER_SO_Obj *)handle;
509 
510  obj->b0 = b0;
511 
512  return;
513 } // end of FILTER_SO_set_b0() function
514 
515 //*****************************************************************************
516 //
524 //
525 //*****************************************************************************
526 static inline void
528 {
529  FILTER_SO_Obj *obj = (FILTER_SO_Obj *)handle;
530 
531  obj->b1 = b1;
532 
533  return;
534 } // end of FILTER_SO_set_b1() function
535 
536 //*****************************************************************************
537 //
545 //
546 //*****************************************************************************
547 static inline void
549 {
550  FILTER_SO_Obj *obj = (FILTER_SO_Obj *)handle;
551 
552  obj->b2 = b2;
553 
554  return;
555 } // end of FILTER_SO_set_b2() function
556 
557 //*****************************************************************************
558 //
566 //
567 //*****************************************************************************
568 static inline void
570 {
571  FILTER_SO_Obj *obj = (FILTER_SO_Obj *)handle;
572 
573  obj->x1 = x1;
574 
575  return;
576 } // end of FILTER_SO_set_x1() function
577 
578 //*****************************************************************************
579 //
587 //
588 //*****************************************************************************
589 static inline void
591 {
592  FILTER_SO_Obj *obj = (FILTER_SO_Obj *)handle;
593 
594  obj->x2 = x2;
595 
596  return;
597 } // end of FILTER_SO_set_x2() function
598 
599 //*****************************************************************************
600 //
608 //
609 //*****************************************************************************
610 static inline void
612 {
613  FILTER_SO_Obj *obj = (FILTER_SO_Obj *)handle;
614 
615  obj->y1 = y1;
616 
617  return;
618 } // end of FILTER_SO_set_y1() function
619 
620 //*****************************************************************************
621 //
629 //
630 //*****************************************************************************
631 static inline void
633 {
634  FILTER_SO_Obj *obj = (FILTER_SO_Obj *)handle;
635 
636  obj->y2 = y2;
637 
638  return;
639 } // end of FILTER_SO_set_y2() function
640 
641 //*****************************************************************************
642 //
652 //
653 //*****************************************************************************
654 extern void
656  const float32_t a2);
657 
658 //*****************************************************************************
659 //
673 //
674 //*****************************************************************************
675 extern void
677  const float32_t x2, const float32_t y1,
678  const float32_t y2);
679 
680 //*****************************************************************************
681 //
693 //
694 //*****************************************************************************
695 extern void
697  const float32_t b1, const float32_t b2);
698 
699 //*****************************************************************************
700 //
701 // Close the Doxygen group.
703 //
704 //*****************************************************************************
705 
706 //*****************************************************************************
707 //
708 // Mark the end of the C bindings section for C++ compilers.
709 //
710 //*****************************************************************************
711 #ifdef __cplusplus
712 }
713 #endif
714 
715 #endif // FILTER_SO_H
_FILTER_SO_Obj_::b1
float32_t b1
the numerator filter coefficient value for z^(-1)
Definition: filter_so.h:79
_FILTER_SO_Obj_::x2
float32_t x2
the input value at time sample n=-2
Definition: filter_so.h:82
float32_t
float float32_t
Definition: sfra_f32.h:42
types.h
FILTER_SO_setNumCoeffs
void FILTER_SO_setNumCoeffs(FILTER_SO_Handle handle, const float32_t b0, const float32_t b1, const float32_t b2)
Sets the second-order filter numerator coefficients.
FILTER_SO_set_b2
static void FILTER_SO_set_b2(FILTER_SO_Handle handle, const float32_t b2)
Sets the second-order filter numerator coefficient b2.
Definition: filter_so.h:548
FILTER_SO_get_a2
static float32_t FILTER_SO_get_a2(FILTER_SO_Handle handle)
Gets the second-order filter denominator coefficient a2.
Definition: filter_so.h:121
FILTER_SO_getInitialConditions
void FILTER_SO_getInitialConditions(FILTER_SO_Handle handle, float32_t *px1, float32_t *px2, float32_t *py1, float32_t *py2)
Gets the initial conditions of the second-order filter.
FILTER_SO_init
FILTER_SO_Handle FILTER_SO_init(void *pMemory, const size_t numBytes)
Initializes the second-order filter.
FILTER_SO_get_x2
static float32_t FILTER_SO_get_x2(FILTER_SO_Handle handle)
Gets the second-order filter input value at time sample n=-2.
Definition: filter_so.h:206
FILTER_SO_setInitialConditions
void FILTER_SO_setInitialConditions(FILTER_SO_Handle handle, const float32_t x1, const float32_t x2, const float32_t y1, const float32_t y2)
Sets the initial conditions of the second-order filter.
FILTER_SO_set_x2
static void FILTER_SO_set_x2(FILTER_SO_Handle handle, const float32_t x2)
Sets the second-order filter input value at time sample n=-2.
Definition: filter_so.h:590
_FILTER_SO_Obj_::y2
float32_t y2
the output value at time sample n=-2
Definition: filter_so.h:84
_FILTER_SO_Obj_::a1
float32_t a1
the denominator filter coefficient value for z^(-1)
Definition: filter_so.h:76
FILTER_SO_setDenCoeffs
void FILTER_SO_setDenCoeffs(FILTER_SO_Handle handle, const float32_t a1, const float32_t a2)
Sets the second-order filter denominator coefficients.
FILTER_SO_getDenCoeffs
void FILTER_SO_getDenCoeffs(FILTER_SO_Handle handle, float32_t *pa1, float32_t *pa2)
Gets the second-order filter denominator coefficients.
FILTER_SO_run
static float32_t FILTER_SO_run(FILTER_SO_Handle handle, const float32_t inputValue)
Runs a second-order filter of the form y[n] = b0*x[n] + b1*x[n-1] + b2*x[n-2] - a1*y[n-1] - a2*y[n-2]...
Definition: filter_so.h:344
FILTER_SO_set_b0
static void FILTER_SO_set_b0(FILTER_SO_Handle handle, const float32_t b0)
Sets the second-order filter numerator coefficient b0.
Definition: filter_so.h:506
_FILTER_SO_Obj_
Defines the second-order filter (FILTER_SO) object.
Definition: filter_so.h:74
FILTER_SO_set_y2
static void FILTER_SO_set_y2(FILTER_SO_Handle handle, const float32_t y2)
Sets the second-order filter output value at time sample n=-2.
Definition: filter_so.h:632
FILTER_SO_set_b1
static void FILTER_SO_set_b1(FILTER_SO_Handle handle, const float32_t b1)
Sets the second-order filter numerator coefficient b1.
Definition: filter_so.h:527
FILTER_SO_set_a2
static void FILTER_SO_set_a2(FILTER_SO_Handle handle, const float32_t a2)
Sets the second-order filter denominator coefficient a2.
Definition: filter_so.h:485
FILTER_SO_get_b1
static float32_t FILTER_SO_get_b1(FILTER_SO_Handle handle)
Gets the second-order filter numerator coefficient b1.
Definition: filter_so.h:155
FILTER_SO_get_a1
static float32_t FILTER_SO_get_a1(FILTER_SO_Handle handle)
Gets the second-order filter denominator coefficient a1.
Definition: filter_so.h:104
FILTER_SO_set_y1
static void FILTER_SO_set_y1(FILTER_SO_Handle handle, const float32_t y1)
Sets the second-order filter output value at time sample n=-1.
Definition: filter_so.h:611
FILTER_SO_set_a1
static void FILTER_SO_set_a1(FILTER_SO_Handle handle, const float32_t a1)
Sets the second-order filter denominator coefficient a1.
Definition: filter_so.h:464
FILTER_SO_get_b0
static float32_t FILTER_SO_get_b0(FILTER_SO_Handle handle)
Gets the second-order filter numerator coefficient b0.
Definition: filter_so.h:138
FILTER_SO_get_y2
static float32_t FILTER_SO_get_y2(FILTER_SO_Handle handle)
Gets the second-order filter output value at time sample n=-2.
Definition: filter_so.h:240
_FILTER_SO_Obj_::a2
float32_t a2
the denominator filter coefficient value for z^(-2)
Definition: filter_so.h:77
_FILTER_SO_Obj_::b2
float32_t b2
the numerator filter coefficient value for z^(-2)
Definition: filter_so.h:80
FILTER_SO_get_y1
static float32_t FILTER_SO_get_y1(FILTER_SO_Handle handle)
Gets the second-order filter output value at time sample n=-1.
Definition: filter_so.h:223
_FILTER_SO_Obj_::b0
float32_t b0
the numerator filter coefficient value for z^0
Definition: filter_so.h:78
FILTER_SO_Handle
struct _FILTER_SO_Obj_ * FILTER_SO_Handle
Defines the second-order filter (FILTER_SO) handle.
Definition: filter_so.h:92
FILTER_SO_set_x1
static void FILTER_SO_set_x1(FILTER_SO_Handle handle, const float32_t x1)
Sets the second-order filter input value at time sample n=-1.
Definition: filter_so.h:569
_FILTER_SO_Obj_::y1
float32_t y1
the output value at time sample n=-1
Definition: filter_so.h:83
FILTER_SO_run_form_1
static float32_t FILTER_SO_run_form_1(FILTER_SO_Handle handle, const float32_t inputValue)
Runs a second-order filter of the form y[n] = b0*x[n] + b1*x[n-1] - a1*y[n-1] - a2*y[n-2].
Definition: filter_so.h:425
FILTER_SO_getNumCoeffs
void FILTER_SO_getNumCoeffs(FILTER_SO_Handle handle, float32_t *pb0, float32_t *pb1, float32_t *pb2)
Gets the second-order filter numerator coefficients.
FILTER_SO_get_b2
static float32_t FILTER_SO_get_b2(FILTER_SO_Handle handle)
Gets the second-order filter numerator coefficient b2.
Definition: filter_so.h:172
_FILTER_SO_Obj_::x1
float32_t x1
the input value at time sample n=-1
Definition: filter_so.h:81
FILTER_SO_run_form_0
static float32_t FILTER_SO_run_form_0(FILTER_SO_Handle handle, const float32_t inputValue)
Runs a simplified second-order filter of the form y[n] = b0*x[n] - a1*y[n-1] - a2*y[n-2].
Definition: filter_so.h:388
FILTER_SO_Obj
struct _FILTER_SO_Obj_ FILTER_SO_Obj
Defines the second-order filter (FILTER_SO) object.
FILTER_SO_get_x1
static float32_t FILTER_SO_get_x1(FILTER_SO_Handle handle)
Gets the second-order filter input value at time sample n=-1.
Definition: filter_so.h:189

Copyright 2023, Texas Instruments Incorporated