blob: 4e0f365f0577579bb15284f543bf237af9dcb13c [file] [log] [blame]
David Rowe10602db2008-10-06 21:41:46 -07001/*
2 * SpanDSP - a series of DSP components for telephony
3 *
4 * fir.h - General telephony FIR routines
5 *
6 * Written by Steve Underwood <steveu@coppice.org>
7 *
8 * Copyright (C) 2002 Steve Underwood
9 *
10 * All rights reserved.
11 *
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.
15 *
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.
20 *
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.
David Rowe10602db2008-10-06 21:41:46 -070024 */
25
David Rowe10602db2008-10-06 21:41:46 -070026#if !defined(_FIR_H_)
27#define _FIR_H_
28
29/*
David Rowe10602db2008-10-06 21:41:46 -070030 Ideas for improvement:
31
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.
35
36 2/ Use the hardware circular buffer facility tohalve memory usage.
37
38 3/ Consider using internal memory.
39
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.
43
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.
47*/
48
Greg Kroah-Hartman56791f02009-08-25 22:07:56 -070049/*
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.
52 */
J.R. Mauroc82895b2008-10-30 19:26:59 -040053struct fir16_state_t {
David Rowe10602db2008-10-06 21:41:46 -070054 int taps;
55 int curr_pos;
56 const int16_t *coeffs;
57 int16_t *history;
J.R. Mauroc82895b2008-10-30 19:26:59 -040058};
David Rowe10602db2008-10-06 21:41:46 -070059
Greg Kroah-Hartman56791f02009-08-25 22:07:56 -070060/*
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.
64 */
J.R. Mauroc82895b2008-10-30 19:26:59 -040065struct fir32_state_t {
David Rowe10602db2008-10-06 21:41:46 -070066 int taps;
67 int curr_pos;
68 const int32_t *coeffs;
69 int16_t *history;
J.R. Mauroc82895b2008-10-30 19:26:59 -040070};
David Rowe10602db2008-10-06 21:41:46 -070071
Greg Kroah-Hartman56791f02009-08-25 22:07:56 -070072/*
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.
75 */
J.R. Mauroc82895b2008-10-30 19:26:59 -040076struct fir_float_state_t {
David Rowe10602db2008-10-06 21:41:46 -070077 int taps;
78 int curr_pos;
79 const float *coeffs;
80 float *history;
J.R. Mauroc82895b2008-10-30 19:26:59 -040081};
David Rowe10602db2008-10-06 21:41:46 -070082
Alexander Beregalovdc57a3e2009-03-12 03:32:45 +030083static inline const int16_t *fir16_create(struct fir16_state_t *fir,
84 const int16_t *coeffs, int taps)
David Rowe10602db2008-10-06 21:41:46 -070085{
86 fir->taps = taps;
87 fir->curr_pos = taps - 1;
88 fir->coeffs = coeffs;
Pekka Enbergdb2af1492008-10-17 20:55:03 +030089 fir->history = kcalloc(taps, sizeof(int16_t), GFP_KERNEL);
David Rowe10602db2008-10-06 21:41:46 -070090 return fir->history;
91}
David Rowe10602db2008-10-06 21:41:46 -070092
Alexander Beregalovdc57a3e2009-03-12 03:32:45 +030093static inline void fir16_flush(struct fir16_state_t *fir)
David Rowe10602db2008-10-06 21:41:46 -070094{
J.R. Mauro4460a862008-10-20 19:01:31 -040095 memset(fir->history, 0, fir->taps * sizeof(int16_t));
David Rowe10602db2008-10-06 21:41:46 -070096}
David Rowe10602db2008-10-06 21:41:46 -070097
Alexander Beregalovdc57a3e2009-03-12 03:32:45 +030098static inline void fir16_free(struct fir16_state_t *fir)
David Rowe10602db2008-10-06 21:41:46 -070099{
Pekka Enbergdb2af1492008-10-17 20:55:03 +0300100 kfree(fir->history);
David Rowe10602db2008-10-06 21:41:46 -0700101}
David Rowe10602db2008-10-06 21:41:46 -0700102
Alexander Beregalovdc57a3e2009-03-12 03:32:45 +0300103static inline int16_t fir16(struct fir16_state_t *fir, int16_t sample)
David Rowe10602db2008-10-06 21:41:46 -0700104{
J.R. Mauro4460a862008-10-20 19:01:31 -0400105 int32_t y;
J.R. Mauro4460a862008-10-20 19:01:31 -0400106 int i;
107 int offset1;
108 int offset2;
David Rowe10602db2008-10-06 21:41:46 -0700109
J.R. Mauro4460a862008-10-20 19:01:31 -0400110 fir->history[fir->curr_pos] = sample;
David Rowe10602db2008-10-06 21:41:46 -0700111
J.R. Mauro4460a862008-10-20 19:01:31 -0400112 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];
J.R. Mauro4460a862008-10-20 19:01:31 -0400119 if (fir->curr_pos <= 0)
120 fir->curr_pos = fir->taps;
121 fir->curr_pos--;
122 return (int16_t) (y >> 15);
David Rowe10602db2008-10-06 21:41:46 -0700123}
David Rowe10602db2008-10-06 21:41:46 -0700124
Alexander Beregalovdc57a3e2009-03-12 03:32:45 +0300125static inline const int16_t *fir32_create(struct fir32_state_t *fir,
126 const int32_t *coeffs, int taps)
David Rowe10602db2008-10-06 21:41:46 -0700127{
J.R. Mauro4460a862008-10-20 19:01:31 -0400128 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;
David Rowe10602db2008-10-06 21:41:46 -0700133}
David Rowe10602db2008-10-06 21:41:46 -0700134
Alexander Beregalovdc57a3e2009-03-12 03:32:45 +0300135static inline void fir32_flush(struct fir32_state_t *fir)
David Rowe10602db2008-10-06 21:41:46 -0700136{
J.R. Mauro4460a862008-10-20 19:01:31 -0400137 memset(fir->history, 0, fir->taps * sizeof(int16_t));
David Rowe10602db2008-10-06 21:41:46 -0700138}
David Rowe10602db2008-10-06 21:41:46 -0700139
Alexander Beregalovdc57a3e2009-03-12 03:32:45 +0300140static inline void fir32_free(struct fir32_state_t *fir)
David Rowe10602db2008-10-06 21:41:46 -0700141{
J.R. Mauro4460a862008-10-20 19:01:31 -0400142 kfree(fir->history);
David Rowe10602db2008-10-06 21:41:46 -0700143}
David Rowe10602db2008-10-06 21:41:46 -0700144
Alexander Beregalovdc57a3e2009-03-12 03:32:45 +0300145static inline int16_t fir32(struct fir32_state_t *fir, int16_t sample)
David Rowe10602db2008-10-06 21:41:46 -0700146{
J.R. Mauro4460a862008-10-20 19:01:31 -0400147 int i;
148 int32_t y;
149 int offset1;
150 int offset2;
David Rowe10602db2008-10-06 21:41:46 -0700151
J.R. Mauro4460a862008-10-20 19:01:31 -0400152 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);
David Rowe10602db2008-10-06 21:41:46 -0700164}
David Rowe10602db2008-10-06 21:41:46 -0700165
David Rowe10602db2008-10-06 21:41:46 -0700166#endif