43 vec
xcorr_old(
const vec &x,
const int max_lag,
const std::string scaleopt)
50 vec
xcorr(
const vec &x,
const int max_lag,
const std::string scaleopt)
52 cvec out(2*x.length() - 1);
58 cvec
xcorr(
const cvec &x,
const int max_lag,
const std::string scaleopt)
60 cvec out(2*x.length() - 1);
61 xcorr(x, x, out, max_lag, scaleopt,
true);
66 vec
xcorr(
const vec &x,
const vec &y,
const int max_lag,
const std::string scaleopt)
68 cvec out(2*x.length() - 1);
74 cvec
xcorr(
const cvec &x,
const cvec &y,
const int max_lag,
const std::string scaleopt)
76 cvec out(2*x.length() - 1);
77 xcorr(x, y, out, max_lag, scaleopt,
false);
82 void xcorr(
const vec &x,
const vec &y, vec &out,
const int max_lag,
const std::string scaleopt)
87 xcorr(xx, yy, oo, max_lag, scaleopt,
false);
92 void xcorr_old(
const vec &x,
const vec &y, vec &out,
const int max_lag,
const std::string scaleopt)
95 double s_plus, s_minus, M_double, coeff_scale = 0.0;
100 M_double = double(M);
109 out.set_size(2*N - 1,
false);
111 it_assert(N <=
std::max(x.size(), y.size()),
"max_lag cannot be as large as, or larger than, the maximum length of x and y.");
113 if (scaleopt ==
"coeff") {
117 for (m = 0; m < N; m++) {
121 for (n = 0;n < M - m;n++) {
126 if (scaleopt ==
"none") {
127 out(N + m - 1) = s_plus;
128 out(N - m - 1) = s_minus;
130 else if (scaleopt ==
"biased") {
131 out(N + m - 1) = s_plus / M_double;
132 out(N - m - 1) = s_minus / M_double;
134 else if (scaleopt ==
"unbiased") {
135 out(N + m - 1) = s_plus / double(M - m);
136 out(N - m - 1) = s_minus / double(M - m);
138 else if (scaleopt ==
"coeff") {
139 out(N + m - 1) = s_plus / coeff_scale;
140 out(N - m - 1) = s_minus / coeff_scale;
143 it_error(
"Incorrect scaleopt specified.");
148 vec
xcorr_old(
const vec &x,
const vec &y,
const int max_lag,
const std::string scaleopt)
156 void xcorr(
const cvec &x,
const cvec &y, cvec &out,
const int max_lag,
const std::string scaleopt,
bool autoflag)
158 int N =
std::max(x.length(), y.length());
162 int fftsize =
pow2i(b);
164 int end = fftsize - 1;
167 if (autoflag ==
true) {
185 if ((max_lag == -1) || (max_lag >= N))
193 out.set_size(1,
false);
197 out =
concat(temp2(end - maxlag + 1, end), temp2(0, maxlag));
201 if (scaleopt ==
"biased")
203 out = out /
static_cast<std::complex<double>
>(N);
204 else if (scaleopt ==
"unbiased") {
206 vec lags =
linspace(-maxlag, maxlag, 2 * maxlag + 1);
207 cvec scale =
to_cvec(static_cast<double>(N) -
abs(lags));
210 else if (scaleopt ==
"coeff") {
211 if (autoflag ==
true)
219 else if (scaleopt ==
"none") {}
221 it_warning(
"Unknow scaling option in XCORR, defaulting to <none> ");
226 mat
cov(
const mat &X,
bool is_zero_mean)
228 int d = X.cols(), n = X.rows();
229 mat R(d, d), m2(n, d);
236 for (
int i = 0; i < d; i++) {
238 m2.set_col(i, tmp -
mean(tmp));
242 for (
int i = 0; i < d; i++) {
243 for (
int j = 0; j <= i; j++) {
244 for (
int k = 0; k < n; k++) {
245 R(i, j) += m2(k, i) * m2(k, j);
253 for (
int i = 0; i < d; i++) {
254 for (
int j = 0; j <= i; j++) {
255 for (
int k = 0; k < n; k++) {
256 R(i, j) += X(k, i) * X(k, j);
270 "nfft must be a power of two in spectrum()!");
272 vec P(nfft / 2 + 1), w(nfft), wd(nfft);
276 double w_energy = nfft == 1 ? 1 : (nfft + 1) * .375;
278 if (nfft > v.size()) {
283 int k = (v.size() - noverlap) / (nfft - noverlap), idx = 0;
284 for (
int i = 0; i < k; i++) {
285 wd =
elem_mult(v(idx, idx + nfft - 1), w);
287 idx += nfft - noverlap;
292 P.set_size(nfft / 2 + 1,
true);
296 vec
spectrum(
const vec &v,
const vec &w,
int noverlap)
300 "The window size must be a power of two in spectrum()!");
302 vec P(nfft / 2 + 1), wd(nfft);
305 double w_energy =
energy(w);
307 if (nfft > v.size()) {
312 int k = (v.size() - noverlap) / (nfft - noverlap), idx = 0;
313 for (
int i = 0; i < k; i++) {
314 wd =
elem_mult(v(idx, idx + nfft - 1), w);
316 idx += nfft - noverlap;
321 P.set_size(nfft / 2 + 1,
true);
328 s.set_size(nfft / 2 + 1,
true);
335 s.set_size(nfft / 2 + 1,
true);