next up previous
Next: About this document ... Up: notes Previous: Appendix I - MATLAB

Appendix II - Creating sounds with MATLAB

WAv files have sampling frequency 0f 22050. If we read a WAV-file into a MATLAB vector Y via
Y=wavread('filename');
we can replay the sound with the command
sound(Y,22050);
provided the computer has a sound card.

This now allows us to create our own sounds. For example the sequence of commands

t=linspace(0,4,40000);
y=sin(400*2*pi*t);
sound(y,10000);
will create a sound of 400 Hz that lasts 4 seconds. We used a sampling rate of 10000 here, since this seems sufficient for a sound of 400 cycles per second, i.e. in each cycle the function $ y=sin(800\pi t)$ is sampled 25 times.

As a little advanced exercise we will write a program that plays a C-major scale. To start we need to find the frequencies of the individual sounds. A middle A is usually used to tune instruments. That note has a frequency of 440 Hz. The base A has half the frequency, and there are twelve notes in the octave between a base A and a middle A. Similar the octave between a low C (the start of the C-major scale) and the high c its end contains twelve notes (including the so called half-tones).

$\displaystyle {\bf C}, {\bf C\char93 }, {\bf D}, {\bf D\char93 }, {\bf E}, {\bf...
...har93 }, {\bf G},
{\bf G\char93 }, {\bf A}, {\bf B}, {\bf B\char93 }, {\bf c}.
$

The frequencies $ f_n$ and $ f_{n+1}$ of two consecutive notes satisfy

$\displaystyle f_{n+1}=C f_n
$

for some constant $ C$. In other words, the frequencies form a geometric sequence not an algebraic (or linear) sequence. To find the value of $ C$ we observe that

$\displaystyle f_{n+12}=2f_n,
$

and therefore

$\displaystyle C^{12}=2,
$

or

$\displaystyle C=2^{1/12}=1.05946
$

This now allows us to compute all the frequencies for the notes from C to c. They are listed below.


Table 1: Approximate Frequencies of the notes between C and c in Hz
C C# D, D# E F F# G G# A B B# c.
262 277 294 311 330 349 370 392 415 440 466 493 523


In order to sound a C for 2 seconds, we write the following program:

t=linspace(0,2,20000);
y=sin(262*2*pi*t);
sound(y,10000);
and call it ``cee.m''. We can write similar programs for all other notes and then the C-major scale
pause;
cee;
pause;
dee;
pause;
ee;
pause;
eff;
pause;
gee;
pause;
aa;
pause;
bee;
pause;
hcee;
call it ``scale .m''. The pause commands are requests for key board input. Pressing any key will move the program to the next line. Typing
scale
will allow us to play a C-major scale by pressing any key to move to the next note. An obvious improvement would be to allow for different length of the notes.


next up previous
Next: About this document ... Up: notes Previous: Appendix I - MATLAB
Werner Horn 2006-06-06