Sampling rate issue with Librosa

被刻印的时光 ゝ 提交于 2021-01-02 05:58:06

问题


When doing a STFT, and then an inverse STFT (iSTFT) on a 16 bits 44.1 khz audio file with the library Librosa :

import librosa

y, sr = librosa.load('test.wav', mono=False)
y1 = y[0,]
S = librosa.core.stft(y1)
z1 = librosa.core.istft(S, dtype=y1.dtype)
librosa.output.write_wav('test2.wav', z1, sr)

the output is only a 22 khz audio file. Why? Where is there the sampling rate change in librosa ?


回答1:


The librosa.load() function enables target sampling, wherein the audio file you import can be re-sampled to the target sample rate specified by the keyword argument sr.

If you want to use the original sample rate, you have to explicitly set the the target sample rate to None: sr=None. By default, sr=22050, which is why your output is ~22khz.

By way of example:

Default Setting - sub-sampling to default 22,050 Hz

In[51]: filename = librosa.util.example_audio_file()
In[52]: y1, sr1 = librosa.load(filename)
In[53]: print sr1
22050

Explicitly Setting sr=None ensures original sampling preserved

In[54]: y2, sr2 = librosa.load(filename,sr=None)
In[55]: print sr2
44100

Sub-sampling to a specified rate, 16,000 Hz

In[56]: y3, sr3 = librosa.load(filename,sr=16000)
In[57]: print sr3
16000

The result:




回答2:


Its because u have not installed the some dependent libraries ,i suggest u to install sudo apt-get install libav-tools because it installs audio and video tools in the linux system.



来源:https://stackoverflow.com/questions/38188359/sampling-rate-issue-with-librosa

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