TimeSeriesComponent.cpp
Go to the documentation of this file.00001 #include <algorithm>
00002 #include <functional>
00003 #include <boost/bind.hpp>
00004 #include <boost/numeric/conversion/cast.hpp>
00005 #include "TimeSeriesComponent.h"
00006 #include "FatalException.h"
00007 #ifdef HAVEGSL
00008 #include <gsl/gsl_spline.h>
00009 #endif
00010
00011 namespace gplib
00012 {
00013
00014 TimeSeriesComponent::TimeSeriesComponent()
00015 {
00016 samplerate = 0.0;
00017 }
00018
00019 TimeSeriesComponent::~TimeSeriesComponent()
00020 {
00021 }
00022
00023 TimeSeriesComponent::TimeSeriesComponent(const TimeSeriesComponent& source) :
00024 data(source.data), samplerate(source.samplerate), name(source.name),
00025 starttime(source.starttime)
00026 {
00027 }
00028
00029 void TimeSeriesComponent::ShiftStart(const int npts)
00030 {
00031
00032 boost::posix_time::time_duration time_shift =
00033 boost::posix_time::microseconds(boost::numeric_cast<unsigned int>(
00034 npts * 1000000 / samplerate));
00035 if (npts < 0)
00036 {
00037 if (static_cast<size_t> (abs(npts)) > data.size())
00038 throw FatalException("Trying to shift by too many points. ");
00039
00040 data.erase(data.begin(), data.begin() + abs(npts));
00041
00042 starttime = starttime + time_shift;
00043 }
00044 else
00045 {
00046 data.insert(data.begin(), npts, 0);
00047 starttime = starttime - time_shift;
00048 }
00049 }
00050
00051 void TimeSeriesComponent::ShiftEnd(const int npts)
00052 {
00053 if (npts < 0)
00054 {
00055 if (static_cast<size_t> (abs(npts)) > data.size())
00056 throw FatalException("Trying to cut too many points. ");
00057 data.erase(data.end() + npts, data.end());
00058 }
00059 else
00060 {
00061 data.insert(data.end(), npts, 0);
00062 }
00063 }
00064
00065 TimeSeriesComponent& TimeSeriesComponent::operator=(
00066 const TimeSeriesComponent& source)
00067 {
00068 if (this != &source)
00069 {
00070 this->samplerate = source.samplerate;
00071 this->name = source.name;
00072 this->starttime = source.starttime;
00073 this->data.assign(source.data.size(), 0);
00074 std::copy(source.data.begin(), source.data.end(),
00075 this->data.begin());
00076 }
00077 return *this;
00078 }
00079
00080
00081
00082 #ifdef HAVEGSL
00083 void TimeSeriesComponent::Resample(const double newdt)
00084 {
00085 const size_t oldlength = data.size();
00086 double *oldtime = new double[oldlength];
00087 const double dt = GetDt();
00088 double start = 0.0;
00089
00090 for (size_t i = 0; i < oldlength; ++i)
00091 {
00092 oldtime[i] = start + i * dt;
00093 }
00094
00095 gsl_interp_accel *acc = gsl_interp_accel_alloc();
00096 gsl_spline *spline = gsl_spline_alloc(gsl_interp_cspline, oldlength);
00097 gsl_spline_init(spline, oldtime, &data[0], oldlength);
00098
00099 const size_t newlength = boost::numeric_cast<size_t>(oldlength * (dt
00100 / newdt));
00101
00102 data.clear();
00103
00104 for (size_t i = 0; i < newlength; ++i)
00105 {
00106 data.push_back(gsl_spline_eval(spline, start + i * newdt, acc));
00107 }
00108 gsl_spline_free(spline);
00109 gsl_interp_accel_free(acc);
00110 samplerate = 1. / newdt;
00111
00112 }
00113 #endif //HAVEGSL
00114
00115 TimeSeriesComponent& TimeSeriesComponent::operator*=(const double factor)
00116 {
00117 std::transform(data.begin(), data.end(), data.begin(), boost::bind(
00118 std::multiplies<double>(), factor, _1));
00119 return *this;
00120 }
00121
00122 TimeSeriesComponent& TimeSeriesComponent::operator/=(const double numerator)
00123 {
00124 const double factor = 1. / numerator;
00125 operator*=(factor);
00126 return *this;
00127 }
00128
00129 TimeSeriesComponent& TimeSeriesComponent::operator+=(const double shift)
00130 {
00131 std::transform(data.begin(), data.end(), data.begin(), boost::bind(
00132 std::plus<double>(), shift, _1));
00133 return *this;
00134 }
00135
00136 TimeSeriesComponent& TimeSeriesComponent::operator+=(
00137 const TimeSeriesComponent &other)
00138 {
00139 std::transform(data.begin(), data.end(), other.data.begin(),
00140 data.begin(), std::plus<double>());
00141 return *this;
00142 }
00143 TimeSeriesComponent& TimeSeriesComponent::operator-=(const double shift)
00144 {
00145 operator+=(-shift);
00146 return *this;
00147 }
00148
00149 TimeSeriesComponent& TimeSeriesComponent::operator-=(
00150 const TimeSeriesComponent &other)
00151 {
00152 std::transform(data.begin(), data.end(), other.data.begin(),
00153 data.begin(), std::minus<double>());
00154 return *this;
00155 }
00156 }