I am stuck trying to calculate the DFT for a given $N$
Given the signal $x[n] = \cos(\frac{\pi}{2}n - \frac{\pi}{2})+2 \cos(\pi n + \frac{\pi}{2})$ and $N = 4$ I tried to calculate the DFT $X[k] = \sum\limits_{n=0}^{N-1}x[n]e^{\frac{-2 \pi ink}{N}}$
Now since
- $X[k] = \sum\limits_{n=0}^{3}x[n]e^{\frac{-2 \pi ink}{4}} = \sum\limits_{n=0}^{3}e^{\frac{-2 \pi ink}{4}} \cos(\frac{\pi}{2}n - \frac{\pi}{2}) + \sum\limits_{n=0}^{3}e^{\frac{-2 \pi ink}{4}} 2 \cos(\pi n + \frac{\pi}{2})$
I can have a look at both signals seperatly and it would come down on how to solve the dft for the cosine. For this matter I looked at different sources like here and here
I tried to solve it using the geoemetric series for $\cos(\frac{\pi}{2}n - \frac{\pi}{2})$ which led to
- $\sum\limits_{n=0}^{3}e^{\frac{-2 \pi ink}{4}} \cos(\frac{\pi}{2}n - \frac{\pi}{2}) = \dots = \frac{1}{2} e^{-\frac{\pi}{2}} \frac{1 - e^{\frac{\pi}{2}3i(1-k)}}{1 - e^{\frac{\pi}{2}i(1-k)}} + \frac{1}{2} e^{\frac{\pi}{2}} \frac{1 - e^{\frac{\pi}{2}3i(1+k)}}{1 - e^{\frac{\pi}{2}i(1+k)}}$
Now something seems wrong because now I get $X[0] \neq 0 $ which is false given my plots using this code
import numpy as np
import matplotlib.pyplot as plt
time = np.arange(0.,5,1)
pnts = len(time)
signal = np.cos(np.pi/2*time - np.pi/2) + 2*np.cos(np.pi*time + np.pi/2)
fourTime = np.array(range(pnts))/pnts
fCoefs = np.zeros((len(signal)),dtype=complex)
for freq in range(pnts):
csw = np.exp( -1j*2*np.pi*freq*fourTime )
fCoefs[freq] = np.sum(np.multiply(signal,csw) )
ampls = 2*np.abs(fCoefs)
plt.plot(time, signal)
plt.xlabel('Time (s)'), plt.ylabel('Amplitude')
plt.xlim(0,10)
plt.show()
plt.stem(ampls)
plt.xlabel('Frequency (Hz)'), plt.ylabel('Amplitude (a.u.)')
plt.xlim(0,10)
plt.show()
Anyways this way of calculating actually seems a little bit overcomplicated as well. I looked different fourier tables and their derivation as well and found contradicting results. I am confused and it seems as if I am still missing something crucial.
updated using the hint from the first answer
$\sum\limits_{n=0}^3 x[n] \cdot e^{\frac{-2 \pi i k}{4}} = x[0] e^{\frac{- \pi i 0 k}{2}} + x[1] e^{\frac{- \pi i 1 k}{2}} + x[2] e^{\frac{- \pi i 2 k}{N}} + x[3] e^{\frac{- \pi i 3 k}{2}}$ $0 \cdot e^{\frac{- \pi i 0 k}{2}} + e^{\frac{- \pi i 1 k}{2}} + 0 \cdot e^{\frac{- \pi i 2 k}{N}} - e^{\frac{- \pi i 3 k}{2}} = e^{\frac{- \pi i k}{2}} - e^{\frac{- \pi i 3 k}{2}}$
So now when calculate $X[k]$ for $k = 0, 1, 2, 3$
- $X[0] = 0$
- $X[1] = e^{\frac{- \pi i \cdot 1}{2}} - e^{\frac{- \pi i 3 \cdot 1}{2}} = - i - i = - 2 i$
- $X[2] = e^{\frac{- \pi i \cdot 2}{2}} - e^{\frac{- \pi i 3 \cdot 2}{2}} = -1 - (-1) = 0$
- $X[3] = e^{\frac{- \pi i \cdot 3}{2}} - e^{\frac{- \pi i 3 \cdot 3}{2}} = i - (-i) = 2i$
So it is a little bit confusing for me that $X[2] = 0$ I expected it to be bigger than $0$ because I thought it would be corresponding to frequency of $1/2$.