Signal Processing First
Lab 04: Synthesis of Sinusoidal Signals-Music Synthesis
T
Pre-Lab and Warm-Up: You should read at least the Pre-Lab and Warm-up sections of this lab assignment
and go over all exercises in the Pre-Lab section before going to your assigned lab session.
Verification: The Warm-up section of each lab must be completed during your assigned Lab time and
the steps marked Instructor Verification must also be signed off during the lab time. One of the laboratory
instructors must verify the appropriate steps by signing on the Instructor Verification line. When you have
completed a step that requires verification, simply demonstrate the step to the TA or instructor. Turn in the
completed verification sheet to your TA when you leave the lab.
Lab Report: It is only necessary to turn in a report on Section 4 with graphs and explanations. You are
asked to label the axes of your plots and include a title for every plot. In order to keep track of plots, include
your plot inlined within your report. If you are unsure about what is expected, ask the TA who will grade
your report.
1 Introduction
This lab includes a project on music synthesis with sinusoids. The piece, Fugue #2 for the Well-Tempered
Clavier by Bach¹ has been selected for doing the synthesis program. The project requires an extensive
programming effort and should be documented with a complete formal lab report. A good report should
include the following items: a cover sheet, commented MATLAB code, explanations of your approach,
conclusions and any additional tweaks that you implemented for the synthesis. Since the project must be
evaluated by listening to the quality of the synthesized song, the criteria for judging a good song are given
at the end of this lab description. In addition, it may be convenient to place the final song on a web site so
that it can be accessed remotely by a lab instructor who can then evaluate its quality. If you would like to
try other songs, the SP First CD-ROM includes information about alternative tunes: Minuet in G, Für Elise,
Beethoven's Fifth, Jesu, Joy of Man's Desiring and Twinkle, Twinkle, Little Star.
The music synthesis will be done with sinusoidal waveforms of the form
x(t) = Σ Ak cos(wkt + Ok)
k
(1)
so it will be necessary to establish the connection between musical notes, their frequencies, and sinusoids.
A secondary objective of the lab is the challenge of trying to add other features to the synthesis in order
to improve the subjective quality for listening. Students who take this challenge will be motivated to learn
more about the spectral representation of signals a topic that underlies this entire course.
McClellan, Schafer, and Yoder, Signal Processing First, ISBN 0-13-065562-7.
Prentice Hall, Upper Saddle River, NJ 07458. ©2003 Pearson Education, Inc.
2 Pre-Lab
In this lab, the periodic waveforms and music signals will be created with the intention of playing them out
through a loudspeaker. Therefore, it is necessary to take into account the fact that a conversion is needed
from the digital samples, which are numbers stored in the computer memory to the actual voltage waveform
that will be amplified for the speakers.
¹See http://www.hnh.com/composer/bach.htm.
CD-ROM
MUSIC
SYN-
THESIS 2.1 Theory of Sampling
Chapter 4 treats sampling in detail, but this lab is usually done prior to lectures on sampling, so we provide
a quick summary of essential facts here. The idealized process of sampling a signal and the subsequent
reconstruction of the signal from its samples is depicted in Fig. 1. This figure shows a continuous-time input
x(t)
x[n] = x(nTs)
y(t)
C-to-D
Converter
D-to-C
Converter
T
Figure 1: Sampling and reconstruction of a continuous-time signal.
signal ä(t), which is sampled by the continuous-to-discrete (C-to-D) converter to produce a sequence of
samples x[n] = x(nTs), where n is the integer sample index and T, is the sampling period. The sampling
rate is fs = 1/T, where the units are samples per second. As described in Chapter 4 of the text, the ideal
discrete-to-continuous (D-to-C) converter takes the input samples and interpolates a smooth curve between
them. The Sampling Theorem tells us that if the input signal x(t) is a sum of sine waves, then the output y(t)
will be equal to the input x(t) if the sampling rate is more than twice the highest frequency ƒmax in the input,
i.e., ƒs > 2ƒmax. In other words, if we sample fast enough then there will be no problems synthesizing the
continuous audio signals from x[n].
2.2 D-to-A Conversion
Most computers have a built-in analog-to-digital (A-to-D) converter and a digital-to-analog (D-to-A) con-
verter (usually on the sound card). These hardware systems are physical realizations of the idealized con-
cepts of C-to-D and D-to-C converters respectively, but for purposes of this lab we will assume that the
hardware A/D and D/A are perfect realizations.
The digital-to-analog conversion process has a number of aspects, but in its simplest form the only
thing we need to worry about at this point is that the time spacing (Ts) between the signal samples must
correspond to the rate of the D-to-A hardware that is being used. From MATLAB, the sound output is done
by the soundsc (xx, fs) function which does support a variable D-to-A sampling rate if the hardware on
the machine has such capability. A convenient choice for the D-to-A conversion rate is 11025 samples per
second, so Tg = 1/11025 seconds; another common choice is 8000 samples/sec. Both of these rates satisfy
the requirement of sampling fast enough as explained in the next section. In fact, most piano notes have
relatively low frequencies, so an even lower sampling rate could be used. If you are using soundsc (), the
vector xx will be scaled automatically for the D-to-A converter, but if you are using sound.m, you must
scale the vector xx so that it lies between ±1. Consult help sound.
=
=
(a) The ideal C-to-D converter is, in effect, being implemented whenever we take samples of a continuous-
time formula, e.g., x(t) at t tn. We do this in MATLAB by first making a vector of times, and then
evaluating the formula for the continuous-time signal at the sample times, i.e., x[n] = x(nT,) if
tn = nTs. This assumes perfect knowledge of the input signal, but we have already been doing it this
way in previous labs.
To begin, create a vector x1 of samples of a sinusoidal signal with A₁
100, W₁ = 2T (800), and
Φ1 -T/3. Use a sampling rate of 11025 samples/second, and compute a total number of samples
equivalent to a time duration of 0.5 seconds. You may find it helpful to recall that a MATLAB statement
such as tt- (0:0.01:3); would create a vector of numbers from 0 through 3 with increments of
2This sampling rate is one quarter the rate (44,100 Hz) used in audio CD players.
McClellan, Schafer, and Yoder, Signal Processing First, ISBN 0-13-065562-7.
Prentice Hall, Upper Saddle River, NJ 07458. ©2003 Pearson Education, Inc.
=
2 0.01. Therefore, it is only necessary to determine the time increment needed to obtain 11025 samples
in one second. You should use the syn_sin () function from a previous lab for this part.
Use soundsc() to play the resulting vector through the D-to-A converter of the your computer,
assuming that the hardware can support the f 11025 Hz rate. Listen to the output.
=
(b) Now create another vector x2 of samples of a second sinusoidal signal (0.8 secs. in duration) for
the case A₂ 80, W₂ = 27 (1200), and 2 +π/4. Listen to the signal reconstructed from these
samples. How does its sound compare to the signal in part (a)?
=
(c) Concatenate the two signals x1 and x2 with a short duration of 0.1 seconds of silence in between.
You should be able to use a statement something like:
=
assuming that both x1 and x2 are row vectors. Determine the correct value of N to make 0.1 seconds
of silence. Listen to this new signal to verify that it is correct.
T
(d) To verify that the concatenation operation was done correctly in the previous part, make the following
plot:
XX =
tt = (1/11025) * (1:length (xx) ) ;
plot ( tt, xx );
This will plot a huge number of points, but it will show the "envelope" of the signal and verify that the
amplitude changes from 100 to zero and then to 80 at the correct times. Notice that the time vector
tt was created to have exactly the same length as the signal vector xx.
=
7;
[ x1, zeros (1,N), x2 ];
(e) Now send the vector xx to the D-to-A converter again, but change the sampling rate parameter in
soundsc (xx, fs) to 22050 samples/second. Do not recompute the samples in xx, just tell the
D-to-A converter that the sampling rate is 22050 samples/second. Describe how the duration and
pitch of the signal were affected. Explain.
x. Amp
x.phase
x. freq = 100;
x.fs = 11025
x.time Interval =
2.3 Structures in MATLAB
MATLAB can do structures. Structures are convenient for grouping information together. For example, run
the following program which plots a sinusoid:
=
-pi/2;
0: (1/x.fs):0.05;
x.values = x. Amp*cos (2*pi* (x.freq) * (x.timeInterval) + x.phase);
x. name =
'My Signal';
X
---- echo the contents of the structure "x"
plot ( x.time Interval, x.values )
title( x.name )
Notice that the members of the structure can contain different types of variables: scalars, vectors or strings.
2.4 Debugging Skills
Testing and debugging code is a big part of any programming job, as you know if you been staying up
late on the first few labs. Almost any modern programming environment provides a symbolic debugger so
that break-points can be set and variables examined in the middle of program execution. Of course, many
McClellan, Schafer, and Yoder, Signal Processing First, ISBN 0-13-065562-7.
Prentice Hall, Upper Saddle River, NJ 07458. ©2003 Pearson Education, Inc.
3 programmers insist on using the old-fashioned method of inserting print statements in the middle of their
code (or the MATLAB equivalent, leaving off a few semi-colons). This is akin to riding a tricycle to commute
around Atlanta.
In order to learn how to use the MATLAB tools for debugging, try help debug. Here is part of what
you'll see:
dbstop
dbclear
dbcont
dbstack
dbstatus
dbstep
dbtype
dbquit
Set breakpoint.
Remove breakpoint.
Resume execution.
List who called whom.
List all breakpoints.
Execute one or more lines.
List M-file with line numbers.
Quit debug mode.
When a breakpoint is hit, MATLAB goes into debug mode. On the PC
and Macintosh the debugger window becomes active and on UNIX and VMS
the prompt changes to a K>. Any MATLAB command is allowed at the
prompt. To resume M-file function execution, use DBCONT or DBSTEP.
To exit from the debugger use DBQUIT.
One of the most useful modes of the debugger causes the program to jump into “debug mode" whenever
an error occurs. This mode can be invoked by typing:
dbstop if error
With this mode active, you can snoop around inside a function and examine local variables that probably
caused the error. You can also choose this option from the debugging menu in the MATLAB editor. It's
sort of like an automatic call to 911 when you've gotten into an accident. Try help dbstop for more
information. Use the following to stop debugging
T
dbclear if error
Create an M-filecoscos.m containing the code below and use the debugger to find the error(s) in the
function. Call the function with the test case: [xn, tn] coscos (2,3,20,1). Use the debugger to:
1. Set a breakpoint to stop execution when an error occurs and jump into “Keyboard" mode,
2. display the contents of important vectors while stopped,
3. determine the size of all vectors by using either the size () function or the whos command.
4. and, lastly, modify variables while in the "Keyboard" mode of the debugger.
cosl =
cos (2*pi*f1*t1);
cos2 = cos (2*pi*f2*t2);
function [xx,tt] = coscos (fl, f2, fs, dur )
% COSCOS multiply two sinusoids
응
t1 = 0: (1/fs): dur;
t2 = 0: (1/f2): dur;
xx = cos1 * cos2;
tt = tl;
=
McClellan, Schafer, and Yoder, Signal Processing First, ISBN 0-13-065562-7.
Prentice Hall, Upper Saddle River, NJ 07458. ©2003 Pearson Education, Inc. 2.5 Piano Keyboard
Section 4 of this lab will consist of synthesizing the notes of a well known piece of music.³ Since these
signals require sinusoidal tones to represent piano notes, a quick introduction to the frequency layout of the
piano keyboard is needed. On a piano, the keyboard is divided into octaves-the notes in one octave being
twice the frequency of the notes in the next lower octave. For example, the reference note is the A above
OCTAVE-
T
41 43
C3 D3 E3 F3 G3 A3 B3 C4 D4 E4 F4 G4 A4 B4 C5 D5 E5 F5 G5 A5 B5
28 30 32 33 35 37 39 40 42 44 45 47 49 51 52 54 56 57 59 61 63
Middle-C
A-440
Figure 2: Layout of a piano keyboard. Key numbers are shaded. The notation C4 means the C-key in the
fourth octave.
middle-C which is usually called A-440 (or A4) because its frequency is 440 Hz. (In this lab, we are using
the number 40 to represent middle C. This is somewhat arbitary; for instance, the Musical Instrument Digital
Interface (MIDI) standard represents middle C with the number 60). Each octave contains 12 notes (5 black
keys and 7 white) and the ratio between the frequencies of the notes is constant between successive notes.
As a result, this ratio must be 2¹/¹2. Since middle C is 9 keys below A-440, its frequency is approximately
261 Hz. Consult chapter 9 for even more details.
Musical notation shows which notes are to be played and their relative timing (half, quarter, or eighth).
Figure 3 shows how the keys on the piano correspond to notes drawn in musical notation. The white keys
are all labeled as A, B, C, D, E, F, and G; but the black keys are denoted with "sharps" or "flats." A sharp
such as A# is one key number larger than A; a flat is one key lower, e.g., A½ is key number 48.
TREBLE
F-SHARP
-BASS
f
21
EIGHTH NOTE
HALF NOTE
8<
42 44.
D4
QUARTER NOTE
E4
T
39 37
B3
-46- -47.
F#4
35
-A4 = (A-440)
-49-
40
-B
34 32- 30
F#3-
McClellan, Schafer, and Yoder, Signal Processing First, ISBN 0-13-065562-7.
Prentice Hall, Upper Saddle River, NJ 07458. ©2003 Pearson Education, Inc.
52
-C4 (middle-C)
-28
27.
C3 B₂
Figure 3: Musical notation is a time-frequency diagram where vertical position indicates which note is to be
played. Notice that the shape of the note defines it as a half, quarter or eighth note, which in turn defines the
duration of the sound.
³If you have little or no experience reading music, don't be intimidated. Only a little music knowledge is needed to carry out this
lab. On the other hand, the experience of working in an application area where you must quickly acquire knowledge is a valuable
one. Many real-world engineering problems have this flavor, especially in signal processing which has such a broad applicability
in diverse areas such as geophysics, medicine, radar, speech, etc.
5