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"
35 template<
typename _realT>
40 std::vector<realT> PSDtn;
42 std::vector<realT> psd2s;
44 std::vector<realT> ac;
52 realT m_precision0 {10};
53 realT m_max_sc0 {100};
54 realT m_dPrecision {3};
59 realT m_minPrecision {0.001};
76 std::vector<realT> & PSDn,
82 PSDtn.resize(PSDt.size());
84 for(
size_t i=0; i< PSDt.size(); ++i)
86 PSDtn[i] = PSDt[i] + PSDn[i] + PSDreg;
91 ac.resize(psd2s.size());
110 template<
bool pr
intout=false>
115 clGainOpt<realT> & go_lp,
116 std::vector<realT> & PSDt,
117 std::vector<realT> & PSDn,
135 min_var = std::numeric_limits<realT>::max();
136 last_var = std::numeric_limits<realT>::max();
140 sc0 = min_sc - precision*m_dPrecision;
146 realT psdReg = PSDt[0];
149 for(realT sc = sc0; sc <= max_sc; sc += precision)
151 if(
calcCoefficients(PSDt, PSDn, psdReg*pow(10, -sc/10), Nc) < 0)
return -1;
156 realT ll = 0, ul = 0;
157 gmax_lp = go_lp.maxStableGain(ll,ul);
159 gopt_lp = go_lp.optGainOpenLoop(var_lp, PSDt, PSDn, gmax_lp);
163 std::cout << -(sc)/10 <<
" " << gmax_lp <<
" " << gopt_lp <<
" " << var_lp <<
"\n";
166 if( var_lp < min_var )
173 if( var_lp/last_var > 10 )
return 0;
187 template<
bool pr
intout=false>
191 clGainOpt<realT> & go_lp,
192 std::vector<realT> & PSDt,
193 std::vector<realT> & PSDn,
198 realT min_var = m_min_var0;
199 realT min_sc = m_min_sc0;
200 realT precision = m_precision0;
201 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)
return -1;
240 realT ll = 0, ul = 0;
241 gmax_lp = go_lp.maxStableGain(ll,ul);
242 gopt_lp = go_lp.optGainOpenLoop(var_lp, PSDt, PSDn, gmax_lp);
252 template<
bool pr
intout=false>
257 clGainOpt<realT> & go_lp,
258 std::vector<realT> & PSDt,
259 std::vector<realT> & PSDn,
264 realT minVar = std::numeric_limits<realT>::max();
266 for(
int n = minNc; n <= maxNc; ++n)
271 regularizeCoefficients<printout>(_gmax_lp, _gopt_lp, _var_lp, go_lp, PSDt, PSDn, n);
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.
int calcCoefficients(std::vector< realT > &ac, size_t Nc, realT condition=0, size_t extrap=0)
Calculate the LP coefficients given an autocorrelation.