00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "ResampleSinc.h"
00020 #include "common.h"
00021
00022 using namespace std;
00023 using namespace Marsyas;
00024
00034 ResampleSinc::ResampleSinc(mrs_string name):MarSystem("ResampleSinc", name)
00035 {
00036
00037
00038
00039
00040
00041
00042
00043 addControls();
00044 }
00045
00046 ResampleSinc::ResampleSinc(const ResampleSinc& a) : MarSystem(a)
00047 {
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 ctrl_offStart_ = getctrl("mrs_real/offStart");
00063 ctrl_offEnd_ = getctrl("mrs_real/offEnd");
00064 ctrl_windowedMode_ = getctrl("mrs_bool/windowedMode");
00065 ctrl_samplingRateAdjustmentMode_ = getctrl("mrs_bool/samplingRateAdjustmentMode");
00066 ctrl_stretch_ = getctrl("mrs_real/stretch");
00067
00068 }
00069
00070 ResampleSinc::~ResampleSinc()
00071 {
00072
00073 }
00074
00075 MarSystem*
00076 ResampleSinc::clone() const
00077 {
00078 return new ResampleSinc(*this);
00079 }
00080
00081 void
00082 ResampleSinc::addControls()
00083 {
00084
00085 addctrl("mrs_real/offStart", 0.0, ctrl_offStart_);
00086 addctrl("mrs_real/offEnd", 0.0, ctrl_offEnd_);
00087 addctrl("mrs_bool/samplingRateAdjustmentMode", (mrs_bool)true , ctrl_samplingRateAdjustmentMode_);
00088 addctrl("mrs_bool/windowedMode", (mrs_bool)false , ctrl_windowedMode_);
00089 addctrl("mrs_real/stretch", 1.0 , ctrl_stretch_);
00090 setctrlState("mrs_real/stretch", true);
00091 setctrlState("mrs_bool/samplingRateAdjustmentMode",(mrs_bool)true);
00092
00093 }
00094
00095 void
00096 ResampleSinc::myUpdate(MarControlPtr sender)
00097 {
00098 MarSystem::myUpdate(sender);
00099
00100
00101 mrs_real alpha = ctrl_stretch_->to<mrs_real>();
00102
00103 ctrl_onSamples_->setValue((mrs_natural) (alpha * ctrl_inSamples_->to<mrs_natural>()), NOUPDATE);
00104
00105 ctrl_onObservations_->setValue(ctrl_inObservations_->to<mrs_natural>());
00106 if (!(ctrl_samplingRateAdjustmentMode_->to<mrs_bool>()))
00107 {
00108 alpha=1.0;
00109 }
00110
00111 ctrl_osrate_->setValue(ctrl_israte_->to<mrs_real>()*alpha);
00112 mrs_string inObsNames = ctrl_inObsNames_->to<mrs_string>();
00113
00114 ctrl_onObsNames_->setValue(obsNamesAddPrefix(inObsNames, "ResampleSinc_"), NOUPDATE);
00115
00116 }
00117
00118 mrs_real
00119 ResampleSinc::sinc(mrs_real t)
00120 {
00121 mrs_real ret=0.0;
00122 if (t==0)
00123 ret=1.0;
00124 else
00125 ret=sin(t*PI)/(t*PI);
00126 return ret;
00127 }
00128
00129 mrs_real
00130 ResampleSinc::window(mrs_real position)
00131 {
00132 mrs_realvec y;
00133 y.create(11);
00134
00135
00136 y(0)=1;
00137 y(1)=1.393725584134;
00138 y(2)=1.749980639738;
00139 y(3)=2.033757714070;
00140 y(4)=2.21650903826;
00141 y(5)=2.279585302336;
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162 mrs_real divisor=2.279585302336;
00163
00164 for (mrs_natural i=0;i<y.getSize();++i)
00165
00166 if (i<=y.getSize()/2)
00167 y(i)=y(i)/divisor;
00168 else
00169 y(i)=y(y.getSize()-i-1);
00170
00171 if ((position <-5) | (position > 5))
00172 return 0;
00173
00174 mrs_natural index=0;
00175 while (index+1<5+position)
00176 {
00177 index=index+1;
00178 }
00179 mrs_real difference=5+position-index;
00180 return (1-difference)*y(index)+difference*y(index+1);
00181 }
00182
00183
00184 void
00185 ResampleSinc::myProcess(realvec& in, realvec& out)
00186 {
00187
00188 for (mrs_natural o=0;o<inObservations_;o++)
00189 {
00190
00191 mrs_bool windowedMode=ctrl_windowedMode_->to<mrs_bool>();
00192 mrs_natural samplesout=getctrl("mrs_natural/onSamples")->to<mrs_natural>();
00193
00194
00195 mrs_real offStart=ctrl_offStart_->to<mrs_real>();
00196 mrs_real offEnd=ctrl_offEnd_->to<mrs_real>();
00197
00198 mrs_real ratio=(inSamples_-1-offStart-offEnd)/(mrs_real)(samplesout-1);
00199 mrs_realvec arrx;
00200 arrx.create(samplesout);
00201 mrs_realvec arr;
00202 arr.create(samplesout);
00203 mrs_natural count=0;
00204 for(mrs_natural i=0;i<samplesout;++i)
00205 {
00206 arrx(count)=offStart+i*ratio;
00207 count=count+1;
00208 }
00209
00210 mrs_natural winlength=5;
00211 for (mrs_natural ansinks=0;ansinks<inSamples_;ansinks++)
00212 {
00213 for (int i=0;i<samplesout;++i)
00214 {
00215 if (abs(arrx(i)-ansinks)<winlength)
00216 {
00217 if (windowedMode)
00218 {
00219 arr(i)=arr(i)+in(o,ansinks)*sinc(arrx(i)-ansinks)*window(arrx(i)-ansinks);
00220 }
00221 else
00222 {
00223 arr(i)=arr(i)+in(o,ansinks)*sinc(arrx(i)-ansinks);
00224 }
00225 }
00226 }
00227
00228 }
00229
00230 for (int i=0;i<samplesout;++i)
00231 {
00232 out(o,i)=arr(i);
00233 }
00234
00235 }
00236
00237 }