Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lab 3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

SYSC 3500 Signals and Systems Fall 2010

Lab #3 Frequency Analysis and Manipulation using MATLAB

Objectives:
• Learn how to transform a discrete time signal into the frequency domain.
• Learn how to extract useful information from the Discrete Fourier Transform (DFT).
• Learn how to reconstruct a discrete time signal using the inverse DFT (IDFT).

Part I: The basics of analog to digital conversion and processing in MATLAB.


1. Digital circuits and computers dominate our modern world. Any digital system that
communicates or functions in the real world must perform some level on analog to digital
conversion. Much of the theory behind analog to digital conversion are beyond the scope of
this lab, but if we are to use a tool such as MATLAB for signal processing, some basics need
to be addressed before we start answering lab questions. Consider the diagram below:

x(t ) CT to DT x[n]

Ts
The diagram illustrates a system that converts a continuous time (CT) signal to a discrete
time (DT) signal. This diagram represents much of what you were doing in Lab #1. This
system samples the input signal, x(t ) , every Ts seconds. The sampling frequency, f s = 1 Ts
, always has to be considered when converting from continuous time to discrete time and vice
versa. We will discuss this in more detail soon.

2. Let's start with an example signal, x(t ) = cos(4π t ) , which is a cosine wave with a frequency
of 2 Hz, as shown below:
1

0.8

0.6

0.4

0.2
x(t)

-0.2

-0.4

-0.6

-0.8

-1
0 0.5 1 1.5 2 2.5 3
t (seconds)
We want to sample this signal with a sampling frequency of f s = 30 Hz. Start a new
MATLAB m-file and place the following code at the top. You can use comments (green text)
as you see fit so that longer m-files don't get confusing.

% Number of points in index values for discrete signal.


N = 90; % number of samples
n = 0:1:N-1; % indices of samples
% We will sample a continuous time signal, x(t), at a rate of 60 Hz.
fs = 30; % sampling frequency (Hz)
Ts = 1 / fs; % sampling period (seconds)
% Values of time when we sample x(t)
t = n*Ts;
% Create x[n]
x_n = cos(4*pi*t);
% Plot x[n]
figure(1);
stem(n, x_n);
xlabel('n');
ylabel('x[n]');

Save your m-file and then run it by pressing F5. The following plot will be produced:
1

0.8

0.6

0.4

0.2
x[n]

-0.2

-0.4

-0.6

-0.8

-1
0 10 20 30 40 50 60 70 80 90
n

We can see right away that x[n] is a DT version of our input signal. Up to this point in the
course we have looked at some different ways to inspect a signal in terms of its frequency.
We will use the Discrete Fourier Transform (DFT) to view x[n] in the frequency domain.
Enter the following code into your m-file:

% Create values for k


k = (0:N-1)-N/2;
% Get the DFT of x[n]
X_k = fft(x_n, N);
% Need to fftshift in order to plot X[k] for k= [-N/2 to (N/2)-1]
X_k = fftshift(X_k);
% Plot the magnitude and phase of X[k]
figure(2);
subplot(2,1,1);
stem(k, abs(X_k));
xlabel('k');
ylabel('|X(k)|');
subplot(2,1,2);
stem(k, angle(X_k));
xlabel('k');
ylabel('\angle{X(k)}');

When you run the above code you should get the following plot:
60

40
|X[k]|

20

0
-50 -40 -30 -20 -10 0 10 20 30 40 50
k
4

2
∠ X[k]

-2

-4
-50 -40 -30 -20 -10 0 10 20 30 40 50
k

The above plot is probably not what you were expecting! MATLAB provides the Fast
Fourier Transform (FFT) to compute the DFT. Below is brief description of the various
Fourier transforms that can be used for signal processing:
• Continuous Time Fourier Transform (CTFT): Used to transform a CT signal, x(t ) , into
the frequency domain, yielding X ( f ) . Both x(t ) , and X ( f ) are continuous functions.
• Discrete Time Fourier Transform (DTFT): Used to transform a DT signal, x[n] , into
the frequency domain, yielding X ( f ) . While x[n] is discrete, X ( f ) is a continuous
function in f .
• Discrete Fourier Transform (DFT): Conceptually, the DFT is a discrete sampling of
the DTFT.

To make sense of the plot for X [k ] we need to map the values on the k-axis to useful
frequency values in the CTFT. To perform this mapping, use the following formula:

kf s
f = (1)
N
The variable N is the number of points used to compute the DFT (in our code N = 90 ), and
f s is the sampling frequency that we used to create the discrete time signal (in our code
f s = 30 Hz).

Now looking back at our plot of X[k], notice that there are two delta functions sitting at
6 × 30
k = ±6 . Applying equation (1) gives f = = 2 Hz, which is the exact frequency of our
90
original CT signal.

3. The DFT/IDFT transform can be used to process a signal. Filtering, modulation,


identification, and noise removal are some examples of signal processing applications.
Perform the IDFT on X [k ] and verify that you get the original signal x[n]. Enter the
following into your m-file:

%Do the IFFT, need to ifftshift before taking the IDFT


X_k = ifftshift(X_k);
xEst_n = ifft(X_k, N);
%The estimated signal should be EXACTLY the same as the original.
figure(3);
title('Estimated x[n] produced from IFFT.');
stem(n, xEst_n);

Part II: Signal processing using the FFT


Please continue to use the same .m file that you used in the previous section, and simply add
your own code at the end of the file. In this section you will analyze and process a discrete time
signal, x 2[n] , that was sampled from x 2(t ) using the same f s as in the previous section. Ensure
that the x2x3.mat file is in the same folder as your .m file. To load the data, use the following
line in your .m file:

load('x2x3.mat');

[Q2.1] Plot x2[n] .

[Q2.2] Using the same approach as in Part I, compute the DFT of x2[n] , X 2[k ] .

[Q2.3] x2[n] was created by adding together two sinusoids of different frequencies. What are
the frequencies of the two sinusoids in x 2[n] ? HINT: If a frequency is not a multiple of k, it
exhibits "leakage" into neighbouring frequencies, and your answer will be an estimate. You can
use fractional values of k in equation (1).

[Q2.4] Modify X 2[k ] so that the resulting signal only contains the lowest frequency sinusoid
(or most of it). Plot the resulting X 2[k ] .
[Q2.5] When you loaded the x2[n] from the x2x3.mat file, it also loaded x3[n]. T he signal
x3[n] is the lowest frequency sinusoid in x2[n]. Perform the IDFT on the modified X 2[k ] and
plot the resulting "processed" x2[n] and x[3] both on the same stem plot. You should be able to
get something close to the example below. Note that because you are filtering x2[n], you may
not be able to exactly get x3[n] = x2[n].
1.5

1
x3[n] and modified x2[n]

0.5

-0.5

-1

-1.5
0 10 20 30 40 50 60 70 80 90
n

You might also like