guide
About 傅里叶变换
everyone should be familiar with it, right? It can be found in China 信号处理
and 高等数学
other places, and the most spoken word about it isTransform a signal in the time and frequency domains, everyone may have a vague understanding of this sentence. Through this article, you will be able to learn the following points:
- What is the Fourier Transform and why is it needed?
- What are the applications of the Fourier transform?
- How to use scipy to use Fourier transform
Fourier transform
傅里叶变换
(Fourier Transform): It is a linear integral transformation for transformation between the time domain (or space domain) and the frequency domain, and is widely used in physics and engineering. The Fourier transform is a very powerful one 信号分析工具
, it can be used for 音频型号的处理
, 图片压缩
, 去噪
.
- Applications of the Fourier Transform
The audio file is realized by Fourier transform 歌曲识别
, which can be realized by Fourier transform 图片的压缩
, the picture data is converted into frequency domain data, the high frequency part is removed to realize the compression of the picture, 语音识别
and the original recover speech from the audio signal.
Many times, when it is difficult for us to analyze the characteristics and useful information of some signals with complex changes in the time domain, we can convert them to the signal through Fourier transform, and find useful information by analyzing the changes of the signals 频域
.频率
振幅
- The use of Fourier transform in scipy
scipy的fft
The module provides many Fourier transform tools, and then we will introduce how to use it
The use of scipy Fourier transform
Let's use a few examples to illustrate how to apply the Fourier transform in scipy
remove noise from signal
- Generate a virtual signal
We define a functiongenerate_audio_wave
to generate an original audio signal
from scipy import fft
import numpy as np
from matplotlib import pyplot as plt
def generate_audio_wave(freq,sample_rate,duration):
"""产生一段虚拟的音频信号
:param freq: 信号的频率
:param sample_rate: 信号的采样率
:param duration: 信号采样的时长
:return:
"""
#生成一段时间序列
t = np.linspace(0,duration,sample_rate*duration,endpoint=False)
#设置产生音频信号的频率
frequencies = t * freq
#设置音频信号的输出的幅值
y = np.sin((2 * np.pi) * frequencies)
return t,y
#设置生成音频的采样率
#1s产生44100个信号
SAMPLE_RATE = 44100
#设置生成音频时间的长度
DURATION = 5
t,y = generate_audio_wave(2,SAMPLE_RATE,DURATION)
plt.plot(t,y)
plt.show()
We generate a sinusoidal virtual audio signal with frequency 2*44100
and amplitude 1
and5s
- To add noise,
we simulated and generated two signals, one is the original audio signal and the other is a high-frequency noise signal, and then the two signals are mixed to generate the final signal. The mixed signal changes from a smooth sine wave to A sine wave of a signal with many glitches
#设置生成音频的频率
#1s产生44100个信号
SAMPLE_RATE = 44100
#设置生成音频时间的长度
DURATION = 5
#生成音频信号
t,wav_signal = generate_audio_wave(400,SAMPLE_RATE,DURATION)
#模拟高频噪声
_,noise_signal = generate_audio_wave(4000,SAMPLE_RATE,DURATION)
#给音频信号添加高频噪声
mixed_signal = 0.3 * noise_signal + wav_signal
#展示添加高频噪声之后的波形图
plt.plot(mixed_signal[:1000])
plt.show()
- Fourier transform
We use Fourier transform on the synthesized signal, and draw the spectrum diagram of the synthesized signal, and find that the x-axis is divided into positive and negative parts, and symmetrical, negative and positive symmetry is the real input of Fourier transform A side effect of the value. By analyzing the spectrogram, it is found that it is mainly composed of two frequency signals, and the frequency of the signal is4000HZ
and400HZ
. Analyzing the magnitude (value on the y-axis) reveals that400HZ
the frequency of the signal is stronger than4000HZ
the signal of .
#计算采样的点数
N = SAMPLE_RATE*DURATION
#傅里叶变换
yf = fft.fft(mixed_signal)
#计算频率的中心,用来绘制x轴的数值
xf = fft.fftfreq(N,1 / SAMPLE_RATE)
#绘制频谱图
plt.plot(xf,np.abs(yf))
plt.show()
- Inverse Fourier transform to remove noise
We first determine the frequency that needs to be removed from the signal, and then use the frequency to find the subscript of the signal. After the signal is cleared in the frequency domain, it will be频域的信号
converted to by the inverse Fourier transform时域信号
to realize the noise removal of .
#找到频率为4000HZ的索引
target_idx = np.squeeze(np.argwhere(np.abs(xf) == 4000))
#将4000HZ的信号清除
yf[target_idx] = 0
#傅里叶逆变换,还原去除噪声之后的信号
inv_yf = fft.ifft(yf)
#绘制信号
plt.plot(inv_yf[:1000])
plt.show()