|
CLAM-Development
1.3
|
00001 /* 00002 * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG) 00003 * UNIVERSITAT POMPEU FABRA 00004 * 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 * 00020 */ 00021 00022 #include "Complex.hxx" 00023 #include "SpecTypeFlags.hxx" 00024 #include "SpectrumInterpolator.hxx" 00025 #include "BPF.hxx" 00026 #include "Point.hxx" 00027 00028 namespace CLAM { 00029 00030 void SpecInterpConfig::DefaultInit() 00031 { 00032 AddAll(); 00033 UpdateData(); 00034 } 00035 00036 SpectrumInterpolator::SpectrumInterpolator(const SpecInterpConfig &c) 00037 : mSize(0) 00038 , mIn1("Input 1",this) 00039 , mIn2("Input 2",this) 00040 , mOut("Output",this) 00041 , mProtoState(SOther) 00042 , mInterpolationFactorCtl("InterpolationFactor",this) 00043 { 00044 Configure(c); 00045 } 00046 00047 bool SpectrumInterpolator::ConcreteConfigure(const ProcessingConfig&c) 00048 { 00049 CopyAsConcreteConfig(mConfig, c); 00050 //Initialize interpolation factor control from value in the configuration 00051 mInterpolationFactorCtl.DoControl(mConfig.GetInterpolationFactor()); 00052 00053 return true; 00054 } 00055 00056 // Unsupervised Do() function. 00057 // TODO use 'const Spectrum& in1, const Spectrum& in2' and spread it to the rest 00058 bool SpectrumInterpolator::Do(Spectrum& in1, Spectrum& in2, Spectrum& out) 00059 { 00060 CLAM_DEBUG_ASSERT(IsRunning(), 00061 "SpectrumInterpolator::Do(): Not in execution mode"); 00062 00063 switch (mProtoState) { 00064 // Fast prototype configurations 00065 case SMagPhase: 00066 InterpolateMagPhase(in1,in2,out); 00067 break; 00068 case SComplex: 00069 InterpolateComplex(in1,in2,out); 00070 break; 00071 case SPolar: 00072 InterpolatePolar(in1,in2,out); 00073 break; 00074 case SBPF: 00075 InterpolateBPF(in1,in2,out); 00076 break; 00077 case SBPFMagPhase: 00078 InterpolateBPFMagPhase(in1,in2,out); 00079 break; 00080 case SBPFComplex: 00081 InterpolateBPFComplex(in1,in2,out); 00082 break; 00083 case SBPFPolar: 00084 InterpolateBPFPolar(in1,in2,out); 00085 break; 00086 case SMagPhaseBPF: 00087 InterpolateMagPhaseBPF(in1,in2,out); 00088 break; 00089 case SComplexBPF: 00090 InterpolateComplexBPF(in1,in2,out); 00091 break; 00092 case SPolarBPF: 00093 InterpolatePolarBPF(in1,in2,out); 00094 break; 00095 // Slow type configurations 00096 case SOther: 00097 Interpolate(in1,in2,out); 00098 break; 00099 default: 00100 CLAM_ASSERT(false,"Do(...) : internal inconsistency (invalid mProtoState)"); 00101 } 00102 00103 return true; 00104 } 00105 00106 bool SpectrumInterpolator::Do(void) 00107 { 00108 CLAM_ASSERT(false, "SpectrumInterpolator::Do(): Not implemented"); 00109 00110 return true; 00111 } 00112 00113 // This function analyses the inputs and decides which prototypes to use 00114 // For the interpolation computation. 00115 bool SpectrumInterpolator::SetPrototypes(const Spectrum& in1,const Spectrum& in2,const Spectrum& out) 00116 { 00117 // Check common attributes 00118 SpecTypeFlags t1; 00119 in1.GetType(t1); 00120 SpecTypeFlags t2; 00121 in2.GetType(t2); 00122 SpecTypeFlags to; 00123 out.GetType(to); 00124 00125 // Sanity check: 00126 CLAM_ASSERT((t1.bMagPhase || t1.bComplex || t1.bPolar || t1.bMagPhaseBPF), 00127 "SpectrumInterpolator: First spectrum has no content"); 00128 CLAM_ASSERT((t2.bMagPhase || t2.bComplex || t2.bPolar || t2.bMagPhaseBPF), 00129 "SpectrumInterpolator: Second spectrum has no content"); 00130 CLAM_ASSERT((to.bMagPhase || to.bComplex || to.bPolar || to.bMagPhaseBPF), 00131 "SpectrumInterpolator: Output spectrum has no content"); 00132 00133 // Interpolateer size. "pure" BPFs are not considered here. 00134 mSize = 0; 00135 if (t1.bMagPhase || t1.bComplex || t1.bPolar) { 00136 mSize = in1.GetSize(); 00137 CLAM_ASSERT(mSize,"SpectrumInterpolator::SetPrototypes: Zero size spectrum"); 00138 } 00139 if (t2.bMagPhase || t2.bComplex || t2.bPolar) 00140 { 00141 if (mSize) { 00142 CLAM_ASSERT(mSize == in2.GetSize(),"SpectrumInterpolator::SetPrototypes:Size mismatch in spectrum interpolation"); 00143 } 00144 else { 00145 mSize = in2.GetSize(); 00146 CLAM_ASSERT(mSize,"SpectrumInterpolator::SetPrototypes: Zero size spectrum"); 00147 } 00148 } 00149 if (to.bMagPhase || to.bComplex || to.bPolar) 00150 { 00151 if (mSize) { 00152 CLAM_ASSERT(mSize == out.GetSize(),"SpectrumInterpolator::SetPrototypes:Size mismatch in spectrum interpolation"); 00153 } 00154 else { 00155 mSize = out.GetSize(); 00156 CLAM_ASSERT(mSize,"SpectrumInterpolator::SetPrototypes: Zero size spectrum"); 00157 } 00158 } 00159 // Spectral Range. 00160 // We could also ignore BPF-only objects here, but in 00161 // practice, if a BPF is designed for a certain spectral 00162 // range, error will probably be too big out of the range, out 00163 // we always force range matching 00164 CLAM_ASSERT(in1.GetSpectralRange() == in2.GetSpectralRange() && 00165 in1.GetSpectralRange() == out.GetSpectralRange() ,"SpectrumInterpolator::SetPrototypes: Spectral range mismatch in spectrum interpolation"); 00166 00167 // Scale. 00168 if (in1.GetScale() == EScale::eLinear) 00169 if (in2.GetScale() == EScale::eLinear) 00170 mScaleState=Slinlin; 00171 else 00172 mScaleState=Slinlog; 00173 else 00174 if (in2.GetScale() == EScale::eLinear) 00175 mScaleState=Sloglin; 00176 else 00177 mScaleState=Sloglog; 00178 // Log scale output might be useful, for example when working 00179 // with BPF objects at the three ports. But right for now... 00180 CLAM_ASSERT(out.GetScale() != EScale::eLog,"SpectrumInterpolator: Log Scale Output not implemented"); 00181 00182 // Prototypes. 00183 00184 // BPF Interpolation. 00185 bool i1BPF=false, i2BPF=false, oBPF=false; 00186 if (t1.bMagPhaseBPF && !t1.bComplex && !t1.bPolar && !t1.bMagPhase) 00187 i1BPF=true; 00188 if (t2.bMagPhaseBPF && !t2.bComplex && !t2.bPolar && !t2.bMagPhase) 00189 i2BPF=true; 00190 if (to.bMagPhaseBPF && !to.bComplex && !to.bPolar && !to.bMagPhase) 00191 oBPF=true; 00192 00193 if (oBPF) { 00194 // BPF output requires interpolating the inputs. 00195 mProtoState=SBPF; 00196 return true; 00197 } 00198 if (i1BPF) { 00199 // States with direct BPF implementation. 00200 if (t2.bMagPhase && to.bMagPhase) { 00201 mProtoState=SBPFMagPhase; 00202 return true; 00203 } 00204 if (t2.bComplex && to.bComplex) { 00205 mProtoState=SBPFComplex; 00206 return true; 00207 } 00208 if (t2.bPolar && to.bPolar) { 00209 mProtoState=SBPFPolar; 00210 return true; 00211 } 00212 // States requiring 1 conversion: 00213 if (t2.bMagPhase || to.bMagPhase) { 00214 mProtoState=SBPFMagPhase; 00215 return true; 00216 } 00217 if (t2.bComplex || to.bComplex) { 00218 mProtoState=SBPFComplex; 00219 return true; 00220 } 00221 if (t2.bPolar || to.bPolar) { 00222 mProtoState=SBPFPolar; 00223 return true; 00224 } 00225 // Should never get here: 00226 CLAM_ASSERT(false, 00227 "SpectrumInterpolator::SetPrototypes: Data flags internal inconsistency"); 00228 } 00229 if (i2BPF) { 00230 // States with direct BPF implementation. 00231 if (t1.bMagPhase && to.bMagPhase) { 00232 mProtoState=SMagPhaseBPF; 00233 return true; 00234 } 00235 if (t1.bComplex && to.bComplex) { 00236 mProtoState=SComplexBPF; 00237 return true; 00238 } 00239 if (t1.bPolar && to.bPolar) { 00240 mProtoState=SPolarBPF; 00241 return true; 00242 } 00243 // States requiring 1 conversion: 00244 if (t1.bMagPhase || to.bMagPhase) { 00245 mProtoState=SMagPhaseBPF; 00246 return true; 00247 } 00248 if (t1.bComplex || to.bComplex) { 00249 mProtoState=SComplexBPF; 00250 return true; 00251 } 00252 if (t1.bPolar || to.bPolar) { 00253 mProtoState=SPolarBPF; 00254 return true; 00255 } 00256 // Should never get here: 00257 CLAM_ASSERT(false, 00258 "SpectrumInterpolator::SetPrototypes:" 00259 " invalid data flags"); 00260 } 00261 // Direct non-BPF states. 00262 if (t1.bMagPhase && t2.bMagPhase && to.bMagPhase) { 00263 mProtoState=SMagPhase; 00264 return true; 00265 } 00266 if (t1.bComplex && t2.bComplex && to.bComplex) { 00267 mProtoState=SComplex; 00268 return true; 00269 } 00270 if (t1.bPolar && t2.bPolar && to.bPolar) { 00271 mProtoState=SPolar; 00272 return true; 00273 } 00274 // States Requiring 1 Conversion 00275 if ( (t1.bMagPhase && t2.bMagPhase) || 00276 (t1.bMagPhase && to.bMagPhase) || 00277 (t2.bMagPhase && to.bMagPhase)) { 00278 mProtoState=SMagPhase; 00279 return true; 00280 } 00281 if ( (t1.bComplex && t2.bComplex) || 00282 (t1.bComplex && to.bComplex) || 00283 (t2.bComplex && to.bComplex)) { 00284 mProtoState=SComplex; 00285 return true; 00286 } 00287 if ( (t1.bPolar && t2.bPolar) || 00288 (t1.bPolar && to.bPolar) || 00289 (t2.bPolar && to.bPolar)) { 00290 mProtoState=SPolar; 00291 return true; 00292 } 00293 // Bad luck. We require 2 conversions... 00294 mProtoState=SMagPhase; 00295 return true; 00296 } 00297 00298 00299 bool SpectrumInterpolator::SetPrototypes() 00300 { 00301 CLAM_ASSERT(false, "SpectrumInterpolator::SetPrototypes(): Not implemented"); 00302 00303 return true; 00304 } 00305 00306 bool SpectrumInterpolator::UnsetPrototypes() 00307 { 00308 mProtoState=SOther; 00309 return true; 00310 } 00311 00312 00313 void SpectrumInterpolator::Interpolate(Spectrum& in1, Spectrum& in2, Spectrum& out) 00314 { 00315 PrototypeState state_copy = mProtoState; 00316 ScaleState state2_copy = mScaleState; 00317 00318 SetPrototypes(in1,in2,out); 00319 Do(in1,in2,out); 00320 00321 mProtoState = state_copy; 00322 mScaleState = state2_copy; 00323 } 00324 00325 00326 void SpectrumInterpolator::InterpolateMagPhase(Spectrum& in1, Spectrum& in2, Spectrum& out) 00327 { 00328 switch(mScaleState) { 00329 case Slinlin: 00330 InterpolateMagPhaseLin(in1,in2,out); 00331 break; 00332 case Sloglog: 00333 InterpolateMagPhaseLog(in1,in2,out); 00334 break; 00335 case Slinlog: 00336 InterpolateMagPhaseLinLog(in1,in2,out); 00337 break; 00338 case Sloglin: 00339 InterpolateMagPhaseLinLog(in2,in1,out); 00340 break; 00341 } 00342 } 00343 00344 void SpectrumInterpolator::InterpolateMagPhaseLin(Spectrum& in1, Spectrum& in2, Spectrum& out) 00345 { 00346 bool remove1=false,remove2=false,remove3=false; 00347 SpecTypeFlags f; 00348 00349 // This function was choosed because some of the data objects had 00350 // their MagPhase attribute instantiated. We don't know which of 00351 // them, though, out we must check and instantiate the attribute 00352 // it it is missed. This could be optimised out by Interpolateing more 00353 // States, see coments on this in the class declaration. 00354 in1.GetType(f); 00355 if (!f.bMagPhase) { 00356 remove1=true; 00357 f.bMagPhase=true; 00358 in1.SetTypeSynchronize(f); 00359 } 00360 in2.GetType(f); 00361 if (!f.bMagPhase) { 00362 remove2=true; 00363 f.bMagPhase=true; 00364 in2.SetTypeSynchronize(f); 00365 } 00366 out.GetType(f); 00367 if (!f.bMagPhase) { 00368 remove3=true; 00369 f.bMagPhase=true; 00370 out.SetType(f); 00371 } 00372 00373 TData *m1 = in1.GetMagBuffer().GetPtr(); 00374 TData *f1 = in1.GetPhaseBuffer().GetPtr(); 00375 TData *m2 = in2.GetMagBuffer().GetPtr(); 00376 TData *f2 = in2.GetPhaseBuffer().GetPtr(); 00377 TData *mo = out.GetMagBuffer().GetPtr(); 00378 TData *fo = out.GetPhaseBuffer().GetPtr(); 00379 /*****************************/ 00380 //OPERATION: MAGPHASE AND MAGPHASE 00381 00382 TData intFactor=mInterpolationFactorCtl.GetLastValue(); 00383 if(intFactor>1) intFactor=1; 00384 if(intFactor<0) intFactor=0; 00385 TData invIntFactor=1-intFactor; 00386 00387 Polar polInvIntFactor=Polar(invIntFactor); 00388 Polar polIntFactor=Polar(intFactor); 00389 00390 for (int i=0;i<mSize;i++) { 00391 Polar po=polInvIntFactor*Polar(m1[i],f1[i])+polIntFactor*Polar(m2[i],f2[i]); 00392 mo[i]=po.Mag(); 00393 fo[i]=po.Ang(); 00394 } 00395 00396 f.bComplex=f.bPolar=f.bMagPhaseBPF=false; 00397 f.bMagPhase=true; 00398 out.SynchronizeTo(f); 00399 00400 if (remove1) { 00401 in1.RemoveMagBuffer(); 00402 in1.RemovePhaseBuffer(); 00403 in1.UpdateData(); 00404 } 00405 if (remove2) { 00406 in2.RemoveMagBuffer(); 00407 in2.RemovePhaseBuffer(); 00408 in2.UpdateData(); 00409 } 00410 if (remove3) { 00411 out.RemoveMagBuffer(); 00412 out.RemovePhaseBuffer(); 00413 out.UpdateData(); 00414 } 00415 00416 } 00417 00418 void SpectrumInterpolator::InterpolateComplex(Spectrum& in1, Spectrum& in2, Spectrum& out) 00419 { 00420 switch(mScaleState) { 00421 case Slinlin: 00422 InterpolateComplexLin(in1,in2,out); 00423 break; 00424 case Sloglog: 00425 InterpolateComplexLog(in1,in2,out); 00426 break; 00427 case Slinlog: 00428 InterpolateComplexLinLog(in1,in2,out); 00429 break; 00430 case Sloglin: 00431 InterpolateComplexLinLog(in2,in1,out); 00432 break; 00433 } 00434 } 00435 00436 void SpectrumInterpolator::InterpolateComplexLin(Spectrum& in1, Spectrum& in2, Spectrum& out) 00437 { 00438 bool remove1=false,remove2=false,remove3=false; 00439 SpecTypeFlags f; 00440 00441 // This function was choosed because some of the data objects had 00442 // their Complex attribute instantiated. We don't know which of 00443 // them, though, out we must check and instantiate the attribute 00444 // it it is missed. This could be optimised out by Interpolateing more 00445 // States, see coments on this in the class declaration. 00446 in1.GetType(f); 00447 if (!f.bComplex) { 00448 remove1=true; 00449 f.bComplex=true; 00450 in1.SetTypeSynchronize(f); 00451 } 00452 in2.GetType(f); 00453 if (!f.bComplex) { 00454 remove2=true; 00455 f.bComplex=true; 00456 in2.SetTypeSynchronize(f); 00457 } 00458 out.GetType(f); 00459 if (!f.bComplex) { 00460 remove3=true; 00461 f.bComplex=true; 00462 out.SetType(f); 00463 } 00464 00465 Complex *c1 = in1.GetComplexArray().GetPtr(); 00466 Complex *c2 = in2.GetComplexArray().GetPtr(); 00467 Complex *co = out.GetComplexArray().GetPtr(); 00468 /*****************************/ 00469 //OPERATION: COMPLEX AND COMPLEX 00470 TData intFactor=mInterpolationFactorCtl.GetLastValue(); 00471 if(intFactor>1) intFactor=1; 00472 if(intFactor<0) intFactor=0; 00473 TData invIntFactor=1-intFactor; 00474 00475 for (int i=0;i<mSize;i++) 00476 co[i]=c1[i]*invIntFactor+c2[i]*intFactor; 00477 00478 f.bMagPhase=f.bPolar=f.bMagPhaseBPF=false; 00479 f.bComplex=true; 00480 out.SynchronizeTo(f); 00481 00482 if (remove1) { 00483 in1.RemoveComplexArray(); 00484 in1.UpdateData(); 00485 } 00486 if (remove2) { 00487 in2.RemoveComplexArray(); 00488 in2.UpdateData(); 00489 } 00490 if (remove3) { 00491 out.RemoveComplexArray(); 00492 out.UpdateData(); 00493 } 00494 } 00495 00496 00497 void SpectrumInterpolator::InterpolatePolar(Spectrum& in1, Spectrum& in2, Spectrum& out) 00498 { 00499 switch(mScaleState) { 00500 case Slinlin: 00501 InterpolatePolarLin(in1,in2,out); 00502 break; 00503 case Sloglog: 00504 InterpolatePolarLog(in1,in2,out); 00505 break; 00506 case Slinlog: 00507 InterpolatePolarLinLog(in1,in2,out); 00508 break; 00509 case Sloglin: 00510 InterpolatePolarLinLog(in2,in1,out); 00511 break; 00512 } 00513 } 00514 00515 void SpectrumInterpolator::InterpolatePolarLin(Spectrum& in1, Spectrum& in2, Spectrum& out) 00516 { 00517 bool remove1=false,remove2=false,remove3=false; 00518 SpecTypeFlags f; 00519 00520 // This function was choosed because some of the data objects had 00521 // their Polar attribute instantiated. We don't know which of 00522 // them, though, out we must check and instantiate the attribute 00523 // it it is missed. This could be optimised out by Interpolateing more 00524 // States, see coments on this in the class declaration. 00525 in1.GetType(f); 00526 if (!f.bPolar) { 00527 remove1=true; 00528 f.bPolar=true; 00529 in1.SetTypeSynchronize(f); 00530 } 00531 in2.GetType(f); 00532 if (!f.bPolar) { 00533 remove2=true; 00534 f.bPolar=true; 00535 in2.SetTypeSynchronize(f); 00536 } 00537 out.GetType(f); 00538 if (!f.bPolar) { 00539 remove3=true; 00540 f.bPolar=true; 00541 out.SetType(f); 00542 } 00543 00544 Polar *p1 = in1.GetPolarArray().GetPtr(); 00545 Polar *p2 = in2.GetPolarArray().GetPtr(); 00546 Polar *po = out.GetPolarArray().GetPtr(); 00547 /*****************************/ 00548 //OPERATION: POLAR AND POLAR 00549 TData intFactor=mInterpolationFactorCtl.GetLastValue(); 00550 if(intFactor>1) intFactor=1; 00551 if(intFactor<0) intFactor=0; 00552 TData invIntFactor=1-intFactor; 00553 00554 Polar polInvIntFactor=Polar(invIntFactor); 00555 Polar polIntFactor=Polar(intFactor); 00556 00557 for (int i=0;i<mSize;i++) 00558 po[i]=polInvIntFactor*p1[i]+polIntFactor*p2[i]; 00559 00560 f.bComplex=f.bMagPhase=f.bMagPhaseBPF=false; 00561 f.bPolar=true; 00562 out.SynchronizeTo(f); 00563 00564 if (remove1) { 00565 in1.RemovePolarArray(); 00566 in1.UpdateData(); 00567 } 00568 if (remove2) { 00569 in2.RemovePolarArray(); 00570 in2.UpdateData(); 00571 } 00572 if (remove3) { 00573 out.RemovePolarArray(); 00574 out.UpdateData(); 00575 } 00576 } 00577 00578 00579 void SpectrumInterpolator::InterpolateBPFMagPhase(Spectrum& in1, Spectrum& in2, Spectrum& out) 00580 { 00581 switch(mScaleState) { 00582 case Slinlin: 00583 InterpolateBPFMagPhaseLin(in1,in2,out); 00584 break; 00585 case Sloglog: 00586 InterpolateBPFMagPhaseLog(in1,in2,out); 00587 break; 00588 case Slinlog: 00589 CLAM_ASSERT(false,"InterpolateBPFMagPhaseLinLog: Not implemented"); 00590 break; 00591 case Sloglin: 00592 InterpolateBPFMagPhaseLogLin(in1,in2,out); 00593 break; 00594 } 00595 } 00596 00597 void SpectrumInterpolator::InterpolateMagPhaseBPF(Spectrum& in1, Spectrum& in2, Spectrum& out) 00598 { 00599 switch(mScaleState) { 00600 case Slinlin: 00601 InterpolateBPFMagPhaseLin(in2,in1,out); 00602 break; 00603 case Sloglog: 00604 InterpolateBPFMagPhaseLog(in2,in1,out); 00605 break; 00606 case Slinlog: 00607 InterpolateBPFMagPhaseLogLin(in2,in1,out); 00608 break; 00609 case Sloglin: 00610 CLAM_ASSERT(false,"InterpolateBPFMagPhaseLinLog: Not implemented"); 00611 break; 00612 } 00613 } 00614 00615 void SpectrumInterpolator::InterpolateBPFMagPhaseLin(Spectrum& in1, Spectrum& in2, Spectrum& out) 00616 { 00617 bool remove2=false,remove3=false; 00618 SpecTypeFlags f; 00619 00620 // This function was choosed because in1 is a BPF Spectrum, 00621 // and some of the non-BPF data objects have their MagPhase 00622 // attribute instantiated. We don't know which of them, 00623 // though, out we must check and instantiate the attribute it 00624 // it is missed. This could be optimised out by Interpolateing more 00625 // States, see coments on this in the class declaration. 00626 in2.GetType(f); 00627 if (!f.bMagPhase) { 00628 remove2=true; 00629 f.bMagPhase=true; 00630 in2.SetTypeSynchronize(f); 00631 } 00632 out.GetType(f); 00633 if (!f.bMagPhase) { 00634 remove3=true; 00635 f.bMagPhase=true; 00636 out.SetType(f); 00637 } 00638 00639 TData pos = 0.0; 00640 TData delta = out.GetSpectralRange() / 00641 ((TData)out.GetSize()-TData(1.0)); 00642 BPF &m1 = in1.GetMagBPF(); 00643 BPF &f1 = in1.GetPhaseBPF(); 00644 TData *m2 = in2.GetMagBuffer().GetPtr(); 00645 TData *f2 = in2.GetPhaseBuffer().GetPtr(); 00646 TData *mo = out.GetMagBuffer().GetPtr(); 00647 TData *fo = out.GetPhaseBuffer().GetPtr(); 00648 /*****************************/ 00649 //OPERATION: BPF AND MAGPHASE 00650 TData intFactor=mInterpolationFactorCtl.GetLastValue(); 00651 if(intFactor>1) intFactor=1; 00652 if(intFactor<0) intFactor=0; 00653 TData invIntFactor=1-intFactor; 00654 00655 Polar polInvIntFactor=Polar(invIntFactor); 00656 Polar polIntFactor=Polar(intFactor); 00657 00658 for (int i=0;i<mSize;i++) { 00659 Polar po = polInvIntFactor*Polar(m1.GetValue(pos),f1.GetValue(pos)) + 00660 polIntFactor*Polar(m2[i],f2[i]); 00661 mo[i]=po.Mag(); 00662 fo[i]=po.Ang(); 00663 pos+=delta; 00664 } 00665 00666 f.bComplex=f.bPolar=f.bMagPhaseBPF=false; 00667 f.bMagPhase=true; 00668 out.SynchronizeTo(f); 00669 00670 if (remove2) { 00671 in2.RemoveMagBuffer(); 00672 in2.RemovePhaseBuffer(); 00673 in2.UpdateData(); 00674 } 00675 if (remove3) { 00676 out.RemoveMagBuffer(); 00677 out.RemovePhaseBuffer(); 00678 out.UpdateData(); 00679 } 00680 } 00681 00682 void SpectrumInterpolator::InterpolateBPFMagPhaseLogLin(Spectrum& in1, Spectrum& in2, Spectrum& out) 00683 { 00684 bool remove2=false,remove3=false; 00685 SpecTypeFlags f; 00686 00687 // This function was choosed because in1 is a BPF Spectrum, 00688 // and some of the non-BPF data objects have their MagPhase 00689 // attribute instantiated. We don't know which of them, 00690 // though, out we must check and instantiate the attribute it 00691 // it is missed. This could be optimised out by Interpolateing more 00692 // States, see coments on this in the class declaration. 00693 in2.GetType(f); 00694 if (!f.bMagPhase) { 00695 remove2=true; 00696 f.bMagPhase=true; 00697 in2.SetTypeSynchronize(f); 00698 } 00699 out.GetType(f); 00700 if (!f.bMagPhase) { 00701 remove3=true; 00702 f.bMagPhase=true; 00703 out.SetType(f); 00704 } 00705 00706 TData pos = 0.0; 00707 TData delta = out.GetSpectralRange() / 00708 ((TData)out.GetSize()-TData(1.0)); 00709 BPF &m1 = in1.GetMagBPF(); 00710 BPF &f1 = in1.GetPhaseBPF(); 00711 TData *m2 = in2.GetMagBuffer().GetPtr(); 00712 TData *f2 = in2.GetPhaseBuffer().GetPtr(); 00713 TData *mo = out.GetMagBuffer().GetPtr(); 00714 TData *fo = out.GetPhaseBuffer().GetPtr(); 00715 /*****************************/ 00716 //OPERATION: BPF(LOG) AND MAGPHASE 00717 TData intFactor=mInterpolationFactorCtl.GetLastValue(); 00718 if(intFactor>1) intFactor=1; 00719 if(intFactor<0) intFactor=0; 00720 TData invIntFactor=1-intFactor; 00721 Polar polInvIntFactor=Polar(invIntFactor); 00722 Polar polIntFactor=Polar(intFactor); 00723 00724 for (int i=0;i<mSize;i++) { 00725 Polar po = polInvIntFactor*Polar(CLAM_pow(TData(10),m1.GetValue(pos)/TData(10.0)),f1.GetValue(pos)) + 00726 polIntFactor*Polar(m2[i],f2[i]); 00727 mo[i]=po.Mag(); 00728 fo[i]=po.Ang(); 00729 pos+=delta; 00730 } 00731 00732 f.bComplex=f.bPolar=f.bMagPhaseBPF=false; 00733 f.bMagPhase=true; 00734 out.SynchronizeTo(f); 00735 00736 if (remove2) { 00737 in2.RemoveMagBuffer(); 00738 in2.RemovePhaseBuffer(); 00739 in2.UpdateData(); 00740 } 00741 if (remove3) { 00742 out.RemoveMagBuffer(); 00743 out.RemovePhaseBuffer(); 00744 out.UpdateData(); 00745 } 00746 } 00747 00748 void SpectrumInterpolator::InterpolateBPFComplex(Spectrum& in1, Spectrum& in2, Spectrum& out) 00749 { 00750 switch(mScaleState) { 00751 case Slinlin: 00752 InterpolateBPFComplexLin(in1,in2,out); 00753 break; 00754 case Sloglog: 00755 InterpolateBPFComplexLog(in1,in2,out); 00756 break; 00757 case Slinlog: 00758 CLAM_ASSERT(false,"InterpolateBPFMagPhaseLinLog: Not implemented"); 00759 break; 00760 case Sloglin: 00761 InterpolateBPFComplexLogLin(in1,in2,out); 00762 break; 00763 } 00764 } 00765 00766 void SpectrumInterpolator::InterpolateComplexBPF(Spectrum& in1, Spectrum& in2, Spectrum& out) 00767 { 00768 switch(mScaleState) { 00769 case Slinlin: 00770 InterpolateBPFComplexLin(in2,in1,out); 00771 break; 00772 case Sloglog: 00773 InterpolateBPFComplexLog(in2,in1,out); 00774 break; 00775 case Slinlog: 00776 InterpolateBPFComplexLogLin(in2,in1,out); 00777 break; 00778 case Sloglin: 00779 CLAM_ASSERT(false,"InterpolateBPFMagPhaseLinLog: Not implemented"); 00780 break; 00781 } 00782 } 00783 00784 void SpectrumInterpolator::InterpolateBPFComplexLin(Spectrum& in1, Spectrum& in2, Spectrum& out) 00785 { 00786 bool remove2=false,remove3=false; 00787 SpecTypeFlags f; 00788 00789 // This function was choosed because in1 is a BPF Spectrum, 00790 // and some of the non-BPF data objects have their Complex 00791 // attribute instantiated. We don't know which of them, 00792 // though, out we must check and instantiate the attribute it 00793 // it is missed. This could be optimised out by Interpolateing more 00794 // States, see coments on this in the class declaration. 00795 in2.GetType(f); 00796 if (!f.bComplex) { 00797 remove2=true; 00798 f.bComplex=true; 00799 in2.SetTypeSynchronize(f); 00800 } 00801 out.GetType(f); 00802 if (!f.bComplex) { 00803 remove3=true; 00804 f.bComplex=true; 00805 out.SetType(f); 00806 } 00807 00808 TData pos = 0.0; 00809 TData delta = out.GetSpectralRange() / 00810 ((TData)out.GetSize()-TData(1.0)); 00811 BPF &m1 = in1.GetMagBPF(); 00812 BPF &f1 = in1.GetPhaseBPF(); 00813 Complex *c2 = in2.GetComplexArray().GetPtr(); 00814 Complex *co = out.GetComplexArray().GetPtr(); 00815 /*****************************/ 00816 //OPERATION: BPF AND COMPLEX 00817 TData intFactor=mInterpolationFactorCtl.GetLastValue(); 00818 if(intFactor>1) intFactor=1; 00819 if(intFactor<0) intFactor=0; 00820 TData invIntFactor=1-intFactor; 00821 00822 for (int i=0;i<mSize;i++) { 00823 TData BRe = fabs(m1.GetValue(pos)) * CLAM_cos(f1.GetValue(pos)); 00824 TData BIm = fabs(m1.GetValue(pos)) * CLAM_sin(f1.GetValue(pos)); 00825 co[i]= Complex(BRe,BIm)*invIntFactor + c2[i]*intFactor; 00826 pos+=delta; 00827 } 00828 00829 f.bMagPhase=f.bPolar=f.bMagPhaseBPF=false; 00830 f.bComplex=true; 00831 out.SynchronizeTo(f); 00832 00833 if (remove2) { 00834 in2.RemoveComplexArray(); 00835 in2.UpdateData(); 00836 } 00837 if (remove3) { 00838 out.RemoveComplexArray(); 00839 out.UpdateData(); 00840 } 00841 } 00842 00843 // This is probably one of the most used methods, because it can be used 00844 // to apply a BPF filter in log scale to a linear complex spectrum, as the 00845 // one naturaly generated from a FFT 00846 void SpectrumInterpolator::InterpolateBPFComplexLogLin(Spectrum& in1, Spectrum& in2, Spectrum& out) 00847 { 00848 bool remove2=false,remove3=false; 00849 SpecTypeFlags f; 00850 00851 // This function was choosed because in1 is a BPF Spectrum, 00852 // and some of the non-BPF data objects have their Complex 00853 // attribute instantiated. We don't know which of them, 00854 // though, out we must check and instantiate the attribute it 00855 // it is missed. This could be optimised out by Interpolateing more 00856 // States, see coments on this in the class declaration. 00857 in2.GetType(f); 00858 if (!f.bComplex) { 00859 remove2=true; 00860 f.bComplex=true; 00861 in2.SetTypeSynchronize(f); 00862 } 00863 out.GetType(f); 00864 if (!f.bComplex) { 00865 remove3=true; 00866 f.bComplex=true; 00867 out.SetType(f); 00868 } 00869 00870 TData pos = 0.0; 00871 TData delta = out.GetSpectralRange() / 00872 ((TData)out.GetSize()-TData(1.0)); 00873 BPF &m1 = in1.GetMagBPF(); 00874 BPF &f1 = in1.GetPhaseBPF(); 00875 Complex *c2 = in2.GetComplexArray().GetPtr(); 00876 Complex *co = out.GetComplexArray().GetPtr(); 00877 /*****************************/ 00878 //OPERATION: BPF(LOG) AND COMPLEX 00879 TData intFactor=mInterpolationFactorCtl.GetLastValue(); 00880 if(intFactor>1) intFactor=1; 00881 if(intFactor<0) intFactor=0; 00882 TData invIntFactor=1-intFactor; 00883 00884 for (int i=0;i<mSize;i++) { 00885 TData BRe = CLAM_pow(TData(10),fabs(m1.GetValue(pos))/TData(10.0)) * CLAM_cos(f1.GetValue(pos)); 00886 TData BIm = CLAM_pow(TData(10),fabs(m1.GetValue(pos))/TData(10.0)) * CLAM_sin(f1.GetValue(pos)); 00887 co[i]= Complex(BRe,BIm)*invIntFactor + c2[i]*intFactor; 00888 pos+=delta; 00889 } 00890 00891 f.bMagPhase=f.bPolar=f.bMagPhaseBPF=false; 00892 f.bComplex=true; 00893 out.SynchronizeTo(f); 00894 00895 00896 if (remove2) { 00897 in2.RemoveComplexArray(); 00898 in2.UpdateData(); 00899 } 00900 if (remove3) { 00901 out.RemoveComplexArray(); 00902 out.UpdateData(); 00903 } 00904 } 00905 00906 00907 void SpectrumInterpolator::InterpolateBPFPolar(Spectrum& in1, Spectrum& in2, Spectrum& out) 00908 { 00909 switch(mScaleState) { 00910 case Slinlin: 00911 InterpolateBPFPolarLin(in1,in2,out); 00912 break; 00913 case Sloglog: 00914 InterpolateBPFPolarLog(in1,in2,out); 00915 break; 00916 case Slinlog: 00917 CLAM_ASSERT(false,"InterpolateBPFPolarLinLog: Not implemented"); 00918 break; 00919 case Sloglin: 00920 InterpolateBPFPolarLogLin(in1,in2,out); 00921 break; 00922 } 00923 } 00924 00925 void SpectrumInterpolator::InterpolatePolarBPF(Spectrum& in1, Spectrum& in2, Spectrum& out) 00926 { 00927 switch(mScaleState) { 00928 case Slinlin: 00929 InterpolateBPFPolarLin(in2,in1,out); 00930 break; 00931 case Sloglog: 00932 InterpolateBPFPolarLog(in2,in1,out); 00933 break; 00934 case Slinlog: 00935 InterpolateBPFPolarLogLin(in2,in1,out); 00936 break; 00937 case Sloglin: 00938 CLAM_ASSERT(false,"InterpolateBPFPolarLinLog: Not implemented"); 00939 break; 00940 } 00941 } 00942 00943 void SpectrumInterpolator::InterpolateBPFPolarLin(Spectrum& in1, Spectrum& in2, Spectrum& out) 00944 { 00945 bool remove2=false,remove3=false; 00946 SpecTypeFlags f; 00947 00948 // This function was choosed because in1 is a BPF Spectrum, 00949 // and some of the non-BPF data objects have their Polar 00950 // attribute instantiated. We don't know which of them, 00951 // though, out we must check and instantiate the attribute it 00952 // it is missed. This could be optimised out by Interpolateing more 00953 // States, see coments on this in the class declaration. 00954 in2.GetType(f); 00955 if (!f.bPolar) { 00956 remove2=true; 00957 f.bPolar=true; 00958 in2.SetTypeSynchronize(f); 00959 } 00960 out.GetType(f); 00961 if (!f.bPolar) { 00962 remove3=true; 00963 f.bPolar=true; 00964 out.SetType(f); 00965 } 00966 00967 TData pos = 0.0; 00968 TData delta = out.GetSpectralRange() / 00969 ((TData)out.GetSize()-TData(1.0)); 00970 BPF &m1 = in1.GetMagBPF(); 00971 BPF &f1 = in1.GetPhaseBPF(); 00972 Polar *p2 = in2.GetPolarArray().GetPtr(); 00973 Polar *po = out.GetPolarArray().GetPtr(); 00974 /*****************************/ 00975 //OPERATION: BPF AND POLAR 00976 TData intFactor=mInterpolationFactorCtl.GetLastValue(); 00977 if(intFactor>1) intFactor=1; 00978 if(intFactor<0) intFactor=0; 00979 TData invIntFactor=1-intFactor; 00980 Polar polInvIntFactor=Polar(invIntFactor); 00981 Polar polIntFactor=Polar(intFactor); 00982 00983 for (int i=0;i<mSize;i++) { 00984 po[i]=polInvIntFactor*Polar(m1.GetValue(pos),f1.GetValue(pos))+polIntFactor*p2[i]; 00985 pos+=delta; 00986 } 00987 00988 f.bMagPhase=f.bComplex=f.bMagPhaseBPF=false; 00989 f.bPolar=true; 00990 out.SynchronizeTo(f); 00991 00992 if (remove2) { 00993 in2.RemovePolarArray(); 00994 in2.UpdateData(); 00995 } 00996 if (remove3) { 00997 out.RemovePolarArray(); 00998 out.UpdateData(); 00999 } 01000 } 01001 01002 void SpectrumInterpolator::InterpolateBPFPolarLogLin(Spectrum& in1, Spectrum& in2, Spectrum& out) 01003 { 01004 bool remove2=false,remove3=false; 01005 SpecTypeFlags f; 01006 01007 // This function was choosed because in1 is a BPF Spectrum, 01008 // and some of the non-BPF data objects have their Polar 01009 // attribute instantiated. We don't know which of them, 01010 // though, out we must check and instantiate the attribute it 01011 // it is missed. This could be optimised out by Interpolateing more 01012 // States, see coments on this in the class declaration. 01013 in2.GetType(f); 01014 if (!f.bPolar) { 01015 remove2=true; 01016 f.bPolar=true; 01017 in2.SetTypeSynchronize(f); 01018 } 01019 out.GetType(f); 01020 if (!f.bPolar) { 01021 remove3=true; 01022 f.bPolar=true; 01023 out.SetType(f); 01024 } 01025 01026 TData pos = 0.0; 01027 TData delta = out.GetSpectralRange() / 01028 ((TData)out.GetSize()-TData(1.0)); 01029 BPF &m1 = in1.GetMagBPF(); 01030 BPF &f1 = in1.GetPhaseBPF(); 01031 Polar *p2 = in2.GetPolarArray().GetPtr(); 01032 Polar *po = out.GetPolarArray().GetPtr(); 01033 /*****************************/ 01034 //OPERATION: BPF(LOG) AND POLAR 01035 TData intFactor=mInterpolationFactorCtl.GetLastValue(); 01036 if(intFactor>1) intFactor=1; 01037 if(intFactor<0) intFactor=0; 01038 TData invIntFactor=1-intFactor; 01039 Polar polInvIntFactor=Polar(invIntFactor); 01040 Polar polIntFactor=Polar(intFactor); 01041 01042 for (int i=0;i<mSize;i++) { 01043 TData BMag = CLAM_pow(TData(10),m1.GetValue(pos)/TData(10.0)); 01044 TData BPha = f1.GetValue(pos); 01045 po[i]=polInvIntFactor*Polar(BMag,BPha)+polIntFactor*p2[i]; 01046 pos+=delta; 01047 } 01048 01049 f.bMagPhase=f.bComplex=f.bMagPhaseBPF=false; 01050 f.bPolar=true; 01051 out.SynchronizeTo(f); 01052 01053 if (remove2) { 01054 in2.RemovePolarArray(); 01055 in2.UpdateData(); 01056 } 01057 if (remove3) { 01058 out.RemovePolarArray(); 01059 out.UpdateData(); 01060 } 01061 } 01062 01063 void SpectrumInterpolator::InterpolateBPF(Spectrum& in1, Spectrum& in2, Spectrum& out) 01064 { 01065 01066 TData intFactor=mInterpolationFactorCtl.GetLastValue(); 01067 if(intFactor>1) intFactor=1; 01068 if(intFactor<0) intFactor=0; 01069 TData invIntFactor=1-intFactor; 01070 01071 // First we check if the abcisas agree 01072 01073 for (int i=0;i<mSize;i++) { 01074 Point &pm1=in1.GetMagBPF().GetPointArray()[i]; 01075 Point &pm2=in2.GetMagBPF().GetPointArray()[i]; 01076 Point &pmo=out.GetMagBPF().GetPointArray()[i]; 01077 Point &pf1=in1.GetPhaseBPF().GetPointArray()[i]; 01078 Point &pf2=in2.GetPhaseBPF().GetPointArray()[i]; 01079 Point &pfo=out.GetPhaseBPF().GetPointArray()[i]; 01080 CLAM_ASSERT(pm1.GetX() == pm2.GetX(), "InterpolateBPF: input BPF abcisas do not match " 01081 "(and BPF merging not yet iplemented)"); 01082 CLAM_ASSERT(pm1.GetX() == pmo.GetX(), "InterpolateBPF: ouput BPF abcisas do not match with imput " 01083 "(and BPF merging not yet iplemented)"); 01084 /*****************************/ 01085 //OPERATION: BPF AND BPF 01086 pmo.SetY(invIntFactor*pm1.GetY()+intFactor*pm2.GetY()); 01087 pfo.SetY(invIntFactor*pf1.GetY()+intFactor*pf2.GetY()); 01088 } 01089 01090 } 01091 01092 // UNINMPLEMENTED METHODS. some day... 01093 void SpectrumInterpolator::InterpolateMagPhaseLog(Spectrum& in1, Spectrum& in2, Spectrum& out) 01094 { 01095 CLAM_ASSERT(false,"InterpolateMagPhaseLog: Not implemented"); 01096 } 01097 void SpectrumInterpolator::InterpolateMagPhaseLinLog(Spectrum& in1, Spectrum& in2, Spectrum& out) 01098 { 01099 CLAM_ASSERT(false,"InterpolateMagPhaseLinLog: Not implemented"); 01100 } 01101 void SpectrumInterpolator::InterpolateComplexLog(Spectrum& in1, Spectrum& in2, Spectrum& out) 01102 { 01103 CLAM_ASSERT(false,"InterpolateComplexLog: Not implemented"); 01104 } 01105 void SpectrumInterpolator::InterpolateComplexLinLog(Spectrum& in1, Spectrum& in2, Spectrum& out) 01106 { 01107 CLAM_ASSERT(false,"InterpolateComplexLinLog: Not implemented"); 01108 } 01109 void SpectrumInterpolator::InterpolatePolarLog(Spectrum& in1, Spectrum& in2, Spectrum& out) 01110 { 01111 CLAM_ASSERT(false,"InterpolatePolarLog: Not implemented"); 01112 } 01113 void SpectrumInterpolator::InterpolatePolarLinLog(Spectrum& in1, Spectrum& in2, Spectrum& out) 01114 { 01115 CLAM_ASSERT(false,"InterpolatePolarLinLog: Not implemented"); 01116 } 01117 void SpectrumInterpolator::InterpolateBPFComplexLog(Spectrum& in1, Spectrum& in2, Spectrum& out) 01118 { 01119 CLAM_ASSERT(false,"InterpolateBPFComplexLog: Not implemented"); 01120 } 01121 void SpectrumInterpolator::InterpolateBPFComplexLinLog(Spectrum& in1, Spectrum& in2, Spectrum& out) 01122 { 01123 CLAM_ASSERT(false,"InterpolateBPFComplexLinLog: Not implemented"); 01124 } 01125 void SpectrumInterpolator::InterpolateBPFPolarLog(Spectrum& in1, Spectrum& in2, Spectrum& out) 01126 { 01127 CLAM_ASSERT(false,"InterpolateBPFPolarLog: Not implemented"); 01128 } 01129 void SpectrumInterpolator::InterpolateBPFPolarLinLog(Spectrum& in1, Spectrum& in2, Spectrum& out) 01130 { 01131 CLAM_ASSERT(false,"InterpolateBPFPolarLinLog: Not implemented"); 01132 } 01133 void SpectrumInterpolator::InterpolateBPFMagPhaseLog(Spectrum& in1, Spectrum& in2, Spectrum& out) 01134 { 01135 CLAM_ASSERT(false,"InterpolateBPFMagPhaseLog: Not implemented"); 01136 } 01137 void SpectrumInterpolator::InterpolateBPFMagPhaseLinLog(Spectrum& in1, Spectrum& in2, Spectrum& out) 01138 { 01139 CLAM_ASSERT(false,"InterpolateBPFMagPhaseLinLog: Not implemented"); 01140 } 01141 } 01142
1.7.6.1