CLAM-Development  1.4.0
SpectrumConversions.cxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 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 
22 #include "SpectrumConversions.hxx"
23 #include "DataTypes.hxx"
24 #include "CLAM_Math.hxx"
25 
26 namespace CLAM
27 {
28  inline void AssertProperSize( TSize size, DataArray& buffer )
29  {
30  if ( buffer.Size() != size )
31  {
32  buffer.Resize( size );
33  buffer.SetSize( size );
34  }
35  }
36 
37 
38  void Polar2MagPhase ( const Array<Polar>& polarBuffer, DataArray& magBuffer,
39  DataArray& phaseBuffer )
40  {
41  TSize nBins = polarBuffer.Size();
42 
43  // target buffers resizing if necessary
44 
45  AssertProperSize( nBins, magBuffer );
46  AssertProperSize( nBins, phaseBuffer );
47 
48  for ( TSize i = 0; i < nBins; i++ )
49  {
50  magBuffer[i] = polarBuffer[i].Mag();
51  phaseBuffer[i] = polarBuffer[i].Ang();
52  }
53  }
54 
55  void Complex2MagPhase( const Array<Complex>& complBuffer, DataArray& magBuffer, DataArray& phaseBuffer )
56  {
57  TSize nBins = complBuffer.Size();
58 
59  AssertProperSize( nBins, magBuffer );
60  AssertProperSize( nBins, phaseBuffer );
61 
62  for ( TSize i = 0; i < nBins; i++ )
63  {
65  magBuffer[i] = complBuffer[i].Mag();
67  phaseBuffer[i] = complBuffer[i].Ang();
68  }
69  }
70 
71  void BPF2MagPhase( const BPF& magBpf,
72  const BPF& phaseBpf,
73  DataArray& magBuffer,
74  DataArray& phaseBuffer,
75  TSize nBins,
76  TData specRange )
77  {
78  AssertProperSize( nBins, magBuffer );
79  AssertProperSize( nBins, phaseBuffer );
80 
81  const TData delta = specRange/TData(nBins-1);
82  TData freq = 0;
83 
84  for ( TSize i = 0; i < nBins; i++ )
85  {
86  magBuffer[i] = magBpf.GetValue( freq );
87  phaseBuffer[i] = phaseBpf.GetValue( freq );
88 
89  freq += delta;
90  }
91  }
92 
93  void Log2LinearMagnitude( const DataArray& logBuffer, DataArray& linBuffer )
94  {
95  const TSize nBins = logBuffer.Size();
96  static const TData inv_20 = 1.0f/20.0f;
97 
98  AssertProperSize( nBins, linBuffer );
99 
100  for ( int i = 0; i < nBins; i++ )
101  {
102  linBuffer[i] = CLAM_pow( TData(10), logBuffer[i]*inv_20 ) ;
103  }
104  }
105 
106  void Log2LinearMagnitude( DataArray& dataBuffer )
107  {
108  const TSize nBins = dataBuffer.Size();
109  static const TData inv_20 = 1.0f/20.0f;
110 
111  for ( int i = 0; i < nBins; i++ )
112  {
113  dataBuffer[i] = CLAM_pow( TData(10), dataBuffer[i]*inv_20 ) ;
114  }
115  }
116 
117  void Complex2LogMagPhase(const Array<Complex> &src, DataArray &destMag, DataArray &destPhase)
118  {
119  CLAM_ASSERT(src.Size() == destMag.Size() && src.Size() == destPhase.Size(), "Complex2LogMagPhase() - input/output sizes inconsistent.");
120 
121  const int n = src.Size();
122  for (int i = 0; i < n; ++i)
123  {
124  static const float minLinSquared = 1.0e-20f;
125  static const float minLog = -200.f; // -200 dB
126 
127  const float re = src[i].Real();
128  const float im = src[i].Imag();
129 
130  const float magSquared = re*re + im*im;
131 
132  if (magSquared <= minLinSquared)
133  {
134  destMag[i] = minLog;
135  }
136  else
137  {
138  destMag[i] = 10.f*CLAM_log10(magSquared); // = 20*log10(CLAM_sqrt(re^2 + im^2))
139  }
140 
141  destPhase[i] = src[i].Ang(); // = atan2(im. re)
142  }
143  }
144 
145  void Linear2LogMagnitude( const DataArray& linearBuffer, DataArray& logData )
146  {
147  const TSize nBins = linearBuffer.Size();
148 
149  AssertProperSize( nBins, logData );
150 
151  for ( int i = 0; i < nBins; i++ )
152  {
153  logData[i]=CLAM_20log10(linearBuffer[i]);
154  }
155 
156  }
157 
158  void Linear2LogMagnitude( DataArray& dataBuffer )
159  {
160  const TSize nBins = dataBuffer.Size();
161 
162  for ( int i = 0; i < nBins; i++ )
163  {
164  dataBuffer[i]=CLAM_20log10( dataBuffer[i] );
165  }
166  }
167 
168 }
169