Sei sulla pagina 1di 16

The Hong Kong University of Science and Technology

Department of Electronic and Computer Engineering

ELEC 1200
A System View of Communications: From Signals to Packets

Lab #7: Time-Frequency Analysis of Signals

Goals:
1. To study signals in both the time and frequency domains.
2. To learn the sinusoidal representation of sampled data.
3. To learn how to approximate a signal and compare it with its original one.
4. To study the frequency responses of our IR channel.

Instructions:
Before your lab session:
1. Complete the pre-lab exercises. You need to submit the pre-lab exercises individually (hardcopy) to the TA
in the first 10mins after the lab starts. Include the following header:

ELEC1200 Prelab7
Name: __________ SID: __________ LAB session: __________ Group number: __________

2. Read over the lab manual before coming.

During your lab session


1. Complete the lab activities and get check-off by TA as stated in the lab manual. Let the TA sign on the
Check-off sheet (the last page of the lab manual).
2. Attend the Post Lab interview before the lab ends.

Lab7-P.1
Overview
Speech sounds are commonly classified as two types, voiced and unvoiced. Voiced speech is associated with
vowels, such as the ah sound in mama. Unvoiced speech is associated with consonants, such as the s in
Sam. In this lab, we will use the Fourier amplitude spectrum to visualize and analyze differences in speech
sounds.

The production of voiced speech signal starts in the larynx, which contains two vocal cords. When tightened,
they can restrict the flow of air. When we breathe, they are relaxed, allowing air to flow in and out of our lungs
freely. However, when we speak, we tighten them so that the air comes out in short periodic puffs. The period
of these puffs determine the pitch of the speech. Singers control their vocal tracts to produce different notes
by tightening or loosening their vocal tracts to produce higher or lower notes. As these puffs of air flow
through the vocal tract, the sound is further shaped by changes in the shape of the vocal tract that are caused by
changing the position of our tongue, teeth and lips. For example, the shape of your mouth is different when you
say ah as in mama or ee as in ECE.

The Human Vocal Tract (yellow area); from http://www.indiana.edu/~hlw/PhonUnits/vowels.html

On the other hand, unvoiced speech is typically produced with the vocal tracts relaxed, so that air is blown
through the vocal tract. Like voiced speech, the sound is altered by the shape of the vocal tract. For example,
consider how the position of your tongue changes when you say the consonant s as in Sam versus sh as
in ship. Sometimes unvoiced speech is formed by the sudden release of air, such as when we say b as in
boy or d as in dog.

We can visualize a recorded speech signal using the amplitude spectrum computed from the Fourier series
expansion of the speech signal. Any arbitrary signal x[n] containing N samples can be represented as a sum of
N/2 sinusoid waves plus a constant using the equation
N

x[n] A0 Ak cos(2 f k n k )
2

(1)
k 1

k
where we have assumed that N is even, and f k is the normalized frequency, which has units of cycles per
N
sample. Each element of the summation is called a frequency component. Note that for all signals with N
samples, the values of the frequencies f k are the same.

Lab7-P.2
The values of Ak and k are different for different signals. The collection of values of Ak for k running from 0
to N 2 is known as the amplitude spectrum. The collection of values of k for k running from 0 to N 2 is known
as the phase spectrum. The first term, A0, is known as the DC component, and determines the average signal
level.

To find the amplitude and phase spectra for an arbitrary signals contained in a MATLAB vector x, we have
provided a function called fseries.m via LMES. The function declaration of this function is

function [A,phi] = fseries(x)

This function takes signal stored as the N dimensional vector x and returns the amplitude and phase spectra of
the signal as two N 2 1 dimensional vectors A and phi. Due to the way MATLAB indexes vectors (starting
from 1, not 0), the value of A0 is stored as the MATLAB vector element A(0). In general, the value of Ak in
equation (1) is stored in MATLAB as A(k+1). Similarly, the value of 1 in equation (1) is stored in MATLAB
as phi(2). The value of phi(0) returned by the function is always zero.

Here is an example of how to use the function fseries.m

N = 64; % use even number of samples


n = 0:(N-1); % vector of sample indices

% a simple sine wave of frequency f and amplitude 1


