29 #ifndef linearPredictor_hpp
30 #define linearPredictor_hpp
35 #include "../math/constants.hpp"
36 #include "../math/eigenLapack.hpp"
38 #include "levinsonRecursion.hpp"
50 template<
typename _realT>
55 Eigen::Array<realT, -1, -1> _c;
57 realT _setCondition {0};
58 realT _actCondition {0};
74 return calcCoefficientsLevinson( ac, Nc, extrap );
78 Eigen::Array<realT,-1,-1> Rmat, Rvec, PInv, LPcoeff;
83 for(
size_t i=0; i<Nc; ++i)
85 for(
size_t j=0; j<Nc; ++j)
87 Rmat(i,j) = ac[ fabs(i-j)];
93 realT tmpCond = condition;
95 _setCondition = condition;
99 _actCondition = tmpCond;
101 _c = Rvec.matrix()*PInv.matrix();
108 int calcCoefficientsLevinson( std::vector<realT> & ac,
113 std::vector<realT> r, x, y;
119 for(
size_t i=0; i< Nc; ++i) r[i] = ac[Nc-i - 1];
120 for(
size_t i=Nc; i< 2*Nc-1; ++i) r[i] = ac[i-Nc+1];
122 for(
size_t i=0;i<Nc; ++i) y[i] = ac[i+1];
124 levinsonRecursion(r.data(), x.data(), y.data(), Nc);
127 for(
size_t i=0; i< Nc; ++i) _c(0,i) = x[i];
132 Eigen::Array<realT, -1, -1> ex(_c.rows(), _c.cols());
133 for(
size_t j=0; j < extrap; ++j)
135 for(
size_t i=0; i< Nc-1; ++i)
137 ex(0,i) = _c(0,0)*_c(0,i) + _c(0,i+1);
139 ex(0,Nc-1) = _c(0,0)*_c(0,Nc-1);
155 realT predict( std::vector<realT> & hist,
160 if(idx < _c.cols())
return 0;
162 for(
int i=0; i< _c.cols(); ++i)
164 x += _c(0,i)*hist[idx-i];
170 realT spectralResponse( realT f, realT fs)
174 std::complex<realT> He = 0;
175 for(
int j=0; j < n; ++j)
177 realT s = (j+1.0)* math::two_pi<realT>();
178 He += _c(0,j) * exp(s*std::complex<realT>(0,-1.0)*f/fs);
182 return std::norm( one/ (one - He));
int eigenPseudoInverse(Eigen::Array< dataT, -1, -1 > &PInv, dataT &condition, int &nRejected, Eigen::Array< dataT, -1, -1 > &U, Eigen::Array< dataT, -1, -1 > &S, Eigen::Array< dataT, -1, -1 > &VT, Eigen::Array< dataT, -1, -1 > &A, dataT &maxCondition, dataT alpha=0, int interact=MX_PINV_NO_INTERACT)
Calculate the pseudo-inverse of a patrix using the SVD.
A class to support linear prediction.
int calcCoefficients(std::vector< realT > &ac, size_t Nc, realT condition=0, size_t extrap=0)
Calculate the LP coefficients given an autocorrelation.