Wavread Function
Wavread Function
jar:file:///C:/Program%20Files/MATLAB/R2011a/help/techdoc/help.jar!/...
wavread
Read WAVE (.wav) sound file
Alternatives
As an alternative to wavread, use the Import Wizard. To activate the Import Wizard, select File > Import Data.
Syntax
y = wavread(filename)
[y, Fs] = wavread(filename)
[y, Fs, nbits] = wavread(filename)
[y, Fs, nbits, opts] = wavread(filename)
[...] = wavread(filename, N)
[...] = wavread(filename, [N1 N2])
[...] = wavread(..., fmt)
siz = wavread(filename,'size')
Description
y = wavread(filename) loads a WAVE file specified by the string filename, returning the sampled data in y. If
filename does not include an extension, wavread appends .wav.
[y, Fs] = wavread(filename) returns the sample rate (Fs) in Hertz used to encode the data in the file.
[y, Fs, nbits] = wavread(filename) returns the number of bits per sample (nbits).
[y, Fs, nbits, opts] = wavread(filename) returns a structure opts of additional information contained in the
WAV file. The content of this structure differs from file to file. Typical structure fields include opts.fmt (audio format
information) and opts.info (text that describes the title, author, etc.).
[...] = wavread(filename, N) returns only the first N samples from each channel in the file.
[...] = wavread(filename, [N1 N2]) returns only samples N1 through N2 from each channel in the file.
[...] = wavread(..., fmt) specifies the data format of y used to represent samples read from the file. fmt can be
either of the following values, or a partial match (case-insensitive):
'double'
'native'
siz = wavread(filename,'size') returns the size of the audio data contained in filename instead of the actual
audio data, returning the vector siz = [samples channels].
Output Scaling
The range of values in y depends on the data format fmt specified. Some examples of output scaling based on typical
bit-widths found in a WAV file are given below for both 'double' and 'native' formats.
Native Formats
1 of 2
Data Range
16
24
32
9/29/2014 6:37 PM
jar:file:///C:/Program%20Files/MATLAB/R2011a/help/techdoc/help.jar!/...
Double Formats
Number of Bits
Data Range
N<32
double
N=32
double
Examples
Create a WAV file from the demo file handel.mat, and read portions of the file back into MATLAB.
% Create WAV file in current folder.
load handel.mat
hfile = 'handel.wav';
wavwrite(y, Fs, hfile)
clear y Fs
% Read the data back into MATLAB, and listen to audio.
[y, Fs, nbits, readinfo] = wavread(hfile);
sound(y, Fs);
% Pause before next read and playback operation.
duration = numel(y) / Fs;
pause(duration + 2)
% Read and play only the first 2 seconds.
nsamples = 2 * Fs;
[y2, Fs] = wavread(hfile, nsamples);
sound(y2, Fs);
pause(4)
% Read and play the middle third of the file.
sizeinfo = wavread(hfile, 'size');
tot_samples = sizeinfo(1);
startpos = tot_samples / 3;
endpos = 2 * startpos;
[y3, Fs] = wavread(hfile, [startpos endpos]);
sound(y3, Fs);
See Also
audioplayer | audiorecorder | mmfileinfo | sound | wavfinfo | wavwrite
Was this topic helpful?
Yes
No
2 of 2
9/29/2014 6:37 PM