gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
depukfb.H
Go to the documentation of this file.
1 /*
2  libaudiomask - hybrid simultaneous audio masking threshold evaluation library
3  Copyright (C) 2000-2018 Dr Matthew Raphael Flax
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #ifndef DEPUKFB_H_
20 #define DEPUKFB_H_
21 
22 #include <iostream>
23 #include <math.h>
24 //#include "../utils/perceptual.H"
25 #include <stdlib.h>
26 
27 #include "AudioMask/MooreSpread.H"
28 
29 extern double rint(double);
30 
31 #define FREQBINCOUNT 44100
32 
33 //Filter bank central frequencies will be placed between this frequency fs/2
34 #define LOWFREQ 50
35 
36 #define AM_C1 24.673
37 #define AM_C2 4.368
38 #define AM_C3 21.366
39 
40 //We are using a level invariant filterbank Hence define X as static
41 #define AM_X 51.0
42 
49 class DepUKFB{
50  static double p_51_1k;
51  int fCount;
52 
57  double erb(double fc){
58  return 24.7*(4.37*(fc/1000.0)+1.0);
59  }
60 
65  double freq2ERB(double freq){
66  return (AM_C3*log10((AM_C2 * freq/1000.0) + 1.0));
67  }
72  double ERB2freq(double erb){
73  return 1000.0 * (pow(10.0,(erb/AM_C3)) - 1.0) / AM_C2;
74  }
75 
81  virtual void af(double fc, int whichFilter){
82  // std::cout<<"DepUKFB::af"<<std::endl;
83  double freqFact=((double)fs/2.0)/(double)FREQBINCOUNT;
84  // std::cout<<freqFact<<'\t';
85  double freq=0.0;
86  for (int i=0;i<FREQBINCOUNT;i++){
87  g[i]=fabs((freq-fc)/fc);
88  freq+=freqFact;
89  }
90 
91  double *filt=w[whichFilter], p;
92  freq=0.0;
93 
94  for (int i=0;i<FREQBINCOUNT;i++){
95  if (freq<fc)
96  p=p_l(fc);
97  else
98  p=p_u(fc);
99  filt[i]=(1.0+p*g[i])*exp(-p*g[i]);
100  freq+=freqFact;
101  }
102  }
103 
104 
108  void findCF(void){
109  double step=(freq2ERB((double)fs/2.0-1.0)-freq2ERB(LOWFREQ))/(fCount-1.0);
110  //std::cout<<"step "<<step<<std::endl;
111  double erbval=freq2ERB(LOWFREQ)-step;
112  for (int i=0;i<fCount;i++){
113  cf[i]=ERB2freq(erbval+=step); //centre frequency locations
114  //std::cout<<cf[i]<<std::endl;
115  }
116 
117  erbval=freq2ERB(LOWFREQ)-step/2.0;
118  ef[0]=0.0;
119  for (int i=1;i<fCount;i++){
120  ef[i]=ERB2freq(erbval+=step); //edge frequency locations
121  //std::cout<<cf[i]<<'\t'<<ef[i]<<std::endl;
122  }
123  }
124 protected:
125  int fs;
126  double *g;
127  double **w;
128 
130  }
131 
136  double p_l(double fc){
137  // The following reduces to p_51 for constant X
138  //double p_51=4.0*fc/erb(fc);
139  // return p_51-0.35*(p_51/p_51_1k)*(X-51.0);
140  return 4.0*fc/erb(fc);
141  }
142 
147  double p_u(double fc){
148  // The following reduces to p_51 for constant X
149  //double p_51=4.0*fc/erb(fc);
150  //return p_51+0.118*(X-51.0);
151  return 4.0*fc/erb(fc);
152  }
153 public:
154  double *cf;
155  double *ef;
156 
162  DepUKFB(int sampleFreq, int fCnt=50){
163  init(sampleFreq, fCnt);
164  }
165 
171  void init(int sampleFreq, int fCnt=50){
172  fCount=fCnt;
173  fs=sampleFreq;
174  cf=ef=g=NULL;
175  w=NULL;
176  if (!(g=new double[FREQBINCOUNT])){
177  std::cerr<<"DepUKFB::DepUKFB: g malloc error"<<std::endl;
178  exit(-1);
179  }
180  if (!(w=new double*[fCount])){
181  std::cerr<<"DepUKFB::DepUKFB: w malloc error"<<std::endl;
182  exit(-1);
183  } else {
184  for (int i=0;i<fCount;i++)
185  w[i]=NULL;
186  for (int i=0;i<fCount;i++){
187  if (!(w[i]=new double[FREQBINCOUNT])){
188  std::cerr<<"DepUKFB::DepUKFB: w[i] malloc error"<<std::endl;
189  exit(-1);
190  }
191  }
192  }
193 
194  if (!(cf=new double[fCount])){
195  std::cerr<<"DepUKFB::DepUKFB: cf malloc error"<<std::endl;
196  exit(-1);
197  }
198 
199  if (!(ef=new double[fCount])){
200  std::cerr<<"DepUKFB::DepUKFB: ef malloc error"<<std::endl;
201  exit(-1);
202  }
203 
204  //Place the filter centre freqs ...
205  findCF();
206  //Step through and fill each filter ...
207  for (int i=0;i<fCount;i++)
208  af(cf[i],i);
209  }
210 
211  virtual ~DepUKFB(){
212  if (g) delete [] g;
213  if (w){
214  for (int i=0;i<fCount;i++)
215  if (w[i]) delete [] w[i];
216  delete [] w;
217  }
218  if (cf) delete [] cf;
219  if (ef) delete [] ef;
220  }
221 
222  int filterCount(void){return fCount;}
223 
228  double* operator[](int i){return w[i];}
235  double operator()(int i, int j, int binCount){
236  int index=(int)rint((double)j*((double)FREQBINCOUNT/(double)binCount));
237  //std::cout<<i<<'\t'<<j<<'\t'<<binCount<<'\t'<<index<<std::endl;
238  return w[i][index];
239  }
240 };
241 
242 #endif //DEPUKFB_H_
243 
244 
245 
double p_l(double fc)
Definition: depukfb.H:136
double ** w
The filters.
Definition: depukfb.H:127
double * cf
The filter centre frequencies.
Definition: depukfb.H:154
double rint(double)
double erb(double fc)
Definition: depukfb.H:57
float p
DepUKFB(int sampleFreq, int fCnt=50)
Definition: depukfb.H:162
double freq2ERB(double freq)
Definition: depukfb.H:65
#define AM_C3
Definition: depukfb.H:38
double * ef
The filter edge frequencies.
Definition: depukfb.H:155
virtual ~DepUKFB()
Definition: depukfb.H:211
double * g
g coeff.
Definition: depukfb.H:126
DepUKFB()
Definition: depukfb.H:129
double ERB2freq(double erb)
Definition: depukfb.H:72
double * operator[](int i)
Definition: depukfb.H:228
#define LOWFREQ
Definition: depukfb.H:34
static double p_51_1k
Definition: depukfb.H:50
double operator()(int i, int j, int binCount)
Definition: depukfb.H:235
int fCount
Definition: depukfb.H:51
int fs
The sample frequency.
Definition: depukfb.H:125
int filterCount(void)
Returns the number of filters.
Definition: depukfb.H:222
void init(int sampleFreq, int fCnt=50)
Definition: depukfb.H:171
#define AM_C2
Definition: depukfb.H:37
virtual void af(double fc, int whichFilter)
Definition: depukfb.H:81
double p_u(double fc)
Definition: depukfb.H:147
void findCF(void)
Definition: depukfb.H:108
#define FREQBINCOUNT
Definition: depukfb.H:31
gtkIOStream: /tmp/gtkiostream/include/AudioMask/depukfb.H Source File
GTK+ IOStream  Beta