Lab 1
Lab 1
In this assignment, you are going to generate various sinusoidal signals and hear what si-
nusoids of different audible frequencies sound like, and experiment with some musical sounds.
We will extend our treatment of sinusoidal waveforms to more complicated signals composed
of sums and products of sinusoidal signals or sinusoids with changing amplitude, frequency,
or phase that are related to the basic sinusoid.
Table 1: Musical notes and the corresponding frequencies over one octave.
Take fo = 440 Hz and evaluate the signal’s defining formula at discrete instants of time.
These are called samples of the signal. In other words, discretize your signal at uniform
1
sampling intervals of Ts = 0.0001 s using x1 [n] = x1 (nTs ) over a duration of 3.0 s. Store the
values of x1 [n] in an array called x1. Plot x1 versus t over a duration of 0.01 s.
Type help plot in the MATLAB command window to see what you can do with the
plot command which is one of the vital commands of MATLAB. Also study the MATLAB
commands xlabel, ylabel, title, xlim, ylim, and grid. These commands are essen-
tial for producing professional looking plots in MATLAB. Make sure that you can properly
use these commands.
Turn on the speakers of your computer. Examine the sound(.) and soundsc(.) com-
mands of MATLAB by typing help sound and help soundsc in the MATLAB command
window. Notice that soundsc(.) requires the sampling rate (frequency) at which the signal
samples were created. Then, type sound(x1) or soundsc(x1). Listen to the sound. Repeat
the same for 2fo = 880 Hz and 4fo = 1760 Hz. What happens to the pitch of the sound as
the frequency increases?
The psychoacoustic properties of the musical scale are fascinating. The musical scale
is based on our perception of frequency and harmonic relationships between frequencies.
Frequencies that are harmonically related tend to sound good together. Among all the
harmonic relationships in the scale, A, C sharp, and E have among the simplest. This
possibly accounts for the predominance of the major triad in western music.
What about 440 × 3 = 1320 Hz? Notice that 1320/2 = 660 Hz, which is almost exactly
the frequency of note E. Thus, 440 × 3 is the note E, one octave above the E immediately
above A-440. E and A are harmonically related, and to most people, they sound good
together. It is because 440 × 3 ≈ 659 × 2.
Where does the C sharp come from in the major triad? Notice that 440 × 5 ≈ 554 × 4.
Repeat the above exercise for the sinusoids corresponding to the notes C sharp and E
and hear what these notes sound like.
Next, try combining A, C sharp, and E additively to create a major triad which can be
expressed as a sum of sinusoids:
Writing a single line of code, compute s(t) for 3.0 s and store it in an array named s.
Make a plot of the array s versus t and listen to s by the soundsc(.) command in the same
way as you did above for x1. If you have musical background, you will recognize this sound
as a major triad. What is special about this frequency combination?
For somewhat more arcane reasons, the interval between A and E, which is a frequency
rise of 3/2, is called a fifth. The note 3/2 above E has the frequency 988 Hz, which is an
octave above B-494. Another 3/2 above that is approximately F sharp (740 Hz). Continuing
in this fashion, multiplying frequencies by 3/2, and then possibly dividing by two, you can
approximately trace the 12 notes of the scale. This progression based on the choice of 12
evenly spaced notes corresponds to the so-called circle of fifths. The notion of key in music
and a scale are based on this circle of fifths.
Musical sounds such as chords can be characterized as sums of pure tones. Of course,
truly musical sounds are much more complex. For one thing, pure tones are not particularly
appealing sounds. Musical instruments produce notes that are more complex than pure
tones. The characteristic sound of an instrument is its timbre and some aspects of timbre
can also be characterized as sums of sinusoids.
2
Part 2: The Effect of Phase
Let x2 (t) = cos(2πfo t + φ) where fo = 587 Hz corresponding to the note D.
a) Let φ = 0. Compute, plot, and listen to x2 (t) in the same way as in Part 1.
b) Repeat for φ = π4 , φ = π2 , φ = 3π
4
, φ = π rad.
How does the volume of the sound that you hear change with φ? How does the pitch of
the sound that you hear change with φ?
Take a = 2 and fo = 440 Hz. Writing a single line of code, compute x3 (t) and store it
in an array named x3. In this code, make use of the element-wise multiplication facility
2
of MATLAB while computing the product of e−(a +2)t and cos (2πfo t). (Recall that in
MATLAB, element-wise multiplication of arrays is achieved by placing a dot in front of
the multiplication symbol. (You can examine the examples provided in the MATLAB
exercises on the course web page.) Provide this code in your report. Make a plot of x3
versus t and listen to x3 by the soundsc(.) command in the same way as you did in
Part 1. Compare your plot and what you hear to the results you obtained for x1 (t) when
fo is 440 Hz. What is the effect of including the exponential term to the sound that you
hear? Which one of x1 (t) and x2 (t) resembles the sound produced by a piano more? Which
one resembles that of a flute more? Now take a = 1, and recompute x3 (do not change fo
and t). Compare the sound you hear with that of the a = 2 case. Repeat for a = 3. How
does the duration of the sound that you hear change as a increases? Can you relate this
signal model to a physical system that we discussed in class that produces sinusoidal signals?
where f1 f2 . Take f1 = 10 Hz and f2 = 1000 Hz. Again using a single line command,
compute the array x4 (provide this code in your report), plot and listen to it in the same
way as in Part 1. Compare your results with those of x1 (t) in Part 1. What is the effect
of the low-frequency cosine term cos (2πf1 t) on the sound that you hear? Recompute x4
for f1 = 5 Hz and f1 = 15 Hz. How does the sound that you hear change? By using the
well-known trigonometric identity
1
cos(θ1 ) cos(θ2 ) = [cos(θ1 + θ2 ) + cos(θ1 − θ2 )],
2
express x4 (t) as the sum of two different-frequency cosine signals and also interpret what
you hear in this part using this equality. Picking two or three frequencies very close together
in the additive form of sinusoids generates beat signals, which is the case here.
Some musical instruments naturally produce beating notes. Musicians use this beating
phenomenon as an aid in tuning two instruments to the same pitch. When two notes are close
3
but not identical in frequency, the beating phenomenon is heard. As one pitch is changed
to become closer to the other, the effect disappears, and the two instruments are then “in
tune.”
Another use for multiplying sinusoids is modulation for radio broadcasting and com-
munication systems. For example, AM radio stations use this method, which is called am-
plitude (AM) modulation. This is the process of multiplying a low-frequency signal v(t)
by a high-frequency sinusoid. Thus, an AM signal is a product of the form:
where it is assumed that the frequency of the cosine term is much higher than any
frequency contained in the spectrum of v(t), which represents the voice or music signal to
be transmitted. The cosine wave is called the carrier signal and its frequency is called the
carrier frequency.
x(t) = A cos[ψ(t)]
The time derivative of the angle in the above equation gives a radian frequency changing
with time:
d
ωi (t) = ψ(t) (rad/s)
dt
but we prefer the cyclic frequency, therefore, we divide by 2π to define the instantaneous
frequency:
1 d
fi (t) = ψ(t) (Hz)
2π dt
A chirp signal is a sinusoid whose frequency is linearly swept so that it changes linearly
from a starting value to an ending one. The formula for such a signal can be obtained by
creating a quadratic angle by defining ψ(t) as:
The time derivative of ψ(t) yields an instantaneous frequency that changes linearly versus
time
fi (t) = 2µt + fo .
The slope of fi (t) is equal to 2µ and its intercept is equal to fo . If the signal starts at time
t = 0 s, then fo is also the starting frequency. The frequency variation produced by such
a time-varying angle is called frequency (FM) modulation. This kind of signal is an
example of a frequency modulated signal. More generally, we often consider them to be part
of a larger class called angle modulated signals. Finally, since the linear variation of the
frequency can produce an audible sound similar to a siren or a chirp, the linear-FM signals
4
are also called “chirps.” In nature, animals such as bats and dolphins produce chirp signals
in the ultrasonic frequency range for echolocation and communication.
To get a feeling about the physical implication of the linearly changing instantaneous
frequency, let us compute x5 (t) = cos(2πµt2 + 2πfo t + φ) and listen to it. As a test case,
generate a chirp signal whose frequency starts at 2500 Hz and ends at 500 Hz over a time
duration of 2.0 s. Use a sampling interval of Ts = 0.01 s. Calculate the value of µ. Listen
to the chirp using the soundsc(.) function. Then modify it so that the starting frequency
is 500 Hz and ending frequency is 2500 Hz for the same duration. Listen to the chirp using
the soundsc(.) function again. How is it different from the first chirp? What happens if
you double the µ coefficient? What happens if you halve it? Comment on the changes you
observe in your report.
A Chirp Puzzle
Synthesize another chirp signal with the following parameters:
A total time duration of 3.0 s, with a sampling interval of Ts = 0.01 s.
The instantaneous frequency starts at 3000 Hz and ends at –2000 Hz (negative frequency).
Listen to the signal. Does it chirp down, chirp up, or both? Use the theory of
the frequency spectrum (with its positive and negative frequency components) to help
explain what you hear. The changing instantaneous frequency implies that the frequency
components in the spectrum are moving.
Try to understand the code and try to compose a simple song. Try to add some different
sound effects on it. Have fun!
5
Note: In this assignment, you are not allowed to use symbolic operations in MATLAB.
To modify the styles of the plots, to add labels, and to scale the plots, use only MATLAB
commands; do not use the GUI of the figure windows. When your program is executed, the
figures must appear exactly the same as you provide in your solution. You need to write
your MATLAB codes not only correctly but efficiently as well.
Submit the results of your own work in the form of a well-documented lab report on
Moodle. Borrowing full or partial code from your peers or elsewhere is not allowed and will
be punished. Please include all evidence (plots, screen dumps, MATLAB codes, MATLAB
command window print-outs, etc.) as needed in your report. Append your MATLAB code
at the end of your assignment, do not upload it separately. The axes of all plots should be
scaled and labeled. Typing your report instead of handwriting some parts will be better. If
you decide to write some parts by hand, please use plain white paper. Please do not upload
any photos/images of your report. Your complete report should be uploaded on Moodle
as a single good-quality pdf file by the given deadline. Please try to upload several hours
before the deadline to avoid last minute problems that may cause you to miss the deadline.
Please DO NOT submit any hardcopies or files by e-mail or on memory stick/CD.
Please name the pdf file you submit on Moodle as follows using only lower-case English
characters for your first name, middle name (if any), and lastname. Please use your full
name as it appears on the Bilkent system.