Matlab Exercise 5
Matlab Exercise 5
Music Synthesis
OBJECTIVE:
Create a program of musical piece using Matlab and play out through a speaker.
PROGRAM:
1. Sampling Theory
Analyze the following program by changing the sampling frequency, fs, to 9kHz,
7kHz, 3kHz, and 1kHz. Explain the result.
dur = 1.0;
fs = 11025;
t = 0 : (1/fs) : dur;
x = sin( 2*pi*5000*t );
sound( x, fs )
1
Signals, Spectra, & Signal Processing Laboratory | eʃernαn dz
Piano keyboard layout. Key numbers are shaded. Notation C4 is in the fourth octave.
You can use the ratio 21/12 to calculate the frequency of notes anywhere on the
piano keyboard. For example, the E-flat above middle-C (black key number 43) is
6 keys below A-440, so its frequency should be f = 440 x 2-6/12 = 440/√2 ≈ 311
Hertz.
(a) Encode the program below and change the key number from 40 to 49.
dur = 1.0;
fs = 11025;
t = 0 : (1/fs) : dur;
freq=440*2^((key-49)/12);
x = sin( 2*pi*5000*t );
sound( x, fs )
(b) Now write an M-file to produce a desired note for a given duration. Your M-file
should be in the form of a function called note.m. Fill-in the following form:
function tone=note(key,dur)
fs= % Sampling frequency
t=0:1/fs:dur;
freq= % Tone frequency equation
tone= % Sinusoid function
sound(tone,fs);
Now modify the program above to make an ascending scale playing notes from
C4 to A4 using for loop. Refer to the piano keyboard for the key numbers.
2
Signals, Spectra, & Signal Processing Laboratory | eʃernαn dz
(a) Determine a sampling frequency that will be used to play out the sound
through the D-to-A system of the computer. This will dictate the time Ts between
samples of the sinusoids.
(b) Determine the total time duration needed for each note.
(c) Determine the frequency (in hertz) for each note (utilize the note.m function
written in section 2.
(d) Synthesize the waveform as a combination of sinusoids, and play it out
through the computer's built-in speaker or headphones using sound().
(e) A chord can be synthesized by adding the sinusoids for each note in the
chord. This will be a vector addition of the sinusoidal values for each note.
Likewise, if you have more than one melody line playing at the same time, you
can produce separate signal vectors for each melody (treble and bass) and then
combine them into one song by adding the signal vectors.
(f) Make a plot of a few periods of two or three of the sinusoids to illustrate that
you have the correct signals for each note.
Song: Fur Elise (Key numbers and corresponding duration is given below)
These are the piano key numbers and the corresponding durations for the song
Fur Elise.
t and tdur are for the treble clef or upper part and b and bdur
are for the bass clef or lower part. A duration of 1 corresponds
to an eighth note and a note value of 0 is a rest.
t=[56 55 56 55 56 51 54 52 49 0 40 44 49 51 0 44 48 51 52 0 44
56 55 ...
56 55 56 51 54 52 49 0 40 44 49 51 0 44 52 51 49 0 51 52 54
56 47 57 56 ...
54 45 56 54 52 44 54 52 51 0 44 56 0 0 56 68 0 0 55 56 0 0
55 56 55 ...
56 55 56 51 54 52 49 0 40 44 49 51 0 44 48 51 52 0 44 56 55
56 55 56 51 54 52 ...
49 0 40 44 49 51 0 44 52 51 49];
tdur = [.5 .5 .5 .5 .5 .5 .5 .5 1 .5 .5 .5 .5 1 .5 .5 .5 .5 1
.5 .5 .5 .5 ...
.5 .5 .5 .5 .5 .5 1 .5 .5 .5 .5 1 .5 .5 .5 .5 1 .5 .5 .5 .5
1.5 .5 .5 .5 ...
1.5 .5 .5 .5 1.5 .5 .5 .5 1 .5 .5 .5 .5 .5 .5 .5 .5 .5 .5 .5
.5 .5 .5 .5 .5 ...
3
Signals, Spectra, & Signal Processing Laboratory | eʃernαn dz
.5 .5 .5 .5 .5 .5 1 .5 .5 .5 .5 1 .5 .5 .5 .5 1 .5 .5 .5 .5
.5 .5 .5 .5 .5 .5 ...
1 .5 .5 .5 .5 1 .5 .5 .5 .5 2];
b=[0 0 25 32 37 0 0 20 32 36 0 0 25 32 39 0 0 ...
0 25 32 37 0 0 20 32 36 0 0 25 32 37 0 28 35 40 0 0 ...
23 35 39 0 0 25 32 37 0 0 20 32 44 0 44 56 0 0 55 56 0 0 55
56 0 0 ...
0 25 32 37 0 0 20 32 36 0 0 25 32 37 0 0 0 ...
25 32 37 0 0 20 32 36 0 0 25 32 37 0];
bdur=[1 3 .5 .5 .5 .5 1 .5 .5 .5 .5 1 .5 .5 .5 .5 1 ...
3 .5 .5 .5 .5 1 .5 .5 .5 .5 1 .5 .5 .5 1.5 .5 .5 .5 .5 1 ...
.5 .5 .5 .5 1 .5 .5 .5 .5 1 .5 .5 .5 1 .5 .5 .5 .5 .5 .5 .5
.5 .5 .5 .5 1 ...
3 .5 .5 .5 .5 1 .5 .5 .5 .5 1 .5 .5 .5 .5 1 3 ...
.5 .5 .5 .5 1 .5 .5 .5 .5 1 .5 .5 .5 .5];
A sample program using the function note the first lines in treble (first column)
and base (second column) is given below. Note that all time duration are reduced
to halves to adjust to the song’s tempo. Other time reduction can be applied also.
Treble Base
note(56,.25); note(0,.5);
note(55,.25); note(0,1.5);
note(56,.25); note(25,.25);
note(55,.25); note(32,.25);
note(56,.25); note(37,.25);
note(51,.25); note(0,.25);
note(54,.25); note(0,.5);
note(52,.25); note(20,.25);
note(49,.5); note(32,.25);
note(0,.25); note(36,.25);
note(40,.25); note(0,.25);
note(44,.25); note(0,.5);
note(49,.25); note(25,.25);
note(51,.5); note(32,.25);
note(0,.25); note(39,.25);
note(44,.25); note(0,.25);
note(48,.25); note(0,.5);
note(51,.25);
note(52,.5);
note(0,.25);
note(44,.25);
note(56,.25);
note(55,.25);
4
Signals, Spectra, & Signal Processing Laboratory | eʃernαn dz
The program below is the melody or combination of both the first lines of treble
and base of the song Fur Elise. Note that before running this program the
function note must be modified first be deleting the sound() command first since
this command is implemented in this program instead. And additional zeros are
added to the b (base) vector to match the size of t (treble) vector.
t=[note(56,.25),note(55,.25),note(56,.25),note(55,.25),note(56,.25),
note(51,.25),note(54,.25),note(52,.25),note(49,0.5),note(0,.25),
note(40,.25),note(44,.25),note(49,.25),note(51,.5),note(0,.25),
note(44,.25),note(48,.25),note(51,.25),note(52,.5),note(0,.25),
note(44,.25),note(56,.25),note(55,.25)];
b=[note(0,.5),note(0,1.5),note(25,.25),note(32,.25),note(37,.25),
note(0,.25),note(0,.5),note(20,.25),note(32,.25),note(36,.25),
note(0,.25),note(0,.5),note(25,.25),note(32,.25),note(39,.25),
note(0,.25),note(0,.5),0,0,0,0,0];
mel=t+b;
sound(mel,11025)
Now finish the entire piece and practice the same approach for the song of
your choice.
The musical passage is likely to sound very artificial, because it is created from
pure sinusoids. Therefore, you might want to try improving the quality of the
sound by incorporating some modifications. For example, you could multiply
each pure tone signal by an envelope E(t) so that it would fade in and out.
A standard way to define the envelop function is to divide E(t) into four sections:
attack (A), delay (D), sustain (S), and release (R). Together these are called
ADSR. The attack is a quickly rising front edge, the delay is a small short -
duration drop, the sustain is more or less constant and the release drops quickly
back to zero. The figure below shows a linear approximation to the ADSR profile.
Now, modify your function note by multiplying the tone by an envelope e-t – e-2t.
Play the music and observe the difference of the sound with the original tone.
5
Signals, Spectra, & Signal Processing Laboratory | eʃernαn dz
Name:________________________ Date:_____________
Verified ____________________
Verified ____________________
Verified ____________________