Before I get judged, I am no expert with this and simply for the sake of curiosity I tried to write some code that performs a Fourier transform. After watching 3Blue1Brown's video on fourier transform I wanted to write the algorithm myself and plot it, simply because... well it looks cool. I tried to do everything in pure python only using numpy and matplotlib, and it sort of works.
Notice: I am plotting everything iteratively, and re-plotting at each increment
But, plotting the wound up wave and the transform is very very slow. I think I am doing some things inefficiently, maybe even wrong.
Here's what it looks like:
Here is the code:
import numpy as np
import matplotlib.pyplot as plt
u = np.linspace(-8*np.pi, 8*np.pi, 1000)
sin1 = np.sin(u) + 2
u2 = np.linspace(-3*np.pi, 3*np.pi, 1000)
sin2 = np.sin(u2) + 2
plt.plot(u + u2, sin1 + sin2)
fig, (winder, integax) = plt.subplots(nrows = 2, ncols = 1)
L = len(sin1)
real = []
imag = []
integral = []
for val in np.arange(0.00001,360,0.00001):
real = []
imag = []
for t,si in zip(np.arange(0,L,val),sin1 + sin2):
complex = si * np.e ** (2 * np.pi * 1j * t)
real.append(complex.real)
imag.append(complex.imag)
fig.set_size_inches(10,10)
point = np.trapz(real)
integral.append(point)
#print(integral[-1], time[-1])
integax.plot(integral)
winder.plot(real, imag, 'b-')
plt.pause(0.00001)
winder.cla()
ax = plt.plot(real, imag, 'b-')
plt.show()
Now I would like to plot it faster, and I think that the integration part is not correct. Since no spikes occur in the resulting plot even after waiting for a long time.
I also don't think that I am using linspace correctly to plot sine waves nor am I doing the frequency part right in the fourier formula.