Thursday, 19 December 2013

Addition and Multiplication of Two Sequences

close all;
clear all;
clc;
x=input('enter sequence -1');
y=input('enter input sequence-2');
M=length(x);
N=length(y);
subplot(2,2,1);
stem(x);
xlabel('time');
ylabel('amplitude');
title('input sequence-1');
subplot(2,2,2);
stem(y);
xlabel('time');
ylabel('amplitude');
title('input sequence-2');
if M>N
z=x+[y,zeros(1,M-N)];
a=x.*[y,zeros(1,M-N)];
else
z=[x,zeros(1,N-M)]+y;
a=[x,zeros(1,N-M)].*y;
end;
subplot(2,2,3);
stem(z);
xlabel('time');
ylabel('amplitude');
title(' Addition of input sequences");
subplot(2,2,4);
stem(a);
xlabel('time');
ylabel('amplitude');
title('multiplication of input sequences ');

Output:
 sequence -1[1 4 8 6]
enter input sequence-2[1 6 8 9]



Sinc funtion and Triangular function

close all;
clear all;
clc;
t=-4:0.1:4;
x=sinc(pi*t);
subplot(2,1,1);
plot(x);
xlabel('time');
ylabel('amplitude');
title('sinc function');
y=0:0.5:2;
for j=0:3
x=(4*j)+y;
subplot(2,1,2);
plot(x,y);
hold on;
end;
for k=0:3
x=(4*k)-y;
subplot(2,1,2);
plot(x,y);
hold on;
end;
hold off;

Output:

Program for Impulse Function and Step function Generation

close all;
clear all;
clc;
n=[-5:5];
i=imp_scr(0,-5,5);
subplot(2,1,1);
stem(n,i);
xlabel('time');
ylabel('amplitude');
title('impulse function');
s=step_scr(0,-5,5);
subplot(2,1,2);
plot(n,s);
xlabel('time');
yalbel('amplitude');
title('step sequence');
%user defined functons

%for imp_scr
function[x,n]=imp_scr(no,n1,n2);
n=[n1:n2];
x=[(n-n0)==0];

%for step_scr
function[x,n]=step_scr[n0,n1,n2];
n=[n1:n2];
x=[(n-n0)>=0];


Note: the user has to type the user defined functions in a new window and save them with the exact same names of imp_scr and step_scr if the user defined functions are to work
The files are to be saved in the same directory as the original file.

Program for Ramp Function and Sequence

close all;
clear all;
clc;
t=0:1:25;
r=t;
subplot(2,1,1);
plot(t,r);
xlabel('time');
ylabel('amplitude');
title('ramp function');
subplot(2,1,2);
stem(t,r);
xlabel('time');
ylabel('amplitude');
title('ramp sequence');

Output:



Basic Waves in Discrete Time Domain

close all;
clear all;
clc;
t=0:0.001:2;
f=1;
w=2*pi*f;
a=sint(w*t);
subplot(2,2,1);
stem(t,a);
xlabel('time');
ylabel('amplitude');
title('sinusoidal signal');
b=cos(w*t);
subplot(2,2,2);
stem(t,b);
xlabel('time');
ylabel('amplitude');
title('co-sinusoidal signal');
c=square(w*t);
subplot(2,2,3);
stem(t,c);
xlabel('time');
ylabel('amplitude');
title('square signal');
d=sawtooth(w*t);
subplot(2,2,2);
stem(t,d);
xlabel('time');
ylabel('amplitude');
title('sawtooth signal');

Various Basic Signals

close all;
clear all;
clc;
t=0:0.001:2;
f=1;
w=2*pi*f;
a=sint(w*t);
subplot(2,2,1);
plot(t,a);
xlabel('time');
ylabel('amplitude');
title('sinusoidal signal');
b=cos(w*t);
subplot(2,2,2);
plot(t,b);
xlabel('time');
ylabel('amplitude');
title('co-sinusoidal signal');
c=square(w*t);
subplot(2,2,3);
plot(t,c);
xlabel('time');
ylabel('amplitude');
title('square signal');
d=sawtooth(w*t);
subplot(2,2,2);
plot(t,d);
xlabel('time');
ylabel('amplitude');
title('sawtooth signal');

Output:



Basic Operations on Matrices Matlab Code

clear all;
close all;
clc;
a=input('enter matrix A:');
b=input('eneter matrix B:');
c=input('enter Matrix C:');
disp('the size of matrix A is :');
disp(size(a));
disp('the size of matrix B is :');
disp(size(b));
disp('the size of matrix C is :');
disp(size(c));
disp('the addition of matrices A & B is:');
disp(a+b);
disp('the substraction of matrices A & B is :');
disp(a-b);
disp('the element by element multiplication of A & B is:');
disp(a.*b);
disp('The trace of matrix C is :');
disp(trace(c));
disp('the determinant of the matrix C is :');
disp(det(c));
disp ('the inverse of matrix C is :');
disp(inv(c));
disp(' the rank of matrix C is :');
disp(rank(c));

Outputs if the specied inputs as given  below are:

Matrix A : [1 2;3 4]
Matrix B : [3 4;5 6]
Matrix C:  [1 2;3 4]

The size of matrix A is
       2     2
The size of matrix B is
      2      2
The size of matrix c is
      2       2
the addition of matrices A & B is:  4     6
                                                    8      10
the substraction of matrices A & B is :  -2    -2
                                                            -2    -2

the element by element multiplication of A & B is:  3     8
                                                                            15  24

The trace of matrix C is : 5

the determinant of the matrix C is : -2

the inverse of matrix C is :   -2.000       1.000
.                                         1.5000      -0.5000

the rank of matrix C is : 2