mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
mx::sigproc::averagePeriodogram< realT > Class Template Reference

template<typename realT>
class mx::sigproc::averagePeriodogram< realT >

Calculate the average periodogram of a time-series.

Implements the overlapped average periodogram, cf. pp 843 to 845 of [18]. This produces the variance normalized 1-sided PSD estimate of the periodogram.

Optionally includes a window (by default no window is used, which is equivalent to the rectangle window).

Can also be used to calculate the unaveraged non-overlapped periodogram of the entire time series.

Example:

typedef float realT;
mx::fftwEnvironment<realT> fftwEnv; //for efficient fft planning
std::vector<realT> ts = getTimeSeries(); //Some function to populate ts.
math::vectorMeanSub(ts); //The time series should be overall mean subtracted.
realT dt = 0.1; //The sampling of ts is 0.1 seconds.
averagePeriodogram<realT> avgPgram(2.0/dt, dt); //This sets the averaging length to 2 seconds (20 samples), with a 1 second overlap (set automatically)
avgPgram.win(window::hann); //Set the Hann window
std::vector<realT> pgram = avgPgram(ts); //Calculate the half-overlapped periodogram estimate for ts.
//Now output the periodogram estimate. This will give two columns, the first frequency, the second power at that frequency.
for(size_t n=0; n< avgPgram.size(); ++n)
{
std::cout << avgPgram[n] << " " << pgram[n] << "\n";
}
realT dt()
Get the sampling interval of the time-series.
void hann(realT *filt, int N)
The Hann Window.
void vectorMeanSub(valueT *vec, size_t sz)
Subtract the mean from a vector.

Definition at line 78 of file averagePeriodogram.hpp.

#include <sigproc/averagePeriodogram.hpp>

Public Member Functions

 averagePeriodogram (size_t avgLen)
 C'tor which sets up the optimum overlapped periodogram of the timeseries. More...
 
 averagePeriodogram (size_t avgLen, realT dt)
 C'tor which sets up the optimum overlapped periodogram of the timeseries and sets the sampling. More...
 
 averagePeriodogram (size_t avgLen, size_t olap, realT dt)
 C'tor setting up an arbitrary overlap. More...
 
 ~averagePeriodogram ()
 D'tor, frees all working memory. More...
 
void dt (realT ndt)
 Set the sampling interval of the time-series. More...
 
realT dt ()
 Get the sampling interval of the time-series. More...
 
realT df ()
 Get the frequency interval of the periodogram. More...
 
int resize (size_t avgLen)
 Resize the periodogram working memory, setting up the 1/2-overlapped optimum case. More...
 
int resize (size_t avgLen, size_t olap)
 Resize the periodogram working memory. More...
 
std::vector< realT > & win ()
 Get a reference to the window vector. More...
 
void win (void(*winFunc)(std::vector< realT > &))
 Set the window vector using a function. More...
 
void resizeWin (bool setRect=true)
 Resize the window and, unless not desired, initialize to the rectangular window by setting all 1s. More...
 
void operator() (realT *pgram, const realT *ts, size_t sz)
 Calculate the periodogram for a time-series. More...
 
void operator() (std::vector< realT > &pgram, const std::vector< realT > &ts)
 Calculate the periodogram for a time-series. More...
 
std::vector< realT > operator() (std::vector< realT > &ts)
 Calculate the periodogram for a time-series. More...
 
size_t size ()
 Return the size of the periodogram. More...
 
realT operator[] (size_t n)
 Get the frequency at a given point in the periodogram. More...
 

Protected Attributes

size_t m_size {0}
 The size of the periodogram vector, calculated as m_avgLen/2 + 1;. More...
 
int m_nOver {0}
 The number of overlapping segments. Calculated from m_avgLen and m_overlap;. More...
 
realT m_df {1}
 The frequency sampling. This is used only for normalization and frequency scale output. More...
 
Configuration Parameters
size_t m_avgLen {0}
 The number of samples in each periodgoram estimate. More...
 
size_t m_overlap {0}
 The number of samples by which to overlap. This should almost always be 0.5*m_avgLen. Set 0 for the non-overlapped case. More...
 
realT m_dt {1}
 The time sampling. Only used for normalization and calculation of the frequency scale. More...
 
std::vector< realT > m_win
 The window function. By default this is empty, which is equivalent to setting it to the rectangular window. More...
 

Constructor & Destructor Documentation

◆ averagePeriodogram() [1/3]

template<typename realT >
mx::sigproc::averagePeriodogram< realT >::averagePeriodogram ( size_t  avgLen)
explicit

C'tor which sets up the optimum overlapped periodogram of the timeseries.

Sets m_overlap = 0.5*m_avgLen. If you desire the non-overlapped case use the alternate constructor:

averagePeriogram p( avgLen, 0, dt);
Parameters
[in]avgLenThe length of averaging in samples.

Definition at line 244 of file averagePeriodogram.hpp.

◆ averagePeriodogram() [2/3]

template<typename realT >
mx::sigproc::averagePeriodogram< realT >::averagePeriodogram ( size_t  avgLen,
realT  dt 
)

C'tor which sets up the optimum overlapped periodogram of the timeseries and sets the sampling.

Sets m_overlap = 0.5*m_avgLen. If you desire the non-overlapped case use the alternate constructor:

averagePeriogram p( avgLen, 0, dt);
Parameters
[in]avgLenThe length of averaging in samples.
[in]dtthe sampling interval of the time-series

Definition at line 251 of file averagePeriodogram.hpp.

◆ averagePeriodogram() [3/3]

template<typename realT >
mx::sigproc::averagePeriodogram< realT >::averagePeriodogram ( size_t  avgLen,
size_t  olap,
realT  dt 
)

C'tor setting up an arbitrary overlap.

Set olap to 0 for the unoverlapped case.

Parameters
[in]avgLenThe number of samples in each periodgoram estimate.
[in]olapThe number of samples by which to overlap. This should almost always be 0.5*m_avgLen. Set 0 for the non-overlapped case.
[in]dtthe sampling interval of the time-series

Definition at line 260 of file averagePeriodogram.hpp.

◆ ~averagePeriodogram()

template<typename realT >
mx::sigproc::averagePeriodogram< realT >::~averagePeriodogram

D'tor, frees all working memory.

Definition at line 270 of file averagePeriodogram.hpp.

References mx::math::fft::fftw_free().

Member Function Documentation

◆ df()

template<typename realT >
realT mx::sigproc::averagePeriodogram< realT >::df

Get the frequency interval of the periodogram.

Returns
m_df

Definition at line 290 of file averagePeriodogram.hpp.

◆ dt() [1/2]

template<typename realT >
realT mx::sigproc::averagePeriodogram< realT >::dt

Get the sampling interval of the time-series.

Returns
m_dt

Definition at line 284 of file averagePeriodogram.hpp.

◆ dt() [2/2]

template<typename realT >
void mx::sigproc::averagePeriodogram< realT >::dt ( realT  ndt)

Set the sampling interval of the time-series.

This also sets the frequency scale of the output.

Definition at line 277 of file averagePeriodogram.hpp.

◆ operator()() [1/3]

template<typename realT >
void mx::sigproc::averagePeriodogram< realT >::operator() ( realT *  pgram,
const realT *  ts,
size_t  sz 
)

Calculate the periodogram for a time-series.

Parameters
[out]pgrama pre-allocated to size() array which will be populated with the periodogram of the time-series
[in]tsthe time-series
[in]szthe length of the time-series array

Definition at line 356 of file averagePeriodogram.hpp.

References mx::sigproc::psdVar1sided(), and mx::math::vectorVariance().

◆ operator()() [2/3]

template<typename realT >
void mx::sigproc::averagePeriodogram< realT >::operator() ( std::vector< realT > &  pgram,
const std::vector< realT > &  ts 
)

Calculate the periodogram for a time-series.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Parameters
[out]pgrama vector which will be allocated and populated with the periodogram of the time-series
[in]tsthe time-series

Definition at line 410 of file averagePeriodogram.hpp.

◆ operator()() [3/3]

template<typename realT >
std::vector< realT > mx::sigproc::averagePeriodogram< realT >::operator() ( std::vector< realT > &  ts)

Calculate the periodogram for a time-series.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Returns
the periodogram as a vector.
Parameters
[in]tsthe time-series

Definition at line 420 of file averagePeriodogram.hpp.

◆ operator[]()

template<typename realT >
realT mx::sigproc::averagePeriodogram< realT >::operator[] ( size_t  n)

Get the frequency at a given point in the periodogram.

Returns
the frequency at point n in the periodogram.
Parameters
[in]nthe point in the periodogram at which the frequency is desired

Definition at line 434 of file averagePeriodogram.hpp.

◆ resize() [1/2]

template<typename realT >
int mx::sigproc::averagePeriodogram< realT >::resize ( size_t  avgLen)

Resize the periodogram working memory, setting up the 1/2-overlapped optimum case.

This sets the overlap to 0.5*avgLen.

Also performs fft planning.

Returns
0 on success
-1 on error
Parameters
[in]avgLenThe number of samples in each periodgoram estimate.

Definition at line 297 of file averagePeriodogram.hpp.

◆ resize() [2/2]

template<typename realT >
int mx::sigproc::averagePeriodogram< realT >::resize ( size_t  avgLen,
size_t  olap 
)

Resize the periodogram working memory.

Also performs fft planning.

Returns
0 on success
-1 on error
Parameters
[in]avgLenThe number of samples in each periodgoram estimate.
[in]olapThe number of samples by which to overlap. This should almost always be 0.5*m_avgLen. Set 0 for the non-overlapped case.

Definition at line 303 of file averagePeriodogram.hpp.

References mx::math::fft::fftw_free().

◆ resizeWin()

template<typename realT >
void mx::sigproc::averagePeriodogram< realT >::resizeWin ( bool  setRect = true)

Resize the window and, unless not desired, initialize to the rectangular window by setting all 1s.

If setRect=false then the window is not initialized.

Parameters
[in]setRect[optional] if false, then the window is not initialized to 1

Definition at line 343 of file averagePeriodogram.hpp.

◆ size()

template<typename realT >
size_t mx::sigproc::averagePeriodogram< realT >::size

Return the size of the periodogram.

Returns
the size of the periodogram estimate for the current setup.

Definition at line 428 of file averagePeriodogram.hpp.

Referenced by mx::AO::analysis::speckleAmpPSD().

◆ win() [1/2]

template<typename realT >
std::vector< realT > & mx::sigproc::averagePeriodogram< realT >::win

Get a reference to the window vector.

This allows population of the window with any arbitrary function. You should call resizeWin() before using this to make sure that the vector has the correct length.

Returns
a reference to m_win.

Definition at line 330 of file averagePeriodogram.hpp.

◆ win() [2/2]

template<typename realT >
void mx::sigproc::averagePeriodogram< realT >::win ( void(*)(std::vector< realT > &)  winFunc)

Set the window vector using a function.

This will resize m_win, and then call the function with m_win as the argument. For example to use the hann window defined signal_windows1D:

avgPgram.win( window::hann );

which will set the Hann window.

Parameters
[in]winFuncpointer to a function which takes a pre-allocated vector and fills it in with a window function

Definition at line 336 of file averagePeriodogram.hpp.

Member Data Documentation

◆ m_avgLen

template<typename realT >
size_t mx::sigproc::averagePeriodogram< realT >::m_avgLen {0}
protected

The number of samples in each periodgoram estimate.

Definition at line 88 of file averagePeriodogram.hpp.

◆ m_df

template<typename realT >
realT mx::sigproc::averagePeriodogram< realT >::m_df {1}
protected

The frequency sampling. This is used only for normalization and frequency scale output.

Definition at line 101 of file averagePeriodogram.hpp.

◆ m_dt

template<typename realT >
realT mx::sigproc::averagePeriodogram< realT >::m_dt {1}
protected

The time sampling. Only used for normalization and calculation of the frequency scale.

Definition at line 91 of file averagePeriodogram.hpp.

◆ m_nOver

template<typename realT >
int mx::sigproc::averagePeriodogram< realT >::m_nOver {0}
protected

The number of overlapping segments. Calculated from m_avgLen and m_overlap;.

Definition at line 99 of file averagePeriodogram.hpp.

◆ m_overlap

template<typename realT >
size_t mx::sigproc::averagePeriodogram< realT >::m_overlap {0}
protected

The number of samples by which to overlap. This should almost always be 0.5*m_avgLen. Set 0 for the non-overlapped case.

Definition at line 89 of file averagePeriodogram.hpp.

◆ m_size

template<typename realT >
size_t mx::sigproc::averagePeriodogram< realT >::m_size {0}
protected

The size of the periodogram vector, calculated as m_avgLen/2 + 1;.

Definition at line 97 of file averagePeriodogram.hpp.

◆ m_win

template<typename realT >
std::vector<realT> mx::sigproc::averagePeriodogram< realT >::m_win
protected

The window function. By default this is empty, which is equivalent to setting it to the rectangular window.

Definition at line 93 of file averagePeriodogram.hpp.


The documentation for this class was generated from the following file: