ich bin gerade dabei, ein mathematisches Modell zu implementieren. Da alle meine Gleichungen analytisch beschrieben sind, stoße ich immer wieder auf Unstimmigkeiten zwischen Informatik und Mathematik bzw. hier gerade zwischen der Ansicht der Mathematik (analytisch integrieren) und der, der Informatik (np.fft).
Ich habe ein Skript geschrieben, wo ich versuche beide anzupassen. Es scheint aber ein Offset zwischen meiner FFT und meiner analytischen Fourrier-Transformierter Funktion. (Dazu siehe bitte plots ganz unten oder angehängte Bilder)
http://www.directupload.net/file/d/4693 ... 8c_png.htm
http://www.directupload.net/file/d/4693 ... ec_png.htm
PS: lässt euch von den Frequenzen f0 und fs nicht verwirren da mein Script eigentlich länger ist, als das was ich für euch gepostet habe.. fs: zur Abtastung und f0 ist meine richtige Frequenz.
Code: Alles auswählen
import pylab as plt
import numpy as np
def fkt(t):
"The example-function to be transformed"
return (1/tau)*np.exp(-t/tau)
def Fkt(f):
"The analytical solution of the Fourier-transformation of the example-function."
om = 2*np.pi*f
return 1/(1+1j*om*tau)
def fourier_transform(t, fkt):
"""
Calculates the Fourier-Transformation of fkt with FFT.
The output Fkt of the Fourier-Transformation of fkt
is correctly normed (multiply with dt)
is correctly ordered (swap first and second half of output-array).
Calculates the frequencies from t.
"""
dt = t[1]-t[0]
f=np.linspace(-1/(2*dt), 1/(2*dt), t.size, endpoint=False)
Fkt = np.fft.fft(fkt)*dt
# swap first and second half of output-array
Fkt2 = np.fft.fftshift(Fkt)
return f, Fkt2
fs =5e6 #Abtastfrequenz
f0 = fs/2/102 # Frequenz mit der, ich anrege
t_min = 0
dt = 1./fs # zeitschritt
t_max = (10772) * dt # The size of the whole signal is 10772
t = np.arange(t_min,t_max,dt)
Td = 1./f0
Time_bin_p = int(np.floor(Td / dt)) # Bins pro Periode Td
tp_n = t[0:int(np.floor(Time_bin_p)/2)] # Nur einen Abschnitt der Zeit betrachten, bzw. eine halbe Periode 0.5*Td
tau =1e-6
s = fkt(tp_n)
f, S1 = fourier_transform(tp_n, s)
S2 = Fkt(f)
plt.figure()
plt.title("Time Domain")
plt.figtext(.6, .5,
r"$s(t) = \frac{1}{\tau} * e^{-\frac{t}{\tau}}$" "\n\n" +
(r"$\tau = %G$" % tau) + "\n"
,
fontsize=20)
plt.plot(tp_n,s)
plt.grid(True)
plt.figure()
plt.title("Fourier-Analysis")
plt.figtext(.13, .25,
r"$S(f)=\frac{1}{1+j2\pi f*\tau}$",
fontsize=24)
plt.plot(f,S2.real, label="analytic real")
plt.plot(f,S2.imag, label="analytic imag")
plt.plot(f,np.abs(S2), label="analytic abs")
plt.plot(f,S1.real, ".", label="from FFT real")
plt.plot(f,S1.imag, ".", label="from FFT imag")
plt.plot(f,np.abs(S1), ".", label="from FFT abs")
plt.grid(True)
plt.legend()
plt.show()