gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
Real2DFFTData.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/FFTCommon.H"
19 #include "fft/Real2DFFTData.H"
20 
21 #include <string.h>
22 
24 Real2DFFTData(int r, int c){
25  x=r; y=c;
26  mem=NULL;
27  out=NULL;
28  in=power=NULL;
30  printf("power size=%d\n",x*(c/2+1));
31 
32  if (!(mem=new fftw_real[x*y+x*(c/2+1)+2*x*(c/2+1)+x+(c/2+1)])){
33  printf("Real2DFFTData: malloc 2 fail\n");
34  memDeInit();
35  } else {
36  in=&mem[0];
37  power=&mem[x*y];
38  out=(fftw_complex*)(power+x*(c/2+1));
39  xSum=(fftw_real*)out+2*x*(c/2+1);
40  ySum=xSum+x;
41  }
42 
43  if (!(timeXSum=new fftw_real[x])){
44  printf("Real2DFFTData: malloc 3 fail\n");
45  memDeInit();
46  }
47  if (!(realXSum=new fftw_real[x])){
48  printf("Real2DFFTData: malloc 3a fail\n");
49  memDeInit();
50  }
51  if (!(imagXSum=new fftw_real[x])){
52  printf("Real2DFFTData: malloc 3b fail\n");
53  memDeInit();
54  }
55 
58 }
59 
62  memDeInit();
63 }
64 
65 void Real2DFFTData::
66 memDeInit(void){
67  if (mem) delete [] mem;
68  mem=in=power=NULL;
69  out=NULL;
70  if (timeXSum) delete [] timeXSum;
71  timeXSum=NULL;
72  if (realXSum) delete [] realXSum;
73  realXSum=NULL;
74  if (imagXSum) delete [] imagXSum;
75  imagXSum=NULL;
76  printf("Real2DFFTData: DeInit Out\n");
77 }
78 
79 void Real2DFFTData::
80 reScale(void){
81  double factor=1.0/((double)x*(double)y);
82  for (int i=0;i<x*(y/2+1);i++){
83  c_re(out[i])*=factor; c_im(out[i])*=factor;
84  }
85 }
86 
87 void Real2DFFTData::
89  maxPower=totalPower=0.0; minPower=9999999.9;
90  double temp;
91  int maxIndexX, maxIndexY;
92  int minIndexX, minIndexY;
93  for (int i=0; i < x; i++){ // The x dimension
94  int index=i*(y/2+1);
95  for (int j=0;j<y/2+1;j++){ // The y dimension
96  temp=c_re(out[index+j])*c_re(out[index+j])+c_im(out[index+j])*c_im(out[index+j]);
97  totalPower += (power[index+j]=temp);
98  // std::cout<<temp<<'\t'<<maxPower<<'\t'<<minPower<<std::endl;
99  //std::cout<<temp<<std::endl;
100  if (temp>maxPower){
101  maxPower=temp; maxIndexX=i; maxIndexY=j;
102  }
103  if (temp<minPower){
104  minPower=temp; minIndexX=i; minIndexY=j;
105  }
106  }
107  }
108  //std::cout <<"Real2DFFTData: compPowerSpec: max indexes : (x, y) "<<maxIndexX<<'\t'<<maxIndexY<<std::endl;
109  //std::cout <<"Real2DFFTData: compPowerSpec: min indexes : (x, y) "<<minIndexX<<'\t'<<minIndexY<<std::endl;
110 }
111 
112 int Real2DFFTData::
114  int maxPowerBin=0;
115  double max=-MAXDOUBLE;
116  for (int i=0; i < x; i++){ // The x dimension
117  int index=i*(y/2+1);
118  for (int j=0;j<y/2+1;j++) // The y dimension
119  if ((power[index+j]=sqrt(power[index+j]))>max){
120  maxPowerBin=index+j;
121  max=(power[maxPowerBin]);
122  }
123  }
124  return maxPowerBin;
125 }
126 
127 
128 // #include <fstream>
129 void Real2DFFTData::
131  int maxIndexX, maxIndexY;
132  int minIndexX, minIndexY;
133  totalPower=0.0;
134  maxPower=-999999999.9, minPower=9999999.9;
135  double temp;
136  // std::ofstream ofs("2Dfft.dat");
137  for (int i=0; i < x; i++){ // The x dimension
138  int index=i*(y/2+1);
139  for (int j=0;j<y/2+1;j++){ // The y dimension
140  temp=c_re(out[index+j])*c_re(out[index+j])+c_im(out[index+j])*c_im(out[index+j]);
141  temp=10.0*log10(temp);
142  // ofs<<temp<<'\t';
143  totalPower += (power[index+j]=temp);
144  // std::cout<<temp<<'\t'<<maxPower<<'\t'<<minPower<<std::endl;
145  //std::cout<<temp<<std::endl;
146  if (temp>maxPower){
147  maxPower=temp; maxIndexX=i; maxIndexY=j;
148  }
149  if (temp<minPower){
150  minPower=temp; minIndexX=i; minIndexY=j;
151  }
152  }
153  // ofs <<std::endl;
154  }
155  // ofs.close();
156  //std::cout <<"Real2DFFTData: compPowerSpec: max indexes : (x, y) "<<maxIndexX<<'\t'<<maxIndexY<<std::endl;
157  //std::cout <<"Real2DFFTData: compPowerSpec: min indexes : (x, y) "<<minIndexX<<'\t'<<minIndexY<<std::endl;
158 }
159 
160 void Real2DFFTData::
161 findYSum(int start, int stop){
162  memset(ySum, 0, (y/2+1)*sizeof(fftw_real));
163  for (int i=start; i < stop; i++){ // The x dimension
164  int index=i*(y/2+1);
165  for (int j=0;j<y/2+1;j++){ // The y dimension
166  ySum[j]+=power[index+j];
167  }
168  }
169  for (int j=0;j<y/2+1;j++){
170  ySum[j]/=x;
171  }
172 }
173 
174 void Real2DFFTData::
175 findYMax(void){
176  ySumMin=99999999999.9;
177  ySumMax=-99999999999.9;
178  for (int j=0;j<y/2+1;j++){
179  if (ySum[j]< ySumMin) ySumMin=ySum[j];
180  if (ySum[j]> ySumMax && j>2){
181  ySumMax=ySum[j];
182  maxYSumIndex=j;
183  }
184  }
185 }
186 
187 void Real2DFFTData::
189  memset(timeXSum, 0, x*sizeof(fftw_real));
190  for (int i=0; i < x; i++){ // The x dimension
191  int index=i*y;
192  for (int j=0;j<y;j++){ // The y dimension
193  timeXSum[i]+=in[index+j];
194  // std::cout<<in[index+j]<<std::endl;
195  }
196  timeXSum[i]/=y;
197  }
198 }
199 
200 void Real2DFFTData::
202  memset(realXSum, 0, x*sizeof(fftw_real));
203  memset(imagXSum, 0, x*sizeof(fftw_real));
204  for (int i=0; i < x; i++){ // The x dimension
205  int index=i*(y/2+1);
206  for (int j=0;j<y/2+1;j++){ // The y dimension
207  realXSum[i]+=c_re(out[index+j]);
208  imagXSum[i]+=c_im(out[index+j]);
209  }
210  realXSum[i]/=y/2+1;
211  imagXSum[i]/=y/2+1;
212  }
213 }
214 
215 void Real2DFFTData::
217  memset(xSum, 0, x*sizeof(fftw_real));
218  memset(ySum, 0, (y/2+1)*sizeof(fftw_real));
219  xSumMin=ySumMin=99999999999.9;
220  xSumMax=ySumMax=-99999999999.9;
221 
222  for (int i=0; i < x; i++){ // The x dimension
223  int index=i*(y/2+1);
224  for (int j=0;j<y/2+1;j++){ // The y dimension
225  // if (i<x/7 || i>x*6/7)
226  // std::cout<<power[index+j]<<'\t';
227  ySum[j]+=power[index+j];
228  xSum[i]+=power[index+j];
229  }
230  xSum[i]/=y/2+1;
231  if (xSum[i]< xSumMin) xSumMin=xSum[i];
232  if (xSum[i]> xSumMax){
233  xSumMax=xSum[i];
234  maxXSumIndex=i;
235  }
236  }
237  for (int j=0;j<y/2+1;j++){
238  ySum[j]/=x;
239  if (ySum[j]< ySumMin) ySumMin=ySum[j];
240  if (ySum[j]> ySumMax){
241  ySumMax=ySum[j];
242  maxYSumIndex=j;
243  }
244  }
245 }
246 
248  memset(in, 0, x*2*(y/2+1)*sizeof(fftw_real));
249 }
250 
252  memset(out, 0, x*(y/2+1)*sizeof(fftw_complex));
253 }
fftw_real * ySum
Definition: Real2DFFTData.H:37
int x
x=row y=column
Definition: Real2DFFTData.H:26
int maxXSumIndex
Row (x) and Column (y) max sum indexes.
Definition: Real2DFFTData.H:48
fftw_complex * out
The output data.
Definition: Real2DFFTData.H:35
int sqrtPowerSpec()
This function computes the square root of the power spectrum and returns the max bin.
void complexSpecAverage()
Updates realXSum and imagXSum.
void findYMax(void)
Finds the y-max for the ySum array, updates ySumMin, ySumMax, maxYSumIndex.
double xSumMin
The minimum/maximum row (x) and column (y) sums.
Definition: Real2DFFTData.H:46
void powerSpecAverage()
Real2DFFTData(int r, int c)
Constructor with all memory to be allocated internally.
Definition: Real2DFFTData.C:24
fftw_real * power
Definition: Real2DFFTData.H:33
fftw_real * timeXSum
A sum across the input time signal.
Definition: Real2DFFTData.H:39
void clearInput(void)
Zeros the in array.
void compPowerSpec()
This function computes the power spectrum and updates the totalPower, maxPower and minPower...
Definition: Real2DFFTData.C:88
double totalPower
The total power in the power spectrum, the maximum and minimum powers too.
Definition: Real2DFFTData.H:44
#define c_re(c)
The real part of the complex number.
Definition: FFTCommon.H:26
void findYSum(int start, int stop)
Finds the y-sum between columns start and stop.
#define c_im(c)
The imaginary part of the complex number.
Definition: FFTCommon.H:27
fftw_real * mem
The total memory used by this class.
Definition: Real2DFFTData.H:28
void reScale(void)
Scales the output down by the number of elements.
Definition: Real2DFFTData.C:80
fftw_real * xSum
Arrays which sum across rows (x) and columns (y)
Definition: Real2DFFTData.H:37
void timeSpecAverage()
Updates timeXSum.
fftw_real * in
The input data and power spectrum.
Definition: Real2DFFTData.H:33
~Real2DFFTData()
Deconstructor.
Definition: Real2DFFTData.C:61
fftw_real * imagXSum
Definition: Real2DFFTData.H:41
void memDeInit(void)
Free the memory.
Definition: Real2DFFTData.C:66
void compLogPowerSpec()
Finds 10*log10(power spectrum) and updates the totalPower, maxPower and minPower. ...
void clearOutput(void)
Zeros the out awway.
#define fftw_real
use double by default
Definition: FFTCommon.H:24
fftw_real * realXSum
Power spectral sums across rows (x) and columns (y)
Definition: Real2DFFTData.H:41
gtkIOStream: /tmp/gtkiostream/src/Real2DFFTData.C Source File
GTK+ IOStream  Beta