Circuit Simulation Laboratory
Assignment 2
Write MATLAB program for generation of impulse function and unit step function. Using
these functions generate the following discrete time signals.
a) x(n)=2*delta(n+2)-delta(n-4) ; -5<n<5
b) x(n)=n*[u(n)-u(n-10)]-10*exp(-0.3(n-10))*[u(n-10)-u(n-20)] ; 0<n<20
c) x(n) = cos(0.04*pi*n) + 0.2*w(n) ; 0<n<50, where w(n) is random noise with zero mean
and unit variance
d) Plot the real part, imaginary part, magnitude and phase of signal
x(n) = exp-(0.1+0.3i)*n ; -10<n<50 using MATLAB subplot function, label the axis and
title each subplot.
Part a)
Method 1:
function Y = my_delta(X)
%DIRAC Delta function.
%DIRAC(X) is zero for all X, except X == 0 where it is %infinite.
%DIRAC(X) is not a function in the strict sense, but rather a
%distribution with int(dirac(x-a)*f(x),-inf,inf) = f(a) and
%diff(heaviside(x),x) = dirac(x).
Y = zeros(size(X),'like',X);
Y(X == 0) = 1;
Y(imag(X) ~= 0) = NaN;
Y(isnan(X)) = NaN;
end
%Using my_delta function to generate given impulse function.
n=linspace(-5,5,11);
x=2*my_delta(n+2)-1*my_delta(n-4);
stem(n,x);
title('Impulse Function');
xlabel('X-axis');
ylabel('Y-axis');
1
Circuit Simulation Laboratory
Fig 2.1 Impulse Function where X-axis: n, Y-axis: x[n]
Method 2)
clear all;
x=[zeros(1,3) 1 zeros(1,7)];
n=linspace(-5,5,11);
w=[zeros(1,9) 1 zeros(1,1)];
z=2*x-1*w;
stem(n,z);
2
Circuit Simulation Laboratory
Fig 2.2 Impulse Function where X-axis: n, Y-axis: x[n]
PART B)
%Creating function my_step with help of DIRAC function
function Y = my_step(X)
Y = zeros(size(X),'like',X);
Y(X >= 0) = 1;
Y(imag(X) ~= 0) = NaN;
Y(isnan(X)) = NaN;
end
%Using function my_step to generate given step function
n=0:20;
x=n.*(my_step(n)-1*my_step(n-10));
y= 10*(exp(-0.3*(n-10)));
z= my_step(n-10)-1*my_step(n-20) ;
a=x+y.*z;
stem(n,a);
title('Step Function');
xlabel('X-axis');
ylabel('Y-axis');
3
Circuit Simulation Laboratory
Fig 2.3 Step Function where X-axis: n, Y-axis: x[n]
PARTC)
%To plot given cosine function where w(n) is random noise with
%zero mean and unit variance.
n=0:50;
x=cos(0.04*pi*n)+0.2*randn(size(n));
stem(n,x);
title('Cosine Function with Noise');
xlabel('X-axis');
ylabel('Y-axis');
4
Circuit Simulation Laboratory
Fig 2.4 Cosine Function with noise where X-axis: n, Y-axis: x[n]
5
Circuit Simulation Laboratory
PART D)
%Plot the real part, imaginary part, magnitude and phase of
%given exponential signal
n=-20:20;
subplot(2,2,1);
x=real(exp((-0.1+0.3i)*n));
plot(n,x);
title('Real part');
xlabel('x-axis');
ylabel('Real part');
subplot(2,2,2);
y=imag(exp((-0.1+0.3i)*n));
plot(n,y);
title('Imaginary part');
xlabel('x-axis');
ylabel('Imaginary part');
subplot(2,2,3);
z=angle(exp((-0.1+0.3i)*n));
plot(n,z);
title('Phase');
xlabel('x-axis');
ylabel('Phase');
subplot(2,2,4);
w=abs(exp((-0.1+0.3i)*n));
plot(n,w);
title('Magnitude');
xlabel('x-axis');
ylabel('Magnitude');
6
Circuit Simulation Laboratory
Fig 2.4 Real part, Imaginary part, Phase, Magnitude of exponential signal
INFORMATION:-
Absolute:
7
Circuit Simulation Laboratory
Stem:
Angle:
8
Circuit Simulation Laboratory
Real:
Randn:
9
Circuit Simulation Laboratory
Imag:
10
Circuit Simulation Laboratory
Conclusion:-
Thus, we have implemented the equations of signals and plotted the real, imaginary, amplitude
and phase spectra of the given exponential signal using MATLAB functions. Also, we have
created our own MATLAB functions such as unit step function and impulse function from
DIRAC function in the MATLAB.
11