# Cross corelation understanding -Digital Signal Communications (searching for specific vector inside the other vector)

I have two vectors , one called v1 and the other v2 , the size of v1 is 10k , and the other v2's size is 32 , I want to search inside the vector v1 if there's v2 , this means when I find that the vector v1 has the same data as v2 then there would be a high amplitude in the plot .. so Im trying to do do cross corelation between those two vectors in matlab, how can I do that? another question, also Im trying to understand the meaning of cross corelation, so I build one for loop and inside it I did shift indexes by 32 (size of v2 vector) to v2 (v(i:i+32)*v1) -product of sum, and then Im trying to see if the output gies me the required plot to see if there's a high amplituide if v2 found inside v1 .. but the plot is suck and couldn't see anything .. any help how can I do the cross corelation by for loop? thanks alot

Not sure which platform you are using . if it's MATLAB , here is small code snippet which I quickly wrote using MATLAB's inbuilt "strfind" function

A = [12  534 5 23 44 66  23 68  86854  7  36 457 68 23 46 87 78 588 86 8 44 66  23 68  86854 87 9 905  0 -213 34];
B = [44 66  23 68  86854]

strfind(A,B)    % returns 5 and 21 (starting index of Array B)


You can always use cross-correlation to find the similarity between A and B

Please go through this post . It has detailed explanation with examples.

https://www.mathworks.com/help/signal/examples/measuring-signal-similarities.html

EDITED :

you can alternatively use xcorr function too

For the same example vectors A, B .. you can do something like this

xCorrResult = xcorr(B,A);
plot(xCorrResult)


You can see two peaks at index 11 and 27 which indicates B vector lies in A at indexes 5 (11 - length(B) -1) and 21 (27 - length(B) - 1)

• Hi @Saira , Im using matlab platform , Im trying to do like this: I have two vectors one called v1 and the other v2 with difference size, trying to find out if v2 is found in v1 and once there's v2 inside v1 then at the plot of cross corelation there would be a high amplitude , this means when I do cross corelation v2 with v1 , and I plot the cross corelation of those two vectors, then the plot would be like there's a high amplitude once v1 figured in v2 and otherwise would be zero .. – Ryan Jun 7 at 18:09
• like something _______|_____|_____| , here for instance v1 appeared in v2 three times because there's three highs with high amplitudes in the plot of cross corelation .. so I want to do something like that any help please? – Ryan Jun 7 at 18:12
• If you go through the "mathworks" link , it gives you a very best explanation of cross-correlation with an example . I have edited my answer using xcorr function. – Saira Jun 7 at 18:29
• Hi Saira, thanks alot! but pelase could you explain to me the same concept of cross corelation by loop for with shifted (as what I explained in my post- the shift was by 32 because the other vector is with size 32 ) ? what Im trying to do the same what cross corelation does but by convolution/for loop with correct shift in the vector – Ryan Jun 7 at 18:43
• I would thank you alot if you explain that by for loop with vector shifted as convolution concept .. like v1(i:i+32) * v2 , v2 is size 32 .. so the shift must be 32 as I did in v1 .. and the product between those two vectors for every i would give me in the total matrix/vector that if I did plot to it then it would give me the same cross corelation plot ..! I understand that inner product with shifted would give me the same as cross corelation but Im trying to implement that by for loop (in other word to implement cross corelation by for loop to understand it correctly ) – Ryan Jun 7 at 18:46