1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright (c) 1992-2001 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate * All rights reserved.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate #include <memory.h>
30*0Sstevel@tonic-gate #include <stddef.h>
31*0Sstevel@tonic-gate #include <sys/types.h>
32*0Sstevel@tonic-gate #include <Fir.h>
33*0Sstevel@tonic-gate
34*0Sstevel@tonic-gate extern "C" {
35*0Sstevel@tonic-gate char *bcopy(char *, char *, int);
36*0Sstevel@tonic-gate char *memmove(char *, char *, int);
37*0Sstevel@tonic-gate }
38*0Sstevel@tonic-gate
39*0Sstevel@tonic-gate #define BCOPY(src, dest, num) memmove(dest, src, num)
40*0Sstevel@tonic-gate
41*0Sstevel@tonic-gate /*
42*0Sstevel@tonic-gate * convolve()
43*0Sstevel@tonic-gate * returns the convolution of coef[length] and in_buf[length]:
44*0Sstevel@tonic-gate *
45*0Sstevel@tonic-gate * convolution = coef[0] * in_buf[length - 1] +
46*0Sstevel@tonic-gate * coef[1] * in_buf[length - 2] +
47*0Sstevel@tonic-gate * ...
48*0Sstevel@tonic-gate * coef[length - 1] * in_buf[0]
49*0Sstevel@tonic-gate */
50*0Sstevel@tonic-gate double
convolve(double * coefs,double * in_buf,int length)51*0Sstevel@tonic-gate convolve(
52*0Sstevel@tonic-gate double *coefs,
53*0Sstevel@tonic-gate double *in_buf,
54*0Sstevel@tonic-gate int length)
55*0Sstevel@tonic-gate {
56*0Sstevel@tonic-gate if (length <= 0)
57*0Sstevel@tonic-gate return (0.0);
58*0Sstevel@tonic-gate else {
59*0Sstevel@tonic-gate in_buf += --length;
60*0Sstevel@tonic-gate double sum = *coefs * *in_buf;
61*0Sstevel@tonic-gate while (length--)
62*0Sstevel@tonic-gate sum += *++coefs * *--in_buf;
63*0Sstevel@tonic-gate return (sum);
64*0Sstevel@tonic-gate }
65*0Sstevel@tonic-gate }
66*0Sstevel@tonic-gate
67*0Sstevel@tonic-gate void // convert short to double
short2double(double * out,short * in,int size)68*0Sstevel@tonic-gate short2double(
69*0Sstevel@tonic-gate double *out,
70*0Sstevel@tonic-gate short *in,
71*0Sstevel@tonic-gate int size)
72*0Sstevel@tonic-gate {
73*0Sstevel@tonic-gate while (size-- > 0)
74*0Sstevel@tonic-gate *out++ = (double)*in++;
75*0Sstevel@tonic-gate }
76*0Sstevel@tonic-gate
77*0Sstevel@tonic-gate short
double2short(double in)78*0Sstevel@tonic-gate double2short(double in) // limit double to short
79*0Sstevel@tonic-gate {
80*0Sstevel@tonic-gate if (in <= -32768.0)
81*0Sstevel@tonic-gate return (-32768);
82*0Sstevel@tonic-gate else if (in >= 32767.0)
83*0Sstevel@tonic-gate return (32767);
84*0Sstevel@tonic-gate else
85*0Sstevel@tonic-gate return ((short)in);
86*0Sstevel@tonic-gate }
87*0Sstevel@tonic-gate
88*0Sstevel@tonic-gate void Fir:: // update state with data[size]
updateState(double * data,int size)89*0Sstevel@tonic-gate updateState(
90*0Sstevel@tonic-gate double *data,
91*0Sstevel@tonic-gate int size)
92*0Sstevel@tonic-gate {
93*0Sstevel@tonic-gate if (size >= order)
94*0Sstevel@tonic-gate memcpy(state, data + size - order, order * sizeof (double));
95*0Sstevel@tonic-gate else {
96*0Sstevel@tonic-gate int old = order - size;
97*0Sstevel@tonic-gate BCOPY((char *)(state + size), (char *)state,
98*0Sstevel@tonic-gate old * sizeof (double));
99*0Sstevel@tonic-gate memcpy(state + order - size, data, size * sizeof (double));
100*0Sstevel@tonic-gate }
101*0Sstevel@tonic-gate }
102*0Sstevel@tonic-gate
103*0Sstevel@tonic-gate void Fir::
update_short(short * in,int size)104*0Sstevel@tonic-gate update_short(
105*0Sstevel@tonic-gate short *in,
106*0Sstevel@tonic-gate int size)
107*0Sstevel@tonic-gate {
108*0Sstevel@tonic-gate double *in_buf = new double[size];
109*0Sstevel@tonic-gate short2double(in_buf, in, size);
110*0Sstevel@tonic-gate updateState(in_buf, size);
111*0Sstevel@tonic-gate delete in_buf;
112*0Sstevel@tonic-gate }
113*0Sstevel@tonic-gate
114*0Sstevel@tonic-gate void Fir::
resetState(void)115*0Sstevel@tonic-gate resetState(void) // reset state to all zero
116*0Sstevel@tonic-gate {
117*0Sstevel@tonic-gate for (int i = 0; i < order; i++)
118*0Sstevel@tonic-gate state[i] = 0.0;
119*0Sstevel@tonic-gate }
120*0Sstevel@tonic-gate
121*0Sstevel@tonic-gate Fir::
Fir(void)122*0Sstevel@tonic-gate Fir(void)
123*0Sstevel@tonic-gate {
124*0Sstevel@tonic-gate }
125*0Sstevel@tonic-gate
126*0Sstevel@tonic-gate Fir::
Fir(int order_in)127*0Sstevel@tonic-gate Fir(int order_in): order(order_in) // construct Fir object
128*0Sstevel@tonic-gate {
129*0Sstevel@tonic-gate state = new double[order];
130*0Sstevel@tonic-gate resetState();
131*0Sstevel@tonic-gate coef = new double[order + 1];
132*0Sstevel@tonic-gate delay = (order + 1) >> 1; // assuming symmetric FIR
133*0Sstevel@tonic-gate }
134*0Sstevel@tonic-gate
135*0Sstevel@tonic-gate Fir::
~Fir()136*0Sstevel@tonic-gate ~Fir() // destruct Fir object
137*0Sstevel@tonic-gate {
138*0Sstevel@tonic-gate delete coef;
139*0Sstevel@tonic-gate delete state;
140*0Sstevel@tonic-gate }
141*0Sstevel@tonic-gate
142*0Sstevel@tonic-gate int Fir::
getOrder(void)143*0Sstevel@tonic-gate getOrder(void) // returns filter order
144*0Sstevel@tonic-gate {
145*0Sstevel@tonic-gate return (order);
146*0Sstevel@tonic-gate }
147*0Sstevel@tonic-gate
148*0Sstevel@tonic-gate int Fir::
getNumCoefs(void)149*0Sstevel@tonic-gate getNumCoefs(void) // returns number of filter coefficients
150*0Sstevel@tonic-gate {
151*0Sstevel@tonic-gate return (order + 1);
152*0Sstevel@tonic-gate }
153*0Sstevel@tonic-gate
154*0Sstevel@tonic-gate void Fir::
putCoef(double * coef_in)155*0Sstevel@tonic-gate putCoef(double *coef_in) // copy coef_in in filter coefficients
156*0Sstevel@tonic-gate {
157*0Sstevel@tonic-gate memcpy(coef, coef_in, (order + 1) * sizeof (double));
158*0Sstevel@tonic-gate }
159*0Sstevel@tonic-gate
160*0Sstevel@tonic-gate void Fir::
getCoef(double * coef_out)161*0Sstevel@tonic-gate getCoef(double *coef_out) // returns filter coefs in coef_out
162*0Sstevel@tonic-gate {
163*0Sstevel@tonic-gate memcpy(coef_out, coef, (order + 1) * sizeof (double));
164*0Sstevel@tonic-gate }
165*0Sstevel@tonic-gate
166*0Sstevel@tonic-gate int Fir:: // filter in[size], and updates the state.
filter_noadjust(short * in,int size,short * out)167*0Sstevel@tonic-gate filter_noadjust(
168*0Sstevel@tonic-gate short *in,
169*0Sstevel@tonic-gate int size,
170*0Sstevel@tonic-gate short *out)
171*0Sstevel@tonic-gate {
172*0Sstevel@tonic-gate if (size <= 0)
173*0Sstevel@tonic-gate return (0);
174*0Sstevel@tonic-gate
175*0Sstevel@tonic-gate double *in_buf = new double[size];
176*0Sstevel@tonic-gate short2double(in_buf, in, size); // convert short input to double
177*0Sstevel@tonic-gate int i;
178*0Sstevel@tonic-gate int init_size = (size <= order)? size : order;
179*0Sstevel@tonic-gate int init_order = order;
180*0Sstevel@tonic-gate double *state_ptr = state;
181*0Sstevel@tonic-gate short *out_ptr = out;
182*0Sstevel@tonic-gate
183*0Sstevel@tonic-gate // the first "order" outputs need state in convolution
184*0Sstevel@tonic-gate for (i = 1; i <= init_size; i++)
185*0Sstevel@tonic-gate *out_ptr++ = double2short(convolve(coef, in_buf, i) +
186*0Sstevel@tonic-gate convolve(coef + i, state_ptr++, init_order--));
187*0Sstevel@tonic-gate
188*0Sstevel@tonic-gate // starting from "order + 1"th output, state is no longer needed
189*0Sstevel@tonic-gate state_ptr = in_buf;
190*0Sstevel@tonic-gate while (i++ <= size)
191*0Sstevel@tonic-gate *out_ptr++ =
192*0Sstevel@tonic-gate double2short(convolve(coef, state_ptr++, order + 1));
193*0Sstevel@tonic-gate updateState(in_buf, size);
194*0Sstevel@tonic-gate delete in_buf;
195*0Sstevel@tonic-gate return (out_ptr - out);
196*0Sstevel@tonic-gate }
197*0Sstevel@tonic-gate
198*0Sstevel@tonic-gate int Fir::
getFlushSize(void)199*0Sstevel@tonic-gate getFlushSize(void)
200*0Sstevel@tonic-gate {
201*0Sstevel@tonic-gate int group_delay = (order + 1) >> 1;
202*0Sstevel@tonic-gate return ((delay < group_delay)? group_delay - delay : 0);
203*0Sstevel@tonic-gate }
204*0Sstevel@tonic-gate
205*0Sstevel@tonic-gate int Fir::
flush(short * out)206*0Sstevel@tonic-gate flush(short *out) // zero input response of Fir
207*0Sstevel@tonic-gate {
208*0Sstevel@tonic-gate int num = getFlushSize();
209*0Sstevel@tonic-gate if (num > 0) {
210*0Sstevel@tonic-gate short *in = new short[num];
211*0Sstevel@tonic-gate memset(in, 0, num * sizeof (short));
212*0Sstevel@tonic-gate num = filter_noadjust(in, num, out);
213*0Sstevel@tonic-gate delete in;
214*0Sstevel@tonic-gate }
215*0Sstevel@tonic-gate return (num);
216*0Sstevel@tonic-gate }
217*0Sstevel@tonic-gate
218*0Sstevel@tonic-gate /*
219*0Sstevel@tonic-gate * filter() filters in[size] with filter delay adjusted to 0
220*0Sstevel@tonic-gate *
221*0Sstevel@tonic-gate * All FIR filters introduce a delay of "order" samples between input and
222*0Sstevel@tonic-gate * output sequences. Most FIR filters are symmetric filters to keep the
223*0Sstevel@tonic-gate * linear phase responses. For those FIR fitlers the group delay is
224*0Sstevel@tonic-gate * "(order + 1) / 2". So filter_nodelay adjusts the group delay in the
225*0Sstevel@tonic-gate * output sequence such that the output is aligned with the input and
226*0Sstevel@tonic-gate * direct comparison between them is possible.
227*0Sstevel@tonic-gate *
228*0Sstevel@tonic-gate * The first call of filter returns "size - group_delay" output samples.
229*0Sstevel@tonic-gate * After all the input samples have been filtered, filter() needs
230*0Sstevel@tonic-gate * to be called with size = 0 to get the residual output samples to make
231*0Sstevel@tonic-gate * the output sequence the same length as the input.
232*0Sstevel@tonic-gate *
233*0Sstevel@tonic-gate */
234*0Sstevel@tonic-gate
235*0Sstevel@tonic-gate int Fir::
filter(short * in,int size,short * out)236*0Sstevel@tonic-gate filter(
237*0Sstevel@tonic-gate short *in,
238*0Sstevel@tonic-gate int size,
239*0Sstevel@tonic-gate short *out)
240*0Sstevel@tonic-gate {
241*0Sstevel@tonic-gate if ((size <= 0) || (in == NULL))
242*0Sstevel@tonic-gate return (flush(out));
243*0Sstevel@tonic-gate else if (delay <= 0)
244*0Sstevel@tonic-gate return (filter_noadjust(in, size, out));
245*0Sstevel@tonic-gate else if (size <= delay) {
246*0Sstevel@tonic-gate update_short(in, size);
247*0Sstevel@tonic-gate delay -= size;
248*0Sstevel@tonic-gate return (0);
249*0Sstevel@tonic-gate } else {
250*0Sstevel@tonic-gate update_short(in, delay);
251*0Sstevel@tonic-gate in += delay;
252*0Sstevel@tonic-gate size -= delay;
253*0Sstevel@tonic-gate delay = 0;
254*0Sstevel@tonic-gate return (filter_noadjust(in, size, out));
255*0Sstevel@tonic-gate }
256*0Sstevel@tonic-gate }
257