1 January 2010 0 Comments

Basic Sound Processing with MATLAB

This page describes some basic sound processing functions in MATLAB. We’ll begin by reading in a wav file. You can download it here 440_sine.wav it contains a complex tone with a 440 Hz fundamental frequency (F0) plus noise.

1
>> [snd, sampFreq, nBits] = wavread('440_sine.wav');

the wav file has two channels and 5060 sample points

1
2
3
>> size(snd)
ans =
5060 2

considering the sample rate (sampFreq = 44110) this corresponds to about 114 ms duration

1
2
3
>> 5060 / sampFreq
ans =
0.1147

we can listen to the tone with the sound function

1
>> sound(snd, 44100, 16)

we’ll select and work only with one of the channels from now onwards

1
>> s1 = snd(:,1);

Plotting the Tone

A time representation of the sound can be obtained by plotting the pressure values against the time axis. However we need to create an array containing the time points first:

1
2
>> timeArray = (0:5060-1) / sampFreq;
>> timeArray = timeArray * 1000; %scale to milliseconds

now we can plot the tone

1
plot(timeArray, s1, 'k')

440ToneTimePlot.png

Plotting the Frequency Content

Another useful graphical representation is that of the frequency content of the tone. We can obtain the frequency content of the sound using the fft function, that implements a Fast Fourier Transform algorithm. We’ll follow closely the following technical document http://www.mathworks.com/support/tech-notes/1700/1702.html to obtain the power spectrum of our sound.

1
2
n = length(s1);
p = fft(s1); % take the fourier transform

notice that compared to the technical document, we didn’t specify the number of points on which to take the fft, by default then the fft is computed on the number of points of the signal (n). Since we’re not using a power of two the computation will be a bit slower, but for signals of this duration this is negligible.

1
2
3
4
nUniquePts = ceil((n+1)/2);
p = p(1:nUniquePts); % select just the first half since the second half
% is a mirror image of the first
p = abs(p); % take the absolute value, or the magnitude

the fourier transform of the tone returned by the fft function contains both magnitude and phase information and is given in a complex representation (i.e. returns complex numbers). By taking the absolute value of the fourier transform we get the information about the magnitude of the frequency components.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
p = p/n; % scale by the number of points so that
% the magnitude does not depend on the length
% of the signal or on its sampling frequency
p = p.^2; % square it to get the power
% multiply by two (see technical document for details)
if rem(n, 2) % odd nfft excludes Nyquist point
p(2:end) = p(2:end)*2;
else
p(2:end -1) = p(2:end -1)*2;
end
freqArray = (0:nUniquePts-1) * (sampFreq / n); % create the frequency array
plot(freqArray/1000, 10*log10(p), 'k')
xlabel('Frequency (kHz)')
ylabel('Power (dB)')

The resulting plot can bee seen below, notice that we’re plotting the power in decibels by taking 10*log10(p), we’re also scaling the frequency array to kilohertz by dividing it by 1000

440ToneSpectrumPlot.png

To confirm that the value we have computed is indeed the power of the signal, we’ll also compute the root mean square (rms) of the signal. Loosely speaking the rms can be seen as a measure of the amplitude of a waveform. If you just took the average amplitude of a sinusoidal signal oscillating around zero, it would be zero since the negative parts would cancel out the positive parts. To get around this problem you can square the amplitude values before averaging, and then take the square root (notice that squaring also gives more weight to the extreme amplitude values):

1
2
3
>> rms_val = sqrt(mean(s1.^2))
rms_val =
0.0615

since the rms is equal to the square root of the overall power of the signal, summing the power values calculated previously with the fft over all frequencies and taking the square root of this sum should give a very similar value.

1
2
3
>> sqrt(sum(p))
ans =
0.0615

References

[via Xoomer]