Non-symmetrical DFT of real valued signal

I have a homework assignment to take the $64$ DFT of $\cos((5\pi/32)n)$ and $\cos((5\pi/64)n)$. However, for the second case I obtained DFT spectrum such that I have that famous symmetry in real part and imaginary part of it because it is real valued signal. However, for the first signal I found something that has no symmetry in imaginary part of the DFT, but it's real too.

Why there is no symmetry although signal is real? Does it have anything to do with "windowing" the signal?

I wanted to calculate it analytically so that here is my code and the results:

....
t = 0:63;
x1 = cos(5*(pi/32)*t);
X1_analytic = zeros(1,64);
temp = 0;
for k = 0:63
for n = 0:63
temp = temp + x1(n+1)*exp(-1i*2*pi/64*k*n);
end
X1_analytic(k+1) = temp;
temp = 0;
end
subplot(5,1,1);
stem(0:63,abs(X1_analytic),'--*r');
subplot(5,1,2);
stem(0:63,(X1_analytic+conj(X1_analytic))*(1/2));
title('Real part');
subplot(5,1,3);
stem(0:63,imag((X1_analytic)));
title('Imaginary part');
subplot(5,1,4);
stem(0:63,abs(X1_analytic));
title('Abs value ');
subplot(5,1,5);
stem(0:63,angle(X1_analytic));
title('Phase ');


• We can't tell because you didn't show us your work. Of course the DFT must be conjugate symmetric because the signal is real-valued, so there must be a mistake in your calculations. – Matt L. Mar 14 '17 at 8:29
• I have just posted them, thanks for the warning – Canberk Mar 14 '17 at 9:01
• It's a numerical issue related with your loop computation of X1_analytic. as x1[n] is a real signal its DFT is conjugate symmetric. In particular for your choosen frequency and window length its imaginary part must be 0 but it's not as can be seen from your computation. This very small but nonzero imaginary part results in the erratic phase computations. For the second choice no numerical problems occurs and the phase computation becomes stable. In fact phase computation is sensistive to ideally zero but practically very small real or imaginary parts. See my answer for details. – Fat32 Mar 14 '17 at 10:32

Your choosen example frequency $\omega_0 = \frac{5 \pi} {32}$ touched a numerically sensitive case, in which the imaginary part of the theoretical DFT which should be zero is not observed so, on the numerical computation. Since the imaginary part is computed as non-zero, the phase which should be zero according to $\phi[k] = \tan^{-1}( \frac{\Im\{X_1[k]\}}{\Re\{X_1[k]\}} )$ whenever $\Im\{X_1[k]\}$ is zero, turns out to be some erratic function of $k$ (Note also that real part of $X_1[k]$ is also not computed to be exactly zero for those $k$ for which it should be, which also contributes to the erratic phase computation behaviour)

Consider the discrete time signal $x_1[n] = \cos(\frac{5\pi n}{32})$ and its 64-point DFT $X_1[k]$ (for ineteger $k \in [0:63]$) which is computed as:

$$X_1[k] = \sum_{n=0}^{63} \cos(\frac{5\pi n}{32}) e^{-j \frac{2\pi}{64} nk}$$

expand the cosine into complex exponentials: $$X_1[k] = 0.5 \sum_{n=0}^{63} ({e^{j \frac{5\pi}{32} n} + e^{-j \frac{5\pi}{32} n}}) e^{-j \frac{2\pi}{64} nk}$$

adjust the cosine based complex exponential arguments: $$X_1[k] = 0.5 \sum_{n=0}^{63} ({e^{j \frac{10\pi}{64} n} + e^{-j \frac{10\pi}{64} n}}) e^{-j \frac{2\pi}{64} nk}$$

separate the sums: $$X_1[k] = 0.5 \sum_{n=0}^{63} {e^{j \frac{10\pi}{64} n} e^{-j \frac{2\pi}{64} nk} + 0.5 \sum_{n=0}^{63}e^{-j \frac{10\pi}{64} n}} e^{-j \frac{2\pi}{64} nk}$$

Now replacing numerical value of $N = 64$ with $N$ we shall see the summations: $$X_1[k] = 0.5 \sum_{n=0}^{N-1} {e^{j \frac{10\pi}{N} n} e^{-j \frac{2\pi}{N} nk} + 0.5 \sum_{n=0}^{N-1} e^{-j \frac{10\pi}{N} n}} e^{-j \frac{2\pi}{N} nk}$$

Merge the complex exponentials: $$X_1[k] = 0.5 \sum_{n=0}^{N-1} {e^{j \frac{2\pi n (5 -k)}{N}}+ 0.5 \sum_{n=0}^{N-1}e^{-j \frac{2\pi n (5+k)}{N}}}$$

Now the above sums add up to $N/2$ whenever $k=5$ or $k=-5$ ( this is also for $k = -5+ 64 = 59$) and they become zero for any other integer $k$ in the range $k \in [0:63]$

As you can now see, $X_1[k]$ is a real and even symmetric function of k. And its imaginary part is zero, however in your loop computation this last condition is not realized due to numerical issues and therefore you observe erratic phase computation behaviour.

The output of a 64-point DFT contains 64 samples, ranging from frequency bins $k=0,\ldots,63$. Now, what does the symmetry of the (continuous) Fourier Transform say? If $x(t)$ is real, then $\Re\left\{X(f)\right\}=\Re\left\{ X(-f)\right\}$ and $\Im \left\{X(f)\right\}=-\Im \left\{X(-f)\right\}$, where $\Re$ and $\Im$ denote real and imaginary part. Or, more compactly $X(f)=X(-f)^*$.

So, the symmetry is around the zero frequency, i.e. the DC bin. For the $K$-point DFT we know $X[k]=X[k+K]=X[k-K]$ due to its periodicity. So, to show the symmetry of the DFT you need to put the DC frequency in the middle of the output, i.e. you have to plot $X[k]$ for $k=-\frac{K}{2},\dots,\frac{K}{2}-1$. The shifting of the DFT output such that the DC bin is in the middle is a common operation, and termed fftshift. Have a look at this code:

n = np.arange(64)
x1 = np.cos(5*np.pi/64*n)
x2 = np.cos(5*np.pi/32*n)

plt.figure(figsize=(10,6))
plt.subplot(121)
plt.plot(n, x1)
plt.plot(n, x2)

X1 = np.fft.fftshift(np.fft.fft(x1))
X2 = np.fft.fftshift(np.fft.fft(x2))

plt.subplot(122)
k = np.arange(-64/2, 64/2)
plt.plot(k, X2.real, label='1/32 real')
plt.plot(k, X2.imag, label='1/32 imag')
plt.plot(k, X1.real, label='1/64 real')
plt.plot(k, X1.imag, label='1/64 imag')
plt.legend(fontsize=10)
plt.grid(True)
plt.xlim((-32,32))


After doing the FFT shift, you can clearly see that both spectra are symmetric according to your expectations.