【发布时间】:2023-03-11 14:42:01
【问题描述】:
以下函数计算两个向量之间的相关性。
对于小值,它不会给出与 matlab 函数相同的结果:
我真的不知道这个错误是否来自这个函数?默认情况下的最大滞后是 N-1 ?这合理吗?
inline int pow2i(int x) { return ((x < 0) ? 0 : (1 << x)); }`
vec xcorr(vec x, vec y,bool autoflag)
{
int maxlag=0;
int N = std::max(x.size(), y.size());
//Compute the FFT size as the "next power of 2" of the input vector's length (max)
int b = ceil(log2(2.0 * N - 1));
int fftsize = pow2i(b);
int e = fftsize - 1;
cx_vec temp2;
if (autoflag == true) {
//Take FFT of input vector
cx_vec X = cx_vec(x,zeros(x.size()));
X= fft(X,fftsize);
//Compute the abs(X).^2 and take the inverse FFT.
temp2 = ifft(X%conj(X));
}
else{
//Take FFT of input vectors
cx_vec X=cx_vec(x,zeros(x.size()));
cx_vec Y=cx_vec(y,zeros(y.size()));
X = fft(X,fftsize);
Y = fft(Y,fftsize);
//cout<< "Y " << Y << endl;
//cout<< "X " << X<< endl;
temp2 =ifft(X%conj(Y));
//cout<< "temp 2 " << temp2 << endl;
}
maxlag=N-1;
vec out=real(join_cols(temp2(span(e - maxlag + 1, e)),temp2(span(0,maxlag))));
return out;
}
【问题讨论】:
-
什么是
vec?可能是一些 typedef? -
抛开浮点问题不谈,您确定向上取整 FFT 窗口大小适合此特定任务吗?对于小的输入,为什么不直接计算时域中的相关性并跳过傅里叶变换的开销?
-
vec 是使用犰狳框架的向量定义arma.sourceforge.net/docs.html#Col
-
@nanofarad:谢谢,我会试试的。