# Cross correlation in signal analysis

Started by April 18, 2008
```Hello,
I'm trying to gain understanding of cross correlation practical use.
My textbook (among others) define auto and cross correlation and
states they are both widely used in signal processing and detecting
signals with noise. So, I simply want to learn how to use it
effectively.
I made simple script in MATLAB:
t = 0:0.1:10; %time
x = 2*sin(t);
plot(x)
z = randn(1,length(t));
hold on;
plot(z,'r');

xcorrelation = xcorr(x,z);
figure(2)
plot(correlation,'g');
hold on
acorrelation = xcorr(x,x)
plot(acorrelation,'m');

I wanted to see how cross corelation look like when signal (sinusiod)
is correlated with itself and with noise.
If you execute this script, you'll see that auto correlation of
sinusoids has much higher amplitude then cross correlation (noise nad
sinusoide) as expected.
Now I'm trying to figure out how to make practical use of that? What
treshold to set in order to make a guess if signal is sinusoid ore
something else?
I simply don't know what to do further.
Can you explain a little bit more? I hope there are people here who
use correlation in practice and research and who can give one or two
Thank you
```
```On Apr 18, 12:16&#2013266080;pm, brob...@yahoo.com wrote:
> Hello,
> I'm trying to gain understanding of cross correlation practical use.
> My textbook (among others) define auto and cross correlation and
> states they are both widely used in signal processing and detecting
> signals with noise.

An 1024 point FFT is basically a cross correlation between the input
signal and 1024 different sinusoids, with excellent noise rejection
due to the reduction in BW of each output bin vs the BW of the input
characteristics).  So I guess that's a practical use you can look into
```
```>If you execute this script, you'll see that auto correlation of
>sinusoids has much higher amplitude then cross correlation (noise nad
>sinusoide) as expected.
Do you mean noise alone correlated with the sinusoid?  Yes, if this is the
case, since AWGN is uncorrelated with a sinusoid.

>Now I'm trying to figure out how to make practical use of that? What
>treshold to set in order to make a guess if signal is sinusoid ore
>something else?
>I simply don't know what to do further.
>Can you explain a little bit more? I hope there are people here who
>use correlation in practice and research and who can give one or two

What are you looking for?

The correlator receiver is identical to a matched filter.  One common
application is the RAKE receiver used in DS-CDMA applications (PCS phones).
Each user in such a system is spread with a unique code, orthogonal to
other users (for forward link, i.e., base to phone, these are typically
Walsh codes).  The input signal is correlated with the desired user code
(including the scrambling code) in order to extract the signal from the
aggregate transmission.  The term RAKE comes from the fact that the
receiver has several correlators, each shifted in time to obtain signals
that result from multipath (a result of reflection/refraction).

For a single signal transmitted through an AWGN channel, the correlator is
an optimal (minimum mean-square error sense) detector.  With the addition
of other impairments, e.g., Rayleigh fading resulting from multipath, or
other signals that are not orthogonal to the desired signal, the detector
is no longer MMSE optimal.  There are a variety of methods that approach an
optimal detector, which is probably beyond what you are interested in.

Mark
```
```On Apr 19, 7:16 am, brob...@yahoo.com wrote:
> Hello,
> I'm trying to gain understanding of cross correlation practical use.
> My textbook (among others) define auto and cross correlation and
> states they are both widely used in signal processing and detecting
> signals with noise. So, I simply want to learn how to use it
> effectively.
> I made simple script in MATLAB:
> t = 0:0.1:10; %time
> x = 2*sin(t);
> plot(x)
> z = randn(1,length(t));
> hold on;
> plot(z,'r');
>
> xcorrelation = xcorr(x,z);
> figure(2)
> plot(correlation,'g');
> hold on
> acorrelation = xcorr(x,x)
> plot(acorrelation,'m');
>
> I wanted to see how cross corelation look like when signal (sinusiod)
> is correlated with itself and with noise.
> If you execute this script, you'll see that auto correlation of
> sinusoids has much higher amplitude then cross correlation (noise nad
> sinusoide) as expected.
> Now I'm trying to figure out how to make practical use of that? What
> treshold to set in order to make a guess if signal is sinusoid ore
> something else?
> I simply don't know what to do further.
> Can you explain a little bit more? I hope there are people here who
> use correlation in practice and research and who can give one or two
> Thank you

Locating a sound uses cross correlation. Using two microphones you can
deduce the bearing (though it is not unique). The time delay bewteen
microphones is estimated using cc. However, you will find that
ordinary cc only gives well defined peaks if the (random) signals are
white. This is not much use since speech is always coloured. Therefore
Generalized Cross Correlation is used - an extension using a weighting
function and FFTs.

K.
```
```On Apr 18, 3:16&#2013266080;pm, brob...@yahoo.com wrote:
> Hello,
> I'm trying to gain understanding of cross correlation practical use.
> My textbook (among others) define auto and cross correlation and
> states they are both widely used in signal processing and detecting
> signals with noise. So, I simply want to learn how to use it
> effectively.
> I made simple script in MATLAB:
> t = 0:0.1:10; %time
> x = 2*sin(t);
> plot(x)
> z = randn(1,length(t));
> hold on;
> plot(z,'r');
>
> xcorrelation = xcorr(x,z);
> figure(2)
> plot(correlation,'g');
> hold on
> acorrelation = xcorr(x,x)
> plot(acorrelation,'m');
>
> I wanted to see how cross corelation look like when signal (sinusiod)
> is correlated with itself and with noise.
> If you execute this script, you'll see that auto correlation of
> sinusoids has much higher amplitude then cross correlation (noise nad
> sinusoide) as expected.
> Now I'm trying to figure out how to make practical use of that? What
> treshold to set in order to make a guess if signal is sinusoid ore
> something else?
> I simply don't know what to do further.
> Can you explain a little bit more? I hope there are people here who
> use correlation in practice and research and who can give one or two
> Thank you

Auto correlation is used to determine if there is an &#2013266065;echo&#2013266066; or time-
delayed version(s) of a signal in the data from one sensor.  It&#2013266066;s
performed by taking the FFT of the sensor data, multiplying the
transform with its complex conjugate, inverse transforming, and then
using a peak detector on the output.  The peak(s) in the output tell
you the time delay or delays, if there are multiple echoes.
Mathematically, auto correlation is similar to the convolution of a
signal with itself (F[k] below is the transform of the signal):

convolution:                F[k] times F[k]     then inverse transform
auto correlation:          F[k] times F[k]*  (* = conjugate) then
inverse transform

The reason for the conjugation operation has to do with the fact that
you don&#2013266066;t want that &#2013266065;time reversal&#2013266066; thing that happens with
convolution.

As with convolution, you should zero-pad to avoid doing a circular
auto correlation.

Cross correlation is used to determine if there is a time delay
between a signal arriving at two spatially separate sensors.  It&#2013266066;s
performed by taking the FFTs of data from each sensor, conjugating one
of them, multiplying them, point by point, and then inverse
transforming.  Once again, you should zero-pad to avoid circular cross
correlation.

If there is a strong, unwanted sinusoid in your input, then the result
of the cross correlation will be based on the strong unwanted
sinusoid.  You can minimize the effect of the unwanted component by
using a window function in the frequency domain prior to inverse
transforming.  That is, FFT each sensor, conjugate one result,
multiply the transforms, multiply by the window, inverse transform,
then peak detect.  But note that the window functions (there are
several, some much better than others) are NOT like the window
functions (e.g.: Hann, Hamming, etc.) used to minimize leakage and
picket fence effects.

Here&#2013266066;s 3 references that may help:

J. C. Hassab, R. E. Boucher, &#2013266067;Optimum Estimation of Time Delay by a
Generalized Correlator,&#2013266068; IEEE T-ASSP, vol. 27, no. 4, Aug. 1979, pp.
373-380.

J. C. Hassab, R. E. Boucher, &#2013266067;Performance of the Generalized Cross
Correlator in the Presence of a Strong Spectral Peak in the Signal,&#2013266068;
IEEE T-ASSP, vol. 29, no. 3, June 1981, pp. 549-555.

J. C. Hassab, R. E. Boucher, &#2013266067;An Experimental Comparison of Optimum
and Sub-Optimum Filters&#2013266066; Effectiveness in the Generalized Correlator,&#2013266068;
J. Sound and Vibration, 1981, (12 pages total - I got this as a class
handout, and it doesn&#2013266066;t have the page numbers).
```
```Thank you for the explanation. I'm beginer in the subject and there is
one more thing that I need to understand. That is "real" definition of
correlation. In one of my textbook there is example of cross
correlation of stohastic signals x(t) and y(t). Cross correlation is
defined as:
C(tau) = (1/N)*sum( x(n)*y(n+tau) ) when N ->inf, while in other cros
correlation is defined as sum( x(n) * y(n+tau) ). I notoced that same
definition is used by Matlab's function xcorr. I'm really confused, it
seems that definitions are different for deterministic and stohastic
signals...
```
```<kevinjmcgee@netscape.net> wrote in message

> Auto correlation is used to determine
> if there is an &#2013266065;echo&#2013266066; or time-
> delayed version(s) of a signal in the
> data from one sensor.  It&#2013266066;s
> performed by taking the FFT of the
> sensor data, multiplying the
> transform with its complex conjugate,
> inverse transforming, and then
> using a peak detector on the output.
> The peak(s) in the output tell
> you the time delay or delays, if there
> are multiple echoes.
> Mathematically, auto correlation is
> similar to the convolution of a
> signal with itself (F[k] below is
> the transform of the signal):
>
> convolution:
>               F[k] times F[k]     then
>               inverse transform
> auto correlation:
>               F[k] times F[k]*  (* = conjugate) then
>               inverse transform

[snip]

> Cross correlation is used to determine
> if there is a time delay
> between a signal arriving at two
> spatially separate sensors.  It&#2013266066;s
> performed by taking the FFTs of data
> from each sensor, conjugating one
> of them, multiplying them, point by
> point, and then inverse
> transforming.  Once again, you should
> zero-pad to avoid circular cross
> correlation.

[snip]

Thanks for an unusually informative post.

```
```On Apr 20, 5:23&#2013266080;am, brob...@yahoo.com wrote:
> Thank you for the explanation. I'm beginer in the subject and there is
> one more thing that I need to understand. That is "real" definition of
> correlation. In one of my textbook there is example of cross
> correlation of stohastic signals x(t) and y(t). Cross correlation is
> defined as:
> C(tau) = (1/N)*sum( x(n)*y(n+tau) ) when N ->inf, while in other cros
> correlation is defined as sum( x(n) * y(n+tau) ). I notoced that same
> definition is used by Matlab's function xcorr. I'm really confused, it
> seems that definitions are different for deterministic and stohastic
> signals...

We were all beginners at some time or another. For some things, I
still am.  A &#2013266067;real&#2013266068; definition of correlation can be found in
Wikipedia.  But perhaps you won&#2013266066;t need it.  Correlation is a measure
of similarity.  If you have 2 identical waveforms, then they will be
correlated with a correlation coefficient of 1 (look up the equation
of correlation coefficient - it&#2013266066;s very similar to what a DFT does).
If the waveforms are exactly the opposite of each other, the
correlation coefficient will be -1.  Waveforms that are dissimilar
will have a correlation coefficient somewhere between the two extremes
of 1 and -1. There&#2013266066;s also a definition of &#2013266067;stochastic&#2013266068; (Wikipedia also
has it).  A stochastic signal is random, like a coin toss.
I wouldn&#2013266066;t worry about the 1/N part of the one definition you have
for the cross correlation.  Ask yourself this: can you calculate
something to infinity?  I don&#2013266066;t think so (and if you can, please tell
the rest of us, because we&#2013266066;d really like to know).  Your MATLAB
definition seems correct: sum( x(n) * y(n+tau) ).  I&#2013266066;m probably not
the best person to ask, since I use C or C++ compilers to test things,
and I don&#2013266065;t know much about MATLAB.
As for being confused about deterministic and stochastic signal, it&#2013266066;s
probably due to my previous explanation not being clear enough.  Let&#2013266066;s
say you have a signal.  Perhaps it&#2013266066;s a strong sinusoid
(deterministic).  In that case, the cross correlation would yield a
result that corresponds to the time delay between the deterministic
signal arriving at the 2 sensors at different times.  Perhaps you may
have a little bit of noise to deal with.  For a strong signal to noise
ratio, this will work out fine.  But suppose your signal is weak
Gaussian noise (stochastic).  The cross correlation will still give
you the time delay.  But suppose furthermore that there is a strong
sinusoidal &#2013266067;noise&#2013266068; signal present.  Your time delay estimate of the
weaker stochastic signal will be corrupted by the strong sinusoid.
You&#2013266066;d have to eliminate it somehow from your processing. That&#2013266066;s why
you use the windowing in my original response.  The lower signal to
noise ratio problems are more difficult than strong signal to noise
problems, which is why most people tend to focus on them.  I hope this
clears things up.
```