1 #ifndef STAN_MATH_PRIM_MAT_FUN_AUTOCORRELATION_HPP 2 #define STAN_MATH_PRIM_MAT_FUN_AUTOCORRELATION_HPP 6 #include <unsupported/Eigen/FFT> 65 vector<complex<T> > freqvec;
68 vector<T> centered_signal(y);
69 centered_signal.insert(centered_signal.end(), Mt2 - N, 0.0);
71 for (
size_t i = 0; i < N; i++)
72 centered_signal[i] -= mean;
74 fft.fwd(freqvec, centered_signal);
75 for (
size_t i = 0; i < Mt2; ++i)
76 freqvec[i] = complex<T>(norm(freqvec[i]), 0.0);
81 for (
size_t i = 0; i < N; ++i) {
85 for (
size_t i = 0; i < N; ++i)
109 template <
typename T,
typename DerivedA,
typename DerivedB>
111 Eigen::MatrixBase<DerivedB>& ac, Eigen::FFT<T>& fft) {
117 Eigen::Matrix<T, Eigen::Dynamic, 1> centered_signal(Mt2);
118 centered_signal.setZero();
119 centered_signal.head(N) = y.array() - y.mean();
121 Eigen::Matrix<std::complex<T>, Eigen::Dynamic, 1> freqvec(Mt2);
122 fft.SetFlag(fft.HalfSpectrum);
123 fft.fwd(freqvec, centered_signal);
125 freqvec = freqvec.cwiseAbs2();
127 Eigen::Matrix<T, Eigen::Dynamic, 1> ac_tmp(Mt2);
128 fft.inv(ac_tmp, freqvec);
129 fft.ClearFlag(fft.HalfSpectrum);
131 for (
size_t i = 0; i < N; ++i)
132 ac_tmp(i) /= (N - i);
134 ac = ac_tmp.head(N).array() / ac_tmp(0);
153 template <
typename T>
159 const Eigen::Map<const Eigen::Matrix<T, Eigen::Dynamic, 1> > y_map(&y[0], N);
160 Eigen::Map<Eigen::Matrix<T, Eigen::Dynamic, 1> > ac_map(&ac[0], N);
161 autocorrelation<T>(y_map, ac_map, fft);
180 template <
typename T,
typename DerivedA,
typename DerivedB>
182 Eigen::MatrixBase<DerivedB>& ac) {
Independent (input) and dependent (output) variables for gradients.
size_t fft_next_good_size(size_t N)
Find the optimal next size for the FFT so that a minimum number of zeros are padded.
boost::math::tools::promote_args< T >::type mean(const std::vector< T > &v)
Returns the sample mean (i.e., average) of the coefficients in the specified standard vector...
void autocorrelation(const std::vector< T > &y, std::vector< T > &ac, Eigen::FFT< T > &fft)
Write autocorrelation estimates for every lag for the specified input sequence into the specified res...