How to generate signals in MATLAB

Electronics & Electrical Engineering Topics
Post Reply
User avatar
Shehani
Lieutenant
Lieutenant
Posts: 61
Joined: Mon Aug 19, 2013 2:11 pm

How to generate signals in MATLAB

Post by Shehani » Fri Oct 04, 2013 1:20 pm

Plotting of Discrete and Continuous signal

The 2 main functions for plotting are

- plot() function - For plotting Continuous signal
- stem() function – For plotting Discrete signal


stem() - 2-D plot function for plotting discrete sequence of data

SYNTAX :

1.stem(y) – Plots the data sequence y specified equally spaced along x axis
2.stem(x,y) – Plots x versus the columns of y. x and y must be of same size
3.stem(…,’fill’) – If we want to fill the marker at the end of stem
4.stem(…,LineSpec) – For specifying line style and color

Plotting an Array of Data

Code: Select all

x=[1 2 3 4];
subplot(2,1,1); # refer NOTE
plot(x,'ro-');
title('Continuous');
subplot(2,1,2);
stem(x);
title('Discrete');
Output :
1.jpg
1.jpg (41.67 KiB) Viewed 2814 times
NOTE:

- subplot() - is a function MATLAB which allows us to draw 2 or more graphs simultaneously on a single figure window. The function breaks the figure into matrix specified by user and selects the corresponding axes for the current plot SYNTAX : subplot(m,n,p) – Divides the figure window into m x n matrix of small axes and selects the pth axes object for the current plot. eg : subplot (2,2,1) – divides the figure into a 2 x 2 matrix (4 equal parts) and selects 1st axes object.
suplot (2,2,1)
suplot (2,2,1)
2.jpg (26.66 KiB) Viewed 2814 times
- The important point to be noted while dealing with the array data is MATLAB index starts with 1, ie by default MATLAB always assumes x(1) as the first element of the array [in above eg: x(1)=1]. So if we have to change the time index we have to define the range.

eg:

Code: Select all

x=[1 2 3 4];
dtx=-2:1; #defining discrete time for x, from -2 to 1
subplot(2,1,1);
plot(dtx,x,'ro-');
title('Continuous');
subplot(2,1,2);
stem(dtx,x);
title('Discrete');
Output :
Plot of Continuous and Discrete signal with index changed
Plot of Continuous and Discrete signal with index changed
3.jpg (42.5 KiB) Viewed 2814 times
Plotting a Function

Now lets plot a function continuously and discretely. I’m going to plot a sine function

Code: Select all

x=linspace(0,2*pi,25); # 0 to 2pi is divided in 25 equidistant points
y=sin(x);
subplot(2,1,1);
plot(x,y,'r');
title('Continuous');
subplot(2,1,2);
stem(x,y);
title('Discrete');
Output :
Sine wave plot
Sine wave plot
4.jpg (51.49 KiB) Viewed 2814 times
Some Basic Test Signals

While generating signals its range has to be specified it can be done in 2 ways

-Directly specifying range value
eg: -5:5 ; 0:10 etc
-Can be specified during the runtime of the program using the input function in MATLAB.

SYNTAX :

input( ‘string’); # returns the input value
Now lets generate same basic test signals.

I. Impulse function

Impulse function given by
? (t) = { 1 for t=0 ; 0 otherwise }

Code: Select all

n1=input('Lower limit')
n2=input('Upper limit')
x=n1:n2;
y=[x==0];
stem(x,y,);
Output :
Plot of Impulse Funtion<br />n1=-5 ; n2=5
Plot of Impulse Funtion
n1=-5 ; n2=5
5.jpg (35.49 KiB) Viewed 2814 times
II. Unit Step Signal

u(t)={ 1 for t >=0 ; 0 for t< 0 }

Discrete

Code: Select all

n1=input('Enter the lower limit');
n2=input('Enter the upper limit');
n=n1:n2;
x=[n>=0];
stem(n,x);
title('Unit Step Signal - Discrete');
Plot of Unit Step Signal – Discrete<br />n1= -4 ; n2 = 4
Plot of Unit Step Signal – Discrete
n1= -4 ; n2 = 4
6.jpg (39.86 KiB) Viewed 2814 times

Continuous

Code: Select all

n=input('Enter the upper limit');
t=0:n;
x=[t>=0];
plot(t,x);
title('Continuous');
Unit step signal – Continuous<br />n = 6
Unit step signal – Continuous
n = 6
7.jpg (33.7 KiB) Viewed 2814 times

III. Unit Ramp Signal


r(t)={ t for t >= 0 ; 0 for t <0 }

Recall that ramp signal r(t)=t*u(t) where u(t) is unit step signal

Code: Select all

n1=input('Enter lower limit');
n2=input('Enter upper limit');
n=n1:n2;
x=n.*[n>=0];
subplot(2,1,1);
plot(n,x,'r');
title('Continuous');
subplot(2,1,2);
stem(n,x,'b');
title('Discrete');
Output :
Unit Ramp Function<br />n1=-10 ; n2=10
Unit Ramp Function
n1=-10 ; n2=10
8.jpg (46.76 KiB) Viewed 2814 times
Shortcut method for Generating Ramp Signal

Code: Select all

n=input('Enter the upper limit')
t=0:n;
subplot(2,1,1);
plot(t,t);
title('Continuous');
subplot(2,1,2);
stem(t,t);
title('Discrete');
Output :
Unit Ramp Function Shortcut Method with n=10
Unit Ramp Function Shortcut Method with n=10
9.jpg (46.08 KiB) Viewed 2814 times
IV. Unit Exponential signal

x(t)= eat

Code: Select all

n1=input('Enter lower limit');
n2=input('Enter upper limit');
t=n1 : n2;
a=input('Enter the value for a');
y=exp(a*t);  # exp() function in matlab SYNTAX : exp(a)n- returns the exponential factor of a
subplot(2,1,1);
plot(t,y,'r');
title('Continuous');
subplot(2,1,2);
stem(t,y,'b');
title('Discrete');
Output :
Exponentially Growing Signal<br />n1 = -5 ; n2= 5 ; a = 0.5
Exponentially Growing Signal
n1 = -5 ; n2= 5 ; a = 0.5
10.jpg (42.78 KiB) Viewed 2814 times
Exponentially Decaying Signal<br />n1 = -5 ; n2 = 5 ; a = -0.5
Exponentially Decaying Signal
n1 = -5 ; n2 = 5 ; a = -0.5
11.jpg (42.67 KiB) Viewed 2814 times


Article courtesy of electrosome.com
Post Reply

Return to “Electronics & Electrical Engineering”