1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * SpanDSP - a series of DSP components for telephony
5 * fir.h - General telephony FIR routines
7 * Written by Steve Underwood <steveu@coppice.org>
9 * Copyright (C) 2002 Steve Underwood
11 * All rights reserved.
18 Ideas for improvement:
20 1/ Rewrite filter for dual MAC inner loop. The issue here is handling
21 history sample offsets that are 16 bit aligned - the dual MAC needs
22 32 bit aligmnent. There are some good examples in libbfdsp.
24 2/ Use the hardware circular buffer facility tohalve memory usage.
26 3/ Consider using internal memory.
28 Using less memory might also improve speed as cache misses will be
29 reduced. A drop in MIPs and memory approaching 50% should be
32 The foreground and background filters currenlty use a total of
33 about 10 MIPs/ch as measured with speedtest.c on a 256 TAP echo
38 * 16 bit integer FIR descriptor. This defines the working state for a single
39 * instance of an FIR filter using 16 bit integer coefficients.
41 struct fir16_state_t
{
44 const int16_t *coeffs
;
49 * 32 bit integer FIR descriptor. This defines the working state for a single
50 * instance of an FIR filter using 32 bit integer coefficients, and filtering
51 * 16 bit integer data.
53 struct fir32_state_t
{
56 const int32_t *coeffs
;
61 * Floating point FIR descriptor. This defines the working state for a single
62 * instance of an FIR filter using floating point coefficients and data.
64 struct fir_float_state_t
{
71 static inline const int16_t *fir16_create(struct fir16_state_t
*fir
,
72 const int16_t *coeffs
, int taps
)
75 fir
->curr_pos
= taps
- 1;
77 fir
->history
= kcalloc(taps
, sizeof(int16_t), GFP_KERNEL
);
81 static inline void fir16_flush(struct fir16_state_t
*fir
)
83 memset(fir
->history
, 0, fir
->taps
* sizeof(int16_t));
86 static inline void fir16_free(struct fir16_state_t
*fir
)
91 static inline int16_t fir16(struct fir16_state_t
*fir
, int16_t sample
)
98 fir
->history
[fir
->curr_pos
] = sample
;
100 offset2
= fir
->curr_pos
;
101 offset1
= fir
->taps
- offset2
;
103 for (i
= fir
->taps
- 1; i
>= offset1
; i
--)
104 y
+= fir
->coeffs
[i
] * fir
->history
[i
- offset1
];
106 y
+= fir
->coeffs
[i
] * fir
->history
[i
+ offset2
];
107 if (fir
->curr_pos
<= 0)
108 fir
->curr_pos
= fir
->taps
;
110 return (int16_t) (y
>> 15);
113 static inline const int16_t *fir32_create(struct fir32_state_t
*fir
,
114 const int32_t *coeffs
, int taps
)
117 fir
->curr_pos
= taps
- 1;
118 fir
->coeffs
= coeffs
;
119 fir
->history
= kcalloc(taps
, sizeof(int16_t), GFP_KERNEL
);
123 static inline void fir32_flush(struct fir32_state_t
*fir
)
125 memset(fir
->history
, 0, fir
->taps
* sizeof(int16_t));
128 static inline void fir32_free(struct fir32_state_t
*fir
)
133 static inline int16_t fir32(struct fir32_state_t
*fir
, int16_t sample
)
140 fir
->history
[fir
->curr_pos
] = sample
;
141 offset2
= fir
->curr_pos
;
142 offset1
= fir
->taps
- offset2
;
144 for (i
= fir
->taps
- 1; i
>= offset1
; i
--)
145 y
+= fir
->coeffs
[i
] * fir
->history
[i
- offset1
];
147 y
+= fir
->coeffs
[i
] * fir
->history
[i
+ offset2
];
148 if (fir
->curr_pos
<= 0)
149 fir
->curr_pos
= fir
->taps
;
151 return (int16_t) (y
>> 15);