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);
Mat< Num_T > elem_div(const Mat< Num_T > &m1, const Mat< Num_T > &m2)
Element wise division of two matrices.
Various functions on vectors and matrices - header file.
T index_zero_pad(const Vec< T > &v, const int index)
Definitions of window functions.
int ceil_i(double x)
The nearest larger integer.
vec xcorr_old(const vec &x, const int max_lag, const std::string scaleopt)
Auto-correlation calculation.
Mat< Num_T > elem_mult(const Mat< Num_T > &m1, const Mat< Num_T > &m2)
Element wise multiplication of two matrices.
vec xcorr(const vec &x, const int max_lag, const std::string scaleopt)
Auto-correlation calculation.
cvec conj(const cvec &x)
Conjugate of complex value.
ITPP_EXPORT void ifft(const cvec &in, cvec &out)
Inverse Fast Fourier Transform.
T sum(const Vec< T > &v)
Sum of all elements in the vector.
ITPP_EXPORT void fft(const cvec &in, cvec &out)
Fast Fourier Transform.
#define it_assert_debug(t, s)
Abort if t is not true and NDEBUG is not defined.
Definitions of signal processing functions.
double mean(const vec &v)
The mean value.
Definitions of converters between different vector and matrix types.
double energy(const Vec< T > &v)
Calculate the energy: squared 2-norm. energy(v)=sum(abs(v).^2)
vec log2(const vec &x)
log-2 of the elements
T max(const Vec< T > &v)
Maximum value of vector.
int pow2i(int x)
Calculate two to the power of x (2^x); x is integer.
#define it_assert(t, s)
Abort if t is not true.
mat cov(const mat &X, bool is_zero_mean)
Covariance matrix calculation.
#define it_error(s)
Abort unconditionally.
vec spectrum(const vec &v, int nfft, int noverlap)
Power spectrum calculation.
Miscellaneous statistics functions and classes - header file.
#define it_warning(s)
Display a warning message.
IT++ compatibility types and functions.
vec sqr(const cvec &data)
Absolute square of elements.
Definitions of special vectors and matrices.
vec linspace(double from, double to, int points)
linspace (works in the same way as the MATLAB version)
vec hanning(int n)
Hanning window.
vec sqrt(const vec &x)
Square root of the elements.
cvec to_cvec(const Vec< T > &v)
Converts a Vec<T> to cvec.
bin abs(const bin &inbin)
absolute value of bin
Elementary mathematical functions - header file.
vec real(const cvec &data)
Real part of complex values.
int levels2bits(int n)
Calculate the number of bits needed to represent n different values (levels).
Vec< T > zero_pad(const Vec< T > &v, int n)
Zero-pad a vector to size n.
vec filter_spectrum(const vec &a, int nfft)
Power spectrum calculation of a filter.
const Array< T > concat(const Array< T > &a, const T &e)
Append element e to the end of the Array a.