## 2 method to calculate CCF gives wrong result

### Bo (view profile)

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!`

## Products

No products are associated with this question.

### Wayne King (view profile)

Answer by Wayne King

### Wayne King (view profile)

on 19 Jul 2013

Hi, look at this example:

```rng default;
x = randn(8,1);
y = randn(8,1);
[c,~] = xcorr(x,y)
```