In [1]:
import numpy as np
from scipy.fftpack import fft, ifft,fftshift,fftfreq
np.set_printoptions(threshold=15)


### Cosine function¶

$$y(t)=cos(2πAt+φ)=cos(ωt+φ)$$

And it's Fourier Transform:

$$F_{transform}\left[y(t)\right](k)=1/2[\delta(f-A)+\delta(f+A)]$$

where $\delta(x)$ is the delta function.

In [2]:
# let's generate cosine signal
A = 2
t = np.linspace(0, 1, 100)
mySignal = np.cos(2.*np.pi*A*t)
print('time', t)
print('signal', mySignal)

time [ 0.          0.01010101  0.02020202 ...,  0.97979798  0.98989899  1.        ]
signal [ 1.          0.99195481  0.9679487  ...,  0.9679487   0.99195481  1.        ]


In [3]:
%matplotlib notebook
import matplotlib.pyplot as plt

fig = plt.figure()
ax = plt.subplot(111)

ax.plot(t, mySignal, "k*-")

plt.xlabel("Time")
plt.ylabel("Magnitude")
plt.show()


### Take FFT of the signal¶

Pay attention these FFTs, one using correct periodic signal, the other one is not, results has significant difference.

In [4]:
# calculate spectrum
myfft1  = fft(mySignal)
myfft2  = fft(mySignal[0:-1])
print('fft1 real', myfft1.real)
print('fft1 imag', myfft1.imag)
print('fft2 real', myfft2.real)
print('fft2 imag', myfft2.imag)

fft1 real [  1.           1.32367609  50.11910572 ...,  -0.82481353  50.11910572
1.32367609]
fft1 imag [ 0.          0.0415982   3.15322686 ...,  0.07796783 -3.15322686
-0.0415982 ]
fft2 real [  3.88578059e-15   5.50352424e-15   4.95000000e+01 ...,   5.63034641e-15
4.95000000e+01   5.50352424e-15]
fft2 imag [  0.00000000e+00  -1.74443834e-15   1.37231808e-14 ...,   2.38356762e-15
-1.37231808e-14   1.74443834e-15]


### Plot real and imag parts of the results¶

Let's plot real and imag. parts of the 2 fft seperately.

In [9]:
%matplotlib notebook
import matplotlib.pyplot as plt

# Real Parts

fig = plt.figure()
ax = plt.subplot(111)

ax.plot(myfft1.real, "*-")
ax.plot(myfft2.real, "o-")

plt.title("Real Parts")
plt.xlabel("Index")
plt.ylabel("Spectrum")
plt.show()

# Imag. Parts

fig = plt.figure()
ax = plt.subplot(111)

ax.plot(myfft1.imag, "*-")
ax.plot(myfft2.imag, "o-")

plt.title("Imaginary Parts")
plt.xlabel("Index")
plt.ylabel("Spectrum")
plt.show()


### Conclusion¶

Only myfft2 has the correct results here. Why?

FFT is a FT, only fully periodic signal can be used, see definition of the FT.

In [ ]: