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