Saturday, February 20, 2010

Signal Generation

**************************************
% generate unit step sequence for N=20.
%Plot discrete values and level it.
**************************************
N=20;
xn=ones(1,N);
n=0:1:N-1;
subplot(2,1,1),stem(n,xn);
subplot(2,1,2),plot(n,xn);
xlabel('n');
ylabel('xn');
title('Unit Step Sequence');
**************************************
% Plot an exponential sequence (0.7)^n
% **************************************
N=20;
n=0:1:N-1;
xn=0.7.^n;
subplot(2,1,1),stem(n,xn);
subplot(2,1,2),plot(n,xn);
xlabel('n');
ylabel('xn');
title('Exponential Sequence');
**************************************
% Plot an sinusoidal sequence
**************************************
N=50;
n=0:1:N-1;
xn=cos(0.2*pi.*n);
subplot(2,2,1),stem(n,xn);
subplot(2,2,2),plot(n,xn);
xlabel('n');
ylabel('xn');
title('Sinusoidal Sequence');
xn=sin(0.2*pi.*n);
subplot(2,2,3),stem(n,xn);
subplot(2,2,4),plot(n,xn);
xlabel('n');
ylabel('xn');
title('Sinusoidal Sequence');
**************************************
% Addition of two sinusoidal sequence
% x= sin(0.2*pi*n) + sin (0.5*pi*n)
**************************************
N=50;
n=0:1:N-1;
xn= sin(0.3*pi.*n) + sin(0.7*pi.*n);
subplot(2,1,1),stem(n,xn);
subplot(2,1,2),plot(n,xn);
xlabel('n');
ylabel('xn');
title('Addition of two Sinusoidal Sequence');
**************************************
% triangular wave generation
**************************************
y=0:0.5:2;
for j=0:3
x=(4*j)+y;
plot(x,y)
hold on
end
for k=0:3;
x=(4*k)-y
plot(x,y)
hold on
end
hold off
**************************************
%sawtooth wave generation
**************************************
y=0:.5:2
for j=0:8
a=(2*j)+y
plot(a,y,'b')
hold on
end
x=2:2:18
for k=0:.01:2;
b=k;
plot(x,b,'b')
hold on
end
hold off
**************************************
% generation of square wave
**************************************
y=0:.001:2;
for j=0:2:12;
x=y;
plot(j,x,'r');
hold on;
end
for k=0:4:12;
x=k+y;
m=2;
plot(x,m,'r')
hold on
end
for k=2:4:12;
x=k+y;
m=0;
plot(x,m,'r');
hold on;
end
hold off
axis([0 12 -0.5 2.5])
**************************************
% Aliasing
**************************************
N=100;
n=0:1:N-1;
xn=3*cos(0.2*pi.*n);
subplot(3,1,1);
plot(n,xn);
grid;
xlabel('n');
ylabel('xn');
x1n=3*cos(2.2*pi.*n);
subplot(3,1,2);
plot(n,x1n);
grid;
xlabel('n');
ylabel('x1n');
x2n=3*cos(4.2*pi.*n);
subplot(3,1,3);
plot(n,x2n);
grid;
xlabel('n');
ylabel('x2n');
title('Sinusoidal Sequence');

No comments: