Why is this DFT of a real symmetric signal resulting in complex valued coefficients? I am trying to understand exactly how sampling the DTFT to get the DFT works. The signal I'm trying to analyze is $x(n)$ seen below.



$$x(n) = \delta(n\pm2) + 2\delta(n\pm1) + 3\delta(n)$$

Taking the DTFT, we have
\begin{align}
X(\omega)&amp;= \sum\limits_{i=-\infty}^\infty x(n)e^{-j\omega}\\
&amp;= \left(e^{j2ω} + e^{-j2ω}\right) + 2\left(e^{jω} + e^{-jω}\right) + 3\\
&amp;= 2\cos⁡(2ω) + 2(2\cos⁡ω) + 3\\
&amp;= 3 + 4\cos⁡ω + 2\cos⁡(2ω)
\end{align}

I next implemented this in MATLAB:

x = [1 2 3 2 1];
N = size(x,2);
w = -pi:0.01:pi;
X_DTFT_computational = freqz(x,1,w);
X_DTFT_analytical = 3 + 4*cos(w) + 2*cos(2*w);


This results in the following graphs:


Next, I compute the DFT in two ways:
1. I apply an FFT to the original signal $x(n)$.
2. I sample the DTFT.

The code that achieves this is as follows:

% FFT of x
X_DFT_computational = fftshift(fft(x))

% DFT = Sampled DTFT
X_DFT_analytical(1) = 3 + 4*cos(-4*pi/N) + 2*cos(2*(-4*pi/N));
X_DFT_analytical(2) = 3 + 4*cos(-2*pi/N) + 2*cos(2*(-2*pi/N));
X_DFT_analytical(3) = 3 + 4*cos(0)       + 2*cos(2*(0));
X_DFT_analytical(4) = 3 + 4*cos(2*pi/N)  + 2*cos(2*(2*pi/N));
X_DFT_analytical(5) = 3 + 4*cos(4*pi/N)  + 2*cos(2*(4*pi/N));


Plotting the DFT (while showing the DTFT for comparison), I have


The magnitude of the FFT does give me the correct result.
But just plotting the FFT alone gives me a complex signal (the graph shows the real part).

Why is this signal giving a complex FFT when $x(n)$ is both real and symmetric?
You defined the signal vector as x = [1 2 3 2 1]. Since the DFT is defined by

$$X[k]=\sum_{n=0}^{N-1}x[n]e^{-j2\pi nk/N}$$

the command fft(x) computes the DFT of the signal

$$x[n]=\delta[n]+2\delta[n-1]+3\delta[n-2]+2\delta[n-3]+\delta[n-4]$$

This signal is not symmetrical with respect to $n=0$, and it is not equal to the signal you computed the DTFT of.

If you want to compute the DFT of the signal

$$x[n]=\delta[n+3]+2\delta[n+1]+3\delta[n]+2\delta[n-1]+\delta[n-3]$$

you have to periodically continue it in the interval $n\in [0,N-1]$ (with $N=5$):

x = [3 2 1 1 2]

This results in a real-valued and symmetrical DFT, as expected:


X = fft(x);
X =

   9.00000   2.61803   0.38197   0.38197   2.61803