00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "RunningAutocorrelation.h"
00020
00021
00022 #include <algorithm>
00023
00024 using namespace std;
00025 using namespace Marsyas;
00026
00027 RunningAutocorrelation::RunningAutocorrelation(mrs_string name) :
00028 MarSystem("RunningAutocorrelation", name)
00029 {
00031 addControls();
00032 }
00033
00034 RunningAutocorrelation::RunningAutocorrelation(const RunningAutocorrelation& a) :
00035 MarSystem(a)
00036 {
00039 ctrl_maxLag_ = getctrl("mrs_natural/maxLag");
00040 ctrl_normalize_ = getctrl("mrs_bool/normalize");
00041 ctrl_doNotNormalizeForLag0_ = getctrl("mrs_bool/doNotNormalizeForLag0");
00042 ctrl_clear_ = getctrl("mrs_bool/clear");
00043 ctrl_unfoldToObservations_ = getctrl("mrs_bool/unfoldToObservations");
00044 }
00045
00046 RunningAutocorrelation::~RunningAutocorrelation()
00047 {
00048 }
00049
00050 MarSystem*
00051 RunningAutocorrelation::clone() const
00052 {
00053 return new RunningAutocorrelation(*this);
00054 }
00055
00056 void RunningAutocorrelation::addControls()
00057 {
00059 addctrl("mrs_natural/maxLag", 15, ctrl_maxLag_);
00060 setctrlState("mrs_natural/maxLag", true);
00061 addctrl("mrs_bool/normalize", false, ctrl_normalize_);
00062 setctrlState("mrs_bool/normalize", true);
00063 addctrl("mrs_bool/doNotNormalizeForLag0", false,
00064 ctrl_doNotNormalizeForLag0_);
00065 setctrlState("mrs_bool/doNotNormalizeForLag0", true);
00066 addctrl("mrs_bool/clear", false, ctrl_clear_);
00067 setctrlState("mrs_bool/clear", true);
00068 addctrl("mrs_bool/unfoldToObservations", false, ctrl_unfoldToObservations_);
00069 setctrlState("mrs_bool/unfoldToObservations", true);
00070 }
00071
00075 mrs_string prefixObservationNamesWithAutocorrelationUnfoldingPrefixes_(
00076 mrs_string inObservationNames, mrs_bool normalize,
00077 mrs_bool doNotNormalizeForLag0, mrs_natural maxLag)
00078 {
00079 vector<mrs_string> inObsNames = obsNamesSplit(inObservationNames);
00080 mrs_string onObsNames("");
00081 for (vector<mrs_string>::iterator it = inObsNames.begin(); it
00082 != inObsNames.end(); it++)
00083 {
00084 for (int lag = 0; lag <= maxLag; lag++)
00085 {
00086 ostringstream oss;
00087 if (normalize && !(doNotNormalizeForLag0 && lag == 0))
00088 {
00089 oss << "Normalized";
00090 }
00091 oss << "Autocorr" << lag << "_" << (*it) << ",";
00092 onObsNames += oss.str();
00093 }
00094
00095 }
00096 return onObsNames;
00097
00098 }
00099
00100 void RunningAutocorrelation::myUpdate(MarControlPtr sender)
00101 {
00102 MRSDIAG("RunningAutocorrelation.cpp - RunningAutocorrelation:myUpdate");
00103
00104
00105 maxLag_ = ctrl_maxLag_->to<mrs_natural> ();
00106 if (maxLag_ < 0)
00107 {
00108 MRSERR("maxLag should be greater than zero.");
00109
00110 maxLag_ = 0;
00111 }
00112 normalize_ = ctrl_normalize_->to<mrs_bool> ();
00113 doNotNormalizeForLag0_ = ctrl_doNotNormalizeForLag0_->to<mrs_bool> ();
00114 unfoldToObservations_ = ctrl_unfoldToObservations_->to<mrs_bool> ();
00115
00116
00117 mrs_natural onObservations = unfoldToObservations_ ? inObservations_
00118 * (maxLag_ + 1) : inObservations_;
00119 mrs_natural onSamples = unfoldToObservations_ ? 1 : maxLag_ + 1;
00120 ctrl_onSamples_->setValue(onSamples, NOUPDATE);
00121 ctrl_onObservations_->setValue(onObservations, NOUPDATE);
00122 ctrl_osrate_->setValue(ctrl_israte_, NOUPDATE);
00123
00124 mrs_string onObsNames = ctrl_inObsNames_->to<mrs_string> ();
00125 if (unfoldToObservations_)
00126 {
00127 onObsNames
00128 = prefixObservationNamesWithAutocorrelationUnfoldingPrefixes_(
00129 onObsNames, normalize_, doNotNormalizeForLag0_, maxLag_);
00130 }
00131 ctrl_onObsNames_->setValue(onObsNames, NOUPDATE);
00132
00133
00134 this->acBuffer_.stretch(inObservations_, maxLag_ + 1);
00135 this->acBuffer_.setval(0.0);
00136 this->memory_.stretch(inObservations_, maxLag_);
00137 this->memory_.setval(0.0);
00138
00139
00140 ctrl_clear_->setValue(false, NOUPDATE);
00141 }
00142
00143 void RunningAutocorrelation::myProcess(realvec& in, realvec& out)
00144 {
00146 for (mrs_natural i = 0; i < inObservations_; ++i)
00147 {
00148
00149 for (mrs_natural lag = 0; lag <= maxLag_; lag++)
00150 {
00151
00152 for (mrs_natural n = 0; n < min(lag, inSamples_); n++)
00153 {
00154 acBuffer_(i, lag) += in(i, n) * memory_(i, maxLag_ - lag + n);
00155 }
00156
00157 for (mrs_natural n = lag; n < inSamples_; n++)
00158 {
00159 acBuffer_(i, lag) += in(i, n) * in(i, n - lag);
00160 }
00161 }
00162
00163
00164 mrs_natural u = (mrs_natural) unfoldToObservations_;
00165 mrs_natural o_base = unfoldToObservations_ ? i * (maxLag_ + 1) : i;
00166 for (mrs_natural lag = 0; lag <= maxLag_; lag++)
00167 {
00168 out(o_base + u * lag, (1 - u) * lag) = acBuffer_(i, lag);
00169
00170 }
00171
00172 if (normalize_ && acBuffer_(i, 0) > 0.0)
00173 {
00174 mrs_natural lag = doNotNormalizeForLag0_ ? 1 : 0;
00175 for (; lag <= maxLag_; lag++)
00176 {
00177 out(o_base + u * lag, (1 - u) * lag) /= acBuffer_(i, 0);
00178 }
00179 }
00180
00181
00182
00183 for (mrs_natural m = 0; m < maxLag_ - inSamples_; m++)
00184 {
00185 memory_(i, m) = memory_(i, m + inSamples_);
00186 }
00187
00188 for (mrs_natural m = 1; m <= min(maxLag_, inSamples_); m++)
00189 {
00190 memory_(i, maxLag_ - m) = in(i, inSamples_ - m);
00191 }
00192 }
00193 }