CLAM-Development  1.4.0
LPModel.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 "LPModel.hxx"
23 #include "Spectrum.hxx"
24 #include "SpectrumConfig.hxx"
25 #include "SpectrumConversions.hxx"
26 #include "ProcessingDataPlugin.hxx"
27 
28 namespace CLAM
29 {
30  namespace Hidden
31  {
32  static ProcessingDataPlugin::Registrator<CLAM::LPModel> dataRegistrator("orange");
33  }
34 
36  {
37  AddAll();
38  UpdateData();
39  // MRJ: 11 seems to be a 'wise' number
40  UpdateModelOrder( 11 );
41  }
42 
44  {
45  SetOrder( order );
46  GetFilterCoefficients().Resize( order );
47  GetFilterCoefficients().SetSize( order );
48  GetReflectionCoefficients().Resize( order );
49  GetReflectionCoefficients().SetSize( order );
50  }
51 
52  void LPModel::ToSpectrum( Spectrum& spec ) const
53  {
54  SpecTypeFlags specFlags;
55  spec.GetType( specFlags );
56  spec.SetScale( EScale::eLinear );
57 
58  spec.SetSpectralRange( GetSpectralRange() );
59 
60  const DataArray& ak_vec = GetFilterCoefficients();
61  int order = ak_vec.Size();
62 
63  // we build the array of polarCoeffs from the filter poles
64  Array< Complex > spectrumCoeffs;
65  Array< Complex > cmplxCoeffs;
66  spectrumCoeffs.Resize( spec.GetSize() );
67  spectrumCoeffs.SetSize( spec.GetSize() );
68  cmplxCoeffs.Resize( order );
69  cmplxCoeffs.SetSize( order );
70 
71  const TData dw = PI/TData(spec.GetSize()-1);
72  TData w =0.0;
73  Complex unitComplex;
74  unitComplex.SetReal( 1.0 );
75  unitComplex.SetImag( 0.0 );
76 
77  for ( int j = 0; j < spec.GetSize(); j++ )
78  {
79  spectrumCoeffs[j].SetReal( 1.0 );
80  spectrumCoeffs[j].SetImag( 0.0 );
81 
82  for ( int i = 0; i < order; i++ )
83  {
84  cmplxCoeffs[i].SetReal( ak_vec[i]*cos( -1.0*(float)(i+1)*w ) );
85  cmplxCoeffs[i].SetImag( ak_vec[i]*sin( 1.0*(float)(i+1)*w ) );
86 
87  spectrumCoeffs[j] += cmplxCoeffs[i];
88  }
89  spectrumCoeffs[j] = unitComplex / spectrumCoeffs[j];
90  w += dw;
91  }
92 
93  if ( specFlags.bComplex )
94  {
95  spec.SetComplexArray( spectrumCoeffs );
96  }
97  if ( specFlags.bPolar )
98  {
99  // Complex2Polar( spectrumCoeffs, spec.GetPolarArray() );
100  }
101  if ( specFlags.bMagPhase )
102  {
103  Complex2MagPhase( spectrumCoeffs, spec.GetMagBuffer(), spec.GetPhaseBuffer() );
104  }
105  }
106 }
107