gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
RealFFTData.C
Go to the documentation of this file.
1 /* Copyright 2000-2018 Matt Flax <flatmax@flatmax.org>
2  This file is part of GTK+ IOStream class set
3 
4  GTK+ IOStream is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  GTK+ IOStream is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You have received a copy of the GNU General Public License
15  along with GTK+ IOStream
16 */
17 
18 #include "fft/RealFFTData.H"
19 
20 #include <math.h>
21 #include <stdlib.h>
22 
24 RealFFTData(int sz){
26  //cout <<"RealFFTData init:"<<this<<endl;
27  size=sz;
28  in = out = power_spectrum = NULL;
29  // powerDeriv = NULL;
30 
31  in = new fftw_real[size];
32  out = new fftw_real[size];
33  power_spectrum = new fftw_real[size/2+1];
34  if (!in || !out || !power_spectrum){
35  printf("Could not allocate enough mem for a RealFFT\n");
36  if (in) delete [] in;
37  if (out) delete [] out;
38  if (power_spectrum) delete [] power_spectrum;
39  exit(-1);
40  }
41  totalPower = 0.0;
42 }
43 
45 RealFFTData(int sz, fftw_real *inp, fftw_real *outp){
47  // cout <<"RealFFTData init:"<<this<<endl;
48  size=sz;
49  if (!inp || !outp){
50  printf("RealFFTData::RealFFTData : input or output array doesn't exist\n");
51  exit(-1);
52  }
53  in = inp;
54  out = outp;
55  power_spectrum = NULL;
56  //powerDeriv = NULL;
57 
58  power_spectrum = new fftw_real[size/2+1];
59  if (!power_spectrum){
60  printf("Could not allocate enough mem for a RealFFT\n");
61  if (power_spectrum) delete [] power_spectrum;
62  exit(-1);
63  }
64  totalPower = 0.0;
65 }
66 
69  if (power_spectrum) delete [] power_spectrum; power_spectrum=NULL;
70  //if (powerDeriv) delete [] powerDeriv; powerDeriv=NULL;
71  // std::cout<<"RealFFTData::~RealFFTData"<<std::endl;
72  if (deleteInOutMemory){
73  if (in) delete [] in; in=NULL;
74  if (out) delete [] out; out=NULL;
75  }
76  //std::cout<<"RealFFTData::~RealFFTData exit"<<std::endl;
77 }
78 
81  fftw_real max=-MAXDOUBLE;
82  for (int i=0; i<getSize(); i++)
83  if (in[i]>max)
84  max=in[i];
85  // cout<<"max "<<max<<endl;
86  return max;
87 }
88 
89 void RealFFTData::
91  double min=MAXDOUBLE;
92  double max=-min;
93  for (int i=0; i<getHalfSize(); i++){
94  if (power_spectrum[i]>max)
96  if (power_spectrum[i]<min)
98  }
99  // cout<<"min bin "<<minPowerBin<<'\t'<<min<<" max poewr bin "<<maxPowerBin<<'\t'<<max<<endl;
100 }
101 
102 
103 int RealFFTData::
104 limitHalfPowerSpec(double lim){
105  double max=0.0;
106  int bin=0;
107  for (int i=0; i<getHalfSize(); i++)
108  if (power_spectrum[i]> max)
109  max=power_spectrum[bin=i];
110  for (int i=0; i<getHalfSize(); i++)
111  power_spectrum[i] /=(max/lim);
112  return bin;
113 }
114 
115 
116 int RealFFTData::
118  // int bin;
119  totalPower = 0.0;
120  double min=MAXDOUBLE;
121  double max=-min;
122  power_spectrum[maxPowerBin=0] = out[0]*out[0]; /* DC component */
123 
124  for (int k = 1; k < (getSize()+1)/2; ++k){ /* (k < N/2 rounded up) */
125  if ((power_spectrum[k] = out[k]*out[k] + out[getSize()-k]*out[getSize()-k])>max){
126  max=power_spectrum[maxPowerBin=k];
127  }
128  if (power_spectrum[k]<min) min=power_spectrum[minPowerBin=k];
130  }
131  if (getSize() % 2 == 0){ /* N is even */
132  power_spectrum[getSize()/2] = out[getSize()/2]*out[getSize()/2]; /* Nyquist freq. */
133  if (power_spectrum[getSize()/2]>max)
134  max=power_spectrum[maxPowerBin=getSize()/2];
135  if (power_spectrum[getSize()/2]<min)
138  }
139  return maxPowerBin;
140 }
141 
142 int RealFFTData::
144  double min=MAXDOUBLE;
145  double max=-MAXDOUBLE;
146  for (int k = 0; k < (getSize()+1)/2; ++k){ /* (k < N/2 rounded up) */
147  if ((power_spectrum[k]=sqrt(power_spectrum[k]))>max)
149  if (power_spectrum[k]<min) min=power_spectrum[minPowerBin=k];
150  }
151  return maxPowerBin;
152 }
153 
154 void RealFFTData::
156  compPowerSpec();
157  sqrtPowerSpec();
158  for (int k = 0; k < (getSize()+1)/2; ++k) /* (k < N/2 rounded up) */
159  power_spectrum[k]=20.*log10(power_spectrum[k]);
160 }
161 
162 /*
163 int RealFFTData::
164 powerSpecDeriv(){
165  if (!powerDeriv){ // create memory if it doesn't exist
166  powerDeriv = new fftw_real[size/2+1];
167  if (!powerDeriv){
168  std::cerr << "Could not allocate enough mem for a powerSpectrum deriv"<<std::endl;
169  exit(-1);
170  }
171  }
172 
173  int pos=0;
174  double max = 0.0;
175  powerDeriv[0] = 0.0; // DC component
176  for (int k = 1; k < (getSize()+1)/2; ++k){ // (k < N/2 rounded up)
177  if (fabs(powerDeriv[k] = power_spectrum[k]-power_spectrum[k-1]) > max){
178  max = fabs(powerDeriv[k] = power_spectrum[k]-power_spectrum[k-1]);
179  pos = k;
180  }
181  }
182 if (getSize() % 2 == 0) // N is even
183  if (fabs(powerDeriv[getSize()/2] = power_spectrum[getSize()/2]-power_spectrum[getSize()/2-1]) > max){
184  max = fabs(powerDeriv[getSize()/2] = power_spectrum[getSize()/2]-power_spectrum[getSize()/2-1]);
185  pos = getSize()/2;
186  }
187  return pos;
188 }
189 */
190 
191 void RealFFTData::
193  //cout<<"here"<<std::endl;
194  for (int i=0;i<getSize();i++)
195  out[i]=0.0;
196 }
197 
198 void RealFFTData::load(const unsigned int i, const fftw_real val){
199  if (i<getSize())
200  in[i]=val;
201 }
202 
203 fftw_real RealFFTData::unload(const unsigned int i){
204  if (i<getSize())
205  return out[i];
206  return 0./0.; // on error return NaN
207 }
208 
209 fftw_real RealFFTData::unloadPS(const unsigned int i){
210  if (i<=getSize()/2+1)
211  return power_spectrum[i];
212  return 0./0.; // on error return NaN
213 }
214 
216  for (int i=0;i<getSize();i++)
217  printf("%f\t", in[i]);
218  printf("\n");
219 }
220 
222  for (int i=0;i<getSize();i++)
223  printf("%f\t", out[i]);
224  printf("\n");
225 }
226 
227 #include "config.h"
228 #ifdef HAVE_EMSCRIPTEN
229 #include <emscripten/bind.h>
230 EMSCRIPTEN_BINDINGS(RealFFTData_ex) {
231  emscripten::class_<RealFFTData>("RealFFTData")
232  .constructor<int>() // the constructor takes in a size
233  .function("limitHalfPowerSpec", &RealFFTData::limitHalfPowerSpec)
234  .function("getSize", &RealFFTData::getSize)
235  .function("getHalfSize", &RealFFTData::getHalfSize)
236  .function("findMaxIn", &RealFFTData::findMaxIn)
237  .function("findMaxMinPowerBins", &RealFFTData::findMaxMinPowerBins)
238  .function("compPowerSpec", &RealFFTData::compPowerSpec)
239  .function("sqrtPowerSpec", &RealFFTData::sqrtPowerSpec)
240  .function("powerInDB", &RealFFTData::powerInDB)
241  .function("zeroFFTData", &RealFFTData::zeroFFTData)
242  .function("load", &RealFFTData::load)
243  .function("unload", &RealFFTData::unload)
244  .function("unloadPS", &RealFFTData::unloadPS)
245  .function("dumpIn", &RealFFTData::dumpIn)
246  .function("dumpOut", &RealFFTData::dumpOut)
247  ;
248 
249 // .function("getFS", &EQ::getFS)
250 // .property("x", &MyClass::getX, &MyClass::setX)
251 // .class_function("getStringFromInstance", &MyClass::getStringFromInstance)
252 }
253 #endif
void findMaxMinPowerBins(void)
Fills the max and min power spectrum bins.
Definition: RealFFTData.C:90
RealFFTData(int sz)
All memory to be allocated internally.
Definition: RealFFTData.C:24
void powerInDB()
This is the power spectrum in dB.
Definition: RealFFTData.C:155
int getSize(void)
Returns the number of elements in the input and output arrays.
Definition: RealFFTData.H:48
int deleteInOutMemory
Var used to specify if the memory was allocated by the RealFFTData class.
Definition: RealFFTData.H:26
void zeroFFTData(void)
This function zeros the output data array (out)
Definition: RealFFTData.C:192
int maxPowerBin
Definition: RealFFTData.H:31
void load(const unsigned int i, const fftw_real val)
Definition: RealFFTData.C:198
int size
Specifies the size of the data array.
Definition: RealFFTData.H:29
int minPowerBin
Specifies the minimum and maximum power bins as used in the methods findMaxMinPowerBins and compPower...
Definition: RealFFTData.H:31
double totalPower
The total power (summed) of the power spectrum as used in the method compPowerSpec.
Definition: RealFFTData.H:35
~RealFFTData(void)
Deconstructor.
Definition: RealFFTData.C:68
int getHalfSize(void)
Returns the number of elements in the power spectrum array.
Definition: RealFFTData.H:50
int compPowerSpec()
This function computes the power spectrum and returns the max bin.
Definition: RealFFTData.C:117
fftw_real unload(const unsigned int i)
Definition: RealFFTData.C:203
int sqrtPowerSpec()
This function computes the square root of the power spectrum and returns the max bin.
Definition: RealFFTData.C:143
fftw_real * power_spectrum
Definition: RealFFTData.H:33
fftw_real unloadPS(const unsigned int i)
Definition: RealFFTData.C:209
fftw_real * out
Definition: RealFFTData.H:33
fftw_real * in
the input, output and power_spectrum arrays
Definition: RealFFTData.H:33
int limitHalfPowerSpec(double lim)
Limits the maximum to &#39;lim&#39; and returns the last fft bin with max.
Definition: RealFFTData.C:104
void dumpIn()
For debugging purposes, dump the in array to stdout.
Definition: RealFFTData.C:215
#define fftw_real
use double by default
Definition: FFTCommon.H:24
fftw_real findMaxIn(void)
Returns the maximum input variable.
Definition: RealFFTData.C:80
void dumpOut()
For debugging purposes, dump the out array to stdout.
Definition: RealFFTData.C:221
gtkIOStream: /tmp/gtkiostream/src/RealFFTData.C Source File
GTK+ IOStream  Beta