SpectrumConversions.cxx
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "SpectrumConversions.hxx"
00023 #include "DataTypes.hxx"
00024 #include "CLAM_Math.hxx"
00025
00026 namespace CLAM
00027 {
00028 inline void AssertProperSize( TSize size, DataArray& buffer )
00029 {
00030 if ( buffer.Size() != size )
00031 {
00032 buffer.Resize( size );
00033 buffer.SetSize( size );
00034 }
00035 }
00036
00037
00038 void Polar2MagPhase ( const Array<Polar>& polarBuffer, DataArray& magBuffer,
00039 DataArray& phaseBuffer )
00040 {
00041 TSize nBins = polarBuffer.Size();
00042
00043
00044
00045 AssertProperSize( nBins, magBuffer );
00046 AssertProperSize( nBins, phaseBuffer );
00047
00048 for ( TSize i = 0; i < nBins; i++ )
00049 {
00050 magBuffer[i] = polarBuffer[i].Mag();
00051 phaseBuffer[i] = polarBuffer[i].Ang();
00052 }
00053 }
00054
00055 void Complex2MagPhase( const Array<Complex>& complBuffer, DataArray& magBuffer, DataArray& phaseBuffer )
00056 {
00057 TSize nBins = complBuffer.Size();
00058
00059 AssertProperSize( nBins, magBuffer );
00060 AssertProperSize( nBins, phaseBuffer );
00061
00062 for ( TSize i = 0; i < nBins; i++ )
00063 {
00065 magBuffer[i] = complBuffer[i].Mag();
00067 phaseBuffer[i] = complBuffer[i].Ang();
00068 }
00069 }
00070
00071 void BPF2MagPhase( const BPF& magBpf,
00072 const BPF& phaseBpf,
00073 DataArray& magBuffer,
00074 DataArray& phaseBuffer,
00075 TSize nBins,
00076 TData specRange )
00077 {
00078 AssertProperSize( nBins, magBuffer );
00079 AssertProperSize( nBins, phaseBuffer );
00080
00081 const TData delta = specRange/TData(nBins-1);
00082 TData freq = 0;
00083
00084 for ( TSize i = 0; i < nBins; i++ )
00085 {
00086 magBuffer[i] = magBpf.GetValue( freq );
00087 phaseBuffer[i] = phaseBpf.GetValue( freq );
00088
00089 freq += delta;
00090 }
00091 }
00092
00093 void Log2LinearMagnitude( const DataArray& logBuffer, DataArray& linBuffer )
00094 {
00095 const TSize nBins = logBuffer.Size();
00096 static const TData inv_20 = 1.0f/20.0f;
00097
00098 AssertProperSize( nBins, linBuffer );
00099
00100 for ( int i = 0; i < nBins; i++ )
00101 {
00102 linBuffer[i] = CLAM_pow( TData(10), logBuffer[i]*inv_20 ) ;
00103 }
00104 }
00105
00106 void Log2LinearMagnitude( DataArray& dataBuffer )
00107 {
00108 const TSize nBins = dataBuffer.Size();
00109 static const TData inv_20 = 1.0f/20.0f;
00110
00111 for ( int i = 0; i < nBins; i++ )
00112 {
00113 dataBuffer[i] = CLAM_pow( TData(10), dataBuffer[i]*inv_20 ) ;
00114 }
00115 }
00116
00117 void Complex2LogMagPhase(const Array<Complex> &src, DataArray &destMag, DataArray &destPhase)
00118 {
00119 CLAM_ASSERT(src.Size() == destMag.Size() && src.Size() == destPhase.Size(), "Complex2LogMagPhase() - input/output sizes inconsistent.");
00120
00121 const int n = src.Size();
00122 for (int i = 0; i < n; ++i)
00123 {
00124 static const float minLinSquared = 1.0e-20f;
00125 static const float minLog = -200.f;
00126
00127 const float re = src[i].Real();
00128 const float im = src[i].Imag();
00129
00130 const float magSquared = re*re + im*im;
00131
00132 if (magSquared <= minLinSquared)
00133 {
00134 destMag[i] = minLog;
00135 }
00136 else
00137 {
00138 destMag[i] = 10.f*CLAM_log10(magSquared);
00139 }
00140
00141 destPhase[i] = src[i].Ang();
00142 }
00143 }
00144
00145 void Linear2LogMagnitude( const DataArray& linearBuffer, DataArray& logData )
00146 {
00147 const TSize nBins = linearBuffer.Size();
00148
00149 AssertProperSize( nBins, logData );
00150
00151 for ( int i = 0; i < nBins; i++ )
00152 {
00153 logData[i]=CLAM_20log10(linearBuffer[i]);
00154 }
00155
00156 }
00157
00158 void Linear2LogMagnitude( DataArray& dataBuffer )
00159 {
00160 const TSize nBins = dataBuffer.Size();
00161
00162 for ( int i = 0; i < nBins; i++ )
00163 {
00164 dataBuffer[i]=CLAM_20log10( dataBuffer[i] );
00165 }
00166 }
00167
00168 }
00169