如何在python中从声音转换为频谱然后回到声音? [英] How do I go from sound to spectrum then back to sound in python?

查看:74
本文介绍了如何在python中从声音转换为频谱然后回到声音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取一个wav文件,每隔几毫秒将其转换为一个频率强度数组,对该数组进行操作,然后将该新数组转换回一个wav文件.

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]

推荐答案

使用 librosa.stft librosa.istft 并使用 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

我正在使用 audio [:len(reconstructed_audio)] ,因为信息会在转换中丢失. istft(stft(foo))可以返回比 foo 短一点且值略有不同的数组.

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.

这篇关于如何在python中从声音转换为频谱然后回到声音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