SS lab 1
SS lab 1
1 INTRODUCTION TO MATLAB
DATE:
What is MATLAB?
A high-performance language for technical computing. Typical uses of MATLAB:
Mathematical computations.
Algorithmic development.
Model prototyping (prior to complex model development).
Data analysis and exploration of data (visualization).
Scientific and engineering graphics for presentation.
Complex analysis using MATLAB toolboxes (i.e., statistics, neural networks, fuzzy logic, H-
infinity control, economics, etc.)
Why is MATLAB?
Because it simplifies the analysis of mathematical models.
It frees you from coding in high-level languages (saves a lot of time - with some computational
speed penalties)
Provides an extensible programming/visualization environment.
Provides professional looking graphs.
Provide a lot of toolbox that help me.
MATLAB is usually faster than Mathematics and Maple in numeric intensive tasks.
MATLAB has more textbooks than other packages combined (350+ books). Perhaps this speaks
on the acceptance by the user community.
INTRODUCTION:
The name MATLAB stands for MATrix LABoratory. MATLAB was written originally to provide
easy access to matrix software developed by the LINPACK (linear system package) and EISPACK
(Eigen system package) projects.
MATLAB is a high-performance language for technical computing. It integrates computation,
visualization, and programming environment. Furthermore, MATLAB is a modern programming
language environment: it has sophisticated data structures, contains built-in editing and debugging tools,
and supports object-oriented programming.
MATLAB has many advantages compared to conventional computer languages (e.g., C,
FORTRAN) for solving technical problems.
MATLAB is an interactive system whose basic data element is an array that does not require
dimensioning.
It has powerful built-in routines that enable a very wide variety of computations. It also has easy to
use graphics commands that make the visualization of results immediately available. Specific
applications are collected in packages referred to as toolbox. There are toolboxes for signal processing,
symbolic computation, control theory, simulation, optimization, neural networks, Fuzzy logic,
communications and various fields of applied science and engineering.
STARTING MATLAB:
One can enter MATLAB by double-clicking on the MATLAB shortcut icon on Windows desktop. Upon
starting of MATLAB, a special window called the MATLAB desktop appears. The desktop is a window
that contains other windows. The major tools within or accessible from the desktop are:
1
The Command Window: the window in which the inputs and out puts can be observed.
The Command History: the window consisting of the instruction given in the command during
the previous sessions.
The Workspace: the window consisting of the variables used in the programming.
The Current Directory: the directory consisting of m-files and other files of use/work.
VARIABLE:
A MATLAB variable is an object belonging to a specific data type. On MATLAB variable is
basically a matrix. Matrices can be made up of real or complex numbers, as well as characters
(ASCII symbols).
Defining MATLAB Variables
In general, the matrix is defined in the MATLAB command interface by input from the keyboard
and assigned a freely chosen variable name in accordance with the following syntax:
>> x = 2.45
With this instruction, after a MATLAB prompt the number 2.45 (a number is a 1 × 1 matrix) will be
assigned to the variable x and can subsequently be addressed under this variable name. All of the
defined variables will be stored in the so-called workspace of MATLAB.
Plotting
Plotting is one of the most useful applications of a math package to plot experimental or generated data.
Basic 2 D plotting:
Plotting a function in MATLAB involves the following three steps:
1. Define the function
2. Specify the range of values over which to plot the function
3. Call the MATLAB plot(x, y) function
3
EX. NO. 2 GENERATION OF ELEMENTARY CONTINUOUS TIME SIGNALS
DATE:
AIM:
To generate the following continuous time signals using MATLAB.
1. Unit impulse signal
2. Unit step signal
3. Unit ramp signal
4. Exponential growing signal
5. Exponential decaying signal
6. Sine signal
7. Cosine signal
APPARATUS REQUIRED:
System with MATLAB.
ALGORITHM:
1. Get the number of samples.
2. Generate the unit impulse, unit step using ‘ones’, ‘zeros’
matrix command.
3. Generate ramp, sine, cosine and exponential signals using
corresponding general formula.
4. Plot the graph.
PROGRAM:
Continuous time
signal:
4
OUTPUT FOR GENERATION OF CONTINUOUS TIME SIGNAL:
Enter the length of unit step sequence(N)= 10
Enter the length of the unit ramp sequence(N)= 10
Enter the length of sinosoidal sequence(N)= 10
Enter the length of cosine sequence(N)= 5
Enter the length of Exponential sequence(N)= 5
Enter the value of Exponential sequence(a)= 1
5
% program for Sine wave
clc;
t2=0:0.1:10;
y2=sin(t2);
subplot(3,2,3);
plot(t2,y2,'k');
xlabel('Time');
ylabel('Amplitude');
title('Sine Wave');
% program for Cosine Wave
clc;
t3=0:0.1:10;
y3=cos(t3);
subplot(3,2,4);
plot(t3,y3,'k');
xlabel('Time');
ylabel('Amplitude');
title('Cosine Wave');
% program for Square wave
clc;
t4=0:0.001:10;
y4=square(t4);
subplot(3,2,5);
plot(t4,y4,'k');
xlabel('Time');
ylabel('Amplitude');
title('Square Wave');
% program for Sawtooth Wave
clc;
t5=0:0.1:10;
y5=sawtooth(t5);
subplot(3,2,6);
plot(t5,y5,'k');
xlabel('Time');
ylabel('Amplitude');
title('Sawtoth Wave');
RESULT:
6
OUTPUT FOR GENERATION OF DISCRETE TIME SIGNAL:
7
EX. NO. 3 GENERATION OF ELEMENTARY DISCRETE TIME SIGNALS
DATE:
AIM:
To generate the following discrete time signals using MATLAB.
1. Unit impulse signal
2. Unit step signal
3. Unit ramp signal
4. Exponential growing signal
5. Exponential decaying signal
6. Sine signal
7. Cosine signal
APPARATUS REQUIRED:
System with MATLAB.
ALGORITHM:
1. Get the number of samples.
2. Generate the unit impulse, unit step using ‘ones’, ‘zeros’ matrix command.
3. Generate ramp, sine, cosine and exponential signals using corresponding general formula.
4. Plot the graph.
PROGRAM:
RESULT:
9
EX. NO. 4 LINEAR CONVOLUTION OF SIGNALS
DATE:
AIM:
To write the program for finding the linear convolution of two signals using MATLAB.
APPARATUS REQUIRED:
System with MATLAB.
ALGORITHM:
1. Get the number of samples.
2. Generate the output sequence of the given two input signals using ‘conv’ command.
3. Plot the graph.
PROGRAM:
% PROGRAM FOR LINEAR CONVOLUTION
%input seq
clc;
clear all;
close all;
x=input('Enter the input sequence(n)= ');
N1=length(x);
n=0:1:(N1-1);
subplot(3,1,1);
stem(n,x,'k');
xlabel('n>');
title('input sequence X(n)');
%impulse seq
h=input('Enter the impulse sequence h(n)= ');
N2=length(h);
n1=0:1:N2-1;
subplot(3,1,2);
stem(n1,h,'k');
xlabel('n>');
ylabel('Amplitude');
title('impulse sequence h(n)');
%Output
conv. y=conv(x,h);
N=N1+N2-1;
n2=0:1:N-1;
subplot(3,1,3);
stem(n2,y,'k');
xlabel('Time');
ylabel('Amplitude');
title(‘Linear convolution of two signals’);
10
OUTPUT FOR LINEAR CONVOLUTION:
11
RESULT:
12
OUTPUT FOR CIRCULAR CONVOLUTION:
13
EX. NO: 5 CIRCULAR CONVOLUTION OF SIGNALS
DATE:
AIM:
To write the program for finding the circular convolution of two signals using MATLAB.
APPARATUS REQUIRED:
System with MATLAB.
ALGORITHM:
1. Get the number of samples.
2. Generate the sequence for the given two input signals using ‘zeros’ and ‘ones’ matrix
command.
3. Generate the convoluted output sequence of the given two input signals using ‘conv’
command.
4. Plot the graph.
PROGRAM:
14
end
end
disp('Convolution of x1(n) & x2(n) is');
disp(y);
subplot(3,1,3);
stem(y);
xlabel('n--->');
ylabel('Amplitude');
title('Convolution of x1(n)&x2(n) Response');
RESULT:
15
EX. NO: 6 FIND THE FREQUENCY AND PHASE RESPONSE OF THE SYSTEM
DATE:
AIM:
To write the program for finding the frequency and phase response of the system using MATLAB.
APPARATUS REQUIRED:
System with MATLAB.
ALGORITHM:
1. Enter the numerator co efficient
2. Enter the denominator coefficient
3. Compute the frequency and phase response
4. Plot the graph
PROGRAM:
clc;
clear all;
b=input('Enter the numerator coefficients:');
a=input('Enter the denominator coefficients:');
[h,w]=freqz(b,a);
subplot(2,1,1);
plot(w/pi,abs(h));
grid;
xlabel('Normalised Frequency');
ylabel('Magnitude in dB');
title('Magnitude Response');
subplot(2,1,2);
plot(w/pi,angle(h));
grid;
xlabel('Normalised Frequency');
ylabel('phase in radians');
title('Phase Response');
RESULT:
16
OUTPUT FOR FREQUENCY AND PHASE RESPONSE:
17
EX. NO: 7 CALCULATION OF DFT AND IDFT
DATE:
AIM:
To write the program for calculating the N-point DFT and IDFT of given input sequence using
MATLAB.
APPARATUS REQUIRED:
System with MATLAB.
ALGORITHM:
1. Get the input sequence and its length.
2. Calculate the Discrete Fourier Transform and Inverse Discrete Fourier Transform of
given sequence using user defined function.
3. Plot the magnitude and phase response of FFT sequence.
4. Plot the IDFT response.
PROGRAM:
%program for DFT & IDFT
clc;
x1=input('Enter the input sequence ');
N=length(x1);
n=0:1:N-1;
k=0:1:N-1;
WN=exp(-1i*2*pi/N); nk=n'*k;
WnNK=WN.^nk;
Xk=x1*WnNK;
Xkmag=abs(Xk);
WNnk=WN.^(-nk);
xn=Xk*WNnk/N;
subplot(3,1,1);
stem(n,x1);
xlabel('time period');
ylabel('Amplitude');
title('input sequence');
subplot(3,1,2);
stem(n,Xkmag);
xlabel('time period');
ylabel('Amplitude');
title('DFT');
subplot(3,1,3);
stem(n,xn);
xlabel('time period');
ylabel('Amplitude');
18
title('IDFT');
RESULT:
19
EX. NO: 8 SAMPLING AND EFFECT OF ALIASING
DATE:
AIM:
To sample the signal and observe the aliasing effect of that signal using MATLAB.
APPARATUS REQUIRED:
System with MATLAB.
ALGORITHM:
1. Get the input frequency and number of samples.
2. The input signal is sampled at the frequencies fs1=2fm, fs2>2fm and fs3<2fm.
3. Plot the input and sampled signal at various frequencies.
PROGRAM:
%prog for sampling and effect of alliasing
clc;
close all;
clear all;
% Time period of 50 Hz signal with 0.1s duration at 1000Hz t=0:0.001:0.1;
fm = 15; x=sin(2*pi*fm*t);
figure(1)
plot(t,x);
xlabel('Time');
ylabel('Amplitude');
title('Original Analog signal');
%Analog sig sampled at fs<<2fm fs=10;
n=0:1/fs:0.1;
xn=sin(2*pi*fm*n);
figure(2)
subplot(2,1,1);
stem(n,xn);
xlabel('Time');
ylabel('Amplitude');
title('Undersampled fs<<2fm signal');
subplot(2,1,2);
plot(n,xn);
xlabel('Time');
ylabel('Amplitude');
title('Reconstructed undersampled fs<<2fm signal');
%Analog sig sampled at fs=2fm fs=30;
n=0:1/fs:0.1;
xn=sin(2*pi*fm*n);
figure(3)
20
subplot(2,1,1);
stem(n,xn);
xlabel('Time');
ylabel('Amplitude');
OUTPUT FOR SAMPLING AND EFFECT OF ALIASING:
21
22
title('Sampled at Nyquist rate fs=2fm signal');
subplot(2,1,2);
plot(n,xn);
xlabel('Time');
ylabel('Amplitude');
title('Reconstructed Nyquist rate fs=2fm signal');
%Analog sig sampled at fs>>2fm fs=500;
n=0:1/fs:0.1;
xn=sin(2*pi*fm*n);
figure(4)
subplot(2,1,1);
stem(n,xn);
xlabel('Time');
ylabel('Amplitude');
title('Oversampled fs>>2fm signal');
subplot(2,1,2);
plot(n,xn);
xlabel('Time');
ylabel('Amplitude');
title('Reconstructed oversampled fs>>2fm signal');
RESULT:
23
OUTPUT FOER AUTO CORRELATION:
24
EX. NO: 9 AUTO CORRELATION
DATE:
AIM:
To compute auto correlation between two sequences.
APPARATUS REQUIRED:
System with MATLAB.
ALGORITHM:
1. Enter the input sequence.
2. Compute the auto correlation of two sequence.
3. Plot the graph.
PROGRAM:
clc;
close all;
clear all;
% two input sequences
x=input('enter input sequence');
subplot(1,2,1);
stem(x);
xlabel('n');
ylabel('x(n)');
title('input sequence');
% auto correlation of input sequence
z=corr(x,x);
disp(‘The values of z are = ‘);
disp(z);
subplot(1,2,2);
stem(z);
xlabel('n');
ylabel('z(n)');
title('auto correlation of input sequence');
RESULT:
25
26