Reading Wave Files

Embed Size (px)

Citation preview

  • 7/26/2019 Reading Wave Files

    1/3

    Reading Wave Files

    On the Windows platform, the most common file extension for audio files is "wav". MATLAB can

    read such wave files via the command "wavread". The following example reads the wave file

    "sunday.wav" and display its waveform directly.

    Example 1.

    [y, fs]=wavread('sunday.wav');

    sound(y, fs); % Playback of the sound data (?????)time=(1:length(y))/fs; % Time vector on x-axis (??????)

    plot(time, y)

    In the above example, "fs" is the sample rate which is 16000 in this case. This indicatesthat there are 44100 samples per second when the clip was recorded. The vector "y" is a

    column vector containing the samples of the speech signals. We can use "sound(y, fs)"to play the audio signals read from the file. "time" is a time vector in which each elementcorresponds to the time of each sample. Therefore we can plot "y" against "t" to show thewaveform directly.

    Most audio signals are digitized to have a bit resolution of 8 or 16 bits. If we want to knowthe bit resolution of the input wave file, we can use an extra output arguments to"wavread" to obtain the information, such as

    [y, fs, nbits]=wavread(sunday.wav');

    Moreover, if we want to know the time duration of a stream of audio signals, we can use"length(y)/fs" directly. The following example can obtain most of the important information

    of the wave file "sunday.wav".

    Example 2

    fileName='sunday.wav';[y, fs, nbits]=wavread(fileName);

    sound(y, fs); % Playback of the sound data (?????)time=(1:length(y))/fs; % Time vector on x-axis (??????)

    plot(time, y)fprintf('Information of the sound file "%s":\n', fileName);

    fprintf('Duration = %g seconds\n', length(y)/fs);

    fprintf('Sampling rate = %g samples/second\n', fs);fprintf('Bit resolution = %g bits/sample\n', nbits);

    From the above example, it can be observed that all the audio signals are between -1and 1. However, each sample point is represented by an 8-bit interger. How are theyrelated? First of all, we need to know the following convention:

  • 7/26/2019 Reading Wave Files

    2/3

    1. If a wave file has a bit resolution of 8 bits, then each sample point is stored as anunsigned integer between 0 and 255 (= 2^8-1).

    2.

    If a wave file has a bit resolution of 16 bits, then each sample point is stored as anunsigned integer between -32768 (= 2^16/2) and 32767 (= 2^16/2-1).

    Since almost all variables in MATLAB have the data type of "double", therefore allsamples are converted into a floating-point number between -1 and 1 for easymanipulation. Therefore to retrieve the original integer values of the audio signals, we canproceed as follows.

    1. For 8-bit resolution, we can multiply "y" (the value obtained by wavread) by 128and then plus 128.

    2. For 16-bit resolution, we can multiply "y" (the value obtained by wavread) by32768.

    Here is an example.

    Example 3

    fileName='sunday.wav';

    [y, fs, nbits]=wavread(fileName);

    y0=y*(2^nbits/2)+(2^nbits/2); % y0 is the original values stored in the wav

    file

    difference=sum(abs(y0-round(y0)))

    In the above example, the difference is zero, indicating the retrived y0 contains no fractional parts.

    We can also use the command "wavread" to read a stereo wave file. The returned variable willbe a matrix of 2 columns, each containing the audio signals from a single channel. Example

    follows.

    Example 4

    fileName='flanger.wav';

    [y, fs]=wavread(fileName); % Read wave file

    sound(y, fs); % Playback

    left=y(:,1); % Left channel

    right=y(:,2); % Right channelsubplot(2,1,1), plot((1:length(left))/fs, left);

    subplot(2,1,2), plot((1:length(right))/fs, right);

    In the above example, MATLAB will read the wave file "flanger.wav", play the stereo sound, and

    plot two streams of audio signals in two subplots. Since the intensities of these two channels are

    more or less complemntary to each other, which gives an illusion that the sound source is moving

    back and forth between two speakers.

  • 7/26/2019 Reading Wave Files

    3/3

    If the wave file is too large to be read into memory directly, we can also use "wavread" to read a

    part of the audio signals directly. See the following example.