Python audio analysis (2) inverse Fourier transform, output wav format audio

Perform Fourier transform on the signal, then inverse Fourier transform, and write back to wav (Is it okay to be idle ...) There is a problem that the audio image is compressed, I don't know what to say.

import scipy.signal as signal
import wave
import pyaudio
import pylab
import numpy as np
import matplotlib.pyplot as plt

start=0 #起始位置
fft_size=4000000 #终点位置
waveData=get_wavedata(wavfile) #获取wave数据
framerate=get_framerate(wavfile) #获取帧率数据

#### 1.取出所需部分进行傅里叶变换,并得到幅值
# rfft,对称保留一半,结果为 fft_size/2-1 维复数数组
fft_y1 = np.fft.rfft(waveData[0][start:start+fft_size-1])/fft_size #左声部

#### 2.计算频域图x值
#最小值为0Hz,最大值一般设为采样频率的一半
freqs = np.linspace(0, framerate/2, fft_size/2)

#### 3.画图
plt.figure(figsize=(20,10))
plt.plot(freqs, np.abs(fft_y1))
pylab.xlabel("frequence(Hz)")

#### 4.逆傅里叶变换
filter_sig=np.fft.ifft(fft_y1*fft_size).real

#### 5.输出为wav
wave_data = filter_sig.astype(np.short)
# 打开WAV文档
f = wave.open(outputfile, "wb")
# 配置声道数、量化位数和取样频率
f.setnchannels(1)
f.setsampwidth(2)
f.setframerate(framerate)
# 将wav_data转换为二进制数据写入文件
f.writeframes(wave_data.tostring())
f.close()
Released eight original articles · won praise 1 · views 3258

Guess you like

Origin blog.csdn.net/sinat_30165411/article/details/102418311