Tuesday 24 April 2018

Published April 24, 2018 by with 0 comment

Matlab for Digital Signal Processing and Processors [Part 1]


Complete Code


clear all
n = [0 : 1 : 10];

x1 = [ones,zeros(1,10)];
subplot(2,4,1)
stem(n,x1,'fill')
title('Impulse')
xlabel('n')
ylabel('Amplitude')

x2 = ones(1,11);
subplot(2,4,2)
stem(n,x2,'fill')
title('Step')
xlabel('n')
ylabel('Amplitude')

x3 = n;
subplot(2,4,3)
stem(n,x3,'fill')
title('Ramp')
xlabel('n')
ylabel('Amplitude')

x4 = (n.*n)./2 ;
subplot(2,4,4)
stem(n,x4,'fill')
title('Parabolic')
xlabel('n')
ylabel('Amplitude')

x5 = [0.5].^n ;
subplot(2,4,5)
stem(n,x5,'fill')
title('Decaying Exponential')
xlabel('n')
ylabel('Amplitude')

x6 = [1.1].^n ;
subplot(2,4,6)
stem(n,x6,'fill')
title('Rising Exponential')
xlabel('n')
ylabel('Amplitude')

x7 = [-0.5].^n;
subplot(2,4,7)
stem(n,x7,'fill')
title('Alternate Decaying Exponential')
xlabel('n')
ylabel('Amplitude')

x8 = [-1.1].^n ;
subplot(2,4,8)
stem(n,x8,'fill')
title('Alternate Rising Exponential')
xlabel('n')
ylabel('Amplitude')


Explanation of the code




clear all
This line of code clears all the variables stored in Matlab Workspace
Before Execution
After Execution


























n = [0 : 1 : 10];
Creates an array from '0' to '10' in steps of 1

For eg: If you wanted to create an array from 50 to 100 in steps of 5 it can be done as 
n = [50 : 5 : 100];


Impulse Signal



We have total 11 sample numbers  i.e. n= 0 to n=10 in our array 'n' that we created above.
So according to the formula for impulse signal, the amplitude for Sample at n=0 will be 1.
And the amplitude at the rest of the samples will be zero.

So we will be using the following matlab code to create an impulse signal array -
x1 = [ones,zeros(1,10)]
Here we create an array x1 such that the first number is a one and the rest other numbers are zeros. Matlab has predefined keywords 'ones' and 'zeros' to create arrays of ones and zeros respectively.

For Eg to create an array of ones of length 5 and array of zeros of length 2
ones(1,5)
zeros(1,2)

Both this arrays can be combined as
[ones(1,5),zeros(1,2)]



To create a 2x5 size Matrix of ones and 2x2 size Matrix zeros we use
ones(2,5)
zeros(2,2)

Unit Step Signal




Magnitude is zero for sample number n smaller than zero.
Magnitude is one for sample number greater than or equal to zero.

So we create an array of ones of the same length as that of 'n'


x2=ones(1,11)

Unit Ramp Signal



Magnitude is zero for sample number n smaller than zero.
Magnitude is same as the sample number, when sample number is greater than or equal to zero.

So we create an array that is the copy of 'n'


x3=n


Unit Parabolic Signal



Magnitude is zero for sample number n smaller than zero.
Magnitude is square of the sample number, when sample number is greater than or equal to zero.

So we create an array that is the square of 'n'


x4=n.^2


If you do not use a dot before ^ you'll get an error.
Dot is used for element wise operation. We do not want the square of the array we need the square of its elements.



NEXT : Exponential Signals and Plotting
Read More
      edit