8#ifndef clAOLinearPredictor_hpp
9#define clAOLinearPredictor_hpp
13#include "../../math/geo.hpp"
15#include "../../sigproc/psdUtils.hpp"
17#include "../../sigproc/autocorrelation.hpp"
18#include "../../sigproc/linearPredictor.hpp"
35template <
typename _realT>
40 std::vector<realT> PSDtn;
42 std::vector<realT> psd2s;
44 std::vector<realT> ac;
50 realT m_min_var0{ 0 };
51 realT m_min_sc0{ 10 };
52 realT m_precision0{ 2 };
53 realT m_max_sc0{ 100 };
54 realT m_dPrecision{ 3 };
59 realT m_minPrecision{ 0.001 };
76 std::vector<realT> &PSDn,
81 PSDtn.resize( PSDt.size() );
83 for(
size_t i = 0; i < PSDt.size(); ++i )
85 PSDtn[i] = PSDt[i] + PSDn[i] + PSDreg;
90 ac.resize( psd2s.size() );
107 template <
bool pr
intout>
112 clGainOpt<realT> &go_lp,
113 std::vector<realT> &PSDt,
114 std::vector<realT> &PSDn,
130 min_var = std::numeric_limits<realT>::max();
135 sc0 = min_sc - precision * m_dPrecision;
141 realT psdReg = PSDt[0];
144 for( realT sc = sc0; sc <= max_sc; sc += precision )
152 realT ll = 0, ul = 0;
153 gmax_lp = go_lp.maxStableGain( ll, ul );
159 gopt_lp = go_lp.optGainOpenLoop( var_lp, PSDt, PSDn, gmax_lp,
false );
163 std::cout << -( sc ) / 10 <<
" " << gmax_lp <<
" " << gopt_lp <<
" " << var_lp <<
"\n";
166 if( var_lp < min_var )
173 if( var_lp > 10 * min_var )
188 template <
bool pr
intout = false>
192 clGainOpt<realT> &go_lp,
193 std::vector<realT> &PSDt,
194 std::vector<realT> &PSDn,
199 realT min_var = m_min_var0;
200 realT min_sc = m_min_sc0;
201 realT precision = m_precision0;
202 realT max_sc = m_max_sc0;
205 while( precision > m_minPrecision && its < m_maxIts )
207 _regularizeCoefficients<printout>( min_var, min_sc, precision, max_sc, go_lp, PSDt, PSDn, Nc );
209 if( min_sc == max_sc )
225 max_sc = min_sc + precision;
226 precision /= m_dPrecision;
233 if(
calcCoefficients( PSDt, PSDn, PSDt[0] * pow( 10, -min_sc / 10 ), Nc ) < 0 )
241 realT ll = 0, ul = 0;
242 gmax_lp = go_lp.maxStableGain( ll, ul );
243 gopt_lp = go_lp.optGainOpenLoop( var_lp, PSDt, PSDn, gmax_lp,
false );
254 template <
bool pr
intout = false>
259 clGainOpt<realT> &go_lp,
260 std::vector<realT> &PSDt,
261 std::vector<realT> &PSDn,
265 realT minVar = std::numeric_limits<realT>::max();
267 for(
int n = minNc; n <= maxNc; ++n )
272 regularizeCoefficients<printout>( _gmax_lp, _gopt_lp, _var_lp, go_lp, PSDt, PSDn, n );
274 if( _var_lp < minVar )
Provides a class to manage closed loop gain optimization.
void augment1SidedPSD(vectorTout &psdTwoSided, vectorTin &psdOneSided, bool addZeroFreq=false, typename vectorTin::value_type scale=0.5)
Augment a 1-sided PSD to standard 2-sided FFT form.
Class to manage the calculation of linear predictor coefficients for a closed-loop AO system.
realT m_gmax_lp
The maximum allowable gain for LP.
int calcCoefficients(std::vector< realT > &PSDt, std::vector< realT > &PSDn, realT PSDreg, int Nc, realT condition=0)
Calculate the LP coefficients for a turbulence PSD and a noise PSD.
int _regularizeCoefficients(realT &min_var, realT &min_sc, realT precision, realT max_sc, clGainOpt< realT > &go_lp, std::vector< realT > &PSDt, std::vector< realT > &PSDn, int Nc)
Worker function for regularizing the PSD for coefficient calculation.
int regularizeCoefficients(realT &gmax_lp, realT &gopt_lp, realT &var_lp, clGainOpt< realT > &go_lp, std::vector< realT > &PSDt, std::vector< realT > &PSDn, int Nc)
Regularize the PSD and calculate the associated LP coefficients.
int optimizeNc(realT &gmax_lp, realT &gopt_lp, int &Nc, realT &var_lp, clGainOpt< realT > &go_lp, std::vector< realT > &PSDt, std::vector< realT > &PSDn, int minNc, int maxNc)
Regularize the PSD and calculate the associated LP coefficients.
Functor for calculating the autocorrelation given a PSD.
A class to support linear prediction.
int calcCoefficients(const std::vector< realT > &ac, size_t Nc, size_t Npred=1, realT condition=0)
Calculate the LP coefficients given an autocorrelation.