Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

2 method to calculate CCF gives wrong result

Asked by Bo on 19 Jul 2013

I am writing code to compute CCF using 2 method, one from formula and one from fft. the sampling rate of the data is 250Hz, the data length is 5s (1250samples), which is in the channel of eeg 8 and 9. i am computing the 3 to 4 s data from both channels.

At first, i will get data out first. but form data2, i don't know if i need to zero padding the beginning of the data or just take the 250 sampls before the data into my array.

data1 = eegp1(8,500:1000);
data1 = data1 - mean(data1);
delay = 250;
data2t = eegp1(9,500:1000);
data2t = data2t - mean(data2t);
data2 = [zeros(1,delay) data2t zeros(1,delay)];

then method1:

xaxis = -delay:delay;
CCF = zeros(1,delay*2+1);
for i = -delay:delay
     sum = 0;
     for j = 1:501
         sum = sum+data1(j)*data2(j+i+delay);
     end
     CCF(i+delay+1)=sum/(501-abs(i));
end
% [c,lags] = xcorr(data1,data2,250);
% figure(2);
% plot(lags,c);
CSD = abs(fft(CCF));
CCF = CCF/max(abs(CCF));
CSD = CSD/max(abs(CSD));
figure(1);
plot(xaxis,CCF);
title('CFF result');

method2:

    fdata1 = fft(data1);
    fdata2 = fft(data2t);
    CSD2 = fdata1.*conj(fdata2);
    CCF2 = ifft(CSD2);
    CCF2 = CCF2/max(abs(CCF2));
    CSD2 = CSD2/max(abs(CSD2));
    figure(2);
    plot(CCF2);
    title('CFF result');
 The result comes out wrong, can someone help me? thanks verymuch!

0 Comments

Bo

Tags

Products

No products are associated with this question.

1 Answer

Answer by Wayne King on 19 Jul 2013

Hi, look at this example:

rng default;
x = randn(8,1);
y = randn(8,1);
npad = length(x)+length(y)-1;
xcor_1 = fftshift(ifft(fft(x,npad).*conj(fft(y,npad))))
[c,~] = xcorr(x,y)

0 Comments

Wayne King

Contact us