electroSome

Signal Generation in MATLAB

MATLAB Logo

MATLAB Logo

Contents

Here I’m going to show you how signals can be generated in MATLAB. If you are a newbie in this field, have a look at our MATLAB tutorials to get familiar with it.

Plotting of Discrete and Continuous signal

The 2 main functions for plotting are

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

For details about plot(), please go to this page.

Plotting an Array of Data

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 :

NOTE:

suplot (2,2,1)

eg:

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

Plotting a Function

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

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

Some Basic Test Signals 

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

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 }

n1=input('Lower limit')
n2=input('Upper limit')
x=n1:n2;
y=[x==0];
stem(x,y,);

Output :

Plot of Impulse Funtion
n1=-5 ; n2=5

II. Unit Step Signal

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

Discrete

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
n1= -4 ; n2 = 4

Continuous

n=input('Enter the upper limit');
t=0:n;
x=[t>=0];
plot(t,x);
title('Continuous');
Unit step signal – Continuous
n = 6

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

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
n1=-10 ; n2=10

Shortcut method for Generating Ramp Signal

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

IV. Unit Exponential signal

x(t)= eat 

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
n1 = -5 ; n2= 5 ; a = 0.5
Exponentially Decaying Signal
n1 = -5 ; n2 = 5 ; a = -0.5
Exit mobile version