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

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