Sunday, April 12, 2015

Wavplot and Wavplay

Matlab has their built in function to play .wav file, but it didn't work in Unix and Linux-based OS, because the wavplay function only supported in Windows. Moreover, I want to plot waveform of .wav file in single command, so I don't need to wavread it before plot. For those two purposes, I made two shorts function namely wavplot and wavfile.

Wavplot
wavplot is simple function to plot .wav file. Why I need it? because I continuously working with .wav file, and I want to plot it directly to get information of sound file via its waveform. Here the code of wavplot function.

function wavplot(wavFile)
% wavplot(wavFile) plot waveform and spectrum of wav file
% bagus@ep.its.ac.id

if nargin>1
    fprintf('Usage: wavplot(wavFile \n');
    return;
end;

[y, fs]=wavread(wavFile);
plot(y)
end

How to use it? it is simple. If you have .wav file, for example it has name tone.wav and you are in the current directory of Matlab command window, you can directly plot it with this function,
wavplot('tone.wav')
And it will plot the wav file like the following,
Output of wavplot function
Wavplay
While wavplot is function to plot wav file, wavplay is function to play wav file. Instead of using "wavread" and then "sound", we can play sound file directly by this function. This function is replacement for built-in Matlab function which only works on Windows OS.

function wavplay(wavFile)
%   Modified by Bagus Tris Atmaja 2015/04/11 12.47
%   bagus@ep.its.ac.id VibrasticLab - ITS


if nargin>1
    fprintf('Usage: wavplay(wavFile \n');
    return;
end;

[y, fs]=wavread(wavFile);
sound(y,fs)
end

Using this function is also simple, just write wavfile('filename.wav') in the command window, it will play sound file filename.wav.

Both function are available in Github, as part of VibrasticLab library developed in VibrasticLab, ITS.
Related Posts Plugin for WordPress, Blogger...