# Find Preamble (pattern) in signal, using Corelation

I have a signal which always has the same preamble (4x 0xAA - 10101010...).

I also have a signal with additive noise, and I want to find my preamble in that a noisy signal using corelation.

U used xcorr, but I was a little confused by that. Can you help me? It looks like that signal was too noisy so that no preamble was found?

I have just used

corel=xcorr(preamble(:,2),u_noisy(:,2));
• Correlation is, for uncorrelated signals, additive, and since the noise should be uncorrelated to your preamble, the signal can't be "too noisy": The correlation would always increase by the preamble's energy when you hit just the exactly right time offset index. (unless the assumption of non-correlatedness breaks down, or the noise is so powerful that the autocorrelation of the preamble doesn't stick out enough) – Marcus Müller Apr 1 '17 at 13:54

You should be able to see an peak correlation as from your plot and the length of your pattern it does not appear to be noisy enough such that you would not see a peak (unless your noise is completely in the time domain as in excessive jitter that I cannot visibly see).

You will also benefit by removing the mean from your data (such that it is bipolar in the correlation computation) as you will see in my example below. Also as shown this does not appear to be the reason you are not getting any result (as shown by my result even with the mean present).

Trying to duplicate your conditions, here is the result I got when I correlated the 4x 0xAA pattern to a random sequence of 0's and 1's for 5000 samples: (although I reversed the correlation from what you did such that the preamble correlation would appear first):

And then with the preamble at the beginning and repeated in the middle of the random sequence (note the noise is very big in this case; as I added the random data to the preamble, just to show what you should expect to see in extremely noisy conditions:

And by removing the mean of the signals prior to correlation the detection becomes quite easy even in the presence of the very significant noise (the noise is a random pattern of 0's and 1's at sample rate):

Also for completion, below shows the result in the case of correlating over just a portion of your sequence (1x 0xAA) with the same preamble in the sequence (4x 0xAA); 20Log(4) less processing gain but showing that a clear detection is still possible with the amount of noise I had added (and such an approach may be done if the rest of the preamble is reserved for timing recovery; meaning by design we need to detect early in the preamble):

Not directly related to why you are having issue, but a challenge also to be aware of with this pattern is that while great for timing recovery (as there is a transition at your data rate on every symbol), your preamble does not have a unique autocorrelation as you can see by the cyclical pattern in the plots above.

The same processing gain in SNR is achieved as in other patterns at the position in cross-correlation where the delay between the patterns is zero , $R_{xy}(0)$, (the gain in SNR is 20Log(N) where N is the number of samples where the noise is still uncorrelated; usually due to symbol rate filtering this is the number of symbols) and if you use a circular correlation you can also achieve that SNR advantage at every offset where the sequence aligns as described above as well. For timing critical applications where it is important to know the actual position within the sequence, Barker codes or other similar pseudo-random sequences are used that have the property that correlation is maximum at shift = 0 and close to 0 everywhere else (such as maximum length sequences). An example 11 bit Barker code is 10110111000. If you treat the 0's as -1 you will see that this sequence will add to +11 when they are added and in a circular correlation will add to +1 everywhere else. Note however the challenge for clock recovery in such a sequence given the longer runs of ones and zeros compared to 1010101010.

• Thank you answer! Now my idea: Send the barker from first (A) device. Receive this signal on second (B) device. Then (with constant delay) send received barker back to first device. Then by using correlation and timer compute the delay between sent and recieved signal - just pulse radar. For transmit/receive use something like si4463 in RAW mode and STM32 for compute the delay. But not sure if this technology can be so accurate. Speed of light 3e8 m/s, It is about 3 m for 10 ns. But make real time correlation and measure time with resolution about 10 ns on STM32 e.g. F4 ? – WITC Apr 5 '17 at 12:44
• Yes I have implemented such exact systems for active ranging using spread spectrum (so with PN codes instead of barker codes). Note your clock rate will ultimately limit RMS range accuracy using rms range = T/sqrt(12) since the signal has a uniform probability of arrival anywhere between two clock edges – Dan Boschen Apr 5 '17 at 13:06