Azzera filtri
Azzera filtri

Autocorrelation function of sin(ωt) -- XCORR complications

16 visualizzazioni (ultimi 30 giorni)
I am an undergraduate student currently researching with a professor, and have been tasked with generating the autocorrelation function of sin(ωt). The result that is produced by my code is, rightly, cos(ωt) with the x axes as tau. The challenge, however, is that the x axes of the sine, and resulting cosine function do not match up: while the sine goes up to 5seconds, its autocorrelation (the cosine function) goes up to 2000seconds. Additionally, while the amplitude of the sine is 1, that of the autocorrelation is about 500. The more pressing issue, however, is the unmatching x axis.
The following is my code, please advice on how to improve it so that the time axes, and amplitude, match up:
%%Test 02
N=1024; % Number of samples
f1=1; % Frequency of the sinewave
FS=200; % Sampling Frequency: perfect reconstruction of signal
%possible when the sampling frequency is greater than twice max freq (Nyquist)
n=0:N-1; % Sample index numbers
x=sin(2*pi*f1*n/FS); % Generate the signal, x(n)
t=[1:N]*(1/FS); % Prepare a time axis
subplot(2,1,1); % Prepare the figure
plot(t,x); % Plot x(n)
title('Sinwave');
xlabel('Time, [s]');
ylabel('Amplitude');
grid;
Rxx=xcorr(x); % Estimate its autocorrelation
subplot(2,1,2); % Prepare the figure
plot(Rxx); % Plot the autocorrelation
grid;
title('Autocorrelation function of the sine function');
xlabel('\tau (s)');
ylabel('Autocorrelation');
Thanks for your input, it is very much appreciated.

Risposte (1)

Wayne King
Wayne King il 29 Apr 2012
To address your first question, the x-axis does not go up to 2000 seconds. What you have to keep in mind is that the autocorrelation is computed for negative and positive lags. The vector is both right and left shifted with respect to itself. This is stated in the documentation. For a real-valued function this results in an even function of the lag.
To address your second question, the autocorrelation is computed by default without any scaling. This is also stated in the documentation. The following code will produce what you expect.
N = 1024; % Number of samples
f1 = 1; % Frequency of the sinewave
FS = 200;
n = 0:N-1; % Sample index numbers
x = sin(2*pi*f1*n/FS); % Generate the signal, x(n)
[xc,lags] = xcorr(x,'coeff');
tau = lags*1/FS;
plot(tau,xc)
  3 Commenti
Wayne King
Wayne King il 29 Apr 2012
Because the summation necessarily involves fewer and fewer terms as the lag increases. Think about shifting one finite length vector with respect to itself, the greater the shift, the less the overlap and therefore the fewer products there will be in the sum.

Accedi per commentare.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by