GPS C/A Code Time Delay Using Ultrasound I am undertaking a project which requires to measure a distance from source to receiver. I am doing this using two ultrasonic transducers operating at 40kHz and using FFT search to determine the time delay and from that the distance. These are the following steps I'm taking to achieve this. All the processing is done in MATLAB.


Transmitting a C/A code at 1kHz using a carrier frequency of 40kHz 
At the receiving end I sample the incoming signal at 160kHz 
Generate a C/A code locally with a sampling frequency (fs) of 160kHz 
Multiply the received signal with the one generated in step (3)
Multiply the signal from step (4) with sin(2*pifct) and 
cos(2*pifct), where fc=40kHz and t=0:1/fs:1.023-(1/fs) to obtain the 
I and Q components down-mixed to 40khz.
Then, compute the fft down-mixed signal, using fft(I+iQ), where 
i=imaginary
Compute the fft of the localy generated C/A code and then take the 
conjugate of that: conj(fft(C/A))
Compute the ifft,square and take the absolute magnitude of the result, 
as so: abs(ifft(step7*step6)).ˆ2 to get the time delat


Are the above steps correct? Is there anything else I have to do or am I missing something? It works fine in matlab but when I use 'real' data the correlation is much weaker. I haven't actually used the ultrasonic yet I'm simply using one microcontroller to generate and send signal and another to sample it (connected with a wire). When I do the processing in matlab the results aren't as good as the pure simulation.
Note: DSP is very new to me, I'm just stuck with this project and learning as I go along.
C/A Code generator 
[taken from][1]

tap=[2 6; 3 7; 4 8; 5 9; 1 9; 2 10; 1 8; 2 9; 3 10; 2 3; 3 4;
 5 6; 6 7; 7 8; 8 9; 9 10; 1 4; 2 5; 3 6; 4 7; 5 8; 6 9; 1 3;
 4 6; 5 7; 6 8; 7 9; 8 10; 1 6; 2 7; 3 8; 4 9; 5 10;4 10;
 1 7; 2 8; 4 10];

G1 = -1*ones(1,10);
G2 = G1;

s1 = tap(n,1);
s2 = tap(n,2);

tmp = 0;

for i = 1:1023
    G(i) = G2(s1)*G2(s2)*G1(10);
    tmp = G1(1);
    G1(1) = G1(3)*G1(10);
    G1(2:10) = [tmp G1(2:9)];

    tmp = G2(1);
    G2(1) = G2(2)*G2(3)*G2(6)*G2(8)*G2(9)*G2(10);
    G2(2:10) = [tmp G2(2:9)];
end

%resample
x=1:n_samples
CAa(x) = G(ceil(x*chiprate/fs));
end


Signal received from the "ADC"

  function send = s()
clear all;
%        CAa = ca(n,chiprate,fs,n_samples)   
CAtest = CAa(1,1e3,160e3,(160e3*1.023)); %generate the outgoing signal
Fs=160e3;
n_data = 160e3*1.023;
t =(0:(n_data-1))/Fs
carrier = cos(2*pi*40e3*t); %carrier @40khz.
send = carrier.*CAtest %modulate

end


Code for xcorrr

close all;
modulated =  send();                          %generate incoming signal
%modulated = circshift(modulated,80000,2);    %simulate a time delay
fc = 40e3;                                    %carrier frequency
Fs = 160e3;
Ts = 1/(Fs);  
n_samples = Fs*1.023;                         %sampling period
t = (0:(n_samples-1))/Fs;                     %generate time vector

b = CAa(1,1e3,160e3,(160e3*1.023));           %generate a C/A code locally

rx = modulated .* exp(-j*2*pi*fc.*t);            %downconvert to baseband

corrOut = abs(ifft(fft(rx).*conj(fft(b)))).^2;
figure
plot(corrOut);
Instead of using "square" for your carrier generation, use "cos". 

Also the -1 square in your receiver will not generate a quadrature signal from + square as you have used, copied below: 

This does not produce a sine and cosine:

x2 = square(2*pi*fc*t); %sin
x3 = -1*square(2*pi*fc*t);%cos


Here as well sin and cos directly would be more appropriate, but the following is how I would approach your receiver, using exp instead of sines and cosines since $e^{-j\omega t} = cos(\omega t)-jsin(\omega t)$:

modulated =  send();  %generate incoming signal at carrier fc
rx = modulated * exp(-j*2*pi*fc*t)   %downconvert to baseband
%the above will have a high frequency image at -2fc that must be filtered
coeff= firls(31, [0 .2 .5 1], [1 1 0 0]);
rxfilt= filter(coeff,1,rx)
corrOut = ifft(fft(rxfilt).*conj(fft(CA_local));


The $e^{-j\omega t}$ will shift the positve half of your modulated signal to baseband (0) and the negative half to $-2f_c$, so the example filter shown is to filter out the image. (Note there was no consideration given for actual filter design, this was quickly "pulled out of the hat" just to demonstrate what will work).