简体   繁体   中英

Fourier Transform excruciatingly slow

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.

Compiling comments into an answer:

If I understood your code, you are using numerical integration with the trapezoidal rule. If you want to compute a Fourier Transform and do it fast, then you should probably implement a Fast Fourier transform algorithm. Note that the FFT is considered (along with the Kalman Filter) to be one of the most important algorithms in engineering, and for a good reason: It is so much faster than naive implementations of the Fourier transform that it turns intractable problems into computing demanding but feasible products.

As mentioned be @user2699, there are methods to speed up plotting, as per this question .

This addresses some algorithmic and Python related bottlenecks of your code, but if you are dissatisfied with the speed it takes to run, the go-to starting point should be profiling your code , and some hints on how to do this with Python have been discussed in this question .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM