Determining Delay with Cross Correlation

In summary: I'm not sure what is happening there.In summary, the cross correlation can be used to determine the degree that two series are related or correlated. The cross correlation bounds that the cross correlation is calculated to will indicate how much correlation at certain delays. The code that was provided outputs a peak at 200 when the delay is 94. However, when I tried to define my x without the delay zeros in front the cross correlation output dropped out at 94. The code that was provided outputs a spike at 200 when the delay is 200. However, when I tried to define my y without the delay zeros in front the cross correlation output dropped out at 94.
  • #1
Evo8
169
0

Homework Statement


Let x(t) be a transmitted radar pulse. The received radar pulse is y(t).

[itex]y(t)=ax(t-t_{d})+v(t)[/itex]

Where v(t) is the additive noise and td is the time delay. The signals x(t) and y(t) are sampled and processed digitally to determine the delay

The discrete time signals are:
x(n) = x(nT)
y(n) = ax(n − D) + v(n)

where v(n) is the noise in the received signal, T is the sampling time of the
receiver, and D is the number of samples the received pulse is delayed by
the round trip to the target.

1. Explain how we can use cross correlation to determine the delay

2.Let x(n) be the 13-point Barker sequence
x(n) = [11111 − 1 − 111 − 11 − 11]

and let v(n) be a Gaussian zero mean sequence with variance [itex]σ^{2} = .01[/itex]
A Gaussian random variable array with σ2 = 1 can be generated using randn(m,n). To convert the sequence to another value of σ2, just multiply the entire sequence by σ. Write a program that generates the sequence y(n), for 0 ≤ n ≤ 199 for variable parameters a and D. Choose D to be the last two digits of your birth year + the day of your birth. If your birthday was August 21, 1989, D = 89 + 21 = 110. Thus, in y(n), there will be D zeros prior to the beginning of the Barker sequence and enough zeros afterwards to increase the length of y(n) to 200.

D=94
a=0.8814

3. Plot the signals x(n) and y(n) for 0≤n≤199

4. Compute and plot the cross correlation. 0≤l≤185. Estimate the delay from the plot

Homework Equations


See code below.


The Attempt at a Solution



For #1 this is what I think. -The cross correlation can be used to determine the degree that two series are related or correlated. When the two signals are calculated and cross correlated the result can be graphed and analyzed for the delay. The bounds that the cross correlation is calculated to will indicate how much correlation at certain delays. If an area of interest is at the positive bound this shows a high correlation, if at the lower bound this indicated no correlation. If below the lower bound this can indicate a negative correlation.

Ok so I thought I had this one down but my results just don't match up to my original delay of 94. I get almost exactly double my original delay. Am I missing something here?

#3
Here is my code:
clear all
n=0:199;
a=0.8814;
D=94;
v=randn(1,200)*.01;
x=[zeros(1,94) 1 1 1 1 1 -1 -1 1 1 -1 1 -1 1 zeros(1,200-(13+D))];
y=a*x+v;
subplot(2,1,1); plot(x); grid
xlabel('...'); ylabel('...');
title('x(n) Plot');
subplot(2,1,2); plot(y); grid
xlabel('...'); ylabel('...');
title('y(n) Plot');

The plots from this code
attachment.php?attachmentid=43063&stc=1&thumb=1&d=1327368831.jpg



For #4 I reused the code and added the cross correlation:

clear all
n=0:185;
a=0.8814;
D=94;
v=randn(1,200)*.01;
x=[zeros(1,94) 1 1 1 1 1 -1 -1 1 1 -1 1 -1 1 zeros(1,200-(13+D))];
y=a*x+v;
z=xcorr(x,y);
subplot(1,1,1); plot(z); grid
xlabel('Delay'); ylabel('Amplitude');
title('Cross Correlation');

The plot of my calculated cross correlation
attachment.php?attachmentid=43064&stc=1&thumb=1&d=1327368831.jpg


From looking at this plot of the cross correlation I would say the delay is at 200. The delay I used in the data was 94 though? Where am I going wrong?

Thanks for any help on this and sorry for the super long post.
 

Attachments

  • Lab5_3.jpg
    Lab5_3.jpg
    23.9 KB · Views: 884
  • Lab5_4.jpg
    Lab5_4.jpg
    27.1 KB · Views: 1,260
Physics news on Phys.org
  • #2
Ive looked over the code again and I have a feeling I am on the right track. Maybe I am just not understanding how the cross correlation in MATLAB works?

Im using the function xcorr

does anyone have experience using this function?
 
  • #3
Hey Evo8! :smile:

If you look at you graphs of x and y, you can see that there is no delay.
The vector y only has a little noise.

You should define x without the delay zeroes in front, and y with the delay zeroes.

Now, your cross correlation gives you a peak at 200.
Since your vector is 200 entries long, this corresponds to a delay of 0, which is what you have now (it wraps around).
 
  • #4
Hi ILS!

I did notice that there is no delay. However I don't think defining my x without the delay zeros in front is the correct approach. The x is supposed to be a 200 elements array with the barker sequence given D zeros (94 in my case) in and then enough zeros after to complete the 200 element.

However I am looking at it again now and I think I see a problem with my y definition. In the problem statement it is defined as follows y(n) = ax(n − D) + v(n). I didnt seem to include that -D in my y definition.

I changed my code to the following: (See changes in bold)

clear all
n=0:185;
a=0.8814;
D=94;
v=randn(1,200)*(.01)^2;
x=[zeros(1,94) 1 1 1 1 1 -1 -1 1 1 -1 1 -1 1 zeros(1,200-(13+D))];
y=a*(x-D)+v;
z=xcorr(x,y);
subplot(1,1,1); plot(z); grid
xlabel('Delay'); ylabel('Amplitude');
title('Cross Correlation');


See the associated plot here:
attachment.php?attachmentid=43093&stc=1&d=1327455259.jpg


No you can see that I've placed a data cursor where the plot drops out. This happens to be right at 94 which is my delay. Does this look a little better? Why does it go back up at 300? I also see a very small spike at 200. This is where my array ends I assume?

For reference here are the input vs. output signals
attachment.php?attachmentid=43094&stc=1&d=1327455407.jpg


I feel like I am getting close but the data my not be as clean as it should be?

Thanks for your response
 

Attachments

  • Lab5_3a.jpg
    Lab5_3a.jpg
    29 KB · Views: 2,277
  • Lab5_3b.jpg
    Lab5_3b.jpg
    28.2 KB · Views: 2,292
  • #5
x=[zeros(1,94) 1 1 1 1 1 -1 -1 1 1 -1 1 -1 1 zeros(1,200-(13+D))];
y=a*(x-D)+v;

I think your y is not quite right yet.
You have subtracted D from the amplitude of x (vertically) and not from the index of x (horizontally), which you can see in your graphs or x and y.

Perhaps you could try something like:
undelayed_x=[1 1 1 1 1 -1 -1 1 1 -1 1 -1 1 zeros(1,200-(13))];
y=a*undelayed_x+v;
 
  • #6
hmmm I am not sure this is quite right either though. First the delay D is not used at all in either x or y. So how could I see a delay of D if its not used? This is what I get for plots if I use this code:

clear all
n=0:185;
a=0.8814;
D=94;
v=randn(1,200)*.01;
x=[1 1 1 1 1 -1 -1 1 1 -1 1 -1 1 zeros(1,200-(13))];
y=a*x+v;
z=xcorr(x,y);
subplot(1,1,1); plot(z); grid
xlabel('Delay'); ylabel('Amplitude');
title('Cross Correlation');

attachment.php?attachmentid=43097&stc=1&d=1327456772.jpg


attachment.php?attachmentid=43096&stc=1&d=1327456610.jpg


Also I am quite confident that my X is correct. My y however I believe is my issue.

See this text taken from the problem statement
Choose D to be the last two digits of your birth year + the day of your birth. If your birthday was August 21, 1989, D = 89 + 21 = 110. Thus, in y(n), there will be D zeros prior to the beginning of the Barker sequence and enough zeros afterwards to increase the length of y(n) to 200.

This one is stumping me. I am not sure how to go about it. The closes I've come is the previous set of plots. But as you said I am affecting the amplitude not the correct delay?
 

Attachments

  • test.jpg
    test.jpg
    28.8 KB · Views: 2,165
  • test2.jpg
    test2.jpg
    28.9 KB · Views: 2,245
  • #7
As you can see in the graph, y is still not shifted in time.

Perhaps you can try with:

x=[zeros(1,D) 1 1 1 1 1 -1 -1 1 1 -1 1 -1 1 zeros(1,200-(13+D))];
undelayed_x=[1 1 1 1 1 -1 -1 1 1 -1 1 -1 1 zeros(1,200-(13))];
y=a*undelayed_x+v;
 
  • #8
I could try this. However it is not different then what is tried in the above post.So my result will not change.

This is from your previous post:
Perhaps you could try something like:
undelayed_x=[1 1 1 1 1 -1 -1 1 1 -1 1 -1 1 zeros(1,200-(13))];
y=a*undelayed_x+v;

This is from my code in the previous post:
x=[1 1 1 1 1 -1 -1 1 1 -1 1 -1 1 zeros(1,200-(13))];
y=a*x+v;

And this is from the last post:
x=[zeros(1,D) 1 1 1 1 1 -1 -1 1 1 -1 1 -1 1 zeros(1,200-(13+D))];
undelayed_x=[1 1 1 1 1 -1 -1 1 1 -1 1 -1 1 zeros(1,200-(13))];
y=a*undelayed_x+v;

The y definition has not changed and is still using the "undelayed_x." Did you mean to try plotting the delayed X vs the undleayed x in your post above? This might work however i don't think its what's intended. The received signal is y not x. I am not sure where to go on this one.
 
  • #9
Reading your problem statement again, I must modify my statement, since the delay is the wrong way around.

It should be:

x=[1 1 1 1 1 -1 -1 1 1 -1 1 -1 1 zeros(1,200-(13))];
delayed_x=[zeros(1,D) 1 1 1 1 1 -1 -1 1 1 -1 1 -1 1 zeros(1,200-(13+D))];
y=a*delayed_x+v;

Either way, your result will change, since your x and y will be shifted in time, as they are supposed to.

You should not do anything else with the "delayed_x".
I only introduced it to construct the proper y, which can also be done in other (and better) ways.
 
  • #10
Ok now i think i may see where you were going with this. I was a bit confused.

So this is what i have for code: Note this is only the parts of interest right now.


x=[1 1 1 1 1 -1 -1 1 1 -1 1 -1 1 zeros(1,200-(13))];
delayed_x=[zeros(1,D) 1 1 1 1 1 -1 -1 1 1 -1 1 -1 1 zeros(1,200-(13+D))];
y=a*delayed_x+v;
z=xcorr(x,y);

The plots below are ordered as follows. Cross correlation on y and x. x and then delayed_x. Where i think i didnt really understand was that the y is using the delayed x, when computing the cross correlation I am using the non delayed x and the y which has the delayed x within. Now I get a time shift.

attachment.php?attachmentid=43117&stc=1&d=1327496185.jpg


The cross correlation shows a peak at 106. This is somewhat close to my delay of 94? How does this look now? where does this discrepancy come from? The nature of computing cross correlation?

Thanks for the help ILS
 

Attachments

  • test3.jpg
    test3.jpg
    21.5 KB · Views: 2,138
  • #11
Good! :)

The discrepancy is because the shift is counted as negative (it is y[t-D]).
Since your vector has length 200, it wraps around and 200-94=106.
 
  • #12
Ah this makes sense!

Thanks so much. I apologize for not understanding where you were going with this. The gears were turning but it took a little bit for them to get up to speed.

Thanks again!
 

1. What is cross correlation in Matlab?

Cross correlation in Matlab is a statistical method used to measure the similarity between two signals or data sets. It calculates the correlation coefficient between two signals at different time lags to determine if there is a relationship between them.

2. How is cross correlation performed in Matlab?

In Matlab, cross correlation can be performed using the "xcorr" function. This function takes two signals as input and returns the cross correlation values at different lags. It also has options to specify the type of correlation and the normalization method to be used.

3. What is the difference between cross correlation and autocorrelation in Matlab?

Autocorrelation in Matlab is the correlation of a signal with itself at different time lags, while cross correlation is the correlation between two different signals. Autocorrelation can be seen as a special case of cross correlation where the two signals are identical.

4. Can cross correlation be used for non-time series data in Matlab?

Yes, cross correlation can be used for any type of data in Matlab, as long as there is a reasonable assumption that there may be a relationship between the two data sets. It is commonly used in image processing and pattern recognition to compare two images.

5. Are there any limitations to using cross correlation in Matlab?

One limitation of cross correlation in Matlab is that it assumes a linear relationship between the two signals, which may not always be the case. It is also sensitive to noise and outliers in the data, so it is important to pre-process the data before performing cross correlation.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
764
  • Engineering and Comp Sci Homework Help
Replies
7
Views
820
  • Engineering and Comp Sci Homework Help
Replies
1
Views
914
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • High Energy, Nuclear, Particle Physics
Replies
9
Views
1K
  • Differential Equations
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
Back
Top