Académique Documents
Professionnel Documents
Culture Documents
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 Veri cation 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 veri cation, simply demonstrate the step to the TA or instructor. Turn in the
completed veri cation sheet to your TA when you leave the lab.
Lab Report: It is only necessary to turn in a report on Sections 4 and 5 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 is built around a single project that involves the implementation of a system for automatically
writing a musical score by analyzing the frequency content of a recording (a sampled signal). A primary
component of such a system is the spectrogram which produces a time-frequency representation of the
recorded waveform. However, to make a working system, several other processing components are needed
after the spectrogram to extract the important information related to the notes. The design of these additional
blocks will lead naturally to a deeper understanding of what the spectrogram actually represents.
2 Overview
In Chapter 13 we introduced the spectrogram as an important tool for time-frequency analysis. For mu-
sic signals the spectrogram tends to produce an image with only a few peaks. Finding these peaks and t
h
k
n
identifying their frequencies and durations is the main issue in this lab. A M ATLAB GUI for showing the
spectrogram along with musical notation is available for experimentation. CD-ROM
In order to make the project manageable, we will progress through several different signal types while Music
GUI
3. Sinusoids that create the tune for Twinkle, Twinkle Little Star.
5. Other recorded songs are available for processing: Jesu, Joy of Man’s Desiring, Minuet in G, Beethoven’s
Fifth, and Für Elise.
is the following:
spectgr.m
where xx is the input signal, Nfft is the FFT length, fs is the sampling frequency, window is a column
vector containing the coef cients of the window, and Noverlap is the number of points in the overlapped
part of consecutive sections.
The outputs are the spectrogram matrix B, a vector T containing the time locations of the windowed
segments,1 and a vector F which contains the list of scaled frequencies corresponding to the spectrogram
analysis. The vectors T and F are useful for labeling plots. Both are scaled by the sampling frequency so the
units are seconds for T, and hertz for F. The spectrogram B contains complex values and its size is such that
it has a column length (number of rows) equal to length(F) and a row length equal to length(T). In
the SP First implementation, the calling program must provide all of the arguments. The M ATLAB function,
on the other hand, allows the caller to omit arguments, but that just adds complexity in the programming.
In this section, we will present the steps in a spectrogram calculation, so that you could write your own
function. The preferred viewpoint for calculation is that of a sliding-window FFT. In this implementation,
we take a segment of the signal of length L, multiply the segment by a window, and then compute a zero-
padded N -point FFT to form one column of the spectrogram matrix. Then the starting point of the data
segment is moved over by an amount L − Noverlap and the process is repeated. Eventually, we run out of
data and the spectrogram is complete.
For a M ATLAB program, we need to write a while loop that tests whether any signal remains. The
following example shows all the code needed for the inner loop:
B = zeros( Nfft/2 + 1, num_segs ); %- Pre-allocate the matrix
L = length(window) %-- assuming a user generated window
iseg = 0;
while( ) %<==== FILL IN THE TEST CONDITION
nstart = 1 + iseg*(L-Noverlap);
xsegw = window .* xx( nstart:nstart+L-1 ); %-- xx is a column
XX = fft( xsegw, Nfft );
iseg = iseg + 1;
B(:,iseg) = XX(1:Nfft/2+1);
end
Explain how each of the steps in the spectrogram are being calculated. Explain how to calculate the number
of segments num segs ahead of time. Also, explain the purpose of the last line in the while loop. And
1
There are several conventions for de ning the time: (1) start of the segment, (2) middle of the segment, or (3) end of the
segment. The middle choice probably makes the most sense, but it really doesn’t matter in this project because only relative times
will be signi cant.
details can be seen, but the conventional printout is a gray scale image with black indicating large values.
show img.m
In addition, if the gray level is proportional to the magnitude of B, small details may be lost, so it might be
advantageous to convert to a logarithmic scale covering 30 or 40 dB (called “log mag”). Finally, the default
orientation in M ATLAB is a matrix orientation with the origin in the upper left-hand corner. To change this
orientation so that the origin is in the lower left-hand corner, use axis xy. The following code fragment
summarizes the display:
A one-dimensional peak-picker is available in the function pkpick.m whose help comments are CD-ROM
given below:
pkpick.m
Test that pkpick.m works as you expect by generating a cosine wave and nding its peaks.
Instructor Verification (separate page)
An unexpected problem with peak picking is the quantization of the frequency axis. The peak-picking
function will give an output that is on the grid of possible frequencies. If we need to estimate the peak
location between these grid points, interpolation is needed. The function pkinterp.m is available for that
purpose.
SHEET
x[n] IMAGE Peak LIST Editing & KEY # Write MUSIC
Spectrogram Picking Merging Notes
Figure 1 shows the major sub-systems needed to extract enough information from a musical recording to
write the sheet music for that input. Each of these should be implemented as a separate M ATLAB function.
Overlap: Determine the time spacing needed to nd the duration of notes. Be careful when making the
time spacing very small because the amount of computation will increase dramatically.
1. Frequency:
(a) How close is the frequency to one of the allowable frequencies of the piano keys.
(b) The harmonics must be eliminated, but there are cases where an octave is played, so the second
harmonic might be allowed. In addition, when the song has both bass and treble sections, note
frequencies can be 4 or 8 times each other.
2. Time:
(a) Check the duration; is it a half note, quarter note, etc? This requires that peaks be merged and
tracked along the time axis.
(b) Timing of the notes. We expect the notes to start at regular times because the music has a rate,
such as 2/4 time, or 4/4 time.
(c) In fact, an interesting sidelight project would be to extract the “beat” of the music. This might
help to establish a time base for the song, and help set the parameters of the expected durations.
(d) There is a minimum duration unless we have a piece with lots of special effects, trills, grace
notes, etc.
3. Amplitude:
wrinotes.m
3. Sinusoids that create the tune for Twinkle, Twinkle Little Star.
In each case, you know what the true answer should be, so you can assess the capabilities of your music
writer.
All the piano songs are sampled at 11.025 kHz. Alternate songs are: Jesu, Joy of Man’s Desiring, Minuet
in G, Beethoven’s Fifth, and Für Elise. Each of these will be quite dif cult and challenging, unless your
editing logic is very sophisticated.
Remember that the objective of the lab is to make a working system containing the major components
listed in Fig. 1. Even with a few simple tests, you should learn quite a bit about the spectrogram, its strengths
and its shortcomings.
Name: Date: