3
$\begingroup$

If we do auto-correlation or even cross-correlation of a sinusoidal signal, I can see multiple peaks, but they decay as the lag index increases, it looks like the output is divided by the lag index?. A sinusoidal signal is a periodic signal, so the shape of its auto-correlation or cross-correlation should ideally repeat with consistent peaks and troughs, but why the auto-correlation and cross-correlation decay as the lag index increases. I was expecting multiple positive/negative peaks but all the peaks should have similar amplitude, like a sin/cos signal, not a diamond shape?

here is a simple code:

clc
clear all
fs = 1e3; % sampling frequency
ts = 1/fs; % sample duration
N = 1024; % number of points
f0 = 20; % signal frequency
n = 0:N-1; % sampling instants
x = cos(2*pi*f0*n*ts);
x_shifted = circshift(x,10);
[a,lags] = xcorr(x,x_shifted);
figure;plot(lags,a)
xlabel('lags')
ylabel('cross-correlation')
[a,lags] = xcorr(x,x);
figure;plot(lags,a)
xlabel('lags')
ylabel('auto-correlation')

enter image description here enter image description here

$\endgroup$
2
  • 2
    $\begingroup$ That’s because the sequence is finite. Look at the documentation for the xcorr function, you’ll find your answer there. $\endgroup$
    – Jdip
    Commented Jul 16 at 12:33
  • 2
    $\begingroup$ You are not doing the correlations of a sine wave, but of truncated sine wave. For periodic signals you are better of doing a circular correlation instead of a linear one. $\endgroup$
    – Hilmar
    Commented Jul 16 at 12:46

2 Answers 2

5
$\begingroup$

This is because the sine wave is of finite duration. Consider the linear autocorrelation of a rectangular pulse - the result is a triangle given the autocorrelation is a sample by sample product (complex conjugate product for complex waveforms) and sum for each an every offset between the two in time. The OP's case is that for a sine wave multiplied by a rectangular pulse, so we get the same envelope as a triangular function combined with the expected periodicity due to the sinusoid.

This is demonstrated as an animation below where the top part of the figure shows two rectangular windowed sinusoids as the offset between the two is changed. The middle part of the figure shows the sample by sample product. For any given offset, the array of all products is summed (the entire middle graphic) to create the one sample indicated by the moving circle in the lower graphic. From this we also see intuitively why the magnitude will increase linearly and then decrease as more or less of the two waveform overlap, resulting in more non-zero samples in the sum for the total correlation result at any given time offset.

autocorrelation animation

The resulting autocorrelation for this example is given in the figure below:

autocorrelation

If you instead do a circular correlation function, then you will see the correlation result constant for all offsets (or in the case of your sinusoid, a constant envelope), which would be equivalent to a linear autocorrelation with the sinusoid extending to $\pm \infty$. This can be implemented using the fft as follows:

$$\text{CIRCORR} = \text{IFFT}\bigg\{\text{FFT}\{x[n]\}\text{FFT}\{y[n]\}^* \bigg\}$$

Where the $(^*)$ represents a complex conjugate. The above shows the more generalized cross-correlation function for $x[n]$ and $y[n]$. Replace $y[n]$ with $x[n]$ to get the autocorrelation function.

To demonstrate this, below is the magnitude plots for linear autocorrelation and cross-correlation for the pseudo-random noise sequences (C/A Code) in GPS signals from SV24 (SV = Space Vehicle) and SV7. Below that I show the same result using the circular autocorrelation and cross-correlation approach introduced above with FFTs:

LINEAR XCORR:

Linear XCORR

CIRCULAR XCORR:

Circular XCORR

$\endgroup$
13
  • 2
    $\begingroup$ There is a reason why I like to define my autocorrelation function a little differently in the code I use for pitch detection. I don't like them ramps either. $\endgroup$ Commented 2 days ago
  • 2
    $\begingroup$ Oh and Dan, of course if you wanna use the FFT and iFFT to do autocorrelation, you can get the same result as the MATLAB xcorr() by zero-padding $x[n]$ with as many zeros as input samples. Then the circular convolution will look just like linear convolution with the rectangularly-windowed input and you'll get the diamond shape. Of course that diamond envelope is deterministic and you can divide by it (someone did in some paper, but I can't remember who). It wouldn't even have to be a rectangular window. You could Hann window it and the envelope would look like the autocorrelation of Hann. $\endgroup$ Commented 2 days ago
  • 1
    $\begingroup$ @robertbristow-johnson nice — yes I typically do the circular (don’t like the ramps) but perhaps it’s also because of the kind of waveforms I work with. Thanks $\endgroup$ Commented 2 days ago
  • 1
    $\begingroup$ ..chat gods are soon going to yell at us for this long thread. - - - - - Yeah, you met Peter. He's the cat that keeps us lil' miceys in line. $\endgroup$ Commented yesterday
  • 1
    $\begingroup$ But I think I get it. You have a constellation of spread spectrum basis symbols. You wanna know how flat the are and how decorrelated they are. And I can see doing that with circular convolution. $\endgroup$ Commented yesterday
3
$\begingroup$

This is because you are using the biased estimate of the auto- and cross-correlation sequence. The biased estimate divides all samples by $N$ where $N$ is the length of the signal. The unbiased estimate scales by $\frac{1}{N-\left\lvert k\right\rvert}$ where $k$ is the lag index (see is the unbiased autocovariance sequence truly unbiased). Because your signal is finite-length, computing the linear convolution with biased estimates will inevitably result in zeros getting averaged into all lags except $k=0$. You can correct this by element-wise multiplying the biased auto- and cross-correlation sequences by $\frac{N}{N-\left\lvert k\right\rvert}$.

The unbiased auto- and cross-correlation sequences, while useful for cases like this, don't have much practical worth. For one, it is an indefinite sequence (see why does the unbiased sample ACS sometimes not peak at lag 0). This poses a lot of problems for it computationally. Second, it's only unbiased if the statistical mean is known, not the sample mean. So, it's not very practical. Here's your updated code to see the change.

clear; clc; close all;
fs = 1e3; % sampling frequency
ts = 1/fs; % sample duration
N = 1024; % number of points
f0 = 20; % signal frequency
n = 0:N-1; % sampling instants
x = cos(2*pi*f0*n*ts);
x_shifted = circshift(x,10);
[a,lags] = xcorr(x,x_shifted);
figure;plot(lags,a)
xlabel('lags')
ylabel('cross-correlation')
figure;plot(lags,a.*(N./(N-abs(lags))))
xlabel('lags')
ylabel('unbiased cross-correlation')
[a,lags] = xcorr(x,x);
figure;plot(lags,a)
xlabel('lags')
ylabel('auto-correlation')
figure;plot(lags,a.*(N./(N-abs(lags))))
xlabel('lags')
ylabel('unbiased auto-correlation')
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.