Calculate autocorrelation using FFT in Matlab

前端 未结 2 1894
挽巷
挽巷 2020-11-30 21:22

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

2条回答
  •  天命终不由人
    2020-11-30 22:15

    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 )
    

    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...)

提交回复
热议问题