00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <iostream>
00019
00020 #include "common.h"
00021 #include "PeakResidual.h"
00022
00023
00024
00025 using std::ostringstream;
00026 using namespace Marsyas;
00027
00028 PeakResidual::PeakResidual(mrs_string name):MarSystem("PeakResidual", name)
00029 {
00030
00031 addControls();
00032 }
00033
00034 PeakResidual::PeakResidual(const PeakResidual& a) : MarSystem(a)
00035 {
00036 ctrl_SNR_ = getctrl("mrs_real/SNR");
00037 }
00038
00039 PeakResidual::~PeakResidual()
00040 {
00041 outFile_.close ();
00042 }
00043
00044 MarSystem*
00045 PeakResidual::clone() const
00046 {
00047 return new PeakResidual(*this);
00048 }
00049
00050 void
00051 PeakResidual::addControls()
00052 {
00053 addctrl("mrs_real/SNR", 0.0, ctrl_SNR_);
00054 addctrl("mrs_bool/snrInDb", true);
00055 addctrl("mrs_string/outFilePath", EMPTYSTRING);
00056 }
00057
00058 void
00059 PeakResidual::myUpdate(MarControlPtr sender)
00060 {
00061 mrs_natural o;
00062 ctrl_onSamples_->setValue(ctrl_inSamples_, NOUPDATE);
00063 ctrl_onObservations_->setValue(ctrl_inObservations_->to<mrs_natural>()/2, NOUPDATE);
00064 ctrl_osrate_->setValue(ctrl_israte_, NOUPDATE);
00065
00066 ostringstream oss;
00067 mrs_string inObsNames = ctrl_inObsNames_->to<mrs_string>();
00068 mrs_string inObsName;
00069 mrs_string temp;
00070 for(o=0; o < ctrl_onObservations_->to<mrs_natural>(); o++)
00071 {
00072 inObsName = inObsNames.substr(0, inObsNames.find(","));
00073 temp = inObsNames.substr(inObsNames.find(",")+1, inObsNames.length());
00074 inObsNames = temp;
00075 oss << inObsName << "_residual,";
00076 }
00077 ctrl_onObsNames_->setValue(oss.str(), NOUPDATE);
00078
00079 outFile_.close ();
00080 outFile_.clear ();
00081
00082 mrs_string fileName = getControl ("mrs_string/outFilePath")->to<mrs_string> ();
00083 if (fileName != EMPTYSTRING)
00084 outFile_.open(fileName.c_str ());
00085
00086 }
00087
00088 void
00089 PeakResidual::myProcess(realvec& in, realvec& out)
00090 {
00091 mrs_bool inDb = getControl ("mrs_bool/snrInDb")->to<mrs_bool>();
00092 mrs_natural t,o;
00093 mrs_real snr = (inDb)? -80.0 : 0.;
00094 mrs_real originalPower;
00095 mrs_real synthPower;
00096 mrs_real diffPower;
00097
00098 #ifdef MARSYAS_MATLAB
00099 #ifdef MTLB_DBG_LOG
00100 MATLAB_PUT(in, "in");
00101 MATLAB_EVAL("figure(31),subplot(211),plot(in(1,:)),axis([1 512 -1 1]),grid on,subplot(212),plot(in(2,:)),axis([1 512 -1 1]),grid on");
00102 #endif
00103 #endif
00104
00105 for (o=0; o < inObservations_/2; o++)
00106 {
00107 originalPower=0;
00108 synthPower=0;
00109 diffPower=0;
00110 for (t = 0; t < inSamples_; t++)
00111 {
00112 out(o,t) = in(o,t)-in(o+1, t);
00113 synthPower += in(o, t)*in(o, t);
00114 diffPower += out(o, t)*out(o, t);
00115 originalPower += in(o+1, t)*in(o+1, t);
00116 }
00117
00118
00119
00120
00121
00122 if(synthPower > .001 && originalPower > .01)
00123 {
00124 snr = (originalPower)/(diffPower+MINREAL);
00125 if (inDb)
00126 snr = 10*log10(snr);
00127 }
00128 }
00129
00130 ctrl_SNR_->setValue(snr);
00131
00132 if (outFile_.good ())
00133 {
00134 outFile_ << snr << std::endl;
00135
00136 }
00137 }
00138