00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "AimLocalMax.h"
00020
00021 using std::ostringstream;
00022 using namespace Marsyas;
00023
00024 AimLocalMax::AimLocalMax(mrs_string name):MarSystem("AimLocalMax",name)
00025 {
00026 is_initialized = false;
00027 initialized_israte = 0.0;
00028
00029 is_reset = false;
00030 reset_inobservations = -1;
00031
00032 addControls();
00033 }
00034
00035 AimLocalMax::AimLocalMax(const AimLocalMax& a) : MarSystem(a)
00036 {
00037
00038
00039
00040 ctrl_decay_time_ms_ = getctrl("mrs_real/decay_time_ms");
00041 ctrl_timeout_ms_ = getctrl("mrs_real/timeout_ms");
00042
00043 is_initialized = false;
00044 initialized_israte = 0.0;
00045
00046 is_reset = false;
00047 reset_inobservations = -1;
00048
00049 }
00050
00051
00052 AimLocalMax::~AimLocalMax()
00053 {
00054 }
00055
00056 MarSystem*
00057 AimLocalMax::clone() const
00058 {
00059 return new AimLocalMax(*this);
00060 }
00061
00062 void
00063 AimLocalMax::addControls()
00064 {
00065 addControl("mrs_real/decay_time_ms", 20.0 , ctrl_decay_time_ms_);
00066 addControl("mrs_real/timeout_ms", 3.0 , ctrl_timeout_ms_);
00067 }
00068
00069 void
00070 AimLocalMax::myUpdate(MarControlPtr sender)
00071 {
00072 (void)sender;
00073
00074 MRSDIAG("AimLocalMax.cpp - AimLocalMax:myUpdate");
00075 ctrl_onSamples_->setValue(ctrl_inSamples_, NOUPDATE);
00076
00077
00078
00079
00080
00081
00082
00083
00084 channel_count_ = ctrl_inObservations_->to<mrs_natural>() / 2;
00085 ctrl_onObservations_->setValue(channel_count_ * 3);
00086
00087 ctrl_osrate_->setValue(ctrl_israte_, NOUPDATE);
00088 ctrl_onObsNames_->setValue("AimLocalMax_" + ctrl_inObsNames_->to<mrs_string>() , NOUPDATE);
00089
00090
00091
00092
00093 if (initialized_israte != ctrl_israte_->to<mrs_real>()) {
00094 is_initialized = false;
00095 }
00096
00097 if (!is_initialized) {
00098 InitializeInternal();
00099 is_initialized = true;
00100 initialized_israte = ctrl_israte_->to<mrs_real>();
00101 }
00102
00103
00104
00105
00106 if (reset_inobservations != ctrl_inObservations_->to<mrs_natural>()) {
00107 is_reset = false;
00108 }
00109
00110 if (!is_reset) {
00111 ResetInternal();
00112 is_reset = true;
00113 reset_inobservations = ctrl_inObservations_->to<mrs_natural>();
00114 }
00115
00116 }
00117
00118
00119 bool
00120 AimLocalMax::InitializeInternal() {
00121 strobe_timeout_samples_ = floor(ctrl_timeout_ms_->to<mrs_real>() * ctrl_israte_->to<mrs_real>() / 1000.0);
00122 strobe_decay_samples_ = floor(ctrl_decay_time_ms_->to<mrs_real>() * ctrl_israte_->to<mrs_real>() / 1000.0);
00123 return true;
00124
00125 }
00126
00127 void
00128 AimLocalMax::ResetInternal() {
00129 threshold_.clear();
00130 threshold_.resize(channel_count_, 0.0);
00131
00132 decay_constant_.clear();
00133 decay_constant_.resize(channel_count_, 1.0);
00134
00135 prev_sample_.clear();
00136 prev_sample_.resize(channel_count_, 10000.0);
00137 curr_sample_.clear();
00138 curr_sample_.resize(channel_count_, 5000.0);
00139 next_sample_.clear();
00140 next_sample_.resize(channel_count_, 0.0);
00141 }
00142
00143
00144
00145 void
00146 AimLocalMax::myProcess(realvec& in, realvec& out)
00147 {
00148 mrs_natural o,t;
00149
00150
00151
00152 strobe_count_.clear();
00153 strobe_count_.resize(channel_count_, 0.0);
00154 last_strobe_.clear();
00155 last_strobe_.resize(channel_count_, 0.0);
00156
00157
00158
00159
00160
00161
00162
00163 prev_sample_.clear();
00164 prev_sample_.resize(channel_count_, 10000.0);
00165 curr_sample_.clear();
00166 curr_sample_.resize(channel_count_, 5000.0);
00167 next_sample_.clear();
00168 next_sample_.resize(channel_count_, 0.0);
00169
00170
00171 mrs_natural skip_channels = channel_count_ + channel_count_;
00172
00173 for (t = 0; t < ctrl_inSamples_->to<mrs_natural>(); t++) {
00174 for (o = 0; o < channel_count_; o++) {
00175
00176 out(o + skip_channels,t) = 0.0;
00177
00178
00179 prev_sample_[o] = curr_sample_[o];
00180 curr_sample_[o] = next_sample_[o];
00181 next_sample_[o] = in(o, t);
00182
00183
00184
00185 if (curr_sample_[o] >= threshold_[o]) {
00186 threshold_[o] = curr_sample_[o];
00187 decay_constant_[o] = threshold_[o] / strobe_decay_samples_;
00188
00189
00190 if (prev_sample_[o] < curr_sample_[o]
00191 && next_sample_[o] < curr_sample_[o]) {
00192
00193
00194
00195
00196
00197 if (strobe_count_[o] > 0) {
00198
00199
00200
00201 int samples_since_last = (t - 1) - last_strobe_[o];
00202 if (samples_since_last > strobe_timeout_samples_) {
00203 out(o + skip_channels, t-1) = 1.0;
00204 strobe_count_[o]++;
00205 last_strobe_[o] = t;
00206 }
00207 } else {
00208 out(o + skip_channels, t-1) = 1.0;
00209 strobe_count_[o]++;
00210 last_strobe_[o] = t;
00211 }
00212 }
00213 }
00214
00215
00216 if (threshold_[o] > decay_constant_[o])
00217 threshold_[o] -= decay_constant_[o];
00218 else
00219 threshold_[o] = 0.0;
00220 }
00221 }
00222
00223
00224
00225 for (t = 0; t < ctrl_inSamples_->to<mrs_natural>(); t++) {
00226 for (o = 0; o < skip_channels; o++) {
00227 out(o,t) = in(o,t);
00228 }
00229 }
00230 }