/* * design and implementation of different types of digital filters * * Copyright (C) 2001 Anders Johansson ajh@atri.curtin.edu.au * * This file is part of mpv. * * mpv 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. * * mpv 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 mpv. If not, see . */ #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; } /****************************************************************************** * 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 ; i1000.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; }