[snippet]使用librosa进行stft/istft等处理

来MSRA之后一直忙得没空写文。只能水一些snippet了。。 吐槽:忙忙忙大过年的还要码paper

最近用到,贴上备忘。 功能,语音和噪声->stft->image和real分别叠加进行混合->mixed->用pysndfile存文件

import scipy  
import numpy  
import matplotlib.pyplot as plt  
import librosa  
import pysndfile

speech_data = librosa.load('F01A2_8k.WAV', sr=8000)  
speech_stft = librosa.stft(speech_data[0], n_fft=320, hop_length=160, window=scipy.signal.hamming)[:, 0:200]  
speech_imag = numpy.imag(speech_stft)  
speech_real = numpy.real(speech_stft)


noise_data = librosa.load('noise100_8k/n1.wav', sr=8000)  
noise_stft = librosa.stft(noise_data[0], n_fft=320, hop_length=160, window=scipy.signal.hamming)[:, 0:200]  
noise_imag = numpy.imag(noise_stft)  
noise_real = numpy.real(noise_stft)

ratio = 0.1  
mixed_imag = speech_imag + ratio * noise_imag  
mixed_real = speech_real + ratio * noise_real  
mixed_stft = mixed_real + mixed_imag * 1j  
mixed_data = librosa.istft(mixed_stft, hop_length=160, window=numpy.ones((320, )))  
pysndfile.sndio.write("mixed.wav", mixed_data, rate=8000, format='wav', enc='pcm16')  

坑:librosa所自带的保存文件(librosa.out.write_wav())非常简单,甚至并不是标准的PCM16格式,之前做实验怎么也不好使,后来在Github上发了个issue才知道:

bmcfee:

The librosa.output function is just a convenience wrapper for scipy's wav writer, so any odd behavior is up to scipy, not librosa.`

Friskit

继续阅读此作者的更多文章