DSP_LAB_Test
DSP_LAB_Test
A = [1,2,3;4,5,6] is a matrix.
x = [1 2 3 4 5 6 7 8 9 10];
x = [-100:20:100];
y = x. ^2;
plot(x, y)
x = [-100:5:100];
y = x. ^2;
plot(x, y)
xlabel('t')
ylabel('sin(t)')
title ('Plot of the Sine Function')
grid on
axis ([-5 10 -7 7])
Page 1 of 15
plot(x, y), xlabel('x'), ylabel('Sin(x)'), title('Sin(x) Graph'),
grid on, axis equal
Using MATLAB, we want to generate samples of x(t) at time instances 0:0.01:1. We will discuss three
approaches.
20. Using the plot command for multiple plots, plot y = atan(x) and y = acot(x) on the same
graph for values of x de_ned by x = -pi/2:pi/30:pi/2.
syms x
f = x^2*exp(x) - 5*x^3;
22. Defne the following function using syms:
t = [0:0.01:5];
y=zeros(size(t))
plot(t,y)
Page 2 of 15
Day 2
26. Write a Matlab code to draw following image.
%plot ‐s 560,300
t = [0:0.01:2];
x = sin(2*pi*t);
plot(t,x,t,zeros(size(t)),'k‐‐'), ylim([‐1.1 1.1])
xlabel('t [sec]');
ylabel('x(t)');
%plot ‐s 560,420
N = 20;
n = 0:N‐1;
x = sin(2*pi/N*n);
subplot(2,1,1); plot(n,x), axis tight
subplot(2,1,2); stem(n,x), axis tight
Page 3 of 15
28. Write a Matlab code to draw the folwing image.
%plot ‐s 560,200
plot(n,x,'k‐‐'), hold on
stem(n,x,'filled','markersize',4), hold off
ylim([‐1.1 1.1])
clc
clear all
x=0:1:40;
y=10*sin(2*pi*x/15);
subplot(2,1,1)
plot(x,y) %CT
title('CT sine wave')
grid
subplot(2,1,2)
stem(x,y) %DT
title('DT sine wave')
grid
Page 4 of 15
30. Plot Complex Signals
Rectangular Form
−n 2 πn
x [ n ] =e N e N
%plot ‐s 560,350
subplot(2,1,1); stem(n,real(x),'markersize',4), ylabel('real')
subplot(2,1,2); stem(n,imag(x),'markersize',4), ylabel('imag'),
xlabel('n')
%plot ‐s 560,420
plot3(n,real(x),imag(x),'o','markersize',4)
xlabel('n'), ylabel('real'), zlabel('imag')
grid on
Page 5 of 15
polar form:
%plot ‐s 560,420
%% magnitude and phase
subplot(2,1,1); stem(n,abs(x),'markersize',3), ylabel('amplitude')
subplot(2,1,2); stem(n,(phase(x)),'markersize',3), ylabel('phase'),
xlabel('n')
%% polar coordinate
polar(angle(x),abs(x),'o') % theta in
radian
Page 6 of 15
Page 7 of 15
Day 3
2. Properties:
1. Signal Properties
%plot ‐s 560,120
n = 0:N‐1;
N = 8;
x = [0 1 2 3 4 3 2 1];
xlim([‐16,23]), ylim([‐1,5])
stem(n,x,'k','filled','markersize',4);
Page 8 of 15
%plot ‐s 560,120
%% periodic using mod
y = [];
n = ‐16:23;
for i = 1:length(n)
y(i) = x(mod(n(i),N)+1);
end
stem(n,y,'k','filled','markersize',4), axis tight
ylim([‐1,5])
xp = repmat(x,1,5);
stem(n,xp,'k','filled','markersize',4), axis tight
ylim([‐1 5])
Page 9 of 15
Identical Signal (Integer Multiple)
N = 15;
k = 1;
n = 1:N‐1;
for i = 1:N‐1
xn(i) = cos(2*pi*k*i/N);
end
subplot(2,1,1), stem(n,xn)
hold on
plot(n,xn)
N = 15;
k = 1;
n = 1:N‐1;
for i = 1:N‐1
xn2(i) = cos((2*pi*k/N + 2*pi)*i);
end
subplot(2,1,2), stem(n,xn2)
N = 15;
k = 1;
n = 1:N‐1;
for i = 1:N‐1
xn2(i) = cos((2*pi*k*i/N)*i);
end
subplot(2,1,2), stem(n,xn2)
Aliasing
Page 10 of 15
Plot a signal:
t = linspace(0,10*2*pi,300);
x = sin(t);
plot(t,x), axis tight, xlabel('sec')
Page 11 of 15
%plot ‐s 560,200 if sample point is 20:
t = linspace(0,10*2*pi,300);
x = sin(t);
ts = linspace(0,10*2*pi,20);
xs = sin(ts);
plot(t,x,ts,xs,'o‐‐'), axis tight, xlabel('sec')
Signal Visualization
1. Unit Sample Sequence
n=-10:20
ns=[zeros(1,10) 1 zeros(1,20)];
subplot(3,1,1);
stem(n,ns);
title('unit impulse');
3. Ramp Sequence
x=input('enter the length of ramp
sequence')
enter the length of ramp sequence
x =7
t=0:7;
subplot(2,2,1);stem(t,t);
xlabel('c');
ylabel('Amplitude');
title(' Ramp Sequence');
Page 12 of 15
stem(n,x2);
xlabel('n');ylabel('x(n)');
title('Exponential Sequence');
Page 13 of 15
Day 4
To write a MATLAB program to find if the given signal is even or
odd
Even/Odd Signals A real signa x[n] l is even if x[-n]=x[n]
clc;
clear all;
close all;
t=-5:0.001:5;
A=0.8;
x1=A.^(t);
x2=A.^(-t);
if(x2==x1)
disp('The given signal is even signal');
else
if(x2==(-x1))
disp('The given signal is odd signal');
else
disp('The given signal is neither even nor odd');
end
end
1. Signal addition
x=[1 2 3 4];
y=[1 1 1 1];
subplot(3,1,1);
stem(x);
title('X');
subplot(3,1,2);
stem(y);
title('Y');
z=x+y;
subplot(3,1,3);
stem(z);
title('Z=X+Y');
Page 14 of 15
Addition:
n1 = 0:4;
x1 = [0 1 2 3 4];
n2 = -2:2;
x2 = [2 2 2 2 2];
n =min(min(n1),min(n2)):max(max(n1),max(n2)); % duration of y(n)
y1 =zeros(1,length(n));y2 = y1; %initialization
y1(find((n>=min(n1))&(n<=max(n1))==1))=x1; % x1 with duration of y
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2; % x2 with duration of y
y = y1+y2; %sequence addition
stem(n,y);
2. Subtraction
n1 = 0:3;
x1 = [1 2 3 4];
n2 = -2:1;
x2 = [1 1 1 1];
n =min(min(n1),min(n2)):max(max(n1),max(n2)); % duration of y(n)
y1 =zeros(1,length(n));y2 = y1; %initialization
y1(find((n>=min(n1))&(n<=max(n1))==1))=x1; % x1 with duration of y
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2; % x2 with duration of y
y = y1-y2; %sequence addition
stem(n,y);
3. Multiplication
By using ‘ * ‘ ( asterisk) operator we can perform multiplication of signals. Eg:
n1 = 0:3;
x1 = [1 2 3 4];
n2 = -2:1;
x2 = [1 3 3 2];
n =min(min(n1),min(n2)):max(max(n1),max(n2)); % duration of y(n)
y1 =zeros(1,length(n));y2 = y1; %initialization
y1(find((n>=min(n1))&(n<=max(n1))==1))=x1; % x1 with duration of y
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2; % x2 with duration of y
y = y1 .* y2; %sequence addition
stem(n,y);
4. Shifting a Signal
Page 15 of 15