-1

I have 2 data files, which links are attached below:

Those binary data are read by this MATLAB code:

%% EDIT:

clear all; close all; format long;

%% initial values:
nsamps          = inf;
nstart          = 0;
Fs              = 8e6; % sample rate
flag            = 1;   % plot in the for loop
c               = 3e8; % speed of light

%% input data
file_tx         = 'TX.dat';
file_rx         = 'RX.dat';
x_tx            = readcplx(file_tx, nsamps,nstart); 
x_rx            = readcplx(file_rx, nsamps,nstart); 
data_time       = 10; % second % we can set time base on the length of vector x_rx
data_time       = floor((length(x_rx) - 8e5)/Fs) * 10;

factor          = data_time/10;
matric          = reshape(x_rx, [Fs/data_time*factor, data_time + 1]); 
matric          = matric';
size_of         = size(matric);
len             = 1:size_of(1);
delay           = zeros(1, data_time + 1);

%% time delay calculation:
aa      = zeros(1, length(matric(1,:)) - length(x_tx));
signal1 = [x_tx aa];    

for i = 1: 1%size_of(1)

    signal2                 = matric(i,:);
    [cc_correlation,lag]    = xcorr(signal1, signal2);
    [cc_maximum, cc_time]   = max(abs(cc_correlation));
    cc_estimation           = abs(length(signal1) - cc_time);
    delay(i)                = cc_estimation/Fs; % in second

    lagDiff                 = lag(cc_time);
    s2                      = signal2(abs(lagDiff)+1:end);
    t2                      = (0:length(s2)-1)/Fs;    
end
%%
fprintf('\n Done! \n\n');

%%%%%%%%%%%%%

function x = readcplx(filename,nsamps,nstart);
fid = fopen(filename);
fseek(fid,4 * nstart,'bof');
y = fread(fid,[2,inf],'short');
fclose(fid);
x = complex(y(1,:),y(2,:)); 

2 Answers2

3

Answering the time delay part, use

[corr,lag] = xcorr(tx, rx))

Where tx is one data set and rx is the other. The xcorr function will return the correlation and the index for each correlation as lag (read the help on xcorr for more info).

With that you can find the index of the max value for the correlation and then use that index to look up the lag. This will be the delay in samples, within the precision of your sample rate:

delay = lag(corr==max(corr))
Dan Boschen
  • 50,942
  • 2
  • 57
  • 135
  • the delay = -2983439 why is minus value? is it in nano-second or micro-second? – Nate Duong Mar 30 '17 at 12:48
  • 1
    The delay is in samples so whatever your sample duration is times that number is the delay. It will be minus or plus depending on which one is ahead of the other. Type help(xcorr) and read about the delay function as that should clarify which way the sign is used. – Dan Boschen Mar 30 '17 at 12:49
  • now, I am trying to use the spectrogram matlab tool which is also mentioned in my post, to make sure the TX file and RX file are similar, admit, they are not 100% similar because after transmitting data (sending TX file, streaming) to the receiver. From receiver now have RX file with TX data including and maybe noise. Do you have any experience for spectrogram? – Nate Duong Mar 30 '17 at 17:47
  • I have experience with the STFFT which is what the spectrogram is but not for your application. To check similarity I have computed the correlation coefficient. I have referenced this in other posts with more details if you are interested I could find a link. – Dan Boschen Mar 30 '17 at 18:22
  • if so, please provide me a link, maybe new technique also help me more understand. Thank you so much for being with me this post and other posts. – Nate Duong Mar 30 '17 at 18:39
  • You welcome Nate! Look at my response to this post for computing the correlation coefficient. http://dsp.stackexchange.com/questions/30847/noise-detection/30854#30854 – Dan Boschen Mar 30 '17 at 19:37
  • the plot above show us 10 seconds receiver received from the transmitter. It meaans, every 1 second the receiver receives single transmit data with noise included. Can I get every single peak of cross-correlation and calculate time delay? if so, I will have 10 different delay, and I can see how much delay change in 10 seconds? for example, I will have a vector of the delay in every second [0.0018,0.0018,0.0018,0.0017,-0.001,-0.001,-0.001,-0.001,-0.001] is it right when I say that? – Nate Duong Mar 31 '17 at 21:19
  • Yes, and it would be the average delay over the window of time that you correlate for; depending on the actual statistics of your noise, if it is stationary etc... What kind of receiver is this? – Dan Boschen Mar 31 '17 at 21:30
  • sorry for the late reply, but I do not understand you question about the kind of receiver, I can tell you if I know what you mean? this is a stationary situation, not static. – Nate Duong Apr 10 '17 at 18:12
  • I guess I mean to ask what are you trying to do; what is the high level application and what are the sources of these signals? – Dan Boschen Apr 10 '17 at 18:14
  • those signals are generated from the device which is called B210 ettus: https://www.ettus.com/product/details/UB210-KIT, using for the gps data collection, – Nate Duong Apr 10 '17 at 18:36
  • 3 receiver antennas, on the ground collect data at the same time and calculate time delay, then I can have distance from the equation: d = 3e8 * time – Nate Duong Apr 10 '17 at 18:38
  • So your signals are GPS signals from the satellites or do you have your own transmitter and custom waveform? – Dan Boschen Apr 10 '17 at 18:41
  • I have to test the system first, therefore I use myown transmitter and custom waveform to make sure it work, then collect GPS signals from the satellites. – Nate Duong Apr 10 '17 at 18:45
  • Are you using GPS C/A codes for your own waveform? The actual signals will be at different Doppler offsets so you will need to implement carrier as well as code tracking – Dan Boschen Apr 10 '17 at 18:48
  • Yes, I am. I am using GPS C/A codes for my own waveform. I know it will be different to to actual signals but for the next step design. Right now, I am care about giving out the right answer for the delay estimation. – Nate Duong Apr 10 '17 at 18:52
  • Ok the xcorr function should do this for you then, or you can use a circular correlation using ifft (fft(a), conj(fft(b))- play with that to see the difference and what happens in the result as the delay is changed – Dan Boschen Apr 10 '17 at 18:55
  • I also have new code update above. and trying to cut out in every second window. I want to see in every second, what different in time delay change, and I can have this plot, https://www.dropbox.com/s/kmhe9rdeq3a6wx3/1_83347.png?dl=0 , can you help me to confirm? is it make sense to you or not? I can not add more link, because lacking of reputation. – Nate Duong Apr 10 '17 at 19:02
  • Do you have any ideal? – Nate Duong Apr 12 '17 at 00:21
  • No the plot does not make any sense to me; it is hard to follow what you are doing exactly, I am sorry – Dan Boschen Apr 12 '17 at 00:38
  • you helped me too much, thank you very much, Dan ! – Nate Duong Apr 12 '17 at 20:18
  • According to the transmitted signal and received signal, I have 2 vector complex signals, If I want to calculate the Maximum Absolute Value (MAV). How to have that value? is it by this equation: mav_tx = max(abs(tx_data)); and mav_rx = max(abs(rx_data)); ? because sometimes, I need to adjust the gain to make sure MAV in this range between 1000 and 10000. – Nate Duong Apr 13 '17 at 18:00
  • Yes your formula for max absolute value is correct – Dan Boschen Apr 13 '17 at 19:24
1

I have not looked at your data set, but if that image is a plot of the cross-correlation, it means that you might have some kind of periodicity in the data that has a period of roughly 600k samples. Depending on your problem domain this could be an inherent ambiguity that is present in your system that has to be resolved using other heuristics.

Depending on what you know about the problem domain, you could take the time delay estimate is the delay of the first peak, or you could take it as the delay of the peak with the highest amplitude-- whichever makes more physical sense.

Robert L.
  • 2,212
  • 11
  • 21