- Main Page
- Classes
- Files
- Examples

- File List

# depukfb.H

    00001 #ifndef DEPUKFB_H_
    00002 #define DEPUKFB_H_
    00003
    00004 #include <fstream>
    00005
    00006 #include <iostream>
    00007 #include <math.h>
    00008 //#include "../utils/perceptual.H"
    00009 #include <stdlib.h>
    00010
    00011 #define FREQBINCOUNT 44100
    00012
    00013 //Filter bank central frequencies will be placed between this frequency fs/2
    00014 #define LOWFREQ 50
    00015
    00016 #define C1 24.673
    00017 #define C2 4.368
    00018 #define C3 21.366
    00019
    00020 //We are using a level invariant filterbank Hence define X as static
    00021 #define X 51.0
    00022
    00029 class DepUKFB{
    00030   static double p_51_1k;
    00031   int fCount;
    00032
    00037   double erb(double fc){
    00038     return 24.7*(4.37*(fc/1000.0)+1.0);
    00039   }
    00040
    00045   double freq2ERB(double freq){
    00046     return (C3*log10((C2 * freq/1000.0) + 1.0));
    00047   }
    00052   double ERB2freq(double erb){
    00053     return 1000.0 * (pow(10.0,(erb/C3)) - 1.0) / C2;
    00054   }
    00055
    00061   virtual void af(double fc, int whichFilter){
    00062     //    std::cout<<"DepUKFB::af"<<std::endl;
    00063     double freqFact=((double)fs/2.0)/(double)FREQBINCOUNT;
    00064     //    std::cout<<freqFact<<'\t';
    00065     double freq=0.0;
    00066     for (int i=0;i<FREQBINCOUNT;i++){
    00067       g[i]=fabs((freq-fc)/fc);
    00068       freq+=freqFact;
    00069     }
    00070
    00071     double *filt=w[whichFilter], p;
    00072     freq=0.0;
    00073
    00074     for (int i=0;i<FREQBINCOUNT;i++){
    00075       if (freq<fc)
    00076         p=p_l(fc);
    00077       else
    00078         p=p_u(fc);
    00079       filt[i]=(1.0+p*g[i])*exp(-p*g[i]);
    00080       freq+=freqFact;
    00081     }
    00082   }
    00083
    00084
    00088   void findCF(void){
    00089     double step=(freq2ERB((double)fs/2.0-1.0)-freq2ERB(LOWFREQ))/(fCount-1.0);
    00090     //std::cout<<"step "<<step<<std::endl;
    00091     double erbval=freq2ERB(LOWFREQ)-step;
    00092     for (int i=0;i<fCount;i++){
    00093       cf[i]=ERB2freq(erbval+=step); //centre frequency locations
    00094       //std::cout<<cf[i]<<std::endl;
    00095     }
    00096
    00097     erbval=freq2ERB(LOWFREQ)-step/2.0;
    00098     ef[0]=0.0;
    00099     for (int i=1;i<fCount;i++){
    00100       ef[i]=ERB2freq(erbval+=step); //edge frequency locations
    00101       //std::cout<<cf[i]<<'\t'<<ef[i]<<std::endl;
    00102     }
    00103   }
    00104 protected:
    00105   int fs;
    00106   double *g;
    00107   double **w;
    00108
    00109   DepUKFB(){
    00110   }
    00111
    00116   double p_l(double fc){
    00117     // The following reduces to p_51 for constant X
    00118     //double p_51=4.0*fc/erb(fc);
    00119     //    return p_51-0.35*(p_51/p_51_1k)*(X-51.0);
    00120     return 4.0*fc/erb(fc);
    00121   }
    00122
    00127   double p_u(double fc){
    00128     // The following reduces to p_51 for constant X
    00129     //double p_51=4.0*fc/erb(fc);
    00130     //return p_51+0.118*(X-51.0);
    00131     return 4.0*fc/erb(fc);
    00132   }
    00133 public:
    00134   double *cf;
    00135   double *ef;
    00136
    00142   DepUKFB(int sampleFreq, int fCnt=50){
    00143     init(sampleFreq, fCnt);
    00144   }
    00145
    00151   void init(int sampleFreq, int fCnt=50){
    00152     fCount=fCnt;
    00153     fs=sampleFreq;
    00154     cf=ef=g=NULL;
    00155     w=NULL;
    00156     if (!(g=new double[FREQBINCOUNT])){
    00157       std::cerr<<"DepUKFB::DepUKFB: g malloc error"<<std::endl;
    00158       exit(-1);
    00159     }
    00160     if (!(w=new double*[fCount])){
    00161       std::cerr<<"DepUKFB::DepUKFB: w malloc error"<<std::endl;
    00162       exit(-1);
    00163     } else {
    00164       for (int i=0;i<fCount;i++)
    00165         w[i]=NULL;
    00166       for (int i=0;i<fCount;i++){
    00167         if (!(w[i]=new double[FREQBINCOUNT])){
    00168           std::cerr<<"DepUKFB::DepUKFB: w[i] malloc error"<<std::endl;
    00169           exit(-1);
    00170         }
    00171       }
    00172     }
    00173
    00174     if (!(cf=new double[fCount])){
    00175       std::cerr<<"DepUKFB::DepUKFB: cf malloc error"<<std::endl;
    00176       exit(-1);
    00177     }
    00178
    00179     if (!(ef=new double[fCount])){
    00180       std::cerr<<"DepUKFB::DepUKFB: ef malloc error"<<std::endl;
    00181       exit(-1);
    00182     }
    00183
    00184     //Place the filter centre freqs ...
    00185     findCF();
    00186     //Step through and fill each filter ...
    00187     for (int i=0;i<fCount;i++)
    00188       af(cf[i],i);
    00189   }
    00190
    00191   virtual ~DepUKFB(){
    00192     if (g) delete [] g;
    00193     if (w){
    00194       for (int i=0;i<fCount;i++)
    00195         if (w[i]) delete [] w[i];
    00196       delete [] w;
    00197     }
    00198     if (cf) delete [] cf;
    00199     if (ef) delete [] ef;
    00200   }
    00201
    00202   int filterCount(void){return fCount;}
    00203
    00208   double* operator[](int i){return w[i];}
    00215   double operator()(int i, int j, int binCount){
    00216     int index=(int)rint((double)j*((double)FREQBINCOUNT/(double)binCount));
    00217     //std::cout<<i<<'\t'<<j<<'\t'<<binCount<<'\t'<<index<<std::endl;
    00218     return w[i][index];
    00219   }
    00220 };
    00221 #endif //DEPUKFB_H_
    00222
    00223
    00224

All Classes Files Functions Variables