scipy's Fourier transform

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:

  1. What is the Fourier Transform and why is it needed?
  2. What are the applications of the Fourier transform?
  3. 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的fftThe 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 function generate_audio_waveto 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*44100and amplitude 1and5s
insert image description here

  • 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()

insert image description here

  • 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 is 4000HZand 400HZ. Analyzing the magnitude (value on the y-axis) reveals that 400HZthe frequency of the signal is stronger than 4000HZthe 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()

insert image description here

  • 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()

insert image description here

reference

  1. https://zh.wikipedia.org/wiki/%E5%82%85%E9%87%8C%E5%8F%B6%E5%8F%98%E6%8D%A2
  2. https://docs.scipy.org/doc/scipy-1.8.0/html-scipyorg/tutorial/fft.html
  3. https://www.youtube.com/watch?v=b06pFMIRO0I
  4. https://realpython.com/python-scipy-fft/

Guess you like

Origin blog.csdn.net/sinat_29957455/article/details/124641840