I reproduced the last piece of code and it works. I'll just add my piece of code to show how it works with any Fs and fft points but only 50% overlap.
resultx = EEG.data(5,1:16384);
resulty = EEG.data(3,1:16384);
Fs = 250;
Navg = 127;
nfft = 256;
samplesread = nfft-1;
window = hamming(nfft);
xmulconjyfft = zeros(nfft/2+1,Navg);
for i=1:Navg % number of iterations, that depends on fft points and the overlap, in my case with fftpoints=256, 50% overlap and inputlength=2^14, the number of iterations is 127
endpoint=startingpoint+samplesread; %read 255 data points every time
x1=resultx(startingpoint:endpoint)'.*window;
y1=resulty(startingpoint:endpoint)'.*window;
xdft1 = fft(x1,nfft);
ydft1 = fft(y1,nfft);
startingpoint=startingpoint+(samplesread+1)/2;
Sxy1a=ydft1.*conj(xdft1);
Sxy1b=Sxy1a./N^2;
Sxy1c=Sxy1b(1:nfft/2+1);
Sxy1c(2:end-1)=2*Sxy1c(2:end-1);
Sxy1c = Sxy1c';
xmulconjyfft(:,i)=Sxy1c;
%Sxy1=abs(Sxy1c);
end
avgxyfft = mean(xmulconjyfft,2); %average the spectrums
avgxyfft= avgxyfft*((length(resultx).^2)/(nfft*Fs)); % normalization factor
Sxy2a=cpsd(resultx,resulty,window,[],nfft,Fs);
From code above Sxy2a and avgxyfft result in two identical vectors with complex values.