Chapter 2: Practical Audio Processing

The emphasis on this chapter is getting started with capturing and using audio within MATLAB. This includes recording and playing back directly within MATLAB using the built-in sound tools, although we note that specialised audio tools such as the excellent free and open source Audacity tool for Linux, Mac OS-X and Windows are usually preferable because they are more intuitive and immediate in operation.

However there are many times when manipulating audio within MATLAB is useful - one of those is when running a perception-based test that couples a user interace (or display of information) with audio input/output.

Recording and replaying directly in MATLAB

Setting up and recording sound directly in MATLAB starts with creating an audio object, and then recording to it:

aro=audiorecorder(16000,16,1);
record(aro);

In fact we can pause and resume recording just as easily:

pause(10); %waits 10 seconds before next command 
pause(aro); %this pauses the recording 
resume(aro); %this resumes recording

before we stop and then replay the audio to test it:

stop(aro); %this ends the recording session
play(aro);

Acutally making use of the recorded audio data, we need to convert this into a standard MATLAB array which we can then process just like any other vector (or array) of information in MATLAB (note: it will be a [1xL] vector for a mono recording of duration L/Fs seconds at Fs Hz sample rate, but a [2xL] matrix for a stereo recording):

speech=getaudiodata(aro, 'double');

Loading and saving sound to/from a file

As discussed in section 2.1.3, it is often more convenient to make use of sound files, and so we can load from a Wave format file, and save to another one very easily

[y,fs]=audioread('soundfile.wav');
audiowrite('another_soundfile.wav',y,fs);

Where y is the array of audio and Fs is the sample rate.


Listings in Chapter 2

To save readers from having to type everything in by hand, the following listings are some of the functions and longer code snippets found in Chapter 2. They can be selected on this page using your mouse, copied, and then pasted into the MATLAB command line, or into the MATLAB editor.

tonegen.m

 function [s]=tonegen(Ft, Fs, Td)
   s=sin([1:Fs*Td]*2*pi*Ft/Fs);

Cepstrum plot

cx=cceps(x.*hamming(length(x)));
stem(abs(cx));
axis tight;
xlabel('Cepstral coefficient')
ylabel('Log magnitude')

Generate sequence of tones

gensnd=[]; %initialise an empty array 
for t=0:0.1:4
  fr=ModF(1+floor(t*Fs));
  % use floor() since index must be integer 
  gensnd=[gensnd,tonegen(fr,8000,0.1)];
  % concatenates new tones to end of gensnd
end
soundsc(gensnd,Fs); % listen then view it 
spectrogram(gensnd,128,0,128,Fs,'yaxis');

freqgen.m

function [snd]=freqgen(frc, Fs)
  th=0;
  fr=frc*2*pi/Fs;
  for si=1:length(fr)
    th=th+fr(si);
    snd(si)=sin(th);
    th=unwrap(th);
end

Notes of a musical chord

C=tonegen(261.63, 8000, 2);
E=tonegen(329.63, 8000, 2);
G=tonegen(783.99, 8000, 2);
B=tonegen(987.77, 8000, 2);
soundsc(C+E+G+B, 8000);

Links to further information

Newnes DSP guide (The Scientist and Engineer's Guide to Digital Signal Processing, by Steve Smith)

This excellent resource is freely available for browsing online, however if you do find the book useful it would be a good investment to purchase a copy. The book covers a lot of the more practically-oriented theory needed to understand and undertake Digital Signal Processing projects. In other words, it seems to be just enough of the theory to get a good grounding in the subject.

Really basic MATLAB intro

A really basic MATLAB introduction for sound processing from McGill University can be found here, but note that the older "wavread" and "wavwrite" have been replaced by the newer "audioread" and "audiowrite" functions that we use in our book. If you have a recent version of MATLAB you will not be able to make use of the older functions, and will need to edit the code examples appropriately.

MATLAB audio file handling

Some simple examples related to MATLAB audio file handling at Dan Ellis's LabROSA, Columbia.