Friday, February 14, 2014
Read wave files in Python
Now a days python developers developed many scientific tools which can be useful in your research. If you started research in Speech signal process then it is necessary to know 'how to read a wavefile in computer'. If you know the MATLAB then it is pretty straight forward to read or write wavefiles, direct command like "wavread" or "wavwrite". But if you want to do the same operations in Python it is not straight forward. However, in Python numerous packages are provided for reading a wavefile like "scipy.io.wavfile.read" or "scikits.audiolab", etc. If you read the wavefiles using the "scipy" package it returns the integer values not the float values like in MATLAB. Here I provided the small snippet to read the wavefile like in MATLAB.
%in matlab to read wavefiles
s, fs = wavread('file')
% values in s are float values and fs represents the sampling frequency
from scipy.io import wavfile as wav
fs, x = wav.read('file') #fs is sampling frequency and x is integer numpy array
# If you know the samplewidth (most of the cases it is 2 bytes (16bits))
# the integer values lies between -2^15 to 2^15
# to convert the integers to float values like in MATLAB
# just divide the integers array by 2^15 = 32768
x = x/32768.0 # don't forget to convert the 32768 to 32768.0
# Now values in x are same as values in s
Scipy package doesn't support the 24-bit samplewidth files. For more information, please go through this link Waveio which provided the solution to read the wavefile without scipy package.
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment