TsSpectrum.cpp

Go to the documentation of this file.
00001 #include "TsSpectrum.h"
00002 
00003 using namespace std;
00004 
00005 namespace gplib
00006   {
00007 
00008     //The constructor copies the parameter value into MultiCalc and sets everything else to indicate pristine state
00009     TsSpectrum::TsSpectrum(bool WantMultiCalc)
00010       {
00011         MultiCalc = WantMultiCalc;
00012         ExistsPlanForward = false;
00013         ExistsPlanReverse = false;
00014         oldsize = 0;
00015         timedomain = NULL;
00016         freqdomain = NULL;
00017       }
00018 
00019     void TsSpectrum::DestroyMem()
00020       {
00021         if (ExistsPlanForward || ExistsPlanReverse) // if we did some calculations before
00022           {
00023             if (ExistsPlanForward)
00024               fftw_destroy_plan(p_forward); // we destroy the old plans
00025             if (ExistsPlanReverse)
00026               fftw_destroy_plan(p_reverse);
00027             fftw_free(timedomain); // and free the memory allocated
00028             fftw_free(freqdomain);
00029             ExistsPlanForward = false; // now there is no plan anymore, so we don't double deallocate
00030             ExistsPlanReverse = false;
00031           }
00032       }
00033 
00034     TsSpectrum::~TsSpectrum()
00035       {
00036         DestroyMem();
00037       }
00038 
00039     //The size for AssignMem is the size of the time series
00040     void TsSpectrum::AssignMem(const int size)
00041       {
00042         DestroyMem(); // destroy possible earlier instances
00043         timedomain = (double *) fftw_malloc(sizeof(double) * size); // reallocate with the required sizes
00044         freqdomain = (fftw_complex *) fftw_malloc(sizeof(fftw_complex) * (size
00045             / 2 + 1));
00046       }
00047 
00048     void TsSpectrum::Finish_Calculation(const int size)
00049       {
00050         oldsize = size; // The size of the (next) last calculation is the size of this calculation
00051       }
00052 
00053     void TsSpectrum::Prepare_Calculation(const int size)
00054       {
00055         if (size != oldsize) // if the size changed
00056           {
00057             AssignMem(size); //reassign memory
00058 
00059             if (MultiCalc) // generate a new plan
00060               {
00061                 p_reverse = fftw_plan_dft_c2r_1d(size, freqdomain, timedomain,
00062                     FFTW_MEASURE);
00063                 p_forward = fftw_plan_dft_r2c_1d(size, timedomain, freqdomain,
00064                     FFTW_MEASURE);
00065               }
00066             else
00067               {
00068                 p_reverse = fftw_plan_dft_c2r_1d(size, freqdomain, timedomain,
00069                     FFTW_ESTIMATE);
00070                 p_forward = fftw_plan_dft_r2c_1d(size, timedomain, freqdomain,
00071                     FFTW_ESTIMATE);
00072               }
00073             ExistsPlanReverse = true; // we will have to deallocate at some point
00074             ExistsPlanForward = true;
00075           }
00076       }
00077   }

Generated on Tue May 4 16:52:15 2010 for GPLIB++ by  doxygen 1.5.8