f = 4/N; % frequency index k=4
x = sin(2*pi*f*n);

% compute Fourier series expansion


[A,phi] = fseries(x);

figure(1);

% plot signal x
subplot(3,1,1);
plot(n,x);
ylabel('signal'); xlabel('n');
axis([0 N min(x) max(x)]);

% plot amplitude spectrum of x


subplot(3,1,2);
k = 0:(N/2); % vector of frequency indices
stem(k,A);
ylabel('amplitude spectrum'); xlabel('k');
axis([0 N/2 min(A) max(A)]);

Lab7-P.3
Running this script will produce the plots shown below.
1

signal
0

-1
0 10 20 30 40 50 60
n

amplitude spectrum
0.8
0.6
0.4
0.2

0 5 10 15 20 25 30
k

To verify that the amplitude 1and phase spectra are correct, we note that since all of the frequency components
reconstructed

have zero amplitude except for


0
the component at k = 4, we can rewrite equation (1) as
4
-1
x[n] A4 *cos(2 n 4 ) (2)
0 10 20 30 6440 50 60
where we have used the fact that the number of samples n N = 64. The following MATLAB commands to
reconstruct the signal x from the amplitude and phase spectra, and plot the reconstructed signal xr. Note that
we use the 5th element of the vectors A, k and phi, due to the way MATLAB indexes vectors as described
above.

xr = zeros(1,N);
xr = A(5)*cos(2*pi*n*k(5)/N+phi(5)); % plot reconstructed signal xr
subplot(3,1,3);
plot(n,xr);
ylabel('reconstructed'); xlabel('n');
axis([0 N min(xr) max(xr)]);

If we run this code, we produce the following plots. Note that the original signal and the reconstructed signal
are identical, even though the original signal was generated with a sine function, but the reconstructed signal
was generated by a cosine function. Recall that sin( x) cos( x 2) . If we check phi(5) in MATLAB, we can
see that it has value -1.5708, which is 2 .
1
signal

-1
0 10 20 30 40 50 60
n
amplitude spectrum

0.8
0.6
0.4
0.2

0 5 10 15 20 25 30
k
1
reconstructed

-1
0 10 20 30 40 50 60
n

Lab7-P.4
Pre-lab7 Exercises:
The following Pre-lab exercises give you some practice in using MATLAB to compute and plot signals and
their amplitude spectra. When turning in your work, please include the Matlab code you ran, copies of the
figures generated, and the numerical answers to the questions below. Zero score for hand drawn figures. Hint:
You can accomplish these tasks by modifying the MATLAB code given in the Overview section.

Q1. Consider the following signal


4 8
x[n] 4 sin 2 n 2 cos 2 n
N N
(a) Use MATLAB to plot the signal x for n running from 0 to 63.
(b) Use the function fseries.m to obtain the amplitude spectrum of x.
(c) Plot the amplitude spectrum versus the frequency index, k.
(d) Which frequency components have non-zero amplitude? (Give their values of k).
(e) Write down the corresponding amplitude and phase for each value of k given in (d).
(f) Use MATLAB to reconstruct the signal from its amplitude and phase spectra. Store the reconstructed signal
as the vector xr and plot it versus n.

Q2. Consider the following signal:

x = square(2*pi*3/N*n); % x is a square wave

(a) Use MATLAB to plot the signal x versus n from 0 to 63.


(b) Find and plot the amplitude spectrum of x.
(c) Reconstruct the signal from its amplitude and phase spectra using the equation

N
k
x[n] A k cos 2
2

n k
k 0 64
Remember that MATLAB indexes vectors starting from 1, not 0. Store the resulting signal as the vector xr and
plot it versus n. Hint: You may find it useful to use a for loop here.
(d) From visual inspection of the graph, which are the three frequency components with the largest amplitude?
(Give their values of k) Hint: you may use sort function described on page 9.
(e) Plot the signal reconstructed by taking only the three frequency components with the largest amplitude. In
other words, if the three frequency components you found in part (d) were k = u, v and w, then the reconstructed
signal is
u v w
y[n] Au cos 2 n u Av cos 2 n v Aw cos 2 n w
64 64 64
Again, in computing this vector in MATLAB remember that vector indices start from 0 not 1.

Lab7-P.5
Lab Activities

Part 1: Time-Frequency Analysis of Speech

Over long time frames, speech signals do not appear periodic, and their characteristics vary widely. However,
over short time periods, speech signals, especially for voiced speech, appear almost periodic. Thus, to analyze a
speech signal using the Fourier series expansion, we break the speech signal into a set of short segments, called
frames, which are typically less than 100ms long. Within each of these frames, the speech signal can often be
well approximated by only a few frequency components.

In this part, we will be examining the waveforms and amplitude spectra of frames taken from a speech
waveform.

1. Record a speech waveform using the following MATLAB commands:

Fs = 11025; % sampling frequency


T = 2; % length of speech waveform to record in seconds
x = record_speech(T,Fs);

The function record_speech records T seconds of speech using a sampling frequency of Fs. The function
record_speech will guide you what to do. When prompted, record the two words ma and me separated
by a short pause.

2. Plot different frames using the function plot_frame.

Determine the frame length (the number of samples in one frame), by finding an integer that satisfies the
following two conditions:
The frame length in samples is is a power of two, i.e. it is 2x samples, where x is an integer to be
determined.
The frame length in seconds is about 50ms long. For this, you will need to use the value of the sampling
frequency.

Store the frame length in MATLAB as variable called frame_length

Run the function plot_frame to visualize the entire speech waveform and one of the frames:

plot_frame(x,frame_length);

Lab7-P.6
When you run this program, you should see plots similar to those shown below

The first subplot plots the entire speech waveform, x. You can see two large amplitude regions separated by a
low amplitude space, corresponding to the two words ma and me. The second subplot shows the signal
power in each frame. The red line indicates the frame with the largest power. The final subplot shows the
waveform from the frame with the largest power. The two vertical red lines in the first subplot show the start
and end of that frame.

If your plots do not look similar to the figure above, record another speech waveform and try again. Note that
you can play back your speech signal by saving it to a file called mema.wav using the MATLAB command

wavwrite(x, Fs, 16, mema.wav);

Use the program Application Sound & Video mhWaveEdit to listen to this file.

Run the plot_frame function again on the same speech waveform x, but looking at different frames in the
bottom subplot. You can change the frame plotted in the bottom subplot by calling plot_frame with an
additional variable, nf, which is the index of the frame you want to look at:

plot_frame(x,frame_length,nf);

Lab7-P.7
Create three figures showing frames in
the ma region
the silence region between ma and me
the me region

For example, if frame 9 is in the ma region, frame 23 is in the silence region, and frame 32 is in the me
region, and you can use the commands

figure(1); plot_frame(x,frame_length,9);
figure(2); plot_frame(x,frame_length,23);
figure(3); plot_frame(x,frame_length,34);

Record the frame numbers you used. You will need them in the later part of the lab.

me region silence region ma region

nf

*******TA Check 1: Demonstrate your work to the TA ******


Tell the TA the frame length you used, play your recorded waveform and show the TA the three figures
you have created.

3. Compute and plot the amplitude spectrum of a frame. (plot_ampspec.m)

In the lab directory, you will find a partially written function, plot_ampspec.m. Your task is to complete
this function. When complete, the function generates a figure with two subplots as shown below.

The top subplot shows one frame of a speech waveform (similar to the bottom subplot in the figure generated
by the function plot_frame). The second subplot shows the amplitude spectrum of that frame. In the figure
above, the frame was taken from a region containing voiced speech, so it looks periodic. The corresponding
amplitude spectrum displays many sharp peaks. The four largest peaks are marked by red s. These
correspond to the four most significant frequency components.

Lab7-P.8
The function has the following declaration:

function [ksig,Asig,phisig] = plot_ampspec(x,frame_length,nf,noplot)

It takes as input the following variables:


x, a vector containing a recorded speech signal
frame_length, a variable containing the frame length as computed above
nf, the index of the frame from x to be plotted in the top subplot
noplot, a variable that when set equal to 1 suppresses the generation of plots

It returns three output variables


