简体   繁体   English

Librosa - 音频频谱图/频率分档到频谱

[英]Librosa - Audio Spectrogram/Frequency Bins to Spectrum

I've read around for several days but haven't been to find a solution... I'm able to build Librosa spectrograms and extract amplitude/frequency data using the following:我已经阅读了几天,但一直没有找到解决方案......我能够使用以下方法构建 Librosa 频谱图并提取幅度/频率数据:

audio, sr = librosa.load('short_piano melody_keyCmin_110bpm.wav', sr = 22500)
spectrum = librosa.stft(audio, n_fft=2048, window=scipy.signal.windows.hamming)
D = librosa.amplitude_to_db(np.abs(spectrum), ref=np.max)
n = D.shape[0]
Nfft = 1+2*(n-1)
freq_bins = librosa.fft_frequencies(sr=sr, n_fft=Nfft)

However, I cannot turn the data in D and freq_bins back into a spectrum.但是,我无法将 D 和 freq_bins 中的数据转换回频谱。 Once I am able to do this I can convert the new spectrum into a.wav file and listen to my reconstructed audio... Any advice would be appreciated.一旦我能够做到这一点,我就可以将新频谱转换为 .wav 文件并收听我重建的音频......任何建议将不胜感激。 Thank you.谢谢你。

When I get your question right, you want to reconstruct the real/imaginary spectrum from your magnitude values.当我正确回答您的问题时,您想根据幅度值重建实/虚频谱。 You will need the phase component for that, then its all simple complex number arithmetic.为此,您将需要相位组件,然后是所有简单的复数运算。 You should be aware that the output of an STFT is an array of complex numbers, and the amplitude is the absulute value of each number, while the phase is the angle of each number你应该知道STFT的output是复数数组,幅值是每个数的绝对值,相位是每个数的角度

Here´s an example of a time-domain signal transformed to magnitude/phase and back without modifying it:这是一个时域信号转换为幅度/相位并返回而不修改它的示例:

% get the complex valued spectrum from a sample
spectrum = librosa.stft(audio, n_fft=2048,window=scipy.signal.windows.hamming) 

# get magnitude and phase from the complex numbers
magnitude = np.abs(spectrum)
phase = np.angle(spectrum)

# reconstruct real/imaginary parts from magnitude and phase
spectrum = magnitude * np.exp(1j*phase)

# transform back to time-domain

In your case, you should first convert the db-values back to amplitude values, of course.在您的情况下,您当然应该首先将分贝值转换回幅度值。 Even having no experience with librosa, I´m sure that there is also a function for that.即使没有使用 librosa 的经验,我敢肯定还有一个 function 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM