# Homework Help: Cross correlation in matlab

1. Jan 23, 2012

### Evo8

1. The problem statement, all variables and given/known data

$y(t)=ax(t-t_{d})+v(t)$

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 $σ^{2} = .01$
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

2. Relevant equations
See code below.

3. 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

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

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.

#### Attached Files:

File size:
23.9 KB
Views:
328
• ###### Lab5_4.jpg
File size:
27.4 KB
Views:
456
2. Jan 24, 2012

### Evo8

Ive looked over the code again and I have a feeling im on the right track. Maybe im just not understanding how the cross correlation in matlab works?

Im using the function xcorr

does anyone have experience using this function?

3. Jan 24, 2012

### I like Serena

Hey Evo8!

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. Jan 24, 2012

### Evo8

Hi ILS!

I did notice that there is no delay. However I dont 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 Im 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:

No you can see that Ive 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

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

#### Attached Files:

File size:
29.2 KB
Views:
1,649
• ###### Lab5_3b.jpg
File size:
28.4 KB
Views:
1,653
5. Jan 24, 2012

### I like Serena

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. Jan 24, 2012

### Evo8

hmmm Im 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');

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

See this text taken from the problem statement
This one is stumping me. Im not sure how to go about it. The closes ive come is the previous set of plots. But as you said im affecting the amplitude not the correct delay?

#### Attached Files:

File size:
29.1 KB
Views:
1,598
• ###### test2.jpg
File size:
29.1 KB
Views:
1,719
7. Jan 25, 2012

### I like Serena

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. Jan 25, 2012

### Evo8

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:
This is from my code in the previous post:
And this is from the last post:
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 dont think its whats intended. The received signal is y not x. Im not sure where to go on this one.

9. Jan 25, 2012

### I like Serena

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. Jan 25, 2012

### Evo8

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 Im using the non delayed x and the y which has the delayed x within. Now I get a time shift.

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

#### Attached Files:

• ###### test3.jpg
File size:
21.5 KB
Views:
1,618
11. Jan 25, 2012

### I like Serena

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. Jan 25, 2012

### Evo8

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!