00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef _VrREALFIRFILTER_H_
00039 #define _VrREALFIRFILTER_H_
00040
00041 #include <VrDecimatingSigProc.h>
00042 #include <fstream>
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 template<class iType,class oType>
00058 class VrRealFIRfilter : public VrDecimatingSigProc<iType,oType> {
00059 protected:
00060 int numTaps;
00061 float* taps;
00062 float cutoff, gain;
00063 int FileDefined;
00064 void buildFilter_real();
00065 public:
00066 virtual const char *name() { return "VrRealFIRfilter"; }
00067 virtual int work(VrSampleRange output, void *o[],
00068 VrSampleRange inputs[], void *i[]);
00069 virtual void initialize();
00070
00071 VrRealFIRfilter(float c,int t, float g);
00072 VrRealFIRfilter(int d,float c,int t, float g);
00073 VrRealFIRfilter(int d,char* file);
00074 ~VrRealFIRfilter();
00075 };
00076
00077 template<class iType,class oType> int
00078 VrRealFIRfilter<iType,oType>::work(VrSampleRange output, void *ao[],
00079 VrSampleRange inputs[], void *ai[]) {
00080 iType **i = (iType **)ai;
00081 oType **o = (oType **)ao;
00082 float result;
00083 unsigned int size=output.size;
00084 for (;size>0;size--,i[0]+=decimation) {
00085 result = 0;
00086 iType* inputArray=i[0];
00087 for (int j=0; j < numTaps; j++)
00088 result += taps[j] * inputArray[j];
00089 *o[0]++=(oType)result;
00090 }
00091 return output.size;
00092 }
00093
00094 template<class iType,class oType> void
00095 VrRealFIRfilter<iType,oType>::buildFilter_real(){
00096 double inSampFreq;
00097 float index, arg;
00098 float N = (float)numTaps;
00099 float M = N-1;
00100
00101 inSampFreq = getInputSamplingFrequencyN(0);
00102
00103
00104 if (cutoff == 0.0){
00105
00106
00107 for (index=0;index < numTaps; index++) {
00108 taps[(int)index] = gain*(0.54-0.46*cos(2*M_PI*index/(M)));
00109
00110 }
00111 } else {
00112
00113
00114 arg = 2*M_PI*cutoff/inSampFreq;
00115 for (index=0;index < numTaps;index++) {
00116 if (index-(M/2) != 0){
00117 taps[(int)index] = gain*(sin(arg*(index-(M/2)))/M_PI/(index-(M/2))*(0.54-0.46*cos(2*M_PI*index/M)));
00118 }
00119
00120 }
00121 if ( (((int)M)/2)*2 == (int)M ){
00122 taps[(int)M/2] = gain*arg/M_PI;
00123 }
00124 }
00125 }
00126
00127
00128 template<class iType,class oType>
00129 VrRealFIRfilter<iType,oType>::VrRealFIRfilter(float c,int t, float g)
00130 :VrDecimatingSigProc<iType, oType>(1,1),numTaps(t),cutoff(c),gain(g),FileDefined(0)
00131 {
00132
00133 }
00134
00135 template<class iType,class oType>
00136 VrRealFIRfilter<iType,oType>::VrRealFIRfilter(int dec, float c,int t, float g)
00137 :VrDecimatingSigProc<iType, oType>(1,dec),numTaps(t),cutoff(c),gain(g),FileDefined(0)
00138 {
00139 }
00140
00141 template<class iType,class oType>
00142 VrRealFIRfilter<iType,oType>::VrRealFIRfilter(int dec,char* filename)
00143 :VrDecimatingSigProc<iType, oType>(1,dec),cutoff(0.0),gain(1.0),
00144 FileDefined(1)
00145 {
00146
00147 std::ifstream file(filename);
00148 if (!file) {
00149 fprintf(stderr, "Failed to open file\n");
00150 exit(0);
00151 }
00152 numTaps = 0;
00153 char foo;
00154 while (file.get(foo)) {
00155 if (foo == '\n')
00156 numTaps++;
00157 }
00158
00159 if (numTaps < 1) {
00160 fprintf(stderr, "No taps defined in file\n");
00161 exit(1);
00162 }
00163 taps = new float[numTaps];
00164 file.close();
00165
00166 std::ifstream file2(filename);
00167 if (!file2) {
00168 fprintf(stderr, "Failed to open file\n");
00169 exit(0);
00170 }
00171
00172 char* asciiTap = new char[100];
00173 int i = 0;
00174 int j = 0;
00175 while (file2.get(asciiTap[i])) {
00176 if (asciiTap[i] == '\n') {
00177 taps[j] = atof(asciiTap);
00178
00179 i = 0;j++;
00180 } else {
00181 i++;
00182 }
00183 }
00184 file2.close();
00185 }
00186
00187 template<class iType,class oType>
00188 void VrRealFIRfilter<iType,oType>::initialize()
00189 {
00190 history=numTaps;
00191 if (!FileDefined)
00192 taps = new float[numTaps];
00193 buildFilter_real();
00194
00195 }
00196
00197 template<class iType,class oType>
00198 VrRealFIRfilter<iType,oType>::~VrRealFIRfilter()
00199 {
00200 delete taps;
00201 }
00202
00203 #endif