TimeFrequency.h

Go to the documentation of this file.
00001 #ifndef TIMEFREQUENCY_H_
00002 #define TIMEFREQUENCY_H_
00003 #include <complex>
00004 #include "TsSpectrum.h"
00005 #include "statutils.h"
00006 #include "VecMat.h"
00007 #include "FatalException.h"
00008 
00009 namespace gplib
00010   {
00011 
00012     /** \addtogroup tstools Time series analysis methods */
00013     /* @{ */
00014 
00015     //! Calculate a sliding windowed fourier transform for a time series and store the results for each segment in a matrix
00016     /*! This function returns the time-frequency matrix for a real time series
00017      *  \param tsbegin iterator to the start of the input time series
00018      *  \param tsend iterator to the end of the input time series
00019      *  \param length of segments used for fourier transform in points, if the last segment is longer than the time-series it is discarded
00020      *  \param Function object for windowing the segments before fourier transform \see WFunc.h for a selection of provided function objects
00021      */
00022     template<typename InputIterator, typename WindowFunctype>
00023     gplib::cmat TimeFrequency(InputIterator tsbegin, InputIterator tsend,
00024         const size_t seglength, WindowFunctype WFunc)
00025       {
00026         const size_t insize = distance(tsbegin, tsend);
00027         if (insize < seglength)
00028           throw FatalException(
00029               "Time Series shorter than requested segment length");
00030         const unsigned int nsegments = insize / seglength; //we do integer division, so the last (not full) segment will be discarded
00031         const unsigned int speclength = seglength / 2 + 1;
00032         TsSpectrum SpecEst(false);
00033         gplib::cmat output(nsegments, speclength);
00034         typedef std::vector<
00035             typename std::iterator_traits<InputIterator>::value_type> tinvector;
00036 
00037         InputIterator currstart = tsbegin; //the start of the current segment
00038         InputIterator currend = tsbegin;
00039         advance(currend, seglength);
00040 
00041         std::vector<std::complex<double> > segspec(speclength);
00042         for (unsigned int i = 0; i < nsegments; ++i)
00043           {
00044             tinvector currseg(currstart, currend); //temporary copy of time series
00045             SubMean(currseg.begin(), currseg.end());
00046             for (typename tinvector::iterator it = currseg.begin(); it
00047                 != currseg.end(); ++it)
00048               {
00049                 //multiply each sample in the segement by the windowing function
00050                 *it *= WFunc(
00051                     static_cast<double> (distance(currseg.begin(), it))
00052                         / seglength); //by passing the relative position to the windowing function
00053               }
00054             SpecEst.CalcSpectrum(currseg.begin(), currseg.end(),
00055                 segspec.begin(), segspec.end());
00056             copy(segspec.begin(), segspec.end(), row(output, i).begin());
00057             advance(currstart, seglength); //shift the boundaries of current segment
00058             advance(currend, seglength);
00059           }
00060         return output;
00061       }
00062   /* @} */
00063   }
00064 #endif /*TIMEFREQUENCY_H_*/

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