在 Matlab 中使用 FFT 计算自相关 [英] Calculate autocorrelation using FFT in Matlab

查看:64
本文介绍了在 Matlab 中使用 FFT 计算自相关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了一些关于如何使用信号的 fft 更有效地计算自相关的解释,将实部乘以复共轭(傅立叶域),然后使用逆 fft,但我无法实现这是在 Matlab 中,因为在详细级别.

I've read some explanations of how autocorrelation can be more efficiently calculated using the fft of a signal, multiplying the real part by the complex conjugate (Fourier domain), then using the inverse fft, but I'm having trouble realizing this in Matlab because at a detailed level.

推荐答案

就像你说的,取 fft 并逐点乘以其复共轭,然后使用逆 fft(或者在两个信号互相关的情况下): Corr(x,y) <=> FFT(x)FFT(y)*)

Just like you stated, take the fft and multiply pointwise by its complex conjugate, then use the inverse fft (or in the case of cross-correlation of two signals: Corr(x,y) <=> FFT(x)FFT(y)*)

x = rand(100,1);
len = length(x);

%# autocorrelation
nfft = 2^nextpow2(2*len-1);
r = ifft( fft(x,nfft) .* conj(fft(x,nfft)) );

%# rearrange and keep values corresponding to lags: -(len-1):+(len-1)
r = [r(end-len+2:end) ; r(1:len)];

%# compare with MATLAB's XCORR output
all( (xcorr(x)-r) < 1e-10 )

事实上,如果你看一下xcorr.m的代码,这正是它在做的(只是它必须处理填充、归一化、向量/矩阵输入等所有情况...)

In fact, if you look at the code of xcorr.m, that's exactly what it's doing (only it has to deal with all the cases of padding, normalizing, vector/matrix input, etc...)

这篇关于在 Matlab 中使用 FFT 计算自相关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