Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

DSP_LAB_Test

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

DSP_LAB_Test

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Day 1

A = [1,2,3;4,5,6] is a matrix.

1. Create a 5-by-1 column vector of zeros using the zeros command.


2. Create a row vector b from 325 to 405 with an interval of 20.
3. Use sum to find the sum of vector A's elements.
4. Find max(A), min(A).
5. Create two different vectors of the same length and add them.
a = [2, 1, 3]; b = [4 2 1]; c = a + b
6. Now subtract them. c=a-b
7. Perform element-by-element multiplication on them. c = a .* b
8. Perform element-by-element division on them. c = a ./ b
9. Raise one of the vectors to the second power. c = a .^ 2
10. Create vector of ones.
11. Create 4 × 4 identity matrix using eye function.
12. rand(3, 5), magic(4)
13. Calculate sin(a)
14. Plotting
x = [0:5:100];
y = x;
plot(x, y)

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)

15. Plot the value of the sine function from 0 to 2π:


T=0:2*pi;
clear;
clc;
t=0:0.001:2*pi;
y=sin(2*pi*t);
plot(t,y,'g^')
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y);
16. Label the axes and add a title.
F=2;
A=5;
T = [0:0.01:10];
y = A*sin(2*pi*t*F);
plot(t,y, 'r*');

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

17. Plot cos(x) for above formula.


18. Create a script file and type the following code −
x = [0 : 0.01: 10];
y = exp(-x).* sin(2*x + 3);
plot(x, y), axis([0 10 -1 1])

19. Consider the following sum of sinusoidal functions.


t = [0 : 0.01: 2];

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.

21. Define the following function using syms:

syms x
f = x^2*exp(x) - 5*x^3;
22. Defne the following function using syms:

23. Plot a line on X-axis

t = [0:0.01:5];
y=zeros(size(t))
plot(t,y)

24. Plot sin(2*pi*t)

t = 0:0.1:10; % sample points from 0 to 2 in steps of 0.01


x = sin(2*pi*t); % Evaluate sin(2 pi t)
plot(t,x, 'b'); % Create plot with blue line
xlabel('t in sec'); ylabel('x(t) '); % Label axis
title('Plot of sin(2\pi t) '); % Title plot
hold on; % Create plot with blue line

Hs = stem(t ,x , 'g', 'filled'); % Stem-plot with handle Hs


set(Hs, 'markersize ',6);
hold off; % Change circle size

25. Stem Plot of sin(0.2 pi n)


n = 0:1:40; % sample index from 0 to 20
x = sin(0.1*pi*n); % Evaluate sin(0.2 pi n)
Hs = stem(n,x, 'b', 'filled'); % Stem-plot with handle Hs
set(Hs, 'markersize',4); % Change circle size
xlabel('n'); ylabel('x(n)'); % Label axis
title('Stem Plot of sin(0.2 pi n) '); % Title plot

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)');

27. Write a Matlab code to draw the folwing image.

%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])

29. Computing the graph of wave sequences both in CT and DT

1. plot(x,y) to obtain the graph in Continuous time(CT)


2. stem(x,y) to obtain the graph in Discrete time (DT)

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

A complex signal can be equivalently represented in two ways

Rectangular Form
−n 2 πn
x [ n ] =e N e N

%% real and imag parts


N = 20;
n = 0:2*N‐1;
x = exp(‐n/N).*exp(1j*2*pi/N*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')

% see the difference between 'phase' and 'angle'


subplot(2,1,1); stem(n,phase(x),'markersize',3), ylabel('phase')
subplot(2,1,2); stem(n,angle(x),'markersize',3), ylabel('angle'),
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

x[n + mN] = x[n] ∀m ∈ Z


Periodic Signals

%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);

31. Write a matlab code to draw following figure.

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])

Same thing we can used repmat command


%plot ‐s 560,120
%% 'repmat' command

xp = repmat(x,1,5);
stem(n,xp,'k','filled','markersize',4), axis tight
ylim([‐1 5])

Shifting Periodic Signals

%plot ‐s 560,400 %% circular shift


n = 0:N‐1;
x = [0 1 2 3 4 3 2 1];
xs = circshift(x',1)';
ys = [];
n = ‐16:23;
for i = 1:length(n)
ys(i) = xs(mod(n(i),N)+1);
end

subplot(3,1,1), stem(n,repmat(x,1,5),'k','filled','markersize',4), axis


tight, ylim([‐15])
subplot(3,1,2), stem(n,repmat(xs,1,5),'k','filled','markersize',4),
axis tight, ylim([‐1 5])
subplot(3,1,3), stem(n,ys,'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')

Anything less than 20 points will cause problems:

if sampling point=12 required 20


x = sin(t);
ts = linspace(0,10*2*pi,12);
xs = sin(ts);
plot(t,x,ts,xs,'o‐‐'), axis tight, xlabel('sec')

%plot ‐s 560,200 if sampling point=11 required 20


t = linspace(0,10*2*pi,300);
x = sin(t);
ts = linspace(0,10*2*pi,11);
xs = sin(ts);
plot(t,x,ts,xs,'o‐‐'), 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');

2. Unit Step sequence:


N=21;
x=ones(1,N);
n=0:1:N-1;
subplot(2,2,1);stem(n,x);
xlabel('n');ylabel('x(n)');
title('Unit Step Sequence');

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');

4. Exponential sequence: (0<a<1)


n = 0:1:20;
x2=0.8.^(n);
subplot(2,2,3);stem(n,x2);
xlabel('n');ylabel('x(n)');
title('Exponential Sequence');

5. Exponential sequence: (1<a)


x2=2.^(n);
subplot(2,2,3);stem(n,x2);
xlabel('n');ylabel('x(n)');
title('Exponential Sequence');

6. Exponential sequence: (-1<a<0)


x2=(-0.8).^(n);

Page 12 of 15
stem(n,x2);
xlabel('n');ylabel('x(n)');
title('Exponential Sequence');

7. Exponential sequence: (a<-1)


x2=(-2).^(n);
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

2. Calculate the Energy and Power of the following signal


x=[1 2 3 4 .5 2 3 7 8 .7 .8];

3. Given the following signal


A1(n)=[3 4 2 -3 5]
A2=[5 6 3 2]
a. Plot and stem A1
b. Plot and stem A2
c. Calculate sum of both sequences
d. Plot and stem the summation sequences.

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

n1=input('Enter the amount to be delayed');


n2=input('Enter the amount to be advanced');
n=-2:2;
x=[-2 3 0 1 5];
subplot(3,1,1);
stem(n,x);
title('Signal x(n)');
m=n+n1;
y=x;
subplot(3,1,2);
stem(m,y);
title('Delayed signal x(n-n1)');
t=n-n2;
z=x;
subplot(3,1,3);
stem(t,z);
title('Advanced signal x(n+n2)');

Page 15 of 15

You might also like