Linux 4.18.10
[linux/fpc-iii.git] / drivers / misc / echo / fir.h
blob4e0f365f0577579bb15284f543bf237af9dcb13c
1 /*
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.
26 #if !defined(_FIR_H_)
27 #define _FIR_H_
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
42 possible.
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
46 can.
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 {
54 int taps;
55 int curr_pos;
56 const int16_t *coeffs;
57 int16_t *history;
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 {
66 int taps;
67 int curr_pos;
68 const int32_t *coeffs;
69 int16_t *history;
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 {
77 int taps;
78 int curr_pos;
79 const float *coeffs;
80 float *history;
83 static inline const int16_t *fir16_create(struct fir16_state_t *fir,
84 const int16_t *coeffs, int taps)
86 fir->taps = taps;
87 fir->curr_pos = taps - 1;
88 fir->coeffs = coeffs;
89 fir->history = kcalloc(taps, sizeof(int16_t), GFP_KERNEL);
90 return fir->history;
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)
100 kfree(fir->history);
103 static inline int16_t fir16(struct fir16_state_t *fir, int16_t sample)
105 int32_t y;
106 int i;
107 int offset1;
108 int offset2;
110 fir->history[fir->curr_pos] = sample;
112 offset2 = fir->curr_pos;
113 offset1 = fir->taps - offset2;
114 y = 0;
115 for (i = fir->taps - 1; i >= offset1; i--)
116 y += fir->coeffs[i] * fir->history[i - offset1];
117 for (; i >= 0; i--)
118 y += fir->coeffs[i] * fir->history[i + offset2];
119 if (fir->curr_pos <= 0)
120 fir->curr_pos = fir->taps;
121 fir->curr_pos--;
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)
128 fir->taps = taps;
129 fir->curr_pos = taps - 1;
130 fir->coeffs = coeffs;
131 fir->history = kcalloc(taps, sizeof(int16_t), GFP_KERNEL);
132 return fir->history;
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)
142 kfree(fir->history);
145 static inline int16_t fir32(struct fir32_state_t *fir, int16_t sample)
147 int i;
148 int32_t y;
149 int offset1;
150 int offset2;
152 fir->history[fir->curr_pos] = sample;
153 offset2 = fir->curr_pos;
154 offset1 = fir->taps - offset2;
155 y = 0;
156 for (i = fir->taps - 1; i >= offset1; i--)
157 y += fir->coeffs[i] * fir->history[i - offset1];
158 for (; i >= 0; i--)
159 y += fir->coeffs[i] * fir->history[i + offset2];
160 if (fir->curr_pos <= 0)
161 fir->curr_pos = fir->taps;
162 fir->curr_pos--;
163 return (int16_t) (y >> 15);
166 #endif