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