Matlab Tutorial 2
Matlab Tutorial 2
Matlab Tutorial 2
Toolboxes are libraries of extra MATLAB routines designed for general purpose
applications in many areas. With the Communication Toolbox and the DSP Toolbox,
we can simulate many systems in communication and signal processing areas.
The Communication Toolbox covers the essential components or blocks needed to build
most communication systems. With MATLAB, you can write expressions that are very
close to the mathematical model that describes the physical system. In MATLAB, we can
simulate communication systems using a combination of SIMULINK, Communications
Toolbox & Blockset, and DSP Toolbox.
Signals can be generated and manipulated using commands contained in these Toolboxes.
Communications and signal processing systems can be modeled using commands
contained in these Toolboxes.
For each of the following examples, copy and paste on the MATLAB editor window.
Name the m-files the corresponding example and run the program.
Example 1:
% MATLAB script for Illustrative Example 1
% Compute the Fourier Series of a Rectangular Pulse Shape.
n = [-20:1:20];
x = abs(sinc(n/2));
stem(n, x);
Example 2:
% MATLAB script for Illustrative Problem 2
echo on
ts = 0.2; % set parameters
fs =1/ts;
df =0.01;
x =[zeros(1,10),[0:0.2:1],ones(1,9),[1:-0.2:0],zeros(1,10)];
[X, x, df1] = fftseq(x, ts, df); % derive the FFT
X1 =X/fs; % scaling
f =[0:df1:df1*(length(x)-1)]-fs/2; % frequency vector for FFT
f1 =[-2.5:0.001:2.5]; % frequency vector for analytic approach
y =4*(sinc(2*f1)).^2-(sinc(f1)).^2; % Exact Fourier Transform
pause % Press a key to see the plot of the Fourier Transform derived analytically
clf
subplot(2,1,1)
plot(f1,abs(y));
xlabel('Frequency')
title('Magnitude-Spectrum of x(t) derived analytically')
pause % Press a key to see the plot of the Fourier Transform derived numerically
subplot(2,1,2)
plot(f,fftshift(abs(X1)));
xlabel('Frequency')
title('Magnitude-Spectrum of x(t) derived numerically')
Example 3:
% MATLAB script for Illustrative Problem 3
% Matlab demonstration script for DSB-AM modulation. The message signal
% is m(t)=sinc(100t).
echo on
t0=.2; % signal duration
ts=0.001; % sampling interval
fc=250; % carrier frequency
snr=20; % SNR in dB (logarithmic)
fs=1/ts; % sampling frequency
df=0.3; % required freq. resolution
t=[-t0/2:ts:t0/2]; % time vector
snr_lin=10^(snr/10); % linear SNR
m=sinc(100*t); % the message signal
c=cos(2*pi*fc.*t); % the carrier signal
u=m.*c; % the DSB-AM modulated signal
[M,m,df1]=fftseq(m,ts,df); % Fourier transform
M=M/fs; % scaling
[U,u,df1]=fftseq(u,ts,df); % Fourier transform
U=U/fs; % scaling
f=[0:df1:df1*(length(m)-1)]-fs/2; % frequency vector
signal_power=spower(u(1:length(t))); % compute modulated signal power
noise_power=signal_power/snr_lin; % compute noise power
noise_std=sqrt(noise_power); % compute noise standard deviation
noise=noise_std*randn(1,length(u)); % generate noise sequence
r=u+noise; % add noise to the modulated signal
[R,r,df1]=fftseq(r,ts,df); % Fourier transform
R=R/fs; % scaling
pause % Press a key to show the modulated signal power
signal_power
pause %Press any key to see a plot of the message
clf
subplot(2,2,1)
plot(t,m(1:length(t)))
xlabel('Time')
title('The message signal')
pause % Press any key to see a plot of the carrier
subplot(2,2,2)
plot(t,c(1:length(t)))
xlabel('Time')
title('The carrier')
pause % Press any key to see a plot of the modulated signal
subplot(2,2,3)
plot(t,u(1:length(t)))
xlabel('Time')
title('The modulated signal')
pause % Press any key to see a plot of the magnitude of the message and the
% modulated signal in the frequency domain.
subplot(2,1,1)
plot(f,abs(fftshift(M)))
xlabel('Frequency')
title('Spectrum of the message signal')
subplot(2,1,2)
plot(f,abs(fftshift(U)))
title('Spectrum of the modulated signal')
xlabel('Frequency')
pause % Press a key to see a noise sample
subplot(2,1,1)
plot(t, noise(1:length(t)))
title('noise sample')
xlabel('Time')
pause % Press a key to see the modulated signal and noise
subplot(2,1,2)
plot(t,r(1:length(t)))
title('Signal and noise')
xlabel('Time')
pause % Press a key to see the modulated signal and noise in freq. domain
subplot(2,1,1)
plot(f,abs(fftshift(U)))
title('Signal spectrum')
xlabel('Frequency')
subplot(2,1,2)
plot(f,abs(fftshift(R)))
title('Signal and noise spectrum')
xlabel('Frequency')
Example 4:
% MATLAB script for Illustrative Problem 4
% Generation of a sinusoidal sequence
n = 0:40;
f = 0.1;
phase = 0;
A = 1.5;
arg = 2*pi*f*n - phase;
x = A*cos(arg);
clf; % Clear old graph
stem(n,x); % Plot the generated sequence
axis([0 40 -2 2]);
grid;
title('Sinusoidal Sequence');
xlabel('Time index n');
ylabel('Amplitude');
axis;
Example 5
% MATLAB script for Illustrative Problem 5
subplot(1,1,1)
b = 1; a = [1,-0.8];
n=[0:100];x = cos(0.05*pi*n);
y = filter(b,a,x);
subplot(2,1,1); stem(n,x);
xlabel('n'); ylabel('x(n)'); title('Input sequence')
subplot(2,1,2); stem(n,y);
xlabel('n'); ylabel('y(n)'); title('Output sequence')
Example 6
% MATLAB script for Illustrative Problem 6
% Simulation of an M-point Moving Average Filter
% Generate the input signal
n = 0:100;
s1 = cos(2*pi*0.05*n); % A low-frequency sinusoid
s2 = cos(2*pi*0.47*n); % A high frequency sinusoid
x = s1+s2;
% Implementation of the moving average filter
M = input('Desired length of the filter = ');
num = ones(1,M);
y = filter(num,1,x)/M;
% Display the input and output signals
clf;
subplot(2,2,1);
plot(n, s1);
axis([0, 100, -2, 2]);
xlabel('Time index n'); ylabel('Amplitude');
title('Signal #1');
subplot(2,2,2);
plot(n, s2);
axis([0, 100, -2, 2]);
xlabel('Time index n'); ylabel('Amplitude');
title('Signal #2');
subplot(2,2,3);
plot(n, x);
axis([0, 100, -2, 2]);
title('Input Signal'); xlabel('Time index n'); ylabel('Amplitude');
subplot(2,2,4);
plot(n, y);
axis([0, 100, -2, 2]);
xlabel('Time index n'); ylabel('Amplitude');
title('Output Signal');
axis;
2.2 Introduction to SIMULINK
Basically, SIMULINK is a tool that allows you to model and simulate a wide variety of
dynamic systems. It is an extension of MATLAB that lets you model systems
graphically. That is, it is a program for simulating dynamic systems by efficiently using
MATLAB ODE solvers. It adds to MATLAB many features specific to dynamic systems
while retaining all of MATLAB's general-purpose functionality.
Each of these steps will usually be repeated many times throughout the simulation
process.
Creating a Model
SIMULINK has its own special window for block diagrams editing. It shares the
MATLAB’s workspace. Lets start by calling up the SIMULINK block library.
To run SIMULINK, start MATLAB as usual, and at the MATLAB prompt type
>> simulink
or you can just double click on the SIMULINK Library Browser on the MATLAB
Command Window.
Either action will display all the available Toolboxes from which you can call up the
SIMULINK Block Library. Before we build a model, lets look at some demo.
Full documentation is available from the MATLAB HelpDesk (requires a web browser).
Type "helpdesk" at the MATLAB prompt to start it up, and then select "Online manuals"
on the main page.
SIMULINK Example:
0 1 0 1 0 1 0 1 0 1 ...
6. Connect an oscilloscope to the output to verify that you are indeed generating the
given sequence. From the SIMULINK Block Library select Sink ! Scope, and
then drop into the design window. Connect the two blocks by simply dragging
from one output to one output. Click on the Run Button. Double click on the
Scope to display the result.
Generally, you pull all the required blocks into the SIMULINK block diagram
window and connect the ports
8. If you are unsure about how any block works, click Help for more information.