How to calculate the circular correlation with 2 sequences/arrays in Matlab?

Hello,
Trying to use Matlab to calculate the circular correlation between x = [2 3];, and y = [4 1 -8]; Unfortunately cannot find appropriate function, such as CXCORR or CIRCORR?
Can calulate using matlab Linear Correlation, Linear Convolution, Circular Convolution as follows:
EDU>> x = [ 1 3 5 ];
EDU>> y = [-2 2 4 6 8];
EDU>> convolution = conv(x,y)
convolution =
-2 -4 0 28 46 54 40
EDU>> a = [1 2 4];
EDU>> b = [-3 2 5 7 9];
EDU>> correlation = xcorr(a,b)
correlation =
9.0000 25.0000 55.0000 40.0000 21.0000 2.0000 -12.0000 0.0000 0
EDU>>
EDU>> x = [ 1 3 5];
EDU>> y = [-2 2 4 6 8];
EDU>> CircularConvolution = cconv(x,y,5)
CircularConvolution =
52 36 0 28 46
Appreciate any help.
kind regards. V.

댓글 수: 2

To calculate circular correlation Lets consider a and b Flip b fliplr(b) And use cconv(a,b) Without giving the intervals, you will get the output for circular convolution
Thank you for your advice Ankith sri, shall give it ago, forgot posting this in 2015!. Useful practical function to know.

댓글을 달려면 로그인하십시오.

답변 (1개)

You can use
ifft(fft(a,5).*conj(fft(b,5)))
or
cconv(a,b([1 end:-1:2]),5)
HTH

댓글 수: 2

Thank you Honglei,

Getting closer to solving the problem, was playing around with your Matlab code, unfortunately getting different answer as written example from the my book gives circular correlation to be [29, 17, 12, 30, 17, 35, 29] ,repeats again.

Which is referred to as "linear cross-correlation", which is also termed "Fast Correlation", according to later chapter in the book.

Found the following in

help cconv
c = cconv(a,conj(fliplr(b)),7)
%%However, the result is flipped every 3rd result?? as compared to result in book.

Remove the "fliplr", but results are different.. Which has me confused...?? see below in code.

EDU>> a = [ 4 3 1 6];
EDU>> b = [5 2 3];
EDU>> ifft(fft(a,5).*conj(fft(b,5)))
ans =
      29    35    17    42    17
EDU>> c = cconv(a,conj(fliplr(b)),7)
c =
     12.0000   17.0000   29.0000   35.0000   17.0000   30.0000    0.0000
EDU>> 

kind regards V.

For this set of a and b, 6 points is already linear convolution, so there is no need to go through cconv although you could. The main issue here is the convention used in your book, which seems to consider moving to the left as index 1. In most literature I believe the convention is opposite. That's why you see the mismatch. Try the following:
fliplr(fftshift(cconv(a,fliplr(b))))
or
fliplr(circshift(ifft(fft(a,6).*conj(fft(b,6))),-1,2))

댓글을 달려면 로그인하십시오.

카테고리

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by