2016-01-10 27 views
3

如何獲取一個wav文件,將其轉換爲頻率強度數組,然後將該數組轉換爲wav文件。如何從聲音轉換到光譜然後回到python中的聲音?

是否有看起來像這樣

wav_data = library.read_wav('aoeu.wav') # [0, 3, 201, ... etc] 

spectrum = library.get_spectrum(wav_data) 
# [[0, 0, 0, .2, 0, .7, ... etc], 
# [0, 0, 0, .3, 0, .8, ... etc], 
# ... etc] 

spectrum[:, 0] = 0 # kill the lowest frequency (assuming spectrum is a numpy array) 

library.spectrum_to_wav(spectrum) # [0, 3, 201, ... etc] 
+0

您需要首先了解底層算法,例如[重疊相加(https://en.wikipedia.org/wiki/Overlap-add_method)。 –

回答

2

圖書館我想我一直在尋找的librosa.core.stftlibrosa.core.istft

from librosa import load 
from librosa.core import stft, istft 

y, sample_rate = load('song.wav') 
spectrum = stft(y) 
back_y = istft(spectrum) 
print(sum(y - back_y)) # close to 0 

scipy.io.wavfile.read()快於librosa.load()

from scipy.io.wavfile import read 

sample_rate, y = read('song.wav') # librosa returns (y, sr) scipy returns (sr, y) 
print(sum(y - istft(stft(y)))) # close to 0