1. CTU: EE 443 – Communications 1: Lab 1: MATLAB Project – Signal Spectrum 1
Colorado Technical University
EE 443 – Communication 1
Lab 1: MATLAB Project – Signal Spectrum
August 2010
Loren Schwappach
ABSTRACT: This lab report was completed as a course requirement to obtain full course credit in EE443,
Communication 1 at Colorado Technical University. Given two time domain signals this lab report uses MATLAB to examine the
frequency content of signals. All of the code mentioned in this lab report was saved as a MATLAB m-file for convenience, quick
reproduction, and troubleshooting of the code. All of the code below can also be found at the end of the report as an
attachment, as well as all figures.
If you have any questions or concerns in regards to this laboratory assignment, this laboratory report, the process
used in designing the indicated circuitry, or the final conclusions and recommendations derived, please send an email to
LSchwappach@yahoo.com. All computer drawn figures and pictures used in this report are of original and authentic content.
function Lab1Prob1 = Comm1Lab2Problem1() %Function
name for calling in MATLAB
I. INTRODUCTION % Colorado Technical University
% EE 443 - Communications I
MATLAB is a powerful program and is useful in the
% Lab 1 - MATLAB Project - Signal Spectrum
visualization of mathematics, physics, and applied
% By Loren K. Schwappach
engineering. In this lab exercise MATLAB will be used to % Uses centeredFFT() for obtaining a two-sided spectrum
determine the frequency content of a composite sinusoidal
signal (to include its magnitude and power spectrum), and %---------------------------
the frequency content of a pulse defined by a rectangular
function (to include its magnitude, energy spectrum, and % Task #1 Magnitude and Power Spectrum for:
autocorrelation). y=5cos(2*pi*400*t)+5cos(2*pi*700*t)
% Composite sinusodial wave
First: Given the following signal: f1 = 400; %frequency of the first sinusoidal wave (400Hz)
(1) a1 = 5; %amplitude of the first sinusoidal wave (5V)
Use MATLAB to plot the magnitude and power spectrum of f2 = 700; %frequency of the second sinusoidal wave (700Hz)
the signal. a2 = 5; %amplitude of the second sinusoidal wave (5V)
fs = 25*f2; %sampling frequency (25*highest freq) (17.5kHz)
Second: Given the following signal: ts = 1/fs; %sampling interval (57us)
t1 = 0:ts:1-ts; %time vector (0:57us:999.943ms)
(2) y = (a1*cos(2*pi*f1*t1) + a2*cos(2*pi*f2*t1)); %composite
Use MATLAB to plot the magnitude and energy spectrum of sinusoidal wave
the signal.
% Plot of sinusodial wave in time domain
Third: Given the second equation: timePlot = figure; %gives graph window a name and keeps it
Use MATLAB to plot the autocorrelation of the signal. available
plot (t1(1:176),y(1:176)); %plots sinusodial wave in time
domain
title('Composite Sinusodial
y(t)=5cos(2*pi*400*t1)+5cos(2*pi*700*t2)');
xlabel('Time (s)'); %adds xlabel to graph
II. PROCEDURE / RESULTS ylabel('Amplitude'); %adds ylabel to graph
To complete the first task the following code was grid; %turns on grid
used in MATLAB to produce and plot the composite
sinusoidal function in the time domain:
2. CTU: EE 443 – Communications 1: Lab 1: MATLAB Project – Signal Spectrum 2
Figure 1: Composite sinusoidal wave in the time domain Figure 2: Composite sinusoidal wave's magnitude in the
y(t)=5cos(2*pi*400*t)+5cos(2*pi*700*t). frequency domain, Y(f).
You can see form figure 1 above that this is indeed a As illustrated by figure 2 above, you should observe
composite sinusoidal result of equation 1. that the composite sinusoidal results in two positive frequency
impulses in the frequency domain, each at their previous
Next, the following code was added to plot the individual frequencies (400Hz and 700Hz), and at half of their
original individual amplitudes (5/2=2.5) as expected.
sinusoidal wave’s magnitude in the frequency domain:
Next, the following code was added to plot the power
% Plot of sinusodial wave (Magnitude) in frequency domain spectrum for the composite sinusoidal:
[YfreqDomain,YfrequencyRange] = centeredFFT(y,fs); %Uses
centeredFFT function % Power Spectrum of sinusodial wave
freqPlot = figure; %gives graph window a name and keeps it % Note: Power = ((A^2)/2T))
available % The LCM of (1/400) and (1/700) is (1/280,000) so T =
stem(YfrequencyRange,abs(YfreqDomain)); %Creates stem (1/280,000)
graph for magnitude spectrum Ey=((abs(YfreqDomain).*abs(YfreqDomain)))*(1/(2*(1/2800
title('Magnitude of y(t) in frequency domain -> Y(f)') 00)));
xlabel('Freq (Hz)'); %adds xlabel to graph powerPlot = figure;
ylabel('Amplitude'); %adds ylabel to graph stem(YfrequencyRange,Ey); %Creates stem graph for
grid; %turns on grid magnitude spectrum
axis([-800,800,-1,3]); %defines axis title('Power spectrum of Y(f)');
[x(min),x(max),y(min),y(max)] xlabel('Freq (Hz)'); %adds xlabel to graph
ylabel('Power (Watts)'); %adds ylabel to graph
grid; %turns on grid
axis([-800,800,-1,1000000]); %defines axis
[x(min),x(max),y(min),y(max)]
3. CTU: EE 443 – Communications 1: Lab 1: MATLAB Project – Signal Spectrum 3
Figure 3: Power Spectrum of Y(f). Figure 4: Rectangular pulse function x(t).
As illustrated by figure 3 above, you should observe As shown by figure 4 above, the MATLAB code
that the power spectrum of the composite sinusoidal is much correctly produces the rectangular pulse required by the
higher in value than the amplitude of Y(f)’s magnitude. This equation 2:
is due to the fact that the signals power is derived by squaring
Y(f)’s amplitude and dividing the result by 2*(1/T) where T is Next knowing that a rectangular pulse results in a
the least common multiple of (1/400) and (1/700). Thus the sinc function the following code was added to plot the
power ends up being very high since (1/T) is very small. rectangular pulse’s magnitude in the frequency domain:
To complete the second task the following code was % Plot of x(t)=2*rect(t/.002) (Magnitude) in frequency
used in MATLAB to produce and plot the rectangular pulse domain
function: [XfreqDomain,XfrequencyRange] = centeredFFT(x,fs);
%Uses centeredFFT function
% Task #2 Magnitude and Energy Spectrum for: x(t) = rFreqPlot = figure; %gives graph window a name and keeps it
2*rect(t/.002) available
% Note: By definition rect(t/x)=u(t+x/2)-u(t-x/2) so.. plot(XfrequencyRange,abs(XfreqDomain)); %Creates stem
% x(t) = 2u(t+1e-3)-2u(t-1e-3) graph for magnitude spectrum
A = 2; title('Magnitude of x(t) in frequency domain -> X(f)');
t0 = -2e-3; xlabel('Freq (Hz)'); %adds xlabel to graph
tf = 2e-3; ylabel('Amplitude'); %adds ylabel to graph
ts = (tf-t0)/1000; %(4us) grid; %turns on grid
axis([-10000,10000,0,1.1]); %defines axis
fs = 1/ts; %(250kHz)
[x(min),x(max),y(min),y(max)]
t=[t0:ts:(tf-ts)]; %(-2ms:4us:2ms)
x = A*rectpuls(t/2e-3);
% Plot of x(t)=2*rect(t/.002) in time domain
rTimePlot = figure;
plot(t,x); %Creates stem graph for magnitude spectrum
title('x(t)=2*rect(t/.002) in time domain');
xlabel('time (s)'); %adds xlabel to graph
ylabel('Amplitude'); %adds ylabel to graph
grid; %turns on grid
axis([t0,tf-ts,0,2.1]); %defines axis
[x(min),x(max),y(min),y(max)]
4. CTU: EE 443 – Communications 1: Lab 1: MATLAB Project – Signal Spectrum 4
Figure 5: Magnitude of x(t) in the frequency domain X(f). Figure 6: Energy spectrum of X(f)
As produced by figure 5 above, you should observe As seen by figure 6 above, the energy spectrum
that the rectangular pulse function equates to a sinc function in (energy spectral density) of the rectangular pulse is merely the
the frequency domain as expected. However, since the frequency domain result squared. This results in a much
magnitude of the function was required the sinc function never smaller amplitude since the frequency domain amplitude is
drops below zero. already <1 (something small^2 = something even smaller).
Next the following code was used to graph the To complete the final task the following code was
energy spectrum of the rectangular pulse: used in MATLAB to plot the autocorrelation of the
rectangular pulse x(t):
% Energy Spectrum of x(t)=2*rect(t/.002)
Ex = (abs(XfreqDomain).*abs(XfreqDomain)); % Task #3 Plot the autocorrelation for the rect function in task
rEnergyPlot = figure; 2.
plot(XfrequencyRange,Ex); %Creates stem graph for % Uses task #2's variables and functions.
magnitude spectrum Rxx=xcorr(x); % Estimate its autocorrelation
title('Energy spectrum of X(f)'); rEnergyPlot = figure;
xlabel('Freq (Hz)'); %adds xlabel to graph plot(Rxx); % Plot the autocorrelation
ylabel('Amplitude'); %adds ylabel to graph title('Autocorrelation function of x(t)=2*rect(t/.002)');
grid; %turns on grid xlabel('lags');
axis([-10000,10000,0,1.1]); %defines axis ylabel('Autocorrelation');
[x(min),x(max),y(min),y(max)] grid;
5. CTU: EE 443 – Communications 1: Lab 1: MATLAB Project – Signal Spectrum 5
Figure 7: Autocorrelation of x(t).
As seen by figure 7 above, the autocorrelation of the
rectangular pulse results in a linear pyramid. The
autocorrelation function is a measure of the similarity between
x (t) and its delayed counterpart x ( ).
III. CONCLUSIONS
. MATLAB is a great utility for representing complex
concepts visually and can easily be manipulated to show
signals in various formats. This lab project was successful in
demonstrating MATLABs powerful features in a quick and
easy method, and demonstrating how MATLAB can be used
for displaying the frequency contents of signals.
REFERENCES
nd
[1] Haykin, S., “Signals and Systems 2 Edition” McGraw-
Hill, New York, NY, 2007.
6. CTU: EE 443 – Communications 1: Lab 1: MATLAB Project – Signal Spectrum 6
Figure 8: Composite sinusoidal wave in the time domain y(t)=5cos(2*pi*400*t)+5cos(2*pi*700*t).
7. CTU: EE 443 – Communications 1: Lab 1: MATLAB Project – Signal Spectrum 7
Figure 9: Composite sinusoidal wave's magnitude in the frequency domain, Y(f).
8. CTU: EE 443 – Communications 1: Lab 1: MATLAB Project – Signal Spectrum 8
Figure 10: Power Spectrum of Y(f).
9. CTU: EE 443 – Communications 1: Lab 1: MATLAB Project – Signal Spectrum 9
Figure 11: Rectangular pulse function x(t).
10. CTU: EE 443 – Communications 1: Lab 1: MATLAB Project – Signal Spectrum 10
Figure 12: Magnitude of x(t) in the frequency domain X(f).
11. CTU: EE 443 – Communications 1: Lab 1: MATLAB Project – Signal Spectrum 11
Figure 13: Energy spectrum of X(f)
12. CTU: EE 443 – Communications 1: Lab 1: MATLAB Project – Signal Spectrum 12
Figure 14: Autocorrelation of x(t).
13. CTU: EE 443 – Communications 1: Lab 1: MATLAB Project – Signal Spectrum 13
%MATLAB CODE
function Lab1Prob1 = Comm1Lab2Problem1() %Function name for calling in MATLAB
% Colorado Technical University
% EE 443 - Communications I
% Lab 1 - MATLAB Project - Signal Spectrum
% By Loren K. Schwappach
% Uses centeredFFT() for obtaining a two-sided spectrum
%---------------------------
% Task #1 Magnitude and Power Spectrum for: y=5cos(2*pi*400*t)+5cos(2*pi*700*t)
% Composite sinusodial wave
f1 = 400; %frequency of the first sinusodial wave (400Hz)
a1 = 5; %amplitude of the first sinusodial wave (5V)
f2 = 700; %frequency of the second sinusodial wave (700Hz)
a2 = 5; %amplitude of the second sinusodial wave (5V)
fs = 25*f2; %sampling frequency (25*highest freq) (17.5kHz)
ts = 1/fs; %sampling interval (57us)
t1 = 0:ts:1-ts; %time vector (0:57us:999.943ms)
y = (a1*cos(2*pi*f1*t1) + a2*cos(2*pi*f2*t1)); %composite sinusodial wave
% Plot of sinusodial wave in time domain
timePlot = figure; %gives graph window a name and keeps it available
plot (t1(1:176),y(1:176)); %plots sinusodial wave in time domain
title('Composite Sinusodial y(t)=5cos(2*pi*400*t1)+5cos(2*pi*700*t2)');
xlabel('Time (s)'); %adds xlabel to graph
ylabel('Amplitude'); %adds ylabel to graph
grid; %turns on grid
% Plot of sinusodial wave (Magnitude) in frequency domain
[YfreqDomain,YfrequencyRange] = centeredFFT(y,fs); %Uses centeredFFT function
freqPlot = figure; %gives graph window a name and keeps it available
stem(YfrequencyRange,abs(YfreqDomain)); %Creates stem graph for magnitude spectrum
title('Magnitude of y(t) in frequency domain -> Y(f)')
xlabel('Freq (Hz)'); %adds xlabel to graph
ylabel('Amplitude'); %adds ylabel to graph
grid; %turns on grid
axis([-800,800,-1,3]); %defines axis [x(min),x(max),y(min),y(max)]
% Power Spectrum of sinusodial wave
% Note: Power = ((A^2)/2T))
% The LCM of (1/400) and (1/700) is (1/280,000) so T = (1/280,000)
Ey = ((abs(YfreqDomain).*abs(YfreqDomain)))*(1/(2*(1/280000)));
powerPlot = figure;
stem(YfrequencyRange,Ey); %Creates stem graph for magnitude spectrum
title('Power spectrum of Y(f)');
xlabel('Freq (Hz)'); %adds xlabel to graph
ylabel('Power (Watts)'); %adds ylabel to graph
grid; %turns on grid
axis([-800,800,-1,1000000]); %defines axis [x(min),x(max),y(min),y(max)]
%---------------------------
% Task #2 Magnitude and Energy Spectrum for: x(t) = 2*rect(t/.002)
% Note: By definition rect(t/x)=u(t+x/2)-u(t-x/2) so..
% x(t) = 2u(t+1e-3)-2u(t-1e-3)
A = 2;
t0 = -2e-3;
tf = 2e-3;
14. CTU: EE 443 – Communications 1: Lab 1: MATLAB Project – Signal Spectrum 14
ts = (tf-t0)/1000; %(4us)
fs = 1/ts; %(250kHz)
t=[t0:ts:(tf-ts)]; %(-2ms:4us:2ms)
x = A*rectpuls(t/2e-3);
% Plot of x(t)=2*rect(t/.002) in time domain
rTimePlot = figure;
plot(t,x); %Creates stem graph for magnitude spectrum
title('x(t)=2*rect(t/.002) in time domain');
xlabel('time (s)'); %adds xlabel to graph
ylabel('Amplitude'); %adds ylabel to graph
grid; %turns on grid
axis([t0,tf-ts,0,2.1]); %defines axis [x(min),x(max),y(min),y(max)]
% Plot of x(t)=2*rect(t/.002) (Magnitude) in frequency domain
[XfreqDomain,XfrequencyRange] = centeredFFT(x,fs); %Uses centeredFFT function
rFreqPlot = figure; %gives graph window a name and keeps it available
plot(XfrequencyRange,abs(XfreqDomain)); %Creates stem graph for magnitude spectrum
title('Magnitude of x(t) in frequency domain -> X(f)');
xlabel('Freq (Hz)'); %adds xlabel to graph
ylabel('Amplitude'); %adds ylabel to graph
grid; %turns on grid
axis([-10000,10000,0,1.1]); %defines axis [x(min),x(max),y(min),y(max)]
% Energy Spectrum of x(t)=2*rect(t/.002)
Ex = (abs(XfreqDomain).*abs(XfreqDomain));
rEnergyPlot = figure;
plot(XfrequencyRange,Ex); %Creates stem graph for magnitude spectrum
title('Energy spectrum of X(f)');
xlabel('Freq (Hz)'); %adds xlabel to graph
ylabel('Amplitude'); %adds ylabel to graph
grid; %turns on grid
axis([-10000,10000,0,1.1]); %defines axis [x(min),x(max),y(min),y(max)]
%---------------------------
% Task #3 Plot the autocorrelation for the rect function in task 2.
% Uses task #2's variables and functions.
Rxx=xcorr(x); % Estimate its autocorrelation
rEnergyPlot = figure;
plot(Rxx); % Plot the autocorrelation
title('Autocorrelation function of x(t)=2*rect(t/.002)');
xlabel('lags');
ylabel('Autocorrelation');
grid;
%---------------------------
% end Comm1Lab1Problem1