/* * design and implementation of different types of digital filters * * Copyright (C) 2001 Anders Johansson ajh@atri.curtin.edu.au * * This file is part of MPlayer. * * MPlayer is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * MPlayer is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with MPlayer; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include #include #include "dsp.h" /****************************************************************************** * FIR filter implementations ******************************************************************************/ /* C implementation of FIR filter y=w*x n number of filter taps, where mod(n,4)==0 w filter taps x input signal must be a circular buffer which is indexed backwards */ inline FLOAT_TYPE af_filter_fir(register unsigned int n, const FLOAT_TYPE* w, const FLOAT_TYPE* x) { register FLOAT_TYPE y; // Output y = 0.0; do{ n--; y+=w[n]*x[n]; }while(n != 0); return y; } /* C implementation of parallel FIR filter y(k)=w(k) * x(k) (where * denotes convolution) n number of filter taps, where mod(n,4)==0 d number of filters xi current index in xq w filter taps k by n big x input signal must be a circular buffers which are indexed backwards y output buffer s output buffer stride */ FLOAT_TYPE* af_filter_pfir(unsigned int n, unsigned int d, unsigned int xi, const FLOAT_TYPE** w, const FLOAT_TYPE** x, FLOAT_TYPE* y, unsigned int s) { register const FLOAT_TYPE* xt = *x + xi; register const FLOAT_TYPE* wt = *w; register int nt = 2*n; while(d-- > 0){ *y = af_filter_fir(n,wt,xt); wt+=n; xt+=nt; y+=s; } return y; } /* Add new data to circular queue designed to be used with a parallel FIR filter, with d filters. xq is the circular queue, in pointing at the new samples, xi current index in xq and n the length of the filter. xq must be n*2 by k big, s is the index for in. */ int af_filter_updatepq(unsigned int n, unsigned int d, unsigned int xi, FLOAT_TYPE** xq, const FLOAT_TYPE* in, unsigned int s) { register FLOAT_TYPE* txq = *xq + xi; register int nt = n*2; while(d-- >0){ *txq= *(txq+n) = *in; txq+=nt; in+=s; } return (++xi)&(n-1); } /****************************************************************************** * FIR filter design ******************************************************************************/ /* Design FIR filter using the Window method n filter length must be odd for HP and BS filters w buffer for the filter taps (must be n long) fc cutoff frequencies (1 for LP and HP, 2 for BP and BS) 0 < fc < 1 where 1 <=> Fs/2 flags window and filter type as defined in filter.h variables are ored together: i.e. LP|HAMMING will give a low pass filter designed using a hamming window opt beta constant used only when designing using kaiser windows returns 0 if OK, -1 if fail */ int af_filter_design_fir(unsigned int n, FLOAT_TYPE* w, const FLOAT_TYPE* fc, unsigned int flags, FLOAT_TYPE opt) { unsigned int o = n & 1; // Indicator for odd filter length unsigned int end = ((n + 1) >> 1) - o; // Loop end unsigned int i; // Loop index FLOAT_TYPE k1 = 2 * M_PI; // 2*pi*fc1 FLOAT_TYPE k2 = 0.5 * (FLOAT_TYPE)(1 - o);// Constant used if the filter has even length FLOAT_TYPE k3; // 2*pi*fc2 Constant used in BP and BS design FLOAT_TYPE g = 0.0; // Gain FLOAT_TYPE t1,t2,t3; // Temporary variables FLOAT_TYPE fc1,fc2; // Cutoff frequencies // Sanity check if(!w || (n == 0)) return -1; // Get window coefficients switch(flags & WINDOW_MASK){ case(BOXCAR): af_window_boxcar(n,w); break; case(TRIANG): af_window_triang(n,w); break; case(HAMMING): af_window_hamming(n,w); break; case(HANNING): af_window_hanning(n,w); break; case(BLACKMAN): af_window_blackman(n,w); break; case(FLATTOP): af_window_flattop(n,w); break; case(KAISER): af_window_kaiser(n,w,opt); break; default: return -1; } if(flags & (LP | HP)){ fc1=*fc; // Cutoff frequency must be < 0.5 where 0.5 <=> Fs/2 fc1 = ((fc1 <= 1.0) && (fc1 > 0.0)) ? fc1/2 : 0.25; k1 *= fc1; if(flags & LP){ // Low pass filter // If the filter length is odd, there is one point which is exactly // in the middle. The value at this point is 2*fCutoff*sin(x)/x, // where x is zero. To make sure nothing strange happens, we set this // value separately. if (o){ w[end] = fc1 * w[end] * 2.0; g=w[end]; } // Create filter for (i=0 ; i Fs/2 fc1 = ((fc1 <= 1.0) && (fc1 > 0.0)) ? fc1/2 : 0.25; fc2 = ((fc2 <= 1.0) && (fc2 > 0.0)) ? fc2/2 : 0.25; k3 = k1 * fc2; // 2*pi*fc2 k1 *= fc1; // 2*pi*fc1 if(flags & BP){ // Band pass // Calculate center tap if (o){ g=w[end]*(fc1+fc2); w[end] = (fc2 - fc1) * w[end] * 2.0; } // Create filter for (i=0 ; i HP filter returns 0 if OK, -1 if fail */ int af_filter_design_pfir(unsigned int n, unsigned int k, const FLOAT_TYPE* w, FLOAT_TYPE** pw, FLOAT_TYPE g, unsigned int flags) { int l = (int)n/k; // Length of individual FIR filters int i; // Counters int j; FLOAT_TYPE t; // g * w[i] // Sanity check if(l<1 || k<1 || !w || !pw) return -1; // Do the stuff if(flags&REW){ for(j=l-1;j>-1;j--){//Columns for(i=0;i<(int)k;i++){//Rows t=g * *w++; pw[i][j]=t * ((flags & ODD) ? ((j & 1) ? -1 : 1) : 1); } } } else{ for(j=0;j1000.0 || Q< 1.0)) return -1; memcpy(at,a,3*sizeof(FLOAT_TYPE)); memcpy(bt,b,3*sizeof(FLOAT_TYPE)); bt[1]/=Q; /* Calculate a and b and overwrite the original values */ af_filter_prewarp(at, fc, fs); af_filter_prewarp(bt, fc, fs); /* Execute bilinear transform */ af_filter_bilinear(at, bt, k, fs, coef); return 0; }