gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
AudioMaskExample.cc
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 #include "../fft/realFFT.H"
20 #include "AudioMask.H"
21 
22 // Filter bank memory de-allocation routine
23 void FBDeMalloc(double **outputGT, int fCount){
24  if (outputGT){
25  for (int i=0;i<fCount;i++)
26  if (outputGT[i]) delete [] outputGT[i];
27  delete [] outputGT;
28  }
29 }
30 
31 // Filter bank memory allocation routine
32 double ** FBMalloc(int fCount, int sCount){
33  // Find the output of the gammatone filters
34  double **outputGT=NULL;
35  if (!(outputGT=new double*[fCount])){
36  cerr<<"filter bank malloc error initial"<<endl;
37  exit(-1);
38  } else {
39  for (int i=0;i<fCount;i++)
40  outputGT[i]=NULL;
41  for (int i=0;i<fCount;i++)
42  if (!(outputGT[i]=new double[sCount])){
43  cerr<<"filter bank malloc error secondary"<<endl;
44  FBDeMalloc(outputGT, fCount);
45  exit(-1);
46  }
47  }
48  return outputGT;
49 }
50 
51 #define INPUTFILENAME "fa.dat"
52 #define TMASKFILENAME "fa.t.mask"
53 #define BMASKFILENAME "fa.b.mask"
54 #define POWFILENAME "fa.pow"
55 #define EXCITEFILENAME "fa.excite"
56 #include <fstream>
57 
58 #define USEGCFB
59 #ifdef USEGCFB
60 #define PFB gcfb
61 #else
62 #define PFB gtfb
63 #endif
64 
65 void main(void){
66  int sampleCount=16384, halfSampleCount=(int)rint((double)sampleCount/2.0);
67  int count=50; int lowFreq=100; int sampleFreq=8192;
68  double input[sampleCount], **outputGT, **output;
69  double **powOutput;
70 
71  // bzero(input, sampleCount*sizeof(double));
72  //input[0]=100.0;
73  // Load the input
74  ifstream inputF(INPUTFILENAME);
75  int temp;
76  for (int i=0; i<sampleCount;i++)
77  inputF >> temp >> input[i];
78  inputF.close();
79 
80  powOutput=FBMalloc(count, halfSampleCount);
81  outputGT=FBMalloc(count, sampleCount);
82  for (int i=0;i<count;i++)
83  bzero(outputGT[i], sampleCount*sizeof(double));
84 
85  output=FBMalloc(count, sampleCount);
86  for (int i=0;i<count;i++)
87  bzero(output[i], sampleCount*sizeof(double));
88 
89 #ifdef USEGCFB
90  // Get our gammachirp filter bank and filter using it
91  GCFB gcfb(lowFreq, sampleFreq, count);
92  gcfb.filter((double*)input, outputGT, output, sampleCount);
93 #else
94  GTFB gtfb(lowFreq, sampleFreq, count);
95  for (int i=1; i<=count;i++)
96  gtfb.grab(i)->filter(input, &outputGT[i-1][0], sampleCount);
97 #endif
98 
99  // Convert the output to the frequency domain ...
100  realFFTData fftData(sampleCount);
101  realFFT fft(&fftData);
102  for (int i=0;i<count;i++){
103  for (int j=0; j<sampleCount;j++)
104  fftData.in[j]=outputGT[i][j];
105  fft.fwdTransform();
106  fftData.compPowerSpec();
107  for (int j=0; j<halfSampleCount;j++){
108  powOutput[i][j]=sqrt(fftData.power_spectrum[j]);
109  // powOutput[i][j]=fftData.power_spectrum[j];
110  //cout<<powOutput[i][j]<<' ';
111  }
112  // cout<<endl;
113  }
114 
115  // Get our spreading function ...
116  AudioMask mask(count);
117  // Set up the frequencies of interest (filter bank centre freqs.)
118  PFB.grab(1);
119  for (int i=0; i<count;i++)
120 #ifdef USEGCFB
121  mask.setCFreq(i, gcfb.prev()->gt->cf);
122 #else
123  mask.setCFreq(i, gtfb.prev()->cf);
124 #endif
125  // Find the spreading function
126  mask.exciteTerhardt(powOutput, sampleCount, sampleFreq);
127 
128  PFB.grab(1);
129  ofstream outputF(TMASKFILENAME);
130  for (int i=0; i<count;i++)
131 #ifdef USEGCFB
132  outputF <<gcfb.prev()->gt->cf*((double)sampleCount/(double)sampleFreq)<<'\t'<< 20*log10(mask.mask[i])<<'\n';
133 #else
134  outputF <<gtfb.prev()->cf*((double)sampleCount/(double)sampleFreq)<<'\t'<< 20*log10(mask.mask[i])<<'\n';
135 #endif
136  //for (int i=0; i<sampleCount;i++)
137  //outputF << input[i]<<'\n';
138  outputF.close();
139 
140  mask.exciteBeerends(powOutput, sampleCount, sampleFreq);
141  cout<<"Done exciting"<<endl;
142  PFB.grab(1);
143  outputF.open(BMASKFILENAME);
144  for (int i=0; i<count;i++)
145 #ifdef USEGCFB
146  outputF <<gcfb.prev()->gt->cf*(sampleCount/sampleFreq)<<'\t'<< 10*log10(mask.mask[i])<<'\n';
147 #else
148  outputF <<gtfb.prev()->cf*(sampleCount/sampleFreq)<<'\t'<< 10*log10(mask.mask[i])<<'\n';
149 #endif
150  //for (int i=0; i<sampleCount;i++)
151  //outputF << input[i]<<'\n';
152  outputF.close();
153 
154  outputF.open(POWFILENAME);
155  for (int j=0; j<sampleCount;j++)
156  fftData.in[j]=input[j];
157  fft.fwdTransform();
158  fftData.compPowerSpec();
159  for (int j=0; j<halfSampleCount;j++){
160  outputF<<j+1<<'\t'<<20*log10(sqrt(fftData.power_spectrum[j]))<<endl;
161  //outputF<<j+1<<'\t'<<20*log10(fftData.power_spectrum[j])<<endl;
162  }
163  outputF.close();
164 
165 
166  FBDeMalloc(powOutput, count);
167  FBDeMalloc(output, count);
168  FBDeMalloc(outputGT, count);
169 }
#define POWFILENAME
#define TMASKFILENAME
void setCFreq(int which, double value)
Definition: AudioMask.H:52
double rint(double)
double ** FBMalloc(int fCount, int sCount)
#define BMASKFILENAME
#define INPUTFILENAME
void exciteTerhardt(double **filterBankOutput, int sampleCount)
Definition: AudioMask.C:59
double * mask
The audio mask.
Definition: AudioMask.H:36
#define USEGCFB
#define PFB
void FBDeMalloc(double **outputGT, int fCount)
void exciteBeerends(double **filterBankOutput, int sampleCount)
Definition: AudioMask.C:126
void main(void)
gtkIOStream: /tmp/gtkiostream/src/AudioMask/AudioMaskExample.cc Source File
GTK+ IOStream  Beta