Python Fourier Transform

1. What is Fourier transform?

In mathematics, transformation techniques are used to map a function into a function space that is different from its original function space. The Fourier transform is also a transformation technique that can transform a function from time domain space to frequency domain space. Taking an audio wave as an example, the Fourier transform can represent it in terms of the volume and frequency of its notes.

We can say that the transformation performed by the Fourier transform of any function is a function of frequency. where the size of the resulting function is a representation of the frequency contained in the original function.

Let's take an example of a signal whose time domain function looks like this:

Image

Get part of another signal on the same time frame

Image

Call these two signals A(n) and B(n), where n is the time domain. So if we add these signals, the structure of the signals will look like this:

C(n) = A(n) + B(n)

Image

It can be seen that the signal addition of the function is the operation of adding two signals. If we try to extract the signal A or B from this added signal C, we will encounter a problem because these signals are just the addition of power. , has nothing to do with time. In other words, the addition operation is the addition of power at the same time.

Image

As you can see in the image above, the frequency domain can easily highlight the differences between signals. If we wish to convert these signals back to the time domain, we can use the inverse Fourier transform.

2. numpy.fft与scipy.fft

Python's numpy and scipy modules provide all the transformation techniques needed in mathematics, so it can be used directly.

The following examples all use scipy, but in actual use, try not to use scipy if you can. After all, using one less library will reduce deployment troubles.

import numpy as np
import matplotlib.pyplot as plt
from scipy.fft import fft, fftfreq

Make a sine wave

# sample points
N = 1200

# sample spacing
T = 1.0 / 1600.0

x = np.linspace(0.0, N*T, N, endpoint=False)
sum = np.sin(50.0 * 2.0 * np.pi*x) + 0.5*np.sin(80.0 * 2.0 * np.pi*x)

plt.plot(sum)
plt.title('Sine wave')
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.grid(True, which='both')
plt.show()

Image

In the above output, you can see the sine wave generated using NumPy, which can now be converted using the FFT module of the scipy library.

sumf = fft(sum)
xf = fftfreq(N, T)[:N//2]
plt.ylabel('frequency')
plt.xlabel('sample')
plt.title("FFT of sum of two sines")
plt.plot(xf, 2.0/N * np.abs(sumf[0:N//2]))
plt.show()

The result output by fft is a complex number, where the real part represents the frequency and the imaginary part represents the camera. If you only care about the frequency, just take the absolute value. 

Image

Now you can clearly see what the frequencies of the various waves are. These are not obvious when formed as a function of the time domain. These differences can only be clearly seen when represented in the frequency domain.

references

The main content comes from below, with slight additions

What is the relationship between neural networks and Fourier transforms?

Guess you like

Origin blog.csdn.net/xhtchina/article/details/132939968