1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Cross correlation in matlab

  1. Jan 23, 2012 #1
    1. The problem statement, all variables and given/known data
    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

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

    Attached Files:

  2. jcsd
  3. Jan 24, 2012 #2
    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?
     
  4. Jan 24, 2012 #3

    I like Serena

    User Avatar
    Homework Helper

    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).
     
  5. Jan 24, 2012 #4
    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:
    attachment.php?attachmentid=43093&stc=1&d=1327455259.jpg

    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
    attachment.php?attachmentid=43094&stc=1&d=1327455407.jpg

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

    Thanks for your response
     

    Attached Files:

  6. Jan 24, 2012 #5

    I like Serena

    User Avatar
    Homework Helper

    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;
     
  7. Jan 24, 2012 #6
    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');

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

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

    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:

  8. Jan 25, 2012 #7

    I like Serena

    User Avatar
    Homework Helper

    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;
     
  9. Jan 25, 2012 #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:
    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.
     
  10. Jan 25, 2012 #9

    I like Serena

    User Avatar
    Homework Helper

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

    Attached Files:

  12. Jan 25, 2012 #11

    I like Serena

    User Avatar
    Homework Helper

    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.
     
  13. Jan 25, 2012 #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!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Cross correlation in matlab
  1. Cross correlation (Replies: 3)

Loading...