Marsyas
0.6.0-alpha
|
00001 #include "Ratio.h" 00002 00003 #include <cmath> 00004 #include <string> 00005 #include <algorithm> 00006 00007 using std::string; 00008 00009 namespace Marsyas { 00010 00011 Ratio::Ratio(std::string name): MarSystem("Ratio", name) 00012 { 00013 addControl("mrs_string/mode", string()); 00014 setControlState("mrs_string/mode", true); 00015 } 00016 00017 void Ratio::myUpdate(MarControlPtr) 00018 { 00019 const string & mode_str = getControl("mrs_string/mode")->to<string>(); 00020 if (mode_str == "log") 00021 m_mode = log; 00022 else if (mode_str == "log10") 00023 m_mode = log10; 00024 else 00025 m_mode = raw; 00026 00027 setControl("mrs_natural/onObservations", std::max(inObservations_ - 1, (mrs_natural) 1)); 00028 setControl("mrs_natural/onSamples", inSamples_); 00029 } 00030 00031 void Ratio::myProcess(realvec& in, realvec& out) 00032 { 00033 if (inObservations_ < 2) 00034 { 00035 double ratio; 00036 switch (m_mode) 00037 { 00038 case raw: 00039 ratio = 1.0; break; 00040 case log: 00041 case log10: 00042 ratio = 0.0; break; 00043 } 00044 00045 for(mrs_natural s = 0; s < inSamples_; ++s) 00046 { 00047 out(0,s) = ratio; 00048 } 00049 00050 return; 00051 } 00052 00053 switch (m_mode) 00054 { 00055 case raw: 00056 for(mrs_natural s = 0; s < inSamples_; ++s) 00057 { 00058 mrs_real reference = in(0,s); 00059 for(mrs_natural o = 1; o < inObservations_; ++o) 00060 out(o-1, s) = in(o,s) / reference; 00061 } 00062 break; 00063 case log: 00064 for(mrs_natural s = 0; s < inSamples_; ++s) 00065 { 00066 mrs_real reference = in(0,s); 00067 for(mrs_natural o = 1; o < inObservations_; ++o) 00068 out(o-1, s) = std::log( in(o,s) / reference ); 00069 } 00070 break; 00071 case log10: 00072 for(mrs_natural s = 0; s < inSamples_; ++s) 00073 { 00074 mrs_real reference = in(0,s); 00075 for(mrs_natural o = 1; o < inObservations_; ++o) 00076 out(o-1, s) = std::log10( in(o,s) / reference ); 00077 } 00078 break; 00079 } 00080 } 00081 00082 } // namespace Marsyas