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);