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 ?
The sampling rate is nothing but samples taken per second, and by default, librosa samples the file at a sampling rate of 22050; you can override it by your desired sampling rate. Take the product of sampling rate and length of the file you will get the total number of samples.
To change the sample rate from 44.1 to 48 kHz, you have to determine a rational number (ratio of integers), P/Q , such that P/Q times the original sample rate, 44100, is equal to 48000 within some specified tolerance.
Using a higher sample rate with your audio music recording can prevent aliasing problems that are common with cymbals, brass, and some string instruments. A sample rate that's moderately higher can also smooth out high frequency filters.
librosa uses soundfile and audioread for reading audio. As of v0. 7, librosa uses soundfile by default, and falls back on audioread only when dealing with codecs unsupported by soundfile (notably, MP3, and some variants of WAV).
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With