How do I go from sound to spectrum then back to sound in python?

此生再无相见时 提交于 2020-12-01 12:24:23

问题


How do I take a wav file, transform it into an array of frequency intensities every couple ms, do something with that array then transform that new array back into a wav file.

Is there a library that looks something like this

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]

回答1:


Use librosa.stft and librosa.istft and read the audio file with librosa.load

import librosa

audio, sample_rate = librosa.load('song.wav')

spectrum = librosa.stft(audio)
reconstructed_audio = librosa.istft(spectrum)

sum(audio[:len(reconstructed_audio)] - reconstructed_audio)  # very close to 0

I'm using audio[:len(reconstructed_audio)] because information is lost in the transform. istft(stft(foo)) can return an array slightly shorter than foo and with slightly different values.



来源:https://stackoverflow.com/questions/34710011/how-do-i-go-from-sound-to-spectrum-then-back-to-sound-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!