Play Stephen Foster's "Beautiful Dreamer" Using MATLAB

Combine your love for math with your love for music by utilizing the exponential and sine functions in MATLAB to create signals that recreate a piece of music. This example delves into the replication of Stephen Foster’s classic parlor song “Beautiful Dreamer.”

"Beautiful Dreamer" sheet music found at http://www.loc.gov/creativity/hampson/dreamer_2.html

Playing “Beautiful Dreamer” on the piano requires the range of keys B3, the key immediately left to Middle C, to D5. Additionally, this song includes only one sharp note, C#4. Each note has its own unique frequency, which we can use to create sine waves on MATLAB for each note at lengths we specify. How can the exponential function aid in the production of a note then? The addition of the exponential function serves to aid in the production of a more natural sounding note. Multiplying the sine waves by a negative exponential gradually quiets the sound of the note as time goes on just as a note slowly fades to silence after hitting a key on the piano. Copy the following code into a script in MATLAB and run the script to hear a reproduction of this classic song.

delta = 0.00005;
notelength = 0:delta:0.6;  %length of notes in seconds
longernote = 0:delta:1;  %length of longer notes in seconds
B3 = exp((-1)*notelength*2).*sin(2*pi*246.942*notelength);  %Sine wave to produce a B3 note with a frequency of 246.942 Hz
MiddleC = exp((-1)*longernote*2).*sin(2*pi*261.626*longernote);  %Sine wave to produce a C4 note with a frequency of 261.626 Hz
Csharp = exp((-1)*notelength*2).*sin(2*pi*277.183*notelength);  %Sine wave to produce a C#4 note with a frequency of 277.183 Hz
D4 = exp((-1)*notelength*2).*sin(2*pi*293.665*notelength);  %Sine wave to produce a D4 note with a frequency of 293.665 Hz
E4 = exp((-1)*notelength*2).*sin(2*pi*329.628*notelength);  %Sine wave to produce an E4 note with a frequency of 329.628 Hz
F4 = exp((-1)*notelength*2).*sin(2*pi*349.228*notelength);  %Sine wave to produce an F4 note with a frequency of 349.228 Hz
G4 = exp((-1)*notelength*2).*sin(2*pi*391.995*notelength);  %Sine wave to produce a G4 note with a frequency of 391.995 Hz
A4 = exp((-1)*notelength*2).*sin(2*pi*440.000*notelength);  %Sine wave to produce an A4 note with a frequency of 440.000 Hz
B4 = exp((-1)*notelength*2).*sin(2*pi*493.883*notelength);  %Sine wave to produce a B4 note with a frequency of 493.883 Hz
C5 = exp((-1)*notelength*2).*sin(2*pi*523.251*notelength);  %Sine wave to produce a C5 note with a frequency of 523.251 Hz
D5 = exp((-1)*notelength*2).*sin(2*pi*587.330*notelength);  %Sine wave to produce a D5 note with a frequency of 587.330 Hz
%create a vector to store the notes of the song in order
beautiful = [C5 B4 C5 G4 E4 D4 Csharp D4 A4 G4 B4 A4 A4 G4 F4 F4 E4 D4 E4 C5 B4 C5 G4 E4 D4 Csharp D4 A4 G4 B4 A4 A4 G4 F4 F4 E4 D4 MiddleC G4 F4 D4 B3 A4 A4 G4 E4 MiddleC C5 B4 C5 A4 D5 C5 B4 C5 A4 G4 C5 B4 C5 G4 E4 D4 Csharp D4 A4 G4 B4 A4 A4 G4 F4 F4 E4 D4 E4 A4 B4 C5 C5 G4 E4 F4 E4 D4 MiddleC];
%play the song at a sample rate of 1/delta
sound(beautiful,1/delta);

Use the code above to listen to the MATLAB version of the beautiful melody or listen to the attached audio recording of the MATLAB output right now. Recording of "Beautiful Dreamer" playing on MATLAB

Alumni Liaison

Basic linear algebra uncovers and clarifies very important geometry and algebra.

Dr. Paul Garrett