I am a beginner in Digital Signal Processing and I was trying to compute and plot an autocorrelation.
I've wrote this piece of code:
r = [zeros(2,1); y(1:98,1)];
r = r.*y;
and I wished to know if this is a valid way of computing an autocorrelation.
3 Answers 3
- Your code contains magic numbers, avoid them
- To calculate the autocorrelation, you do not add zeros.
y(1+lag:end).*y(1:end-lag)
would match the definition. - Multiplying requires your data to be binary -1 or 1, not 0 or 1. I would use a version which processes logic arrays.
Are you trying to calculate the correlation factor or the cross correlation function?
The correlation factor is basically a normalized dot product of two vectors.
The cross correlation could be calculated using the conv
function (By flipping one of the vectors).