Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

VrRealFIRfilter.h

Go to the documentation of this file.
00001 /* -*- Mode: c++ -*- */
00002 /*
00003  * Copyright 2001 Free Software Foundation, Inc.
00004  * 
00005  * This file is part of GNU Radio
00006  * 
00007  * GNU Radio is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2, or (at your option)
00010  * any later version.
00011  * 
00012  * GNU Radio is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with GNU Radio; see the file COPYING.  If not, write to
00019  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00020  * Boston, MA 02111-1307, USA.
00021  */
00022 /*
00023  *  Copyright 1997 Massachusetts Institute of Technology
00024  * 
00025  *  Permission to use, copy, modify, distribute, and sell this software and its
00026  *  documentation for any purpose is hereby granted without fee, provided that
00027  *  the above copyright notice appear in all copies and that both that
00028  *  copyright notice and this permission notice appear in supporting
00029  *  documentation, and that the name of M.I.T. not be used in advertising or
00030  *  publicity pertaining to distribution of the software without specific,
00031  *  written prior permission.  M.I.T. makes no representations about the
00032  *  suitability of this software for any purpose.  It is provided "as is"
00033  *  without express or implied warranty.
00034  * 
00035  */
00036 
00037 
00038 #ifndef _VrREALFIRFILTER_H_
00039 #define _VrREALFIRFILTER_H_
00040 
00041 #include <VrDecimatingSigProc.h>
00042 #include <fstream>
00043 
00044 /*  FIR filter definition:
00045     
00046     VrRealFIRfilter( cutoff freq., Num of taps, decimation factor, center frequency)
00047     
00048     cutoff (Hz) = 0.0  => LPF using Hamming window, o/w LPF transformed to have higher cutoff freq
00049     decimation factor => set to one unless some integer >1 specified
00050     center_freq (Hz) => used to specify a composite frequency-shifting filter (i.e. channel filter)
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; /* filter Order */
00100 
00101   inSampFreq = getInputSamplingFrequencyN(0); 
00102   
00103   // Build Real Filter
00104   if (cutoff == 0.0){
00105     
00106     // cut-off == 0.0 => Hamming window
00107     for (index=0;index < numTaps; index++) {
00108       taps[(int)index] =  gain*(0.54-0.46*cos(2*M_PI*index/(M)));
00109       //      printf("index: %f   tap: %f\n",index,taps[(int)index]);
00110     }
00111   } else {
00112     
00113     // cut-off != 0.0 => Hamming window * transform to move cutoff to correct place
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       //   printf("index: %f   tap: %f\n",index,taps[(int)index]);
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   // First parse file to determine the numebr of taps
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   // Re-open and read in taps
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       //      cout << "Tap # " << j << " = " << taps[j] << "\n";
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   //inputArray = new iType[numTaps];
00195 }
00196 
00197 template<class iType,class oType> 
00198 VrRealFIRfilter<iType,oType>::~VrRealFIRfilter()
00199 {
00200   delete taps;
00201 }
00202 
00203 #endif

Generated on Tue Mar 15 23:46:36 2005 for GNU Radio by  doxygen 1.4.0