GPLIB++
TsSpectrum.cpp
Go to the documentation of this file.
1 #include "TsSpectrum.h"
2 
3 using namespace std;
4 
5 namespace gplib
6  {
7 
8  //The constructor copies the parameter value into MultiCalc and sets everything else to indicate pristine state
9  TsSpectrum::TsSpectrum(bool WantMultiCalc)
10  {
11  MultiCalc = WantMultiCalc;
12  ExistsPlanForward = false;
13  ExistsPlanReverse = false;
14  oldsize = 0;
15  timedomain = NULL;
16  freqdomain = NULL;
17  }
18 
19  void TsSpectrum::DestroyMem()
20  {
21  if (ExistsPlanForward || ExistsPlanReverse) // if we did some calculations before
22  {
23  if (ExistsPlanForward)
24  fftw_destroy_plan(p_forward); // we destroy the old plans
25  if (ExistsPlanReverse)
26  fftw_destroy_plan(p_reverse);
27  fftw_free(timedomain); // and free the memory allocated
28  fftw_free(freqdomain);
29  ExistsPlanForward = false; // now there is no plan anymore, so we don't double deallocate
30  ExistsPlanReverse = false;
31  }
32  }
33 
34  TsSpectrum::~TsSpectrum()
35  {
36  DestroyMem();
37  }
38 
39  //The size for AssignMem is the size of the time series
40  void TsSpectrum::AssignMem(const int size)
41  {
42  DestroyMem(); // destroy possible earlier instances
43  timedomain = (double *) fftw_malloc(sizeof(double) * size); // reallocate with the required sizes
44  freqdomain = (fftw_complex *) fftw_malloc(sizeof(fftw_complex) * (size
45  / 2 + 1));
46  }
47 
48  void TsSpectrum::Finish_Calculation(const int size)
49  {
50  oldsize = size; // The size of the (next) last calculation is the size of this calculation
51  }
52 
53  void TsSpectrum::Prepare_Calculation(const int size)
54  {
55  if (size != oldsize) // if the size changed
56  {
57  AssignMem(size); //reassign memory
58 
59  if (MultiCalc) // generate a new plan
60  {
61  p_reverse = fftw_plan_dft_c2r_1d(size, freqdomain, timedomain,
62  FFTW_MEASURE);
63  p_forward = fftw_plan_dft_r2c_1d(size, timedomain, freqdomain,
64  FFTW_MEASURE);
65  }
66  else
67  {
68  p_reverse = fftw_plan_dft_c2r_1d(size, freqdomain, timedomain,
69  FFTW_ESTIMATE);
70  p_forward = fftw_plan_dft_r2c_1d(size, timedomain, freqdomain,
71  FFTW_ESTIMATE);
72  }
73  ExistsPlanReverse = true; // we will have to deallocate at some point
74  ExistsPlanForward = true;
75  }
76  }
77  }
const int size
Definition: perftest.cpp:14