How to get the autocorrelation of this finite discrete function?

This is the function:

$$x(n) = \begin{cases} 1, & \text{-N \leq n \leq N ;}\\ 0, & \text{otherwise.} \end{cases}$$

And I tried to solve it, using sigma notation or summation:

$$\ r_{xx}(l) = \displaystyle\sum_{n=-\infty}^{\infty} x(n)x(n-l)$$

I get 2N-1 but that is wrong. Because when I do the autocorrelation in MATLAB I get something like: For exmple if N = 2, the discrete function rxx(l) would be like:

Which is something like rxx(l) = 2N-1+|l| But I don't know how to get there... Can you help me?

• In your definition $\displaystyle r_{xx}(l) = \displaystyle\sum_{n=-\infty}^{\infty} x(n)x(n-l),$ for $l = 3$ and with $N=5$, say, can you list all the nonzero terms in that sum $r_{xx}(3)$? If you think this is too much work, how about just the value of $x(6)x(6-3)$ and $x(5)x(5-3)$? Jun 8 '15 at 2:34

Calculating the autocorrelation of this function is relatively easy. You just need to follow the formula you have written:

$$\ r_{xx}(l) = \displaystyle\sum_{n=-\infty}^{\infty} x(n)x(n-l)$$

It gets much easier because your function is $\neq0$ for only a few values ($2N+1$ exactly).

Because of this the previous sum can be replaced with this much simpler one:

$$\ r_{xx}(l) = \displaystyle\sum_{n=-N}^{N} x(n-l)$$

Which is basically just counting how many times $x(n-l)$ is not zero on that finite interval. Because of this it appears easily that

$$r_{xx}(l)=max(2N-1-|l|,0)$$

You almost had it in your question but the sign was wrong.