summaryrefslogtreecommitdiff
path: root/usr/src/cmd/audio/utilities/Fir.cc
blob: 3aa43366f0b6c9c5b4429d04da437f9900db610e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 1992-2001 by Sun Microsystems, Inc.
 * All rights reserved.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <memory.h>
#include <stddef.h>
#include <sys/types.h>
#include <Fir.h>

extern "C" {
	char *bcopy(char *, char *, int);
	char *memmove(char *, char *, int);
}

#define	BCOPY(src, dest, num) memmove(dest, src, num)

/*
 * convolve()
 * returns the convolution of coef[length] and in_buf[length]:
 *
 * convolution = coef[0] * in_buf[length - 1] +
 *		 coef[1] * in_buf[length - 2] +
 *		 ...
 *		 coef[length - 1] * in_buf[0]
 */
double
convolve(
	double	*coefs,
	double	*in_buf,
	int	length)
{
	if (length <= 0)
		return (0.0);
	else {
		in_buf += --length;
		double sum = *coefs * *in_buf;
		while (length--)
			sum += *++coefs * *--in_buf;
		return (sum);
	}
}

void				// convert short to double
short2double(
	double *out,
	short *in,
	int size)
{
	while (size-- > 0)
		*out++ = (double)*in++;
}

short
double2short(double in)			// limit double to short
{
	if (in <= -32768.0)
		return (-32768);
	else if (in >= 32767.0)
		return (32767);
	else
		return ((short)in);
}

void Fir::				// update state with data[size]
updateState(
	double	*data,
	int	size)
{
	if (size >= order)
		memcpy(state, data + size - order, order * sizeof (double));
	else {
		int old = order - size;
		BCOPY((char *)(state + size), (char *)state,
		    old * sizeof (double));
		memcpy(state + order - size, data, size * sizeof (double));
	}
}

void Fir::
update_short(
	short	*in,
	int	size)
{
	double *in_buf = new double[size];
	short2double(in_buf, in, size);
	updateState(in_buf, size);
	delete in_buf;
}

void Fir::
resetState(void)			// reset state to all zero
{
	for (int i = 0; i < order; i++)
		state[i] = 0.0;
}

Fir::
Fir(void)
{
}

Fir::
Fir(int order_in): order(order_in)	// construct Fir object
{
	state = new double[order];
	resetState();
	coef = new double[order + 1];
	delay = (order + 1) >> 1;	// assuming symmetric FIR
}

Fir::
~Fir()					// destruct Fir object
{
	delete coef;
	delete state;
}

int Fir::
getOrder(void)				// returns filter order
{
	return (order);
}

int Fir::
getNumCoefs(void)			// returns number of filter coefficients
{
	return (order + 1);
}

void Fir::
putCoef(double *coef_in)		// copy coef_in in filter coefficients
{
	memcpy(coef, coef_in, (order + 1) * sizeof (double));
}

void Fir::
getCoef(double *coef_out)		// returns filter coefs in coef_out
{
	memcpy(coef_out, coef, (order + 1) * sizeof (double));
}

int Fir::		// filter in[size], and updates the state.
filter_noadjust(
	short	*in,
	int	size,
	short	*out)
{
	if (size <= 0)
		return (0);

	double *in_buf = new double[size];
	short2double(in_buf, in, size);		// convert short input to double
	int	i;
	int	init_size = (size <= order)? size : order;
	int	init_order = order;
	double	*state_ptr = state;
	short	*out_ptr = out;

	// the first "order" outputs need state in convolution
	for (i = 1; i <= init_size; i++)
		*out_ptr++ = double2short(convolve(coef, in_buf, i) +
		    convolve(coef + i, state_ptr++, init_order--));

	// starting from "order + 1"th output, state is no longer needed
	state_ptr = in_buf;
	while (i++ <= size)
		*out_ptr++ =
		    double2short(convolve(coef, state_ptr++, order + 1));
	updateState(in_buf, size);
	delete in_buf;
	return (out_ptr - out);
}

int Fir::
getFlushSize(void)
{
	int group_delay = (order + 1) >> 1;
	return ((delay < group_delay)? group_delay - delay : 0);
}

int Fir::
flush(short *out)		// zero input response of Fir
{
	int num = getFlushSize();
	if (num > 0) {
		short *in = new short[num];
		memset(in, 0, num * sizeof (short));
		num = filter_noadjust(in, num, out);
		delete in;
	}
	return (num);
}

/*
 * filter() filters in[size] with filter delay adjusted to 0
 *
 * All FIR filters introduce a delay of "order" samples between input and
 * output sequences. Most FIR filters are symmetric filters to keep the
 * linear phase responses. For those FIR fitlers the group delay is
 * "(order + 1) / 2". So filter_nodelay adjusts the group delay in the
 * output sequence such that the output is aligned with the input and
 * direct comparison between them is possible.
 *
 * The first call of filter returns "size - group_delay" output samples.
 * After all the input samples have been filtered, filter() needs
 * to be called with size = 0 to get the residual output samples to make
 * the output sequence the same length as the input.
 *
 */

int Fir::
filter(
	short	*in,
	int	size,
	short	*out)
{
	if ((size <= 0) || (in == NULL))
		return (flush(out));
	else if (delay <= 0)
		return (filter_noadjust(in, size, out));
	else if (size <= delay) {
		update_short(in, size);
		delay -= size;
		return (0);
	} else {
		update_short(in, delay);
		in += delay;
		size -= delay;
		delay = 0;
		return (filter_noadjust(in, size, out));
	}
}