SpectrumAdder2.cxx

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

Generated on Tue Aug 12 22:33:45 2008 for CLAM by  doxygen 1.5.5