gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
ComplexFFTData.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 #include "fft/ComplexFFTData.H"
18 #include <stdlib.h>
19 
21 ComplexFFTData(int sz) {
22  size=sz;
23  in = out = NULL;
24  power_spectrum = NULL;
25 
26  //in = new fftw_complex[size];
27  //out = new fftw_complex[size];
28  //power_spectrum = new fftw_real[size];
29  in = (fftw_complex*)fftw_malloc(size*sizeof(fftw_complex));
30  out = (fftw_complex*)fftw_malloc(size*sizeof(fftw_complex));
31  power_spectrum = (fftw_real*)fftw_malloc(size*sizeof(fftw_real));
32  if (!in || !out || !power_spectrum) {
33  printf("Could not allocate enough mem for a ComplexFFT\n");
34  //if (in) delete [] in;
35  //if (out) delete [] out;
36  //if (power_spectrum) delete [] power_spectrum;
37  if (in) fftw_free(in);
38  in=NULL;
39  if (out) fftw_free(out);
40  out=NULL;
41  if (power_spectrum) fftw_free(power_spectrum);
42  power_spectrum=NULL;
43  exit(-1);
44  }
45  totalPower = 0.0;
46 }
47 
49  //if (in) delete [] in;
50  //if (out) delete [] out;
51  //if (power_spectrum) delete [] power_spectrum;
52  if (in) fftw_free(in);
53  in=NULL;
54  if (out) fftw_free(out);
55  out=NULL;
56  if (power_spectrum) fftw_free(power_spectrum);
57  power_spectrum=NULL;
58 }
59 
61  int bin;
62  totalPower = 0.0;
63  double min=MAXDOUBLE;
64  double max = power_spectrum[bin=0] = c_re(out[0])*c_re(out[0])+c_im(out[0])*c_im(out[0]); // DC component
65  for (int k = 1; k < getSize(); ++k) {
66  if ((power_spectrum[k] = c_re(out[k])*c_re(out[k]) + c_im(out[k])*c_im(out[k]))>max) {
67  max=power_spectrum[bin=k];
68  }
69  if (power_spectrum[k]<min) min=power_spectrum[minPowerBin=k];
71  }
72  /* if (getSize() % 2 == 0){ // N is even
73  power_spectrum[getSize()/2] = out[getSize()/2]*out[getSize()/2]; // Nyquist freq.
74  if (power_spectrum[getSize()/2]>max)
75  max=power_spectrum[bin=getSize()/2];
76  totalPower += power_spectrum[getSize()/2];
77  }*/
78  return bin;
79 }
80 
82  double min=MAXDOUBLE;
83  double max=-MAXDOUBLE;
84  for (int k = 0; k < getSize(); ++k) { /* (k < N/2 rounded up) */
85  if ((power_spectrum[k]=sqrt(power_spectrum[k]))>max)
87  if (power_spectrum[k]<min) min=power_spectrum[minPowerBin=k];
88  }
89  return maxPowerBin;
90 }
int sqrtPowerSpec()
This function computes the square root of the power spectrum and returns the max bin.
fftw_complex * out
fftw_real * power_spectrum
the power_spectrum array
int compPowerSpec()
This function computes the power spectrum and returns the max bin.
fftw_complex * in
the input and output arrays
int getSize()
Returns the number of elements in the input and output arrays.
int size
Specifies the size of the data array.
#define c_re(c)
The real part of the complex number.
Definition: FFTCommon.H:26
~ComplexFFTData(void)
Deconstructor.
ComplexFFTData(int sz)
Constructor with all memory to be allocated internally.
#define c_im(c)
The imaginary part of the complex number.
Definition: FFTCommon.H:27
double totalPower
The total power (summed) of the power spectrum as used in the method compPowerSpec.
int minPowerBin
Specifies the minimum and maximum power bins as used in the methods findMaxMinPowerBins and compPower...
#define fftw_real
use double by default
Definition: FFTCommon.H:24
gtkIOStream: /tmp/gtkiostream/src/ComplexFFTData.C Source File
GTK+ IOStream  Beta