Commit 259e90d1 authored by thomas.forbriger's avatar thomas.forbriger Committed by thomas.forbriger
Browse files

other filters work

This is a legacy commit from before 2015-03-01.
It may be incomplete as well as inconsistent.
See COPYING.legacy and README.history for details.


SVN Path:     http://gpitrsvn.gpi.uni-karlsruhe.de/repos/TFSoftware/trunk
SVN Revision: 1818
SVN UUID:     67feda4a-a26e-11df-9d6e-31afc202ad0c
parent a0a6285d
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* ---------------------------------------------------------------------------- * ----------------------------------------------------------------------------
* *
* $Id: filter.cc,v 1.6 2005-07-05 11:26:38 tforb Exp $ * $Id: filter.cc,v 1.7 2005-07-05 15:48:58 tforb Exp $
* \author Thomas Forbriger * \author Thomas Forbriger
* \date 05/07/2005 * \date 05/07/2005
* *
...@@ -19,19 +19,118 @@ ...@@ -19,19 +19,118 @@
#define TF_FILTER_CC_VERSION \ #define TF_FILTER_CC_VERSION \
"TF_FILTER_CC V1.0 " "TF_FILTER_CC V1.0 "
#define TF_FILTER_CC_CVSID \ #define TF_FILTER_CC_CVSID \
"$Id: filter.cc,v 1.6 2005-07-05 11:26:38 tforb Exp $" "$Id: filter.cc,v 1.7 2005-07-05 15:48:58 tforb Exp $"
#include <cmath>
#include <tsxx/filter.h> #include <tsxx/filter.h>
#include <tsxx/filtre.h> #include <aff/functions/avg.h>
#include <tsxx/filavg.h> #include <aff/seriesoperators.h>
#include <aff/subarray.h>
namespace ts { namespace ts {
namespace filter { namespace filter {
typedef aff::Tsubscript Tindex;
typedef aff::Tsize Tsize;
typedef Ttimeseries::Tvalue Tvalue;
typedef Ttimeseries::Tseries Tseries;
//! remove trend //! remove trend
Ttimeseries Trend::operator()(const Ttimeseries& s) const Ttimeseries RemoveTrend::operator()(const Ttimeseries& s) const
{ lpb(s, s.header.dt, Mt0, Mo); return s; } {
/*
* The trend function (index l) is
*
* f(l)= a + b * l
*
* We minimize
*
* \sum_l ( x(l) - a - b * l )**2
*
* where x(l) is the time series.
*
* The least squares condition is satisfied for a and b that satisfy
*
* a * N + b * sum_l l = sum_l x(l)
* a * sum_l l + b * sum_l l**2 = sum_l l * x(l)
*
* N = number of samples
*
* in case the summation starts at index 1:
*
* sum_l l = 0.5 * N * ( N + 1 )
* sum_l l**2 = N * ( N + 1 ) * ( 2 * N + 1 ) / 6
*/
// shift first index to 1 (to make values given above applicable)
Tseries x=s;
x.shift(1-x.f());
Tsize n= (Mn<1) ? x.size() : Mn;
n= (n < x.size()) ? n : x.size();
double m11=n;
double m12=0.5*n*(n+1);
double m21=m12;
double m22=n*(n+1)*(2*n+1)/6.;
double det=m11*m22-m12*m21;
double L1=0, L2=0;
for (Tindex l=x.f(); l<=Tindex(n); ++l)
{
L1 += x(l);
L2 += l*x(l);
}
double a=(L1*m22-L2*m12)/det;
double b=(-L1*m21+L2*m11)/det;
for (Tindex l=x.f(); l<=x.l(); ++l)
{ x(l) -= (a+l*b); }
return s;
}
//! remove average
Ttimeseries RemoveAverage::operator()(const Ttimeseries& s) const
{
Tsize n= (Mn<1) ? s.size() : Mn;
n= (n < s.size()) ? n : s.size();
Tindex f=s.f();
Tindex l=s.f()+n-1;
s -= aff::func::avg<Tseries>(aff::subarray<Tseries>(s)(f,l));
return s;
}
//! hanning taper
Ttimeseries HanningTaper::operator()(const Ttimeseries& s) const
{
double p=double(2.*M_PI/s.size());
for (Tindex i=s.f(); i<=s.l(); ++i)
{ s(i) *= M_SQRT1_2*(1.-cos(p*(i-s.f()))); }
return s;
}
//! scale sample values
Ttimeseries Scale::operator()(const Ttimeseries& s) const
{ s *= Mv; return s; }
//! add offset to sample values
Ttimeseries Add::operator()(const Ttimeseries& s) const
{ s += Mv; return s; }
//! force signal to baseline
Ttimeseries ForceToBase::operator()(const Ttimeseries& s) const
{
Tindex s1=s.f();
int n1= (Mn1 < 1) ? 1 : Mn1;
Tindex e1=s1+n1-1;
Tindex m1=(s1+e1)/2;
int n2= (Mn2 < 1) ? Mn1 : Mn2;
Tindex e2= (Mne < 1) ? s.l() : Mne;
Tindex s2= e2-n2+1;
Tindex m2=(s2+e2)/2;
Tvalue avg1=aff::func::avg<Tseries>(aff::subarray<Tseries>(s)(s1,e1));
Tvalue avg2=aff::func::avg<Tseries>(aff::subarray<Tseries>(s)(s2,e2));
double a=(avg2-avg1)/double(m2-m1);
for (Tindex i=s.f(); i<=s.l(); ++i)
{ s(i) -= (a*double(i-m1)+avg1); }
return s;
}
} // namespace filter } // namespace filter
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* ---------------------------------------------------------------------------- * ----------------------------------------------------------------------------
* *
* $Id: filter.h,v 1.8 2005-07-05 11:26:38 tforb Exp $ * $Id: filter.h,v 1.9 2005-07-05 15:48:58 tforb Exp $
* \author Thomas Forbriger * \author Thomas Forbriger
* \date 05/07/2005 * \date 05/07/2005
* *
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
#define TF_FILTER_H_VERSION \ #define TF_FILTER_H_VERSION \
"TF_FILTER_H V1.0 " "TF_FILTER_H V1.0 "
#define TF_FILTER_H_CVSID \ #define TF_FILTER_H_CVSID \
"$Id: filter.h,v 1.8 2005-07-05 11:26:38 tforb Exp $" "$Id: filter.h,v 1.9 2005-07-05 15:48:58 tforb Exp $"
#include<tsxx/filterbase.h> #include<tsxx/filterbase.h>
...@@ -31,17 +31,89 @@ namespace ts { ...@@ -31,17 +31,89 @@ namespace ts {
namespace filter { namespace filter {
//! remove trend /*! remove trend
class Trend: public ts::filter::BasicFilter { *
* We calculate the linear trend over the first \p n samples by a linear
* regression.
* This trend is removed from the whole series.
*
* \param n number of samples to calculate average from
* (will be set to n=number of samples, if n=0 is passed)
*/
class RemoveTrend: public ts::filter::BasicFilter {
public: public:
RemoveTrend(const int& n): Mn(n) { }
Ttimeseries operator()(const Ttimeseries& s) const; Ttimeseries operator()(const Ttimeseries& s) const;
}; // class Trend private:
int Mn;
}; // class RemoveTrend
//! remove average /*! remove average
class Average: public ts::filter::BasicFilter { *
* Calculate average over first \p n samples and remove this value from
* the whole time series.
*
* \param n number of samples to calculate average from
* (will be set to n=number of samples, if n=0 is passed)
*/
class RemoveAverage: public ts::filter::BasicFilter {
public: public:
RemoveAverage(const int& n): Mn(n) { }
Ttimeseries operator()(const Ttimeseries& s) const; Ttimeseries operator()(const Ttimeseries& s) const;
}; // class Average private:
int Mn;
}; // class RemoveAverage
//! hanning taper
class HanningTaper: public ts::filter::BasicFilter {
public:
Ttimeseries operator()(const Ttimeseries& s) const;
}; // class HanningTaper
//! scale with factor
class Scale: public ts::filter::BasicFilter {
public:
Scale(const double& v): Mv(v) { }
Ttimeseries operator()(const Ttimeseries& s) const;
private:
double Mv;
}; // class Scale
//! add a constant
class Add: public ts::filter::BasicFilter {
public:
Add(const double& v): Mv(v) { }
Ttimeseries operator()(const Ttimeseries& s) const;
private:
double Mv;
}; // class Add
/*! force signal to a baseline
*
* The filter removes a linear trend from the time series.
* After this operation the average in the index ranges [i1,i2] and
* [i3,i4] will vanish.
* The index range limits are
* - i1 = index of first sample
* - i2 = i1 + \p n1 - 1
* - i3 = i4 - \p n2 + 1
* - i4 = \p ne
*
* \param n1 define size of first range
* (will be set to n1=1 if n1=0 is passed)
* \param n2 define size of second range
* (will be set to n2=n1 if n2=0 is passed)
* \param ne define end of second range
* (will be set to n2=index of last sample, if n2=0 is passed)
*/
class ForceToBase: public ts::filter::BasicFilter {
public:
ForceToBase(const int& n1, const int& n2, const int& ne):
Mn1(n1), Mn2(n2), Mne(ne) { }
Ttimeseries operator()(const Ttimeseries& s) const;
private:
int Mn1, Mn2, Mne;
}; // class ForceToBase
} // namespace filter } // namespace filter
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment