00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "MaxArgMax.h"
00020
00021 using std::cout;
00022 using std::endl;
00023
00024 using std::ostringstream;
00025 using std::max;
00026 using std::min;
00027
00028 using namespace Marsyas;
00029
00030 MaxArgMax::MaxArgMax(mrs_string name):MarSystem("MaxArgMax",name)
00031 {
00032 addControls();
00033 }
00034
00035 MaxArgMax::~MaxArgMax()
00036 {
00037 }
00038
00039 MarSystem*
00040 MaxArgMax::clone() const
00041 {
00042 return new MaxArgMax(*this);
00043 }
00044
00045 void
00046 MaxArgMax::addControls()
00047 {
00048 addctrl("mrs_natural/nMaximums", (mrs_natural)1);
00049 setctrlState("mrs_natural/nMaximums", true);
00050
00051
00052 addctrl("mrs_natural/fanoutLength", (mrs_natural)1);
00053 setctrlState("mrs_natural/fanoutLength", true);
00054
00055 addctrl("mrs_natural/interpolation", (mrs_natural)0);
00056 }
00057
00058 void
00059 MaxArgMax::myUpdate(MarControlPtr sender)
00060 {
00061 (void) sender;
00062
00063 mrs_natural k = getctrl("mrs_natural/nMaximums")->to<mrs_natural>();
00064 mrs_natural fanoutLength_ = getctrl("mrs_natural/fanoutLength")->to<mrs_natural>();
00065
00066
00067 mrs_natural size = 2 * max(k, fanoutLength_);
00068 setctrl("mrs_natural/onSamples", size);
00069 setctrl("mrs_natural/onObservations", getctrl("mrs_natural/inObservations"));
00070 setctrl("mrs_real/osrate", getctrl("mrs_real/israte"));
00071
00072
00073 }
00074
00075 void quadraticInterpolation(mrs_real *ix, mrs_real *iy, realvec& data)
00076 {
00077
00078 mrs_natural index = (mrs_natural) *ix;
00079 mrs_real d = (data(index-1)-data(index+1))/(2*(-2*data(index)+data(index-1)+data(index+1)));
00080 *ix += d;
00081 *iy -= d*(data(index-1)-data(index+1))/4;
00082
00083 }
00084
00085
00086 void
00087 MaxArgMax::myProcess(realvec& in, realvec& out)
00088 {
00089 mrs_natural t,o;
00090 out.setval(MINREAL);
00091 mrs_natural k = getctrl("mrs_natural/nMaximums")->to<mrs_natural>();
00092
00093 mrs_natural interpolationMode = getctrl("mrs_natural/interpolation")->to<mrs_natural>();
00094
00095 for (o=0; o < inObservations_; o++)
00096 {
00097 for (t=0; t < inSamples_; t++)
00098 {
00099 mrs_real newmax = in(o,t);
00100 mrs_real newmax_i = t;
00101
00102
00103
00104
00105 for (ki=0; ki < k; ++ki)
00106 {
00107 if (newmax > out(o, 2*ki))
00108 {
00109 mrs_real oldmax = out(o, 2*ki);
00110 mrs_real oldmax_i = out(o,2*ki+1);
00111 out(o,2*ki) = newmax;
00112 out(o,2*ki+1) = newmax_i;
00113 newmax = oldmax;
00114 newmax_i = oldmax_i;
00115 }
00116 }
00117 }
00118 if(interpolationMode)
00119 for (ki=0; ki < k; ++ki)
00120 {
00121 mrs_real ix = out(o,2*ki+1), iy = out(o,2*ki);
00122
00123
00124 if ((ix != 0.0) && (iy != 0.0))
00125 quadraticInterpolation(&ix, &iy, in);
00126 out(o,2*ki) = iy;
00127 out(o,2*ki+1) = ix;
00128 }
00129 }
00130
00131
00132
00133
00134
00135 }