-1
$\begingroup$

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,:)); 
$\endgroup$
3
$\begingroup$

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))
$\endgroup$
  • $\begingroup$ the delay = -2983439 why is minus value? is it in nano-second or micro-second? $\endgroup$ – Nate Duong Mar 30 '17 at 12:48
  • 1
    $\begingroup$ 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. $\endgroup$ – Dan Boschen Mar 30 '17 at 12:49
  • $\begingroup$ 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? $\endgroup$ – Nate Duong Mar 30 '17 at 17:47
  • $\begingroup$ 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. $\endgroup$ – Dan Boschen Mar 30 '17 at 18:22
  • $\begingroup$ 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. $\endgroup$ – Nate Duong Mar 30 '17 at 18:39
1
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.