David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 1 | /* |
| 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 Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 24 | */ |
| 25 | |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 26 | #if !defined(_FIR_H_) |
| 27 | #define _FIR_H_ |
| 28 | |
| 29 | /* |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 30 | 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-Hartman | 56791f0 | 2009-08-25 22:07:56 -0700 | [diff] [blame] | 49 | /* |
| 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. Mauro | c82895b | 2008-10-30 19:26:59 -0400 | [diff] [blame] | 53 | struct fir16_state_t { |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 54 | int taps; |
| 55 | int curr_pos; |
| 56 | const int16_t *coeffs; |
| 57 | int16_t *history; |
J.R. Mauro | c82895b | 2008-10-30 19:26:59 -0400 | [diff] [blame] | 58 | }; |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 59 | |
Greg Kroah-Hartman | 56791f0 | 2009-08-25 22:07:56 -0700 | [diff] [blame] | 60 | /* |
| 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. Mauro | c82895b | 2008-10-30 19:26:59 -0400 | [diff] [blame] | 65 | struct fir32_state_t { |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 66 | int taps; |
| 67 | int curr_pos; |
| 68 | const int32_t *coeffs; |
| 69 | int16_t *history; |
J.R. Mauro | c82895b | 2008-10-30 19:26:59 -0400 | [diff] [blame] | 70 | }; |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 71 | |
Greg Kroah-Hartman | 56791f0 | 2009-08-25 22:07:56 -0700 | [diff] [blame] | 72 | /* |
| 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. Mauro | c82895b | 2008-10-30 19:26:59 -0400 | [diff] [blame] | 76 | struct fir_float_state_t { |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 77 | int taps; |
| 78 | int curr_pos; |
| 79 | const float *coeffs; |
| 80 | float *history; |
J.R. Mauro | c82895b | 2008-10-30 19:26:59 -0400 | [diff] [blame] | 81 | }; |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 82 | |
Alexander Beregalov | dc57a3e | 2009-03-12 03:32:45 +0300 | [diff] [blame] | 83 | static inline const int16_t *fir16_create(struct fir16_state_t *fir, |
| 84 | const int16_t *coeffs, int taps) |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 85 | { |
| 86 | fir->taps = taps; |
| 87 | fir->curr_pos = taps - 1; |
| 88 | fir->coeffs = coeffs; |
Pekka Enberg | db2af149 | 2008-10-17 20:55:03 +0300 | [diff] [blame] | 89 | fir->history = kcalloc(taps, sizeof(int16_t), GFP_KERNEL); |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 90 | return fir->history; |
| 91 | } |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 92 | |
Alexander Beregalov | dc57a3e | 2009-03-12 03:32:45 +0300 | [diff] [blame] | 93 | static inline void fir16_flush(struct fir16_state_t *fir) |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 94 | { |
J.R. Mauro | 4460a86 | 2008-10-20 19:01:31 -0400 | [diff] [blame] | 95 | memset(fir->history, 0, fir->taps * sizeof(int16_t)); |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 96 | } |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 97 | |
Alexander Beregalov | dc57a3e | 2009-03-12 03:32:45 +0300 | [diff] [blame] | 98 | static inline void fir16_free(struct fir16_state_t *fir) |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 99 | { |
Pekka Enberg | db2af149 | 2008-10-17 20:55:03 +0300 | [diff] [blame] | 100 | kfree(fir->history); |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 101 | } |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 102 | |
Alexander Beregalov | dc57a3e | 2009-03-12 03:32:45 +0300 | [diff] [blame] | 103 | static inline int16_t fir16(struct fir16_state_t *fir, int16_t sample) |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 104 | { |
J.R. Mauro | 4460a86 | 2008-10-20 19:01:31 -0400 | [diff] [blame] | 105 | int32_t y; |
J.R. Mauro | 4460a86 | 2008-10-20 19:01:31 -0400 | [diff] [blame] | 106 | int i; |
| 107 | int offset1; |
| 108 | int offset2; |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 109 | |
J.R. Mauro | 4460a86 | 2008-10-20 19:01:31 -0400 | [diff] [blame] | 110 | fir->history[fir->curr_pos] = sample; |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 111 | |
J.R. Mauro | 4460a86 | 2008-10-20 19:01:31 -0400 | [diff] [blame] | 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]; |
J.R. Mauro | 4460a86 | 2008-10-20 19:01:31 -0400 | [diff] [blame] | 119 | if (fir->curr_pos <= 0) |
| 120 | fir->curr_pos = fir->taps; |
| 121 | fir->curr_pos--; |
| 122 | return (int16_t) (y >> 15); |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 123 | } |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 124 | |
Alexander Beregalov | dc57a3e | 2009-03-12 03:32:45 +0300 | [diff] [blame] | 125 | static inline const int16_t *fir32_create(struct fir32_state_t *fir, |
| 126 | const int32_t *coeffs, int taps) |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 127 | { |
J.R. Mauro | 4460a86 | 2008-10-20 19:01:31 -0400 | [diff] [blame] | 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; |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 133 | } |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 134 | |
Alexander Beregalov | dc57a3e | 2009-03-12 03:32:45 +0300 | [diff] [blame] | 135 | static inline void fir32_flush(struct fir32_state_t *fir) |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 136 | { |
J.R. Mauro | 4460a86 | 2008-10-20 19:01:31 -0400 | [diff] [blame] | 137 | memset(fir->history, 0, fir->taps * sizeof(int16_t)); |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 138 | } |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 139 | |
Alexander Beregalov | dc57a3e | 2009-03-12 03:32:45 +0300 | [diff] [blame] | 140 | static inline void fir32_free(struct fir32_state_t *fir) |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 141 | { |
J.R. Mauro | 4460a86 | 2008-10-20 19:01:31 -0400 | [diff] [blame] | 142 | kfree(fir->history); |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 143 | } |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 144 | |
Alexander Beregalov | dc57a3e | 2009-03-12 03:32:45 +0300 | [diff] [blame] | 145 | static inline int16_t fir32(struct fir32_state_t *fir, int16_t sample) |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 146 | { |
J.R. Mauro | 4460a86 | 2008-10-20 19:01:31 -0400 | [diff] [blame] | 147 | int i; |
| 148 | int32_t y; |
| 149 | int offset1; |
| 150 | int offset2; |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 151 | |
J.R. Mauro | 4460a86 | 2008-10-20 19:01:31 -0400 | [diff] [blame] | 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); |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 164 | } |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 165 | |
David Rowe | 10602db | 2008-10-06 21:41:46 -0700 | [diff] [blame] | 166 | #endif |