00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "SpectralTransformations.h"
00020
00021 using std::ostringstream;
00022 using namespace Marsyas;
00023
00024 SpectralTransformations::SpectralTransformations(mrs_string name):MarSystem("SpectralTransformations", name)
00025 {
00026 addControls();
00027 }
00028
00029 SpectralTransformations::SpectralTransformations(const SpectralTransformations& a) : MarSystem(a)
00030 {
00031 ctrl_gain_ = getctrl("mrs_real/gain");
00032 ctrl_mode_ = getctrl("mrs_string/mode");
00033 }
00034
00035
00036 SpectralTransformations::~SpectralTransformations()
00037 {
00038 }
00039
00040 MarSystem*
00041 SpectralTransformations::clone() const
00042 {
00043 return new SpectralTransformations(*this);
00044 }
00045
00046 void
00047 SpectralTransformations::addControls()
00048 {
00049 addctrl("mrs_real/gain", 1.0, ctrl_gain_);
00050 addctrl("mrs_string/mode", "singlebin", ctrl_mode_);
00051 }
00052
00053 void
00054 SpectralTransformations::myUpdate(MarControlPtr sender)
00055 {
00056 MRSDIAG("SpectralTransformations.cpp - SpectralTransformations:myUpdate");
00057
00058
00059
00060 N2_ = ctrl_inObservations_->to<mrs_natural>()/2 + 1;
00061
00062
00063 MarSystem::myUpdate(sender);
00064 }
00065
00066
00067 void
00068 SpectralTransformations::phaseRandomize(realvec& in, realvec& out)
00069 {
00070 mrs_natural t,o;
00071 for(t=0; t < inSamples_; ++t)
00072 for (o=0; o < N2_; o++)
00073 {
00074 if (o==0)
00075 {
00076 re_ = in(0,t);
00077 im_ = 0.0;
00078 }
00079 else if (o == N2_-1)
00080 {
00081 re_ = in(1,t);
00082 im_ = 0.0;
00083 }
00084 else
00085 {
00086 re_ = in(2*o, t);
00087
00088 im_ = in(2*o+1,t);
00089 }
00090
00091 mag_ = sqrt(re_ * re_ + im_ * im_);
00092
00093 phs_ = ((mrs_real)rand() / (mrs_real)(RAND_MAX)) * TWOPI;
00094 phs_ -= PI;
00095
00096 if (o < N2_-1)
00097 {
00098 out(2*o,t) = mag_ * cos(phs_);
00099 out(2*o+1,t) = mag_ * sin(phs_);
00100 }
00101
00102 }
00103 }
00104
00105
00106
00107 void
00108 SpectralTransformations::compress_magnitude(realvec& in, realvec& out)
00109 {
00110 mrs_natural t,o;
00111 for(t=0; t < inSamples_; ++t)
00112 for (o=0; o < N2_; o++)
00113 {
00114 if (o==0)
00115 {
00116 re_ = in(0,t);
00117 im_ = 0.0;
00118 }
00119 else if (o == N2_-1)
00120 {
00121 re_ = in(1,t);
00122 im_ = 0.0;
00123 }
00124 else
00125 {
00126 re_ = in(2*o, t);
00127 im_ = in(2*o+1,t);
00128 }
00129
00130 mag_ = sqrt(re_ * re_ + im_ * im_);
00131 phs_ = -atan2(im_, re_);
00132
00133 if (o < N2_-1)
00134 {
00135 out(2*o,t) = log(1+1000.0 * mag_) * cos(phs_);
00136 out(2*o+1,t) = log(1+1000.0 * mag_) * sin(phs_);
00137
00138
00139
00140 }
00141
00142 }
00143
00144
00145 }
00146
00147
00148
00149 void
00150 SpectralTransformations::three_peaks(realvec& in, realvec& out)
00151 {
00152 mrs_natural t,o;
00153 mrs_real max_mag = 0.0;
00154 mrs_real second_max_mag = 0.0;
00155 mrs_real third_max_mag = 0.0;
00156 mrs_natural max_o = 0;
00157 mrs_natural second_max_o = 0 ;
00158 mrs_natural third_max_o = 0;
00159
00160
00161
00162
00163
00164 for(t=0; t < inSamples_; ++t)
00165 for (o=0; o < N2_; o++)
00166 {
00167 if (o==0)
00168 {
00169 re_ = in(0,t);
00170 im_ = 0.0;
00171 }
00172 else if (o == N2_-1)
00173 {
00174 re_ = in(1,t);
00175 im_ = 0.0;
00176 }
00177 else
00178 {
00179 re_ = in(2*o, t);
00180 im_ = in(2*o+1,t);
00181 }
00182
00183 mag_ = sqrt(re_ * re_ + im_ * im_);
00184
00185
00186
00187 if ((mag_ > max_mag) && (o > 2))
00188 {
00189 max_mag = mag_;
00190 max_o = o;
00191 }
00192 if ((mag_ < max_mag) && (mag_ > second_max_mag) && (o > 2))
00193 {
00194 second_max_mag = mag_;
00195 second_max_o = o;
00196 }
00197
00198
00199 if ((mag_ < max_mag) && (mag_ < second_max_mag) && (mag_ > third_max_mag) && (o > 2))
00200 {
00201 third_max_mag = mag_;
00202 third_max_o = o;
00203 }
00204
00205
00206 phs_ = -atan2(im_, re_);
00207
00208 }
00209
00210
00211
00212
00213 for(t=0; t < inSamples_; ++t)
00214 for (o=0; o < N2_; o++)
00215 {
00216 if (o==0)
00217 {
00218 re_ = in(0,t);
00219 im_ = 0.0;
00220 }
00221 else if (o == N2_-1)
00222 {
00223 re_ = in(1,t);
00224 im_ = 0.0;
00225 }
00226 else
00227 {
00228 re_ = in(2*o, t);
00229 im_ = in(2*o+1,t);
00230 }
00231
00232 mag_ = sqrt(re_ * re_ + im_ * im_);
00233 phs_ = -atan2(im_, re_);
00234
00235 if (o < N2_-1)
00236 {
00237 if ((o == max_o) || (o == second_max_o) || (o == third_max_o))
00238 {
00239 out(2*o,t) = 2.0 * mag_ * cos(phs_);
00240 out(2*o+1,t) = 2.0 * mag_ * sin(phs_);
00241 }
00242 else {
00243 out(2*o,t) = 0 * cos(phs_);
00244 out(2*o+1,t) = 0 * sin(phs_);
00245 }
00246 }
00247
00248 }
00249
00250
00251 }
00252
00253
00254
00255
00256 void
00257 SpectralTransformations::singlebin(realvec& in, realvec& out)
00258 {
00259 mrs_natural t,o;
00260 for(t=0; t < inSamples_; ++t)
00261 for (o=0; o < N2_; o++)
00262 {
00263 if (o==4)
00264 {
00265 re_ = in(0,t);
00266 im_ = 0.0;
00267 }
00268 else if (o == N2_-1)
00269 {
00270 re_ = in(1,t);
00271 im_ = 0.0;
00272 }
00273 else
00274 {
00275
00276
00277 if (o == 5)
00278 {
00279 re_ = 0.5;
00280 im_ = 0.0;
00281 }
00282 else
00283 {
00284 re_ = 0.0;
00285 im_ = 0.0;
00286 }
00287
00288 }
00289
00290
00291 mag_ = sqrt(re_ * re_ + im_ * im_);
00292 phs_ = -atan2(im_, re_);
00293
00294
00295
00296 if (o < N2_-1)
00297 {
00298 out(2*o,t) = mag_ * cos(phs_);
00299 out(2*o+1,t) = mag_ * sin(phs_);
00300 }
00301
00302 }
00303 }
00304
00305
00306
00307 void
00308 SpectralTransformations::myProcess(realvec& in, realvec& out)
00309 {
00310
00311 if (ctrl_mode_->to<mrs_string>() == "PhaseRandomize")
00312 {
00313 MRSMSG("PhaseRandomize");
00314 phaseRandomize(in, out);
00315 }
00316 else if (ctrl_mode_->to<mrs_string>() == "singlebin")
00317 {
00318 MRSMSG("SingleBin");
00319 singlebin(in, out);
00320 }
00321
00322
00323 if (ctrl_mode_->to<mrs_string>() == "three_peaks")
00324 {
00325 three_peaks(in,out);
00326 }
00327
00328
00329 if (ctrl_mode_->to<mrs_string>() == "compress_magnitude")
00330 {
00331 compress_magnitude(in,out);
00332 }
00333
00334
00335
00336 }