CLAM-Development  1.4.0
SpectralPeakArray.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
23 // Class SpectralPeakArray: Processing Data class to store an array of spectral peaks
25 // Description:
26 // This processing data class offers an interface for storing an array of spectral
27 // peaks while maintaining efficient data access. This is accomplished by actually
28 // storing in memory an independent buffer for each of the attributes of a spectral
29 // peak. The interface to use SpectralPeak class is simulated adding an interface
30 // that constructs these objects on the fly, whenever a Peak is accessed.
31 // Furthermore, this class holds an array of indices that can be used for different
32 // purposes such as sorting, assigning tracks...Any peak can be accessed throug its
33 // actual position in the array or its associated index
35 
36 
37 
38 #ifndef _SpectralPeakArray_
39 #define _SpectralPeakArray_
40 
41 #include "DynamicType.hxx"
42 #include "Array.hxx"
43 #include "IndexArray.hxx"
44 #include "DataTypes.hxx"
45 #include "OSDefines.hxx"
46 #include "Err.hxx"
47 #include "ProcessingData.hxx"
48 #include "SpectralPeak.hxx"
49 
50 
51 
52 namespace CLAM{
53 
54 
69 {
70 public:
72  DYN_ATTRIBUTE (0, public, EScale, Scale);
73  DYN_ATTRIBUTE (1, public, DataArray, MagBuffer);
74  DYN_ATTRIBUTE (2, public, DataArray, FreqBuffer);
75  DYN_ATTRIBUTE (3, public, DataArray, PhaseBuffer);
76  DYN_ATTRIBUTE (4, public, DataArray, BinPosBuffer);
77  DYN_ATTRIBUTE (5, public, DataArray, BinWidthBuffer);
78  DYN_ATTRIBUTE (6, public, IndexArray, IndexArray);
79  DYN_ATTRIBUTE (7, public, bool, MinimizeResizes);
80 
81 protected:
82  void DefaultInit();
83 public:
84 
90  SpectralPeakArray(const SpectralPeak& prototype);
91 
97  void InitFromPrototype(const SpectralPeak& spectralPeak);
98 
104  void InitPeak(SpectralPeak& spectralPeak) const;
105 
106 
111  TData GetMag(TIndex pos) const{return GetMagBuffer()[pos];}
116  TData GetFreq(TIndex pos)const{return GetFreqBuffer()[pos];}
121  TData GetPhase(TIndex pos)const{return GetPhaseBuffer()[pos];}
126  TData GetBinPos(TIndex pos)const{return GetBinPosBuffer()[pos];}
131  int GetBinWidth(TIndex pos)const{return int(GetBinWidthBuffer()[pos]);}
136  void SetMag(TIndex pos,TData newMag){GetMagBuffer()[pos]=newMag;}
141  void SetFreq(TIndex pos,TData newFreq){GetFreqBuffer()[pos]=newFreq;}
146  void SetPhase(TIndex pos,TData newPhase){GetPhaseBuffer()[pos]=newPhase;}
151  void SetBinPos(TIndex pos,TData newBinPos){GetBinPosBuffer()[pos]=newBinPos;}
156  void SetBinWidth(TIndex pos,int newBinWidth){GetBinWidthBuffer()[pos]=TData(newBinWidth);}
157 
161  TIndex GetMaxMagPos() const;
162 
166  TIndex GetMaxMagIndex() const;
167 
168 
175  void InitSpectralPeak(SpectralPeak& spectralPeak) const;
181  bool IsCorrectPrototype(const SpectralPeak& spectralPeak) const;
200  void SetSpectralPeak(TIndex pos,const SpectralPeak& spectralPeak,TIndex index=-1);
210  void InsertSpectralPeak(TIndex pos,const SpectralPeak& spectralPeak,
211  bool insertIndex=false,TIndex index=-1);
219  void AddSpectralPeak(const SpectralPeak& spectralPeak,bool addIndex=false,TIndex index=-1);
226  void DeleteSpectralPeak(TIndex pos,bool deleteIndex=false);
227 
228 
229 
230 /* index handling */
231 
235  int GetnIndexedPeaks() const;
236 
241  TIndex GetPositionFromIndex(TIndex index) const;
242 
247  void SetIndex(TIndex pos,TIndex index){GetIndexArray()[pos]=index;};
252  TIndex GetIndex(TIndex pos) const {return GetIndexArray()[pos];}
256  void DeleteIndex (TIndex index);
261  void ResetIndices();
266  void InitIndices();
271  void SetIndicesTo(TIndex val);
275  void AddIndex(TIndex index);
276 
287  bool IsValidIndex(TIndex position) const;
292  TIndex IsIndexExisting(TIndex index) const;
296  TIndex GetMaxIndex() const;
297 
298 /* Peak Array size handling*/
299 
305  TSize GetnPeaks() const
306  {
307  if(HasMagBuffer()) return GetMagBuffer().Size();
308  if(HasFreqBuffer()) return GetFreqBuffer().Size();
309  if(HasPhaseBuffer()) return GetPhaseBuffer().Size();
310  if(HasBinPosBuffer()) return GetBinPosBuffer().Size();
311  if(HasBinWidthBuffer()) return GetBinWidthBuffer().Size();
312  else return 0;
313  }
314 
319 // I think we need the Resize() as well. It does not hurt at least. - Yushen Han
320 
321  void SetnPeaks(TSize npeaks)
322  {
323  if (npeaks>GetnMaxPeaks()) SetnMaxPeaks(npeaks);
324  if(HasMagBuffer()) {
325  GetMagBuffer().Resize(npeaks);
326  GetMagBuffer().SetSize(npeaks);}
327  if(HasFreqBuffer()) {
328  GetFreqBuffer().Resize(npeaks);
329  GetFreqBuffer().SetSize(npeaks);}
330  if(HasPhaseBuffer()){
331  GetPhaseBuffer().Resize(npeaks);
332  GetPhaseBuffer().SetSize(npeaks);}
333  if(HasBinPosBuffer()) {
334  GetBinPosBuffer().Resize(npeaks);
335  GetBinPosBuffer().SetSize(npeaks);}
336  if(HasBinWidthBuffer()) {
337  GetBinWidthBuffer().Resize(npeaks);
338  GetBinWidthBuffer().SetSize(npeaks);}
339  if(HasIndexArray()) {
340  GetIndexArray().Resize(npeaks);
341  GetIndexArray().SetSize(npeaks);}
342 
343 
344  }
345 
349  void SetnMaxPeaks(TSize npeaks)
350  {
351  if(HasMagBuffer()){
352  GetMagBuffer().Resize(npeaks);
353  if(GetMinimizeResizes())
354  GetMagBuffer().SetStep(npeaks);}
355  if(HasFreqBuffer()){
356  GetFreqBuffer().Resize(npeaks);
357  if(GetMinimizeResizes())
358  GetFreqBuffer().SetStep(npeaks);}
359  if(HasPhaseBuffer()){
360  GetPhaseBuffer().Resize(npeaks);
361  if(GetMinimizeResizes())
362  GetPhaseBuffer().SetStep(npeaks);}
363  if(HasBinPosBuffer()){
364  GetBinPosBuffer().Resize(npeaks);
365  if(GetMinimizeResizes())
366  GetBinPosBuffer().SetStep(npeaks);}
367  if(HasBinWidthBuffer()){
368  GetBinWidthBuffer().Resize(npeaks);
369  if(GetMinimizeResizes())
370  GetBinWidthBuffer().SetStep(npeaks);}
371  if(HasIndexArray()){
372  GetIndexArray().Resize(npeaks);
373  if(GetMinimizeResizes())
374  GetIndexArray().SetStep(npeaks);}
375  }
376 
383  {
384  if(HasMagBuffer()) return GetMagBuffer().AllocatedSize();
385  if(HasFreqBuffer()) return GetFreqBuffer().AllocatedSize();
386  if(HasPhaseBuffer()) return GetPhaseBuffer().AllocatedSize();
387  if(HasBinPosBuffer()) return GetBinPosBuffer().AllocatedSize();
388  if(HasBinWidthBuffer()) return GetBinWidthBuffer().AllocatedSize();
389  else return 0;
390  }
391 
392  //index getters and setters
393  double GetThruIndexFreq(TIndex pos) const;
394  double GetThruIndexMag(TIndex pos) const;
395  double GetThruIndexPhase(TIndex pos) const;
396  double GetThruIndexBinPos(TIndex pos) const;
397  TSize GetThruIndexBinWidth(TIndex pos) const;
398  void SetThruIndexFreq(TIndex pos,double freq);
399  void SetThruIndexMag(TIndex pos,double mag) ;
400  void SetThruIndexPhase(TIndex pos,double phase) ;
401  void SetThruIndexBinPos(TIndex pos,double binPos);
402  void SetThruIndexBinWidth(TIndex pos,TSize binWidth);
404 
405  void SetIsIndexUpToDate(bool isUpToDate)
406  {
407  mIsIndexUpToDate = isUpToDate;
408  }
409 
411  void TodB();
413  void ToLinear();
414 
420  void CopyMembers(SpectralPeakArray& sourceSpectralPeakArray);
421 
423 private:
424  bool mIsIndexUpToDate;
425 
426 };
427 
428 }; // namespace CLAM
429 
430 #endif
431