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