SpectralPeakArray.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 "SpectralPeakArray.hxx"
00023 #include "ProcessingDataPlugin.hxx"
00024 
00025 namespace CLAM{
00026 
00027 namespace Hidden
00028 {
00029         static ProcessingDataPlugin::Registrator<SpectralPeakArray> dataRegistrator("lightcoral");
00030 }
00031 
00032 
00033 
00035 //
00036 // SpectralPeakArray
00037 //
00039 
00040 
00041 SpectralPeakArray::SpectralPeakArray(const SpectralPeak& prototype):ProcessingData(eNumAttr)
00042 {
00043         MandatoryInit();
00044         InitFromPrototype(prototype);
00045 }
00046 
00047 void SpectralPeakArray::DefaultInit()
00048 {
00049         //Initializing minimum set of attributes (mag, freq and scale)
00050         AddFreqBuffer();
00051         AddMagBuffer();
00052         // MRJ: More forgotten donuts here. What am I missing here?
00053         // SpectralPeakDetect::CheckOutputType demands all SpectralPeakArrays to comply
00054         // with the following dyn attribs
00055         AddBinWidthBuffer();
00056         AddBinPosBuffer();
00057         AddPhaseBuffer();
00058         // MRJ: End of SpectralPeakDetect::CheckOutputType attributes requirements. It
00059         // seems that someone did an optimization that consisted in not adding these
00060         // in SpectralPeakDetect::CheckOutputType... Merlijn?
00061         AddScale();
00062         AddMinimizeResizes();
00063         UpdateData();
00064         SetScale(EScale(EScale::eLinear));
00065         SetnPeaks(0);
00066         SetMinimizeResizes(1);
00067         mIsIndexUpToDate=false;
00068 }
00069 
00070 void SpectralPeakArray::InitPeak(SpectralPeak& spectralPeak) const
00071 {
00072         if (HasMagBuffer())
00073                 spectralPeak.AddMag();
00074         else spectralPeak.RemoveMag();
00075         if (HasFreqBuffer())
00076                 spectralPeak.AddFreq();
00077         else spectralPeak.RemoveFreq();
00078         if (HasPhaseBuffer())
00079                 spectralPeak.AddPhase();
00080         else spectralPeak.RemovePhase();
00081         if (HasBinWidthBuffer())
00082                 spectralPeak.AddBinWidth();
00083         else spectralPeak.RemoveBinWidth();
00084         if (HasBinPosBuffer())
00085                 spectralPeak.AddBinPos();
00086         else spectralPeak.RemoveBinPos();
00087         spectralPeak.UpdateData();
00088         spectralPeak.SetScale(GetScale());
00089 }
00090 
00091 void SpectralPeakArray::InitFromPrototype(const SpectralPeak& spectralPeak)
00092 {
00093         if (spectralPeak.HasMag())
00094                 AddMagBuffer();
00095         else if (HasMagBuffer())
00096                 RemoveMagBuffer();
00097         if (spectralPeak.HasFreq())
00098                 AddFreqBuffer();
00099         else if (HasFreqBuffer())
00100                 RemoveFreqBuffer();
00101         if (spectralPeak.HasPhase())
00102                 AddPhaseBuffer();
00103         else if (HasPhaseBuffer())
00104                 RemovePhaseBuffer();
00105         if (spectralPeak.HasBinWidth())
00106                 AddBinWidthBuffer();
00107         else if (HasBinWidthBuffer())
00108                 RemoveBinWidthBuffer();
00109         if (spectralPeak.HasBinPos())
00110                 AddBinPosBuffer();
00111         else if (HasBinPosBuffer())
00112                 RemoveBinPosBuffer();
00113         UpdateData();
00114 }
00115 
00116 
00117 void SpectralPeakArray::InitSpectralPeak(SpectralPeak& spectralPeak) const
00118 {
00119         if (HasMagBuffer()) spectralPeak.AddMag();
00120         else spectralPeak.RemoveMag();
00121         if (HasFreqBuffer()) spectralPeak.AddFreq();
00122         else spectralPeak.RemoveFreq();
00123         if (HasPhaseBuffer()) spectralPeak.AddPhase();
00124         else spectralPeak.RemovePhase();
00125         if (HasBinWidthBuffer()) spectralPeak.AddBinWidth();
00126         else spectralPeak.RemoveBinWidth();
00127         if (HasBinPosBuffer()) spectralPeak.AddBinPos();
00128         else spectralPeak.RemoveBinPos();
00129 
00130         spectralPeak.UpdateData();
00131 }
00132 
00133 bool SpectralPeakArray::IsCorrectPrototype(const SpectralPeak& spectralPeak) const
00134 {
00135         if (HasMagBuffer()!=spectralPeak.HasMag())
00136                 return false;
00137         if (HasFreqBuffer()!=spectralPeak.HasFreq())
00138                 return false;
00139         if (HasPhaseBuffer()!=spectralPeak.HasPhase())
00140                 return false;
00141         if (HasBinWidthBuffer()!=spectralPeak.HasBinWidth())
00142                 return false;
00143         if (HasBinPosBuffer()!=spectralPeak.HasBinPos())
00144                 return false;
00145         return true;
00146 }
00147 
00148 
00149 SpectralPeak SpectralPeakArray::GetThruIndexSpectralPeak(TIndex pos) const
00150 {
00151         return GetSpectralPeak(GetIndexArray()[pos]);
00152 }
00153 
00154 SpectralPeak SpectralPeakArray::GetSpectralPeak(TIndex pos) const
00155 {
00156         SpectralPeak tmpPeak;
00157         CLAM_ASSERT(pos<GetnPeaks()&&pos>=0,"SpectralPeakArray::GetSpectralPeak:Out of bounds in peak array");
00158         InitSpectralPeak(tmpPeak);
00159         if (HasMagBuffer())
00160                 tmpPeak.SetMag(GetMag(pos));
00161         if (HasFreqBuffer())
00162                 tmpPeak.SetFreq(GetFreq(pos));
00163         if (HasPhaseBuffer())
00164                 tmpPeak.SetPhase(GetPhase(pos));
00165         if (HasBinWidthBuffer())
00166                 tmpPeak.SetBinWidth(GetBinWidth(pos));
00167         if (HasBinPosBuffer())
00168                 tmpPeak.SetBinPos(GetBinPos(pos));
00169         tmpPeak.SetScale(GetScale());
00170 
00171         return tmpPeak;
00172 }
00173 
00174 
00175 void SpectralPeakArray::SetSpectralPeak(TIndex pos,const SpectralPeak& spectralPeak,TIndex index)
00176 {
00177         CLAM_ASSERT(spectralPeak.GetScale()==GetScale(),"SpectralPeakArray::SetSpectralPeak:Incorrect scale in input SpectralPeak");
00178         CLAM_ASSERT(pos<GetnPeaks(),"SpectralPeakArray::SetSpectralPeak:Out of bounds in peak array");
00179         CLAM_ASSERT(IsCorrectPrototype(spectralPeak),"SpectralPeakArray::SetSpectralPeak:Incorrect prototype for Spectral Peak");
00180 
00181         if (HasMagBuffer())
00182                 GetMagBuffer()[pos]=spectralPeak.GetMag();
00183         if (HasFreqBuffer())
00184                 GetFreqBuffer()[pos]=spectralPeak.GetFreq();
00185         if (HasPhaseBuffer())
00186                 GetPhaseBuffer()[pos]=spectralPeak.GetPhase();
00187         if (HasBinWidthBuffer())
00188                 GetBinWidthBuffer()[pos]=spectralPeak.GetBinWidth();
00189         if (HasBinPosBuffer())
00190                 GetBinPosBuffer()[pos]=spectralPeak.GetBinPos();
00191         if(HasIndexArray())
00192                 GetIndexArray()[pos]=index;
00193 }
00194 
00195 void SpectralPeakArray::InsertSpectralPeak(TIndex pos,const SpectralPeak& spectralPeak,bool insertIndex,TIndex index)
00196 {
00197         CLAM_ASSERT(spectralPeak.GetScale()==GetScale(),"SpectralPeakArray::InsertSpectralPeak:Incorrect scale in input SpectralPeak");
00198         CLAM_ASSERT(pos<GetnPeaks(),"SpectralPeakArray::InsertSpectralPeak:Out of bounds in peak array");
00199         CLAM_ASSERT(IsCorrectPrototype(spectralPeak),"SpectralPeakArray::InsertSpectralPeak:Incorrect prototype for Spectral Peak");
00200         if (HasMagBuffer())
00201                 GetMagBuffer().InsertElem(pos,spectralPeak.GetMag());
00202         if (HasFreqBuffer())
00203                 GetFreqBuffer().InsertElem(pos,spectralPeak.GetFreq());
00204         if (HasPhaseBuffer())
00205                 GetPhaseBuffer().InsertElem(pos,spectralPeak.GetPhase());
00206         if (HasBinWidthBuffer())
00207                 GetBinWidthBuffer().InsertElem(pos,spectralPeak.GetBinWidth());
00208         if (HasBinPosBuffer())
00209                 GetBinPosBuffer().InsertElem(pos,spectralPeak.GetBinPos());
00210         if(insertIndex&&HasIndexArray())
00211                 GetIndexArray().InsertElem(pos,index);
00212 }
00213 
00214 void SpectralPeakArray::AddSpectralPeak(const SpectralPeak& spectralPeak,bool addIndex,TIndex index)
00215 {
00216         CLAM_ASSERT(spectralPeak.GetScale()==GetScale(),"SpectralPeakArray::AddSpectralPeak:Incorrect scale in input SpectralPeak");
00217         CLAM_ASSERT(IsCorrectPrototype(spectralPeak),"SpectralPeakArray::AddSpectralPeak:Incorrect prototype for Spectral Peak");
00218         if (HasMagBuffer())
00219                 GetMagBuffer().AddElem(spectralPeak.GetMag());
00220         if (HasFreqBuffer())
00221                 GetFreqBuffer().AddElem(spectralPeak.GetFreq());
00222         if (HasPhaseBuffer())
00223                 GetPhaseBuffer().AddElem(spectralPeak.GetPhase());
00224         if (HasBinWidthBuffer())
00225                 GetBinWidthBuffer().AddElem(spectralPeak.GetBinWidth());
00226         if (HasBinPosBuffer())
00227                 GetBinPosBuffer().AddElem(spectralPeak.GetBinPos());
00228         if(addIndex&&HasIndexArray())
00229                 GetIndexArray().AddElem(index);
00230 }
00231 
00232 void SpectralPeakArray::DeleteSpectralPeak(TIndex pos,bool deleteIndex)
00233 {
00234         CLAM_ASSERT(GetnPeaks()>0&&pos<GetnPeaks(),"SpectralPeakArray::DeleteSpectralPeak:No spectral peak at that position");
00235         if (HasMagBuffer())
00236                 GetMagBuffer().DeleteElem(pos);
00237         if (HasFreqBuffer())
00238                 GetFreqBuffer().DeleteElem(pos);
00239         if (HasPhaseBuffer())
00240                 GetPhaseBuffer().DeleteElem(pos);
00241         if (HasBinWidthBuffer())
00242                 GetBinWidthBuffer().DeleteElem(pos);
00243         if (HasBinPosBuffer())
00244                 GetBinPosBuffer().DeleteElem(pos);
00245         if(deleteIndex&&HasIndexArray())
00246                 GetIndexArray().DeleteElem(pos);
00247 }
00248 
00249 /*Index functionality*/
00250 
00251 
00252 TIndex SpectralPeakArray::GetPositionFromIndex(TIndex index) const
00253 {
00254         CLAM_ASSERT(HasIndexArray(),"SpectralPeakArray::GetPositionFromIndex: Index array is not instantiated");
00255         CLAM_ASSERT(mIsIndexUpToDate,"SpectralPeakArray::GetPositionFromIndex: Index table is not up to date");
00256 
00257         //Note: cannot use searching routines because index may not be sorted
00258 
00259         IndexArray& indexArray=GetIndexArray();
00260         for(int i=0;i<indexArray.Size();i++)
00261         {
00262                 if(indexArray[i]==index) return i;//index found
00263         }
00264         return -1;//index not found
00265 
00266 }
00267 
00268 TIndex SpectralPeakArray::GetMaxMagPos() const// returns position of mag maximum
00269 {
00270         const DataArray & peakMagBuffer=GetMagBuffer();
00271         // initialize to the first element
00272         double max = peakMagBuffer[0];
00273         TIndex maxPos=0;
00274         for (TIndex i=1;i<GetnPeaks();i++)
00275         {
00276                 const double & mag = peakMagBuffer[i];
00277                 if (mag>max)
00278                 {
00279                         max = mag;
00280                         maxPos = i;
00281                 }
00282         }
00283         return maxPos;
00284 
00285 }
00286 
00287 
00288 TIndex SpectralPeakArray::GetMaxMagIndex() const// returns position of mag maximum
00289 {
00290         const DataArray & peakMagBuffer=GetMagBuffer();
00291         // only indexed peaks. It returns the max position
00292 
00293         CLAM_ASSERT(HasIndexArray(),"SpectralPeakArray::GetMaxMagPosition: Index array is not instantiated");
00294         CLAM_ASSERT(mIsIndexUpToDate,"SpectralPeakArray::GetMaxMagPosition: IndexTable is not up to date");
00295         const IndexArray & indexArray = GetIndexArray();
00296         TIndex maxPos=0;
00297         double max = peakMagBuffer[indexArray[0]];
00298         // initialize to the first element
00299         for (TIndex i=1; i<indexArray.Size(); i++)
00300         {
00301                 const double & mag = peakMagBuffer[indexArray[i]];
00302                 if (mag>max)
00303                 {
00304                         max = mag;
00305                         maxPos = i;
00306                 }
00307         }
00308         return maxPos;
00309 
00310 }
00311 
00312 void SpectralPeakArray::ResetIndices() // reset all indices
00313 {
00314 
00315         CLAM_ASSERT(HasIndexArray(),"SpectralPeakArray::ResetIndices: Index array is not instantiated");
00316         IndexArray& indexArray=GetIndexArray();
00317         TSize nPeaks=GetnPeaks();
00318         //Resize will only be done once
00319         if(indexArray.AllocatedSize()!=GetnMaxPeaks())
00320                 indexArray.Resize(GetnMaxPeaks());
00321         // set size to the number of Peaks
00322         indexArray.SetSize(nPeaks);
00323 
00324         indexArray.Reset();
00325         mIsIndexUpToDate=true;
00326 }
00327 
00328 void SpectralPeakArray::InitIndices() // Initialize all indices to -1 and set size
00329 {
00330         CLAM_ASSERT(HasIndexArray(),"SpectralPeakArray::InitIndices: Index array is not instantiated");
00331         int i;
00332         IndexArray& indexArray=GetIndexArray();
00333         TSize nPeaks=GetnPeaks();
00334         TSize nMaxPeaks=GetnMaxPeaks();
00335         indexArray.Resize(nMaxPeaks);
00336         indexArray.SetSize(nPeaks);
00337         for(i=0;i<nPeaks;i++)
00338         {
00339                 indexArray[i]=-1;
00340         }
00341         mIsIndexUpToDate=false;
00342 }
00343 
00344 void SpectralPeakArray::SetIndicesTo(TIndex val) // Initialize all indices to -1 without resize
00345 {                                                // is upToDate = true !!!
00346         CLAM_ASSERT(HasIndexArray(),"SpectralPeakArray::SetIndicesTo: Index array is not instantiated");
00347         int i;
00348         IndexArray& indexArray=GetIndexArray();
00349         TSize nPeaks=GetnPeaks();
00350         for(i=0;i<nPeaks;i++)
00351         {
00352                 indexArray[i]=-1;
00353         }
00354         mIsIndexUpToDate=true;
00355 }
00356 
00357 /* Delete an element from the index array, but not deleting the Peak information */
00358 void SpectralPeakArray::DeleteIndex (TIndex index)
00359 {
00360         CLAM_ASSERT(HasIndexArray(),"SpectralPeakArray::DeleteIndex: Index array is not instantiated");
00361         TIndex pos = GetPositionFromIndex(index);
00362         if (pos>-1)
00363                 GetIndexArray().DeleteElem(pos);
00364 }
00365 
00366 void SpectralPeakArray::AddIndex(TIndex index)
00367 {
00368         CLAM_ASSERT(HasIndexArray(),"SpectralPeakArray::AddIndex: Index array is not instantiated");
00369         GetIndexArray().AddElem(index);
00370 }
00371 
00372 TIndex SpectralPeakArray::GetFirstNonValidIndexPosition(TIndex beginAt) const
00373 {
00374         IndexArray& indexArray=GetIndexArray();
00375         CLAM_ASSERT(HasIndexArray(),"SpectralPeakArray::GetFirstNonValidIndexPosition: Index array is not instantiated");
00376         CLAM_ASSERT(beginAt<indexArray.Size()&&beginAt>=0,"SpectralPeakArray::SetThruIndexFreq: Out of bounds in Index Array");
00377         for(int i=beginAt;i<indexArray.Size();i++)
00378         {
00379                 if(indexArray[i]==-1) return i;
00380         }
00381         return -1;
00382 }
00383 
00384 int SpectralPeakArray::GetnIndexedPeaks() const
00385 {
00386         int i, nIndexedPeaks=0;
00387         IndexArray& indexArray=GetIndexArray();
00388         CLAM_ASSERT(HasIndexArray(),"SpectralPeakArray::GetFirstNonValidIndexPosition: Index array is not instantiated");
00389         for(i=0;i<indexArray.Size();i++)
00390         {
00391                 if(indexArray[i]!=-1) nIndexedPeaks++;
00392         }
00393         return nIndexedPeaks;
00394 }
00395 
00396 /* Getters from index */
00397 
00398 double SpectralPeakArray::GetThruIndexFreq(TIndex pos) const
00399 {
00400         CLAM_ASSERT(HasIndexArray(),"SpectralPeakArray::GetThruIndexFreq: Index array is not instantiated");
00401         CLAM_ASSERT(mIsIndexUpToDate,"SpectralPeakArray::GetThruIndexFreq: IndexTable is not up to date");
00402         CLAM_ASSERT(pos<GetIndexArray().Size()&&pos>=0,"SpectralPeakArray::GetThruIndexFreq: Out of bounds in Index Array");
00403         return GetFreq(GetIndexArray()[pos]);
00404 }
00405 
00406 double SpectralPeakArray::GetThruIndexMag(TIndex pos) const
00407 {
00408         CLAM_ASSERT(HasIndexArray(),"SpectralPeakArray::GetThruIndexMag: Index array is not instantiated");
00409         CLAM_ASSERT(mIsIndexUpToDate,"SpectralPeakArray::GetThruIndexMag: IndexTable is not up to date");
00410         CLAM_ASSERT(pos<GetIndexArray().Size()&&pos>=0,"SpectralPeakArray::GetThruIndexMag: Out of bounds in Index Array");
00411         return GetMag(GetIndexArray()[pos]);
00412 }
00413 
00414 double SpectralPeakArray::GetThruIndexPhase(TIndex pos) const
00415 {
00416         CLAM_ASSERT(HasIndexArray(),"SpectralPeakArray::GetThruIndexPhase: Index array is not instantiated");
00417         CLAM_ASSERT(mIsIndexUpToDate,"SpectralPeakArray::GetThruIndexPhase: IndexTable is not up to date");
00418         CLAM_ASSERT(pos<GetIndexArray().Size()&&pos>=0,"SpectralPeakArray::GetThruIndexPhase: Out of bounds in Index Array");
00419         return GetPhase(GetIndexArray()[pos]);
00420 }
00421 
00422 double SpectralPeakArray::GetThruIndexBinPos(TIndex pos) const
00423 {
00424         CLAM_ASSERT(HasIndexArray(),"SpectralPeakArray::GetThruIndexBinPos: Index array is not instantiated");
00425         CLAM_ASSERT(mIsIndexUpToDate,"SpectralPeakArray::GetThruIndexBinPos: IndexTable is not up to date");
00426         CLAM_ASSERT(pos<GetIndexArray().Size()&&pos>=0,"SpectralPeakArray::GetThruIndexBinPos: Out of bounds in Index Array");
00427         return GetBinPos(GetIndexArray()[pos]);
00428 }
00429 
00430 TSize SpectralPeakArray::GetThruIndexBinWidth(TIndex pos) const
00431 {
00432         CLAM_ASSERT(HasIndexArray(),"SpectralPeakArray::GetThruIndexBinWidth: Index array is not instantiated");
00433         CLAM_ASSERT(mIsIndexUpToDate,"SpectralPeakArray::GetThruIndexBinWidth: IndexTable is not up to date");
00434         CLAM_ASSERT(pos<GetIndexArray().Size()&&pos>=0,"SpectralPeakArray::GetThruIndexBinWidth: Out of bounds in Index Array");
00435         return GetBinWidth(GetIndexArray()[pos]);
00436 }
00437 
00438 /* Setters from index */
00439 void SpectralPeakArray::SetThruIndexSpectralPeak(TIndex pos,SpectralPeak& peak)
00440 {
00441         CLAM_ASSERT(HasIndexArray(),"SpectralPeakArray::SetThruIndexSpectralPeak: Index array is not instantiated");
00442         CLAM_ASSERT(mIsIndexUpToDate,"SpectralPeakArray::SetThruIndexSpectralPeak: IndexTable is not up to date");
00443         CLAM_ASSERT(pos<GetIndexArray().Size()&&pos>=0,"SpectralPeakArray::SetThruIndexSpectralPeak: Out of bounds in Index Array");
00444         SetSpectralPeak(GetIndexArray()[pos],peak);
00445 }
00446 
00447 
00448 
00449 void SpectralPeakArray::SetThruIndexFreq(TIndex pos,double freq)
00450 {
00451         CLAM_ASSERT(HasIndexArray(),"SpectralPeakArray::SetThruIndexFreq: Index array is not instantiated");
00452         CLAM_ASSERT(mIsIndexUpToDate,"SpectralPeakArray::SetThruIndexFreq: IndexTable is not up to date");
00453         CLAM_ASSERT(pos<GetIndexArray().Size()&&pos>=0,"SpectralPeakArray::SetThruIndexFreq: Out of bounds in Index Array");
00454         SetFreq(GetIndexArray()[pos],freq);
00455 }
00456 
00457 void SpectralPeakArray::SetThruIndexMag(TIndex pos,double mag)
00458 {
00459         CLAM_ASSERT(HasIndexArray(),"SpectralPeakArray::SetThruIndexMag: Index array is not instantiated");
00460         CLAM_ASSERT(mIsIndexUpToDate,"SpectralPeakArray::SetThruIndexMag: IndexTable is not up to date");
00461         CLAM_ASSERT(pos<GetIndexArray().Size()&&pos>=0,"SpectralPeakArray::SetThruIndexMag: Out of bounds in Index Array");
00462         SetMag(GetIndexArray()[pos],mag);
00463 }
00464 
00465 void SpectralPeakArray::SetThruIndexPhase(TIndex pos,double phase)
00466 {
00467         CLAM_ASSERT(HasIndexArray(),"SpectralPeakArray::SetThruIndexPhase: Index array is not instantiated");
00468         CLAM_ASSERT(mIsIndexUpToDate,"SpectralPeakArray::SetThruIndexPhase: IndexTable is not up to date");
00469         CLAM_ASSERT(pos<GetIndexArray().Size()&&pos>=0,"SpectralPeakArray::SetThruIndexPhase: Out of bounds in Index Array");
00470         SetPhase(GetIndexArray()[pos],phase);
00471 }
00472 
00473 void SpectralPeakArray::SetThruIndexBinPos(TIndex pos,double binPos)
00474 {
00475         CLAM_ASSERT(HasIndexArray(),"SpectralPeakArray::SetThruIndexBinPos: Index array is not instantiated");
00476         CLAM_ASSERT(mIsIndexUpToDate,"SpectralPeakArray::SetThruIndexBinPos: IndexTable is not up to date");
00477         CLAM_ASSERT(pos<GetIndexArray().Size()&&pos>=0,"SpectralPeakArray::SetThruIndexBinPos: Out of bounds in Index Array");
00478         SetBinPos(GetIndexArray()[pos],binPos);
00479 }
00480 
00481 void SpectralPeakArray::SetThruIndexBinWidth(TIndex pos,TSize binWidth)
00482 {
00483         CLAM_ASSERT(HasIndexArray(),"SpectralPeakArray::SetThruIndexBinWidth: Index array is not instantiated");
00484         CLAM_ASSERT(mIsIndexUpToDate,"SpectralPeakArray::SetThruIndexBinWidth: IndexTable is not up to date");
00485         CLAM_ASSERT(pos<GetIndexArray().Size()&&pos>=0,"SpectralPeakArray::SetThruIndexBinWidth: Out of bounds in Index Array");
00486         SetBinWidth(GetIndexArray()[pos],binWidth);
00487 }
00488 
00489 void SpectralPeakArray::TodB()
00490 {
00491         if(GetScale()==EScale::eLog) return;
00492 
00493         DataArray & mag = GetMagBuffer();
00494         const int nPeaks=GetnPeaks();
00495         for (int i=0; i<nPeaks; i++)
00496         {
00497                 if(mag[i]==0) mag[i]=TData(0.0001);
00498                 mag[i]= CLAM_20log10(mag[i]);
00499         }
00500         SetScale(EScale::eLog);
00501 }
00502 
00503 void SpectralPeakArray::ToLinear()
00504 {
00505         if(GetScale()==EScale::eLinear) return;
00506 
00507         DataArray & mag = GetMagBuffer();
00508         const int nPeaks=GetnPeaks();
00509         for (int i=0; i<nPeaks; i++)
00510         {
00511                 if(mag[i]==0.0001) mag[i]=0;
00512                 mag[i]= log2lin(mag[i]);
00513         }
00514         SetScale(EScale::eLinear);
00515 }
00516 
00517 void SpectralPeakArray::CopyMembers(SpectralPeakArray& sourceSpectralPeakArray)
00518 {
00519         SetScale( sourceSpectralPeakArray.GetScale() );
00520         SetMinimizeResizes( sourceSpectralPeakArray.GetMinimizeResizes() );
00521 
00522         SetnMaxPeaks(sourceSpectralPeakArray.GetnMaxPeaks());
00523         SetnPeaks(sourceSpectralPeakArray.GetnPeaks());
00524         ResetIndices();
00525 
00526         DataArray& srcFreqBuffer = sourceSpectralPeakArray.GetFreqBuffer();
00527         DataArray& srcMagBuffer = sourceSpectralPeakArray.GetMagBuffer();
00528         DataArray& srcPhaseBuffer = sourceSpectralPeakArray.GetPhaseBuffer();
00529         DataArray& srcBinPosBuffer = sourceSpectralPeakArray.GetBinPosBuffer();
00530         DataArray& srcBinWidthBuffer = sourceSpectralPeakArray.GetBinWidthBuffer();
00531         IndexArray& srcIndexArray = sourceSpectralPeakArray.GetIndexArray();
00532 
00533         DataArray& targetFreqBuffer = GetFreqBuffer();
00534         DataArray& targetMagBuffer = GetMagBuffer();
00535         DataArray& targetPhaseBuffer = GetPhaseBuffer();
00536         DataArray& targetBinPosBuffer = GetBinPosBuffer();
00537         DataArray& targetBinWidthBuffer = GetBinWidthBuffer();
00538         IndexArray& targetIndexArray = GetIndexArray();
00539 
00540         int numberOfPeaks = sourceSpectralPeakArray.GetnPeaks();
00541         for (int r=0; r < numberOfPeaks;r++)
00542         {
00543                 // get frequency , mag and phase
00544                 targetFreqBuffer[r] = srcFreqBuffer[r];
00545                 targetMagBuffer[r] = srcMagBuffer[r];
00546                 targetPhaseBuffer[r] = srcPhaseBuffer[r];
00547                 targetBinPosBuffer[r] = srcBinPosBuffer[r];
00548                 targetBinWidthBuffer[r] = srcBinWidthBuffer[r];
00549                 targetIndexArray[r] = srcIndexArray[r];
00550         }
00551 
00552 }
00553 
00554 //xamat: this operator is bound to fail if operands have different attributes, should add checking?
00555 SpectralPeakArray SpectralPeakArray::operator+(const SpectralPeakArray& in)
00556 {
00557         CLAM_ASSERT(in.HasMagBuffer(), "SpectralPeakArray::operator+: second operand needs to have a Magnitude Buffer");
00558         CLAM_ASSERT(in.HasFreqBuffer(), "SpectralPeakArray::operator+: second operand needs to have a Frequency Buffer");
00559         CLAM_ASSERT(in.HasPhaseBuffer(), "SpectralPeakArray::operator+: second operand needs to have a Phase Buffer");
00560         CLAM_ASSERT(in.HasIndexArray(), "SpectralPeakArray::operator+: second operand needs to have an Index Array");
00561         CLAM_ASSERT(HasMagBuffer(), "SpectralPeakArray::operator+: first operand needs to have a Magnitude Buffer");
00562         CLAM_ASSERT(HasFreqBuffer(), "SpectralPeakArray::operator+: first operand needs to have a Frequency Buffer");
00563         CLAM_ASSERT(HasPhaseBuffer(), "SpectralPeakArray::operator+: first operand needs to have a Phase Buffer");
00564         CLAM_ASSERT(HasIndexArray(), "SpectralPeakArray::operator+: first operand needs to have an Index Array");
00565 
00566 
00567         SpectralPeakArray tmp;
00568         tmp.AddIndexArray();
00569         tmp.UpdateData();
00570         tmp.SetScale(in.GetScale());
00571 
00572         tmp.SetnMaxPeaks(GetnMaxPeaks()+in.GetnMaxPeaks());
00573         tmp.SetnPeaks(tmp.GetnMaxPeaks());
00574 
00575         int origIndex=0,inIndex=0;
00576         DataArray& inPeakMagArray = in.GetMagBuffer();
00577         DataArray& inPeakFreqArray = in.GetFreqBuffer();
00578         DataArray& inPeakPhaseArray = in.GetPhaseBuffer();
00579         IndexArray& inPeakIndexArray = in.GetIndexArray();
00580 
00581         DataArray& origPeakMagArray = GetMagBuffer();
00582         DataArray& origPeakFreqArray = GetFreqBuffer();
00583         DataArray& origPeakPhaseArray = GetPhaseBuffer();
00584         IndexArray& origPeakIndexArray = GetIndexArray();
00585 
00586         DataArray& tmpPeakMagArray = tmp.GetMagBuffer();
00587         DataArray& tmpPeakFreqArray = tmp.GetFreqBuffer();
00588         DataArray& tmpPeakPhaseArray = tmp.GetPhaseBuffer();
00589         IndexArray& tmpPeakIndexArray = tmp.GetIndexArray();
00590 
00591         bool finished=false,finishedOrig=false, finishedIn=false;
00592         TSize origSize,inSize;
00593         origSize=GetnPeaks();
00594         inSize=in.GetnPeaks();
00595         //xamat optimizing
00596         int nAddedPeaks = 0;
00597         while(!finished)
00598         {
00599                 if(origIndex>=origSize-1) finishedOrig=true;
00600                 if(inIndex>=inSize-1) finishedIn=true;
00601                 //add always peak with lower freq. If both are equal, add magnitudes (and take original phase?)
00602                 if(finishedOrig)
00603                 {
00604                         if(!finishedIn)
00605                         {
00606                                 //note: we could overload the SetSpectralPeak operator but not passing a SpectralPeaks but the values
00607                                 tmpPeakMagArray[nAddedPeaks] = inPeakMagArray[inIndex];
00608                                 tmpPeakFreqArray[nAddedPeaks] = inPeakFreqArray[inIndex];
00609                                 tmpPeakPhaseArray[nAddedPeaks] = inPeakMagArray[inIndex];
00610                                 tmpPeakIndexArray[nAddedPeaks] = inPeakIndexArray[inIndex]*2+1;
00611 
00612                                 nAddedPeaks++;
00613                                 inIndex++;
00614                         }
00615                         else finished=true;
00616                 }
00617                 else if(finishedIn)
00618                 {
00619                         if(!finishedOrig)
00620                         {
00621                                 tmpPeakMagArray[nAddedPeaks] = origPeakMagArray[origIndex];
00622                                 tmpPeakFreqArray[nAddedPeaks] = origPeakFreqArray[origIndex];
00623                                 tmpPeakPhaseArray[nAddedPeaks] = origPeakMagArray[origIndex];
00624                                 tmpPeakIndexArray[nAddedPeaks] = origPeakIndexArray[origIndex]*2;
00625 
00626                                 nAddedPeaks++;
00627                                 origIndex++;
00628                         }
00629                         else finished=true;
00630                 }
00631                 else
00632                 {
00633                         if(origPeakFreqArray[origIndex]<inPeakFreqArray[inIndex])
00634                         {
00635                                 tmpPeakMagArray[nAddedPeaks] = origPeakMagArray[origIndex];
00636                                 tmpPeakFreqArray[nAddedPeaks] = origPeakFreqArray[origIndex];
00637                                 tmpPeakPhaseArray[nAddedPeaks] = origPeakMagArray[origIndex];
00638                                 tmpPeakIndexArray[nAddedPeaks] = origPeakIndexArray[origIndex]*2;
00639                                 nAddedPeaks++;
00640                                 origIndex++;
00641                         }
00642                         else if(origPeakFreqArray[origIndex]>inPeakFreqArray[inIndex])
00643                         {
00644                                 tmpPeakMagArray[nAddedPeaks] = inPeakMagArray[inIndex];
00645                                 tmpPeakFreqArray[nAddedPeaks] = inPeakFreqArray[inIndex];
00646                                 tmpPeakPhaseArray[nAddedPeaks] = inPeakMagArray[inIndex];
00647                                 tmpPeakIndexArray[nAddedPeaks] = inPeakIndexArray[inIndex]*2+1;
00648 
00649                                 nAddedPeaks++;
00650                                 inIndex++;
00651                         }
00652                         else
00653                         {
00654                                 tmpPeakMagArray[nAddedPeaks] = origPeakMagArray[origIndex];
00655                                 tmpPeakFreqArray[nAddedPeaks] = origPeakFreqArray[origIndex];
00656                                 tmpPeakPhaseArray[nAddedPeaks] = origPeakMagArray[origIndex];
00657                                 tmpPeakIndexArray[nAddedPeaks] = origPeakIndexArray[origIndex]*2;
00658 
00659                                 nAddedPeaks++;
00660                                 origIndex++;
00661                                 tmpPeakMagArray[nAddedPeaks] = inPeakMagArray[inIndex];
00662                                 tmpPeakFreqArray[nAddedPeaks] = inPeakFreqArray[inIndex];
00663                                 tmpPeakPhaseArray[nAddedPeaks] = inPeakMagArray[inIndex];
00664                                 tmpPeakIndexArray[nAddedPeaks] = inPeakIndexArray[inIndex]*2+1;
00665 
00666                                 nAddedPeaks++;
00667                                 inIndex++;
00668                         }
00669                 }
00670         }
00671         tmp.SetnPeaks(nAddedPeaks);
00672         return tmp;
00673 }
00674 
00675 };//namespace
00676 
Generated by  doxygen 1.6.3