Fourier transform is in the high mathematics is a very important knowledge point, today will be combined with Python code to implement the Fourier transform.
(math.) a Fourier transform
How do we usually decompose a complex problem? A classical method is to decompose the complex problem into a number of simple operational sub-problems, and the Fourier transform is based on this idea.
Fourier analysis is the field that studies how mathematical functions can be decomposed into a series of simpler trigonometric functions. The Fourier transform is a tool in this field for decomposing functions into their component frequencies.
In this tutorial, the Fourier Transform is a tool to take a signal and look at the power at each frequency in it. Take a look at the important terms in that Fourier transform:
- Signal: A signal is information that changes over time. For example, audio, video and voltage alignments are all examples of signals.
- Frequency: Frequency is the rate at which something repeats itself. For example, a clock ticks at a frequency of 1 hertz (Hz), or 1 repetition per second.
- Power: Power indicates the intensity of each frequency.
Below is a visual demonstration of the frequency and power of some sine waves:
The first is a low-frequency sine wave, the second is a high-frequency sine wave, and the third is a low-frequency, low-power sine wave, so the low-power sine wave has a smaller peak than the other two.
Time and frequency domains
The time domain and the frequency domain are two different ways of looking at signals, i.e., information about their constituent frequencies or changes over time.
In the time domain, a signal is a wave that varies in amplitude (y-axis) with time (x-axis). You are most likely to view a graph in the time domain, for example:
This is an image of some audio which is a time domain signal. The horizontal axis represents time and the vertical axis represents amplitude.
In the frequency domain, a signal is represented as a series of frequencies (x-axis), each with an associated power (y-axis). The figure below shows the above audio signal after Fourier transform:
Code to implement a sine wave
Audio is essentially a sine wave.
Here is the code to generate a sine wave:
import numpy as np from matplotlib import pyplot as plt SAMPLE_RATE = 44100 # Hertz DURATION = 5 # seconds def generate_sine_wave(freq, sample_rate, duration): x = (0, duration, sample_rate * duration, endpoint=False) frequencies = x * freq y = ((2 * ) * frequencies) return x, y # Generate a 2 Hz sine wave for 5 seconds x, y = generate_sine_wave(2, SAMPLE_RATE, DURATION) (x, y) ()
The x-axis represents time in seconds, and since each second of time has two peaks, the sine wave can be seen to oscillate twice per second.
mixed audio
Below will be two sine waves, mixing the audio signal consists of only two steps:
The sine waves are added together and then normalized.
The code for the specific implementation is as follows.
_, nice_tone = generate_sine_wave(400, SAMPLE_RATE, DURATION) _, noise_tone = generate_sine_wave(4000, SAMPLE_RATE, DURATION) noise_tone = noise_tone * 0.3 mixed_tone = nice_tone + noise_tone
The next step is to normalize, or scale, the signal to fit the target format. Because of how the audio will be stored later, the target format is a 16-bit integer ranging from -32768 to 32767:
normalized_tone = np.int16((mixed_tone / mixed_tone.max()) * 32767) (normalized_tone[:1000]) ()
The sine wave seen is the generated 400 Hz tone, the easiest way to convert the above sine wave to audio is to use theSciPy
(used form a nominal expression)method stores it in the
WAV
file. 16-bit integers are the standard data type for WAV files, so signals need to be normalized to 16-bit integers:
from import write # Remember, sample rate = 44,100 Hz is our playback rate # write("", SAMPLE_RATE, normalized_tone)
This audio sounds high pitched.
Once this step is completed, it is treated as an audio sample. The next step is to eliminate the high pitches using the Fourier Transform!
(math.) a Fourier transform
Now it's time to use the FFT on the generated audio. the FFT is an algorithm that implements the Fourier transform and can compute the spectrum for a signal in the time domain.
from import fft, fftfreq # of samples in standardized tones N = SAMPLE_RATE * DURATION yf = fft(normalized_tone) xf = fftfreq(N, 1 / SAMPLE_RATE) (xf, (yf)) ()
We can see two peaks in the positive frequency, the positive frequency peaks are located at 400 Hz and 4000 Hz, corresponding to the frequencies of the previously generated audio.
Compute the Fourier transform
yf = fft(normalized_tone) xf = fftfreq(N, 1 / SAMPLE_RATE)
The function of the above code
- fft() computes the transformation itself.
- fftfreq() calculates the frequency fft() at the center of each bin in the output. Without this, it is impossible to plot the x-axis on the spectrum
The output of fft() reflects the spectrum around the y-axis, so the negative half is a mirror image of the positive half, and we generally only need to compute half of the symmetry values to perform the Fourier transform faster. Implement this speed hack rfft() in the form of.
from import rfft, rfftfreq # Note the redundant "r". yf = rfft(normalized_tone) xf = rfftfreq(N, 1 / SAMPLE_RATE) (xf, (yf)) ()
Filtered Signals
One of the great advantages of the Fourier transform is that it is invertible, and we can use this to our advantage to filter audio and get rid of high pitch frequencies.
# Maximum frequency is half the sampling rate points_per_freq = len(xf) / (SAMPLE_RATE / 2) # Our target frequency is 4000 hertz # Turn 44100 into 4000 # target_idx = int(points_per_freq * 4000)
You can then get rid of it by setting it yf to 0 target frequency near index:
yf[target_idx - 1 : target_idx + 2] = 0 (xf, (yf)) ()
Since there is only one peak, the Fourier inverse transform is applied below to return to the time domain.
Applying the inverse FFT is similar to applying the FFT:
from import irfft new_sig = irfft(yf) (new_sig[:1000]) ()
Since you are using rfft(), you need to use irfft() to apply the inverse function. However, if you have used ffft(), the inverse function will be ifft(). Your plot should now look like the following:
You now have a sine wave oscillating at 400 Hz and you have successfully eliminated the 4000 Hz noise.
Normalize the signal before writing it to a file.
norm_new_sig = np.int16(new_sig * (32767 / new_sig.max())) write("", SAMPLE_RATE, norm_new_sig)
To this article on the use of Python to carry out the university classic Fourier transform article is introduced to this, more related Python Fourier transform content please search for my previous articles or continue to browse the following related articles I hope you will support me more in the future!