29 #ifndef averagePeriodogram_hpp
30 #define averagePeriodogram_hpp
77 template<
typename realT>
104 math::fft::fftT< realT, std::complex<realT>, 1, 0> m_fft;
106 realT * m_tsWork {
nullptr};
107 size_t m_tsWorkSize {0};
109 std::complex<realT> * m_fftWork {
nullptr};
110 size_t m_fftWorkSize {0};
168 int resize(
size_t avgLen );
176 int resize(
size_t avgLen,
186 std::vector<realT> &
win();
196 void win(
void(*winFunc)(std::vector<realT> &) );
218 const std::vector<realT> & ts
226 std::vector<realT>
operator()(std::vector<realT> & ts );
243 template<
typename realT>
246 resize(avgLen, 0.5*avgLen);
250 template<
typename realT>
255 resize(avgLen, 0.5*avgLen);
259 template<
typename realT>
265 resize(avgLen, olap);
269 template<
typename realT>
276 template<
typename realT>
280 m_df = 1.0/(m_avgLen*m_dt);
283 template<
typename realT>
289 template<
typename realT>
296 template<
typename realT>
299 return resize(avgLen, 0.5*avgLen);
302 template<
typename realT>
309 m_size = m_avgLen/2 + 1;
311 m_df = 1.0/(m_avgLen*m_dt);
315 m_nOver = (m_avgLen-m_overlap);
317 m_fft.plan(m_avgLen, MXFFT_FORWARD,
false);
320 m_tsWork = math::fft::fftw_malloc<realT>( m_avgLen );
324 m_fftWork = math::fft::fftw_malloc<std::complex<realT>>( (m_avgLen/2 + 1) );
329 template<
typename realT>
335 template<
typename realT>
342 template<
typename realT>
347 m_win.resize(m_avgLen,
static_cast<realT
>(1));
351 m_win.resize(m_avgLen);
355 template<
typename realT>
361 if( m_win.size() > 0 && m_win.size() != m_avgLen )
363 std::cerr <<
"averagePeriodogram: Window size not correct.\n";
366 int Navg = sz/m_nOver;
368 while(Navg*m_nOver + m_avgLen > sz) --Navg;
370 if(Navg < 1) Navg = 1;
374 for(
int i=0;i<Navg;++i)
376 if(m_win.size() == m_avgLen)
378 for(
size_t j=0;j<m_avgLen;++j)
380 m_tsWork[j] = ts[i*m_nOver + j] * m_win[j];
385 for(
size_t j=0;j<m_avgLen;++j)
387 m_tsWork[j] = ts[i*m_nOver + j];
391 m_fft( m_fftWork, m_tsWork);
393 for(
size_t j=0;j<m_size;++j) pgram[j] += norm(m_fftWork[j]);
405 for(
size_t j =0; j< m_size; ++j) pgram[j] *= tsVar/pgramVar;
409 template<
typename realT>
411 const std::vector<realT> & ts
414 pgram.resize(m_size);
415 operator()( pgram.data(), ts.data(), ts.size());
419 template<
typename realT>
422 std::vector<realT> pgram;
423 operator()(pgram, ts);
427 template<
typename realT>
433 template<
typename realT>
Calculate the average periodogram of a time-series.
~averagePeriodogram()
D'tor, frees all working memory.
int resize(size_t avgLen)
Resize the periodogram working memory, setting up the 1/2-overlapped optimum case.
realT df()
Get the frequency interval of the periodogram.
realT m_dt
The time sampling. Only used for normalization and calculation of the frequency scale.
size_t m_avgLen
The number of samples in each periodgoram estimate.
size_t m_size
The size of the periodogram vector, calculated as m_avgLen/2 + 1;.
void operator()(realT *pgram, const realT *ts, size_t sz)
Calculate the periodogram for a time-series.
size_t m_overlap
The number of samples by which to overlap. This should almost always be 0.5*m_avgLen....
std::vector< realT > m_win
The window function. By default this is empty, which is equivalent to setting it to the rectangular w...
void resizeWin(bool setRect=true)
Resize the window and, unless not desired, initialize to the rectangular window by setting all 1s.
averagePeriodogram(size_t avgLen)
C'tor which sets up the optimum overlapped periodogram of the timeseries.
size_t size()
Return the size of the periodogram.
realT operator[](size_t n)
Get the frequency at a given point in the periodogram.
int m_nOver
The number of overlapping segments. Calculated from m_avgLen and m_overlap;.
realT dt()
Get the sampling interval of the time-series.
std::vector< realT > & win()
Get a reference to the window vector.
realT m_df
The frequency sampling. This is used only for normalization and frequency scale output.
void fftw_free(realT *p)
Call to fftw_free.
realT psdVar1sided(realT df, const realT *PSD, size_t sz, realT half=0.5)
Calculate the variance of a 1-D, 1-sided PSD.
valueT vectorVariance(const valueT *vec, size_t sz, valueT mean)
Calculate the variance of a vector relative to a supplied mean value.
Tools for working with PSDs.