ksig, a 4-dimensional vector containing the frequency indices of the four frequency components that
have the largest amplitudes (i.e., the most significant frequency components). These determine the
horizontal locations of the red crosses in the bottom subplot.
Asig, a 4-dimensional vector containing the corresponding amplitudes. These determine the vertical
locations of the red crosses in the bottom subplot.
phisig, a 4-dimensional vector containing the corresponding phases (not plotted)

As mentioned above, the function plot_ampspec.m is only partially written. Your job is to complete this
function by filling in the missing code in the section shown below:

%----Missing part---to be done by you----%%--see more below--%%


% First, compute the amplitude/phase spectrum of the frame,
% assign A = amplitude spectrum, phi = phase spectrum
% Then, compute the frequency, amplitude and phase of the
% four most significant frequency components and store it as
% ksig = frequency indices (1x4 vector)
% Asig = amplitudes (1x4 vector)
% phisig = phases (1x4 vector)

%
% LEAVE THE REST OF THIS FUNCTION BELOW UNCHANGED
%

In particular, you should:

a. Use the function fseries.m (used in the prelab) to find the amplitude and phase spectra of the frame. Store
these in MATLAB variables A and phi.

b. Find the frequency indices of the four components with the largest amplitudes. For this, the following
MATLAB command should be useful:

[B,I] = sort(A,1,descend)

This command returns the values of the vector A sorted in descending order from largest to smallest as the
vector B, and their corresponding indices in A as the vector I.

c. Store the frequency indices, the amplitudes and the phases of these components in the 4-dimensional vectors
ksig, Asig and phisig, where entries are sorted so that the first element corresponds to the most significant
frequency component, the second to the second most significant, and so on. In finding the frequency indices,
remember that MATLAB array indices start from 1 not 0.

Lab7-P.9
Once you have finished your function, run it three times using the values of nf you recorded in above.
Generate three different figures, and compare them. How do the amplitude spectra for the different sounds
differ? Show the resulting plots to the TA.

*******TA Check 2: Demonstrate your work and show result to TA ******

4. Approximating Speech Signals (plot_approx.m)

Equation (1) states that any signal of N samples can be expressed exactly as the sum of N/2 cosines plus a
constant. Each cosine is known as a frequency component. In this section, we will approximate one frame of
the speech waveform using only a few of the most significant frequency components.

The lab directory contains a partially written function called plot_approx.m, your task is to complete this
function. When run, the finished function should plot one frame of a speech signal and show three
approximations to the speech signal, which are generated using one, two and four largest frequency
components, as shown in the figure below.

In each of the three subplots, the original speech waveform from the frame is shown in blue. The approximation
is shown in red. The approximation in the first sub plot is created by taking only the largest frequency
component, the approximation in the second plot by summing the largest and the next largest frequency
components, and the approximation in the bottom plot by summing the four largest frequency components.

The approximation gets better as the number of frequency components used increases. This can be seen by
visually comparing the waveforms, and numerically by comparing the Normalized Mean Squared Error
(NMSE) between the original and the approximate waveform. We defined the NMSE in lab 2. The NMSE is
shown in the title of each subplot, where lower NMSE indicates a better approximation.

Lab7-P.10
The function declaration of plot_approx is shown below.

function plot_approx(x,frame_length,nf)

The function takes three input arguments


x, a vector containing a recorded speech signal
frame_length, a variable containing the frame length in samples
nf, the index of the frame from x to be approximated

Your job is to complete the function plot_approx by filling in the missing code in the section shown below:

%----Missing part---to be done by you----%%--see more below--%%


% Using values of ksig, Asig and phisig
% to obtain approximate waveforms of a frame
% fa1 = approximation based on 1 cosine
% fa2 = approximation based on 2 cosines
% fa4 = approximation based on 4 cosines

%
% LEAVE THE REST OF THIS FUNCTION BELOW UNCHANGED
%

In particular, the code prior to this section uses the function plot_ampspec you worked on in the previous
section to find the vectors containing the frequency indices (ksig), the amplitudes (Asig) and phases
(phisig) of the most significant frequency components in the frame nf. Each of these vectors is four
dimensional, and is ordered from most significant to least significant.

Using these vectors, your added code should generate three approximations to the waveform in frame nf using
the one, two and four most significant frequency components, and assign these approximations to the
MATLAB variables fa1, fa2, and fa4. For example, the first approximation can be found by

