00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "AimHCL2.h"
00020
00021 using std::ostringstream;
00022 using namespace Marsyas;
00023
00024 AimHCL2::AimHCL2(mrs_string name):MarSystem("AimHCL2",name)
00025 {
00026 is_initialized = false;
00027 initialized_lowpass_cutoff = 0;
00028
00029 is_reset = false;
00030 reseted_inobservations = 0;
00031 reseted_lowpass_order = 0;
00032
00033 addControls();
00034 }
00035
00036
00037 AimHCL2::~AimHCL2()
00038 {
00039 }
00040
00041
00042 MarSystem*
00043 AimHCL2::clone() const
00044 {
00045 return new AimHCL2(*this);
00046 }
00047
00048 void
00049 AimHCL2::addControls()
00050 {
00051 addControl("mrs_bool/do_lowpass", true , ctrl_do_lowpass_);
00052 addControl("mrs_bool/do_log", false , ctrl_do_log_);
00053 addControl("mrs_real/lowpass_cutoff", 1200.0 , ctrl_lowpass_cutoff_);
00054 addControl("mrs_natural/lowpass_order", 2 , ctrl_lowpass_order_);
00055 }
00056
00057 void
00058 AimHCL2::myUpdate(MarControlPtr sender)
00059 {
00060 (void) sender;
00061 MRSDIAG("AimHCL2.cpp - AimHCL2:myUpdate");
00062 ctrl_onObservations_->setValue(ctrl_inObservations_->to<mrs_natural>(), NOUPDATE);
00063 ctrl_onSamples_->setValue(ctrl_inSamples_->to<mrs_natural>(), NOUPDATE);
00064 ctrl_osrate_->setValue(ctrl_israte_->to<mrs_real>());
00065 ctrl_onObsNames_->setValue("AimHCL2_" + ctrl_inObsNames_->to<mrs_string>() , NOUPDATE);
00066
00067
00068
00069
00070 if (initialized_lowpass_cutoff != ctrl_lowpass_cutoff_->to<mrs_real>()) {
00071 is_initialized = false;
00072 }
00073
00074 if (!is_initialized) {
00075 InitializeInternal();
00076 is_initialized = true;
00077 initialized_lowpass_cutoff = ctrl_lowpass_cutoff_->to<mrs_real>();
00078 }
00079
00080
00081
00082
00083 if (reseted_inobservations != ctrl_inObservations_->to<mrs_natural>() ||
00084 reseted_lowpass_order != ctrl_lowpass_order_->to<mrs_natural>()) {
00085 is_reset = false;
00086 }
00087
00088 if (!is_reset) {
00089 ResetInternal();
00090 is_reset = true;
00091 reseted_inobservations = ctrl_inObservations_->to<mrs_natural>();
00092 reseted_lowpass_order = ctrl_lowpass_order_->to<mrs_natural>();
00093 }
00094
00095 }
00096
00097
00098 bool
00099 AimHCL2::InitializeInternal() {
00100 time_constant_ = 1.0 / (2.0 * PI * ctrl_lowpass_cutoff_->to<mrs_real>());
00101 return true;
00102 }
00103
00104 void
00105 AimHCL2::ResetInternal() {
00106 xn_ = 0.0;
00107 yn_ = 0.0;
00108 yns_.clear();
00109 yns_.resize(ctrl_inObservations_->to<mrs_natural>());
00110 mrs_natural _lowpass_order = ctrl_lowpass_order_->to<mrs_natural>();
00111 for (int c = 0; c < ctrl_inObservations_->to<mrs_natural>(); ++c) {
00112 yns_[c].resize(_lowpass_order, 0.0);
00113 }
00114 }
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124 void
00125 AimHCL2::myProcess(realvec& in, realvec& out)
00126 {
00127 mrs_natural o,t;
00128 mrs_real _israte = ctrl_israte_->to<mrs_real>();
00129 mrs_natural _inObservations = ctrl_inObservations_->to<mrs_natural>();
00130 mrs_natural _inSamples = ctrl_inSamples_->to<mrs_natural>();
00131 mrs_natural _onSamples = ctrl_onSamples_->to<mrs_natural>();
00132 mrs_natural _lowpass_order = ctrl_lowpass_order_->to<mrs_natural>();
00133 mrs_bool _do_lowpass = ctrl_do_lowpass_->to<mrs_bool>();
00134 mrs_bool _do_log = ctrl_do_log_->to<mrs_bool>();
00135
00136 mrs_natural _num_channels = _inObservations;
00137 double b = exp(-1.0 / (_israte * time_constant_));
00138 double gain = 1.0 / (1.0 - b);
00139
00140 for (o = 0; o < _num_channels; ++o) {
00141 for (t = 0; t < _inSamples; ++t) {
00142
00143 if (in(o,t) < 0.0) {
00144
00145 out(o, t) = 0.0;
00146 } else {
00147 double s = in(o,t);
00148 if (_do_log) {
00149 s *= pow(2.0, 15);
00150 if (s < 1.0) s = 1.0;
00151 s = 20.0 * log10(s);
00152 }
00153
00154 out(o, t) = s;
00155 }
00156 }
00157 if (_do_lowpass) {
00158
00159 for (int j = 0; j < _lowpass_order; j++) {
00160 for (int k = 0; k < _onSamples; ++k) {
00161 xn_ = out(o,k);
00162 yn_ = xn_ + b * yns_[o][j];
00163 yns_[o][j] = yn_;
00164
00165
00166 out(o, k) = yn_ / gain;
00167 }
00168 }
00169 }
00170 }
00171
00172
00173
00174 }