Files
FYS-STK4155/doc/Programs/JupyterFiles/Dylan Smith Total Examples/DFT.ipynb
T
2020-08-23 19:35:43 +02:00

42 KiB

In [96]:
import numpy as np
import matplotlib.pyplot as plt
import scipy.fftpack
import math as mt

"""N=500
T=1.0/500.0
t=np.arange(0, N*T, T)
y = np.sin(50.0 * 2.0*np.pi*t) + 0.5*np.sin(80.0 * 2.0*np.pi*t)
dft=np.fft.fft(y)
ft=np.arange(0, N/2, 1) #Dividing the amount by 2 cuts out the arbitrary frequency spikes at the end of the data
plt.plot(t[:200], y[:200], color='orange')
plt.show()
plt.plot(ft,np.abs(dft)[:N//2])
plt.show()"""


#Does N and T change the frequency value?


N=500
T=1/500.0
t=np.arange(0, N*T, T)
y = np.sin(50.0 * 2.0*np.pi*t) + 0.5*np.sin(80.0 * 2.0*np.pi*t)
dft=np.fft.fft(y)
ft=np.arange(0, N/2, 1) #Dividing the amount by 2 cuts out the arbitrary frequency spikes at the end of the data
plt.plot(t[:200], y[:200], color='orange')
plt.show()
plt.plot(ft,np.abs(dft)[:N//2])
plt.show()

# Number of samplepoints
N = 600
# sample spacing
T = 1.0 / 800.0
x = np.linspace(0.0, N*T, N)
y = np.sin(50.0 * 2.0*np.pi*x) + 0.5*np.sin(80.0 * 2.0*np.pi*x)
yf = np.fft.fft(y)
xf = np.linspace(0.0, 1.0/(2.0*T), N/2)

plt.plot(xf, 2.0/N * np.abs(yf[:N//2]))
plt.show()

print (x.shape, y.shape, yf.shape, xf.shape)

print (yf[:N//2].shape)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-96-5364dc22575e> in <module>()
     32 peak=mt.ceil(max(np.abs(dft)))
     33 dft=np.ndarray.tolist(dft)
---> 34 fre=dft.index(peak)
     35 fro=ft[fre]
     36 print (fro)

ValueError: 250 is not in list
In [ ]:
In [ ]: