2 * SpanDSP - a series of DSP components for telephony
4 * fir.h - General telephony FIR routines
6 * Written by Steve Underwood <steveu@coppice.org>
8 * Copyright (C) 2002 Steve Underwood
10 * All rights reserved.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2, as
14 * published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 Ideas for improvement:
32 1/ Rewrite filter for dual MAC inner loop. The issue here is handling
33 history sample offsets that are 16 bit aligned - the dual MAC needs
34 32 bit aligmnent. There are some good examples in libbfdsp.
36 2/ Use the hardware circular buffer facility tohalve memory usage.
38 3/ Consider using internal memory.
40 Using less memory might also improve speed as cache misses will be
41 reduced. A drop in MIPs and memory approaching 50% should be
44 The foreground and background filters currenlty use a total of
45 about 10 MIPs/ch as measured with speedtest.c on a 256 TAP echo
50 * 16 bit integer FIR descriptor. This defines the working state for a single
51 * instance of an FIR filter using 16 bit integer coefficients.
53 struct fir16_state_t {
56 const int16_t *coeffs;
61 * 32 bit integer FIR descriptor. This defines the working state for a single
62 * instance of an FIR filter using 32 bit integer coefficients, and filtering
63 * 16 bit integer data.
65 struct fir32_state_t {
68 const int32_t *coeffs;
73 * Floating point FIR descriptor. This defines the working state for a single
74 * instance of an FIR filter using floating point coefficients and data.
76 struct fir_float_state_t {
83 static inline const int16_t *fir16_create(struct fir16_state_t *fir,
84 const int16_t *coeffs, int taps)
87 fir->curr_pos = taps - 1;
89 fir->history = kcalloc(taps, sizeof(int16_t), GFP_KERNEL);
93 static inline void fir16_flush(struct fir16_state_t *fir)
95 memset(fir->history, 0, fir->taps * sizeof(int16_t));
98 static inline void fir16_free(struct fir16_state_t *fir)
103 static inline int16_t fir16(struct fir16_state_t *fir, int16_t sample)
110 fir->history[fir->curr_pos] = sample;
112 offset2 = fir->curr_pos;
113 offset1 = fir->taps - offset2;
115 for (i = fir->taps - 1; i >= offset1; i--)
116 y += fir->coeffs[i] * fir->history[i - offset1];
118 y += fir->coeffs[i] * fir->history[i + offset2];
119 if (fir->curr_pos <= 0)
120 fir->curr_pos = fir->taps;
122 return (int16_t) (y >> 15);
125 static inline const int16_t *fir32_create(struct fir32_state_t *fir,
126 const int32_t *coeffs, int taps)
129 fir->curr_pos = taps - 1;
130 fir->coeffs = coeffs;
131 fir->history = kcalloc(taps, sizeof(int16_t), GFP_KERNEL);
135 static inline void fir32_flush(struct fir32_state_t *fir)
137 memset(fir->history, 0, fir->taps * sizeof(int16_t));
140 static inline void fir32_free(struct fir32_state_t *fir)
145 static inline int16_t fir32(struct fir32_state_t *fir, int16_t sample)
152 fir->history[fir->curr_pos] = sample;
153 offset2 = fir->curr_pos;
154 offset1 = fir->taps - offset2;
156 for (i = fir->taps - 1; i >= offset1; i--)
157 y += fir->coeffs[i] * fir->history[i - offset1];
159 y += fir->coeffs[i] * fir->history[i + offset2];
160 if (fir->curr_pos <= 0)
161 fir->curr_pos = fir->taps;
163 return (int16_t) (y >> 15);