fa1 = Asig(1)*cos(2*pi*ksig(1)*n/frame_length + phisig(1));

where the vector n = 0:frame_length-1 was defined in the code above this section. Recall that the frame
contains frame_length samples.

Once you have completed the function plot_approx.m, run it three times with the three nf values you used
in the previous section for frames in
the 'ma' region.
the silence region between 'me' and 'ma'
the 'me' region.

Create three different figures, and show the plots to TA.

*******TA Check 3: Demonstrate your work and show result to TA ******

Lab7-P.11
5. Perceptual Quality of Sinusoid Approximations (approximate.m)

We have seen that better approximations to the speech signal can be achieved by including more frequency
components. In this section, we compare the perceptual quality of these approximations.

We have provide you with the function approximate.m in the lab7 folder. This function has declaration

function x_app = approximate(x,frame_length,numcom)

This function takes a speech waveform stored in the vector x and generates an approximation to the speech
waveform by breaking the waveform into frames of frame_length samples, and approximating each frame
using the numcom most significant frequency components.

We obtain an exact replica of the original speech signal if we set numcom = frame_length/2 + 1. In other
words, if we run the following MATLAB commands,

numcom = frame_length/2+1;
x_app = approximate(x,frame_length,numcom);

then x_app will be the same as x. Verify this by saving x and x_app to files using the following commands:

wavwrite(x, Fs, 16, x.wav);


wavwrite(x_app, Fs, 16, x_app1.wav);

Compare the two waveforms by playing them both back using the function mhWaveEdit used previously. They
should sound the same.

Then, created approximations using fewer and fewer frequency components (set numcom equal to a value less
than frame_length/2+1. Listen to the resulting approximations. At what value of numcom do you begin to
hear differences between the original waveform and the approximation? (Usually this will be much smaller
than frame_length/2+1.) At what value of numcom does the the speech becomes unintelligible (you cannot
understand it).

*******TA Check 4: Demonstrate your work and show result to TA ******


Play the original waveform, the approximation where you begin to hear differences, and the
approximation that can no longer be understood.

Lab7-P.12
6. Speech intelligibility test

In this part, we will study how many frequency components are necessary in order for someone to understand a
reconstructed speech signal.

One lab partner (the recording partner) should use the record_speech function to record a 2 second long
speech waveform. Use the same sample frequency Fs as before. However, do not let the other partner (the
listening partner) hear what you are recording.

Obtain an approximation to the speech waveform by using approximate.m with numcom = 1. Use the same
value of frame_length as before.

Save the approximation to a file and play it back to the listening partner. See if he/she can understand it.

Increase the number of frequency components (numcom) in the approximation one by one until the listening
partner can understand what was said. How many components were necessary? How many components were
needed for the recording partner to think that the speech was understandable?

recording partner listening partner


number of components
needed

You will probably find that the number of components needed for the recording partner to think the speech was
understandable to be lower than that required by the listening partner. This is because the recording partner
already knows what was said, and his/her brain subconsciously fills in the missing frequency components.

*******TA Check 5: Demonstrate your work and show result to TA ******


Let TA listen to the approximate speech waveforms for the two numcom values in the table.

Part 2: Frequency Response of the IR channel


In this part, we will measure the amplitude of the frequency response of the IR channel, both with and without
equalization. Recall that the IR channel can be modelled as a linear and time invariant (LTI) system. From the
lecture notes, we know that when a cosine is input to an LTI system, the output is a cosine with the same
frequency, but possibly scaled in amplitude and shifted in phase. The amplitude scaling and the phase shift
usually varies with the frequency of the input cosine. The function mapping the input frequency to the
amplitude scaling is known as the amplitude of the frequency response, and is what we will be measuring here.

Lab7-P.13
1. Frequency response without equalization (run_part2_ir.m)

In order to measure the amplitude of the frequency response, we will use cosine waves with the same amplitude
but different frequencies as input to the IR channel and measure the peak to peak amplitudes of the resulting
output. The peak to peak amplitude of a signal is the difference between the maximum value and the minimum
value. If the input amplitude is kept constant, then a plot of the peak to peak amplitude of the output as a
function of the frequency will be proportional to the amplitude of the frequency response

In the lab7 folder, we have provided a script called run_part2_ir.m that does this. Its code is shown below.

figure(1); count=0; count_plot=1;


for f=[1e3 3e3 7e3 1e4 2e4 3e4 4e4 5e4 7e4 1e5 2e5] %different freq.
[peak2peak, signal_max, signal_min, rx_wave] = frequency_response_ir(f);

if (mod(count,2)==0)
subplot(6,1,count_plot);
plot(rx_wave);
axis([1 8000 signal_min signal_max]);
title(f);
count_plot=count_plot+1;
end

ir(count+1)=peak2peak;
count=count+1;
end

figure(2); title(Frequency response of the IR channel);


f=[1e3 3e3 7e3 1e4 2e4 3e4 4e4 5e4 7e4 1e5 2e5];
x=log10(f);
plot(x,ir); % plot peak2peak

This script needs a function called frequency_response_ir. This function is provided for you, but is only
partially complete. Your task is to complete this function, whose original code is shown below.

function [peak2peak,signal_max,signal_min,rx_wave]=frequency_response_ir(frequency)

t=0:1e-6:0.01; % time index of tx_wave

%%-------missing part-------write you codes----%%


% tx_wave is a sine wave with amplitude 1,its frequency from the input
% argument 'frequency', zero phase.

tx_wave = ???; % put down the general expression

% transmit and receive signal


[rx_wave, rx_max, rx_min, signal_max, signal_min] = channel_lab7(tx_wave);

%% compute largest peak to peak amplitude of rx_wave, use rx_min and rx_max
peak2peak = ???; % the largest peak to peak in rx_wave

%-------------

This function takes in a single input argument frequency, which specifies the frequency of the sine wave to
be sent over the channel (note that a cosine wave will also work). Your task is to write the code that creates this
cosine wave and stores it in the vector tx_wave, and the code that extracts the peak to peak amplitude of the
received waveform rx_wave.

Lab7-P.14
Once you have completed the function frequency_response_ir, run the script run_part2_ir.m, and
show the resulting frequency response to the TA.

*******TA Check 6: Demonstrate your work and show result to TA ******

2. Frequency response of the IR channel with equalization (run_part2_eq.m)

In this section, we will measure the amplitude of the frequency of the IR channel followed by equalization. In
order to do this, you should modify the functions used in the previous section.

Copy frequency_response_ir.m and save it as frequency_response_eq.m in the lab7 folder

Modify the file frequency_response_eq.m to add the equalizer after the signal has passed through the
channel. To help you, we have provided the function equalizer.m in the lab7 folder. This function
implements the equalizer, and has already been written. You only need to call this function. Its function
declaration is given by

function [rx_wave, rx_max, rx_min, signal_max, signal_min] = equalizer(x,a)

where x is the input waveform (which should be the output of the channel), and a is the base of the exponential
in the step response of the channel (which should be set to 0.93). The waveform after equalization is given by
rx_wave. After adding the equalizer, make sure that the value of peaktopeak is correctly calculated, so that it
is the peak to peak amplitude of the received waveform after equalization and that the rx_wave returned by
frequency_response_eq is the received waveform after equalization.

Copy run_part2_ir.m and save it as run_part2_eq.m in the lab7 folder.

Modify run_part2_eq.m as necessary so that it plot the received waveforms and the peak to peak amplitude
for the received waveform after equalization. Update the plot title as well.

Once you are done, compare the plots of the frequency response of the channel with and without equalization.
What you observe? Explain it to TA.

*******TA Check 7: Demonstrate your work and show result to TA ******

Lab7-P.15
LAB7 TA Checkoff:
Group: ______________
Name1:______________ StdID1:______________.
Name2:______________ StdID2:______________.

Part 1
Check1: TA check & signature: ______________.
Check2: TA check & signature: ______________.
Check3: TA check & signature: ______________.
Check4: TA check & signature: ______________.
Check5: TA check & signature: ______________.

Part 2
Check6: TA check & signature: ______________.
Check7: TA check & signature: ______________.

Lab7-P.16

Potrebbero piacerti anche