Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 8

Signals and Systems Lab DEE, FURC

Lab 10: Sound Manipulation in Matlab


Syntax
y=wavread(filename)
[y,Fs]=wavread(filename)
[y,Fs,nbits]=wavread(filename)
[y,Fs,nbits,opts]=wavread(filename)
[___]=wavread(filename,N)
[___]=wavread(filename,[N1N2])
[___]=wavread(___,fmt)
size = wavread(filename,'size')

wavplay(sound signal, fs)

wavplay(sound signal, 0.5*fs)

wavplay(sound signal,2*fs)

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.).

[y, Fs] = wavread(filename, N) returns only the first N samples from each channel in
the file.

[y, Fs] = wavread(filename, [N1 N2]) returns only samples N1 through N2 from
each channel in the file.

1
Signals and Systems Lab DEE, FURC

[y, Fs] = 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' Double-precision normalized samples (default).

'native' Samples in the native data type found in the file.

size = wavread(filename,'size') returns the size of the audio data contained in


filename instead of the actual audio data, returning the vector size = [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

Number of Bits MATLAB Data Type Data Range

8 uint8 (unsigned integer) 0 <= y <= 255

16 int16 (signed integer) -32768 <= y <= +32767

24 int32 (signed integer) -2^23 <= y <= 2^23-1

32 single (floating point) -1.0 <= y < +1.0

Double Formats

Number of MATLAB Data Data Range


Bits Type

N<32 double -1.0 <= y < +1.0

N=32 double -1.0 <= y <= +1.0


Note: Values in y might exceed -1.0 or +1.0 for the case of

2
Signals and Systems Lab DEE, FURC

Number of MATLAB Data Data Range


Bits Type

N=32 bit data samples stored in the WAV file.

wavread supports multi-channel data, with up to 32 bits per sample.

wavread supports Pulse-code Modulation (PCM) data format only.

Examples

Create a WAV file from the example file handel.mat, and read portions of the file back
into MATLAB®.

Example 1

clc

[soundsig,fs,nbits]=wavread('c:\soundlab\road.wav');

left=soundsig(:,1);

right=soundsig(:,2);

L=length(left);

reverse=flipud(left);

wavplay(soundsig,fs);
3
Signals and Systems Lab DEE, FURC

Repeat this example for the following commands:

wavplay(sound signal, fs)

wavplay(sound signal, 0.5*fs)

wavplay(sound signal,2*fs)
Example 2

Echo Signal

clc

attn=0.2

td=1;

[signal,fs,nbits]=wavread('c:\soundlab\road.wav');

[signal,echo,ref]=echo11(signal,fs,td,attn)

wavplay(signal,fs);

wavplay(echo,fs);

%Function file

function[sigx,echosig,reflectedsig]=echo11(sig,fs,td,attn);

zer=zeros(td*fs,2);

sigx=[sig;zer];

reflectedsig=[zer;sig*attn];

echosig=sigx+reflectedsig;

4
Signals and Systems Lab DEE, FURC

Example 3

% 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;

5
Signals and Systems Lab DEE, FURC

endpos = 2 * startpos;

[y3, Fs] = wavread(hfile, [startpos endpos]);

sound(y3, Fs);

%Example 4

%loading music to test the above properties

load handel

Fs =8192

%sound(y,Fs)

Ts=1/Fs;

t1 = -36556*Ts:Ts:36556*Ts

figure(1); plot(t1,y); grid;

6
Signals and Systems Lab DEE, FURC

y1=flipud(y);

%sound(y1)
%we need to make the previous sound command to be a command->
%sound(y,Fs)

figure(2); plot(t1,y1); grid;

%down sampling

m=2;

z=downsample(y,m); %should use an integer value for m

%sound(z);

%to get the result we should make the terms 'sound(y,Fs)& sound(y1)' as

%command-> %sound(y,Fs)& %sound(y1)

%up sampling

7
Signals and Systems Lab DEE, FURC

z1=upsample(y,m) %should use an integer value for m

%sound(z1)
%to get the result we should make the terms 'sound(y,Fs)& sound(y1)&
sound(z)'

%as command-> %sound(y,Fs)& %sound(y1)& %sound(z)

%Example 5

%loading and playing mp3 music

[y, Fs] = audioread('C:\Users\HAPPY\Desktop\file_name.mp3');


% Fs=4000;
player = audioplayer(y, Fs);
play(player);

You might also like