Lab 3-Synthesis of Sinusoidal Signals and Sampling Theorem
Lab 3-Synthesis of Sinusoidal Signals and Sampling Theorem
Introduction:
In this lab, the periodic waveforms and music signals will be created with the intention of
playing them out through a loudspeaker, and then we will try to add other features to
1
synthesis in order to improve the subjective quality for listening. 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.
Theory of Sampling
Sampling is the process of converting an analog signal into a sequence of discrete values.
If done correctly, sampling does not introduce distortions into the system. The sampling
theorem states that when the sampling rate is greater than twice the highest frequency
contained in the spectrum of analog signal, the original signal can be reconstructed
exactly from samples.
The idealized process of sampling a signal and the subsequent reconstruction of the
signal from its samples is depicted in Figure 3. 1.
This figure shows a transformation or operation that acts on a continuous- time input
signal x (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 Ts is the
sampling period. The sampling rate is fs = 1/Ts where the units are samples per second.
The actual hardware systems that do sampling is an analog to digital (A-to-D) converter,
which approximate the perfect sampling of the C-to-D converter.
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 fmax in the input, i.e. fs >
2
2fmax. In other words, if sampling is fast enough then there will be no problems
synthesizing the continuous signals from x[n].
Most computers have a built-in analog-to-digital (A-to-D) converter and a digital-to-
analog (D-to-A) converter (usually on the sound card). These hardware systems are
physical realizations of the idealized concepts 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.
Aliasing
Analog signals contain frequency components that make up the characteristics of the
signal’s waveform. If these analog signals are sampled at low rate lower than the Nyquist
rate, the finer detail of the signal may be missed. Slow sampling tends to miss the high
frequency components while measuring only the signal’s lower frequency components or
inadvertently introducing nonexistent components. This is called aliasing error. Figure
3.2 is an example of aliasing phenomena, where the reconstructed signal built from data
sampled at a rate lower than the Nyquist rate of the original signal.
Many sound processing functions may be used for a windows PC. Some functions are
used in the playing of MATLAB vectors (matrices) through the PC sound board, while the
others are used to read and write wave files to and from the MATLAB workspace. Table
3.1 lists some of these functions.
3
Table 3.1: MATLAB Sound Functions
Sound (y,Fs) Convert a vector into sound and play at sampling rate
Fs. Amplitude values must lie between -1 and +1.
[y, Fs, nbits] Load windows wav format sound file into workspace
=wavread(fname) variable y. Also returns sampling rate and bits/sample.
From MATLAB, the sound output is done by the soundsc (xx,fs) function which 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 Ts =
1/11025 seconds; another common choice is 8000 samples/sec. Both of these rates satisfy
the requirement of sampling fast enough. Usually sampling rate should typically be
sampled at one of the standard Windows audio device rates: 8000, 11025, 22050, or
44100 Hz.
Analog audio is recorded by sampling it 44,100 times per second because the high
frequency limit of human hearing is about 20 kHz (the range of hearing of the human ears
is roughly 20 Hz to 20,000 Hz), and the sampling theorem state that the sampling
rate must be twice of the maximum frequency so the sampling rate had to be at least
40 kHz.
If you are using soundsc(xx,fs ), the vector xx will be scaled automatically for the D-to-A
converter, but if you are using sound(xx,fs ) , you must scale the vector xx so that it lies
between ±1.
4
Procedure
Task one
2. Use soundsc (x1,fs) to play the resulting vector through the D-to-A converter of
your computer, assuming that the hardware can support the fs = 11025 Hz rate.
Listen to the output.
3. Now create another vector x2 of samples of a second sinusoidal signal (0.5 secs. in
duration) for this case A2 = 80, ω1 = 2π (1200), and Φ1= − π /4. Listen to the
signal reconstructed from these samples. How does its sound, compare it with the
signal in step 2?
4. Add x1 to x2 to create x3, Listen to the signal reconstructed from these samples.
How does its sound compare it with the signal in step 3?
5. Concatenate the two signals x1 and x2 with a short duration of 0.2 seconds of
silence in between. You should be able to use a statement something like:
xx = [ x1, zeros(1,N), x2 ] ;
Assuming that both x1 and x2 are row vectors. Determine the correct value of N
to make about 0.2 seconds of silence.
6. Verify that the concatenation operation was done correctly in the previous part by
making the following plot:
tt = (1/11025)*(1:length(xx)); plot( tt, xx );
5
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.
8. 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 re-compute
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.
Task Two:
3. Plot the sampled signal, the reconstructed signal and the signal plotted in step 1 in
the same graph (x axis should be from 0 to 2), use stem command for sampled
signal. Make sure to label x and y axis and title of the figure.
4. Repeat step 2 and 3 with 21 Hz sampling frequency , Compare the graphs with
what you get in step 3 ,what do you note ?
6
Lab Exercise
2. Complex amplitudes for the periodic signal that approximate the waveform
produced by a man speaking the vowel sound “ah “ with 100 Hz fundamental
frequency are represented in Table 3.2 . Create a MATLAB code that generate x2
and add x2 to the 400 Hz cosine signal to represent x4 , then add x4 to the 500 Hz
cosine signal to represent x5 and so on .Your code must do the following :
b) Listen to the sound that is generated from x2 , x4 ,x5, x16 and x17 and compare
the generated sound , What do you note ?
c) Listen to these signals with a short duration of 0.2 seconds of silence in between
each one as the following form:
d) Verify that the concatenation operation was done correctly in the previous part,
make the following plot:
tt = (1/fs)*(1:length(xx)); plot( tt, xx )
Table 3.2
K fk (Hz) Ak Mag Phase
1 100 0 0 0
2 200 386+j 6101 6113 1.508
3 300 0 0 0
7
4 400 -4433+j 14024 14708 1.877
5 500 24000-j4498 24418 -0.185
6 600 0 0 0
. . . . .
. . . . .
15 1500 0 0 0
16 1600 828- j6760 6811 -1.449
17 1700 2362+j0 2362 0
Sources