#!/usr/bin/env python # coding: utf-8 # 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) # Plot your signal versus time. # In[3]: get_ipython().run_line_magic('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) # ### Plot real and imag parts of the results # Let's plot real and imag. parts of the 2 fft seperately. # In[9]: get_ipython().run_line_magic('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[ ]: