Debian Codegraph @

drivers/dahdi/fir.h

raw
1 /*
2 * SpanDSP - a series of DSP components for telephony
3 *
4 * fir.h - General telephony FIR routines
5 *
6 * Written by Steve Underwood <[email protected]>
7 *
8 * Copyright (C) 2002 Steve Underwood
9 *
10 * All rights reserved.
11 *
12 */
13
14 /*
15 * See http://www.asterisk.org for more information about
16 * the Asterisk project. Please do not directly contact
17 * any of the maintainers of this project for assistance;
18 * the project provides a web site, mailing lists and IRC
19 * channels for your use.
20 *
21 * This program is free software, distributed under the terms of
22 * the GNU General Public License Version 2 as published by the
23 * Free Software Foundation. See the LICENSE file included with
24 * this program for more details.
25 */
26
27 #if !defined(_FIR_H_)
28 #define _FIR_H_
29
30 typedef struct
31 {
32 int taps;
33 int curr_pos;
34 int16_t *coeffs;
35 int16_t *history;
36 } fir16_state_t;
37
38 typedef struct
39 {
40 int taps;
41 int curr_pos;
42 int32_t *coeffs;
43 int16_t *history;
44 } fir32_state_t;
45
46 static inline void fir16_create (fir16_state_t *fir,
47 int16_t *coeffs,
48 int taps)
49 {
50 fir->taps = taps;
51 fir->curr_pos = taps - 1;
52 fir->coeffs = coeffs;
53 fir->history = kmalloc(taps*sizeof (int16_t), GFP_KERNEL);
54 if (fir->history)
55 memset (fir->history, '\0', taps*sizeof (int16_t));
56 }
57 /*- End of function --------------------------------------------------------*/
58
59 static inline void fir16_free (fir16_state_t *fir)
60 {
61 kfree(fir->history);
62 }
63 /*- End of function --------------------------------------------------------*/
64
65 static inline int16_t fir16 (fir16_state_t *fir, int16_t sample)
66 {
67 int i;
68 int offset1;
69 int offset2;
70 int32_t y;
71
72 fir->history[fir->curr_pos] = sample;
73 offset2 = fir->curr_pos + 1;
74 offset1 = fir->taps - offset2;
75 y = 0;
76 for (i = fir->taps - 1; i >= offset1; i--)
77 y += fir->coeffs[i]*fir->history[i - offset1];
78 for ( ; i >= 0; i--)
79 y += fir->coeffs[i]*fir->history[i + offset2];
80 if (fir->curr_pos <= 0)
81 fir->curr_pos = fir->taps;
82 fir->curr_pos--;
83 return y >> 15;
84 }
85 /*- End of function --------------------------------------------------------*/
86
87 static inline void fir32_create (fir32_state_t *fir,
88 int32_t *coeffs,
89 int taps)
90 {
91 fir->taps = taps;
92 fir->curr_pos = taps - 1;
93 fir->coeffs = coeffs;
94 fir->history = kmalloc(taps*sizeof (int16_t), GFP_KERNEL);
95 if (fir->history)
96 memset (fir->history, '\0', taps*sizeof (int16_t));
97 }
98 /*- End of function --------------------------------------------------------*/
99
100 static inline void fir32_free (fir32_state_t *fir)
101 {
102 kfree(fir->history);
103 }
104 /*- End of function --------------------------------------------------------*/
105
106 static inline int16_t fir32 (fir32_state_t *fir, int16_t sample)
107 {
108 int i;
109 int offset1;
110 int offset2;
111 int32_t y;
112
113 fir->history[fir->curr_pos] = sample;
114 offset2 = fir->curr_pos + 1;
115 offset1 = fir->taps - offset2;
116 y = 0;
117 for (i = fir->taps - 1; i >= offset1; i--)
118 y += fir->coeffs[i]*fir->history[i - offset1];
119 for ( ; i >= 0; i--)
120 y += fir->coeffs[i]*fir->history[i + offset2];
121 if (fir->curr_pos <= 0)
122 fir->curr_pos = fir->taps;
123 fir->curr_pos--;
124 return y >> 15;
125 }
126 /*- End of function --------------------------------------------------------*/
127
128 #endif
129 /*- End of file ------------------------------------------------------------*/