ScaleCnv.hxx

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 #ifndef _ScaleCnv_
00023 #define _ScaleCnv_
00024 
00025 #include "Array.hxx"
00026 #include "Spectrum.hxx"
00027 #include "SpectralPeakArray.hxx"
00028 
00029 namespace CLAM {
00030 
00031 template <class T> class ScaleCnv
00032 {
00033 public:
00034 
00035 /* Constructors and destructors */
00036         ScaleCnv(){};
00037         ~ScaleCnv(){};
00038 
00039 /* Converting routines */
00040         void Convert(DataArray<T>& inputArray,
00041                 DataArray<T>& outputArray,EScaleType oScaleType=eUnknown)// non-destructive overload
00042         {
00043                 if(oScaleType==0) oScaleType=outputArray.GetScale();
00044                 if(inputArray.GetScale()!=oScaleType)
00045                 {
00046                         if(inputArray.GetScale()==eLinear)
00047                         {
00048                                 ConvertTodB(inputArray,outputArray,(int)oScaleType);
00049                         }
00050                         else if(oScaleType==eLinear)
00051                         {
00052                                 ConvertToLin(inputArray,outputArray,inputArray.GetScale());
00053                         }
00054                         else
00055                         {
00056                                 double factor = ((double)oScaleType)/((double)inputArray.GetScale());
00057                                 for(int i=0;i<inputArray.Size();i++)
00058                                 {
00059                                         outputArray[i]=inputArray[i]*factor;
00060                                 }
00061                         }
00062                 }
00063                 if(outputArray.GetScale() != oScaleType) outputArray.SetScale(oScaleType);
00064         }
00065 
00066         void Convert(DataArray<T>& array,EScaleType oDataType)//destructive overload
00067         {
00068                 Convert(array,array,oDataType);
00069         }
00070 
00071         /* Scale convert for a Spectrum */
00072         void Convert(Spectrum &inputSpectrum, 
00073                                  Spectrum &outputSpectrum, EScaleType oScaleType=eUnknown)
00074         {
00075                 if(oScaleType==0) oScaleType=outputSpectrum.GetScale();
00076                 CLAM_ASSERT(inputSpectrum.GetSpectrum() == eMagPhase,
00077                         "ScaleConv::Convert: spectrum must be eMagPhase"); 
00078                 // Other Spectrum types should be implemented
00079 
00080                 Convert(inputSpectrum.GetRefToMagBuffer(), outputSpectrum.GetRefToMagBuffer(), oScaleType);
00081                 outputSpectrum.SetScale(oScaleType);
00082         }
00083         
00084         void Convert(Spectrum &spectrum, EScaleType oScaleType)
00085         {
00086                 Convert(spectrum,spectrum,oScaleType);
00087         }
00088 
00089         /* Scale convert for a Peak Array */
00090         void Convert(SpectralPeakArray &inputPeakArray, 
00091                                  SpectralPeakArray &outputPeakArray, EScaleType oScaleType=eUnknown)
00092         {
00093                 if(oScaleType==0) oScaleType=outputPeakArray.GetScale();         
00094                 Convert(inputPeakArray.GetRefToPeakArray(), outputPeakArray.GetRefToPeakArray(), oScaleType);
00095                 outputPeakArray.SetScale(oScaleType);
00096         }
00097         
00098         void Convert(SpectralPeakArray &spectrum, EScaleType oScaleType)
00099         {
00100                 Convert(spectrum,spectrum,oScaleType);
00101         }
00102                         
00103 private:
00104                 
00105         void ConvertTodB(const DataArray<T>& inputArray,
00106                 DataArray<T>& outputArray,int scalingFactor)//non-destructive dB conv.
00107         {
00108                 outputArray=inputArray;
00109                 ConvertTodB(outputArray,scalingFactor);
00110         }
00111         
00112         void ConvertTodB(DataArray<T>& array,int scalingFactor)//destructive dB conv.
00113         {
00114                 array.Apply(DB,(scalingFactor));
00115         }
00116 
00117         void ConvertToLin(const Array<T>& inputArray,
00118                 Array<T>& outputArray,int scalingFactor)//non-destructive lin conv.
00119         {
00120                 outputArray=inputArray;
00121                 ConvertToLin(outputArray,scalingFactor);
00122         }
00123 
00124         void ConvertToLin(Array<T>& array,int scalingFactor)//destruct. lin. conv.
00125         {
00126                 array.Apply(Lin,scalingFactor);
00127         }
00128         
00129 };
00130 
00131 }
00132 
00133 #endif
00134 

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