Wednesday, 18 June 2014

code for the generation of signals in matlab

Dear friends generation of signals in matlab is very simple method once you understand the code you can easily plot any kind of signal for your engineering works etc , here is some code and prepare plot signals along with diagram, read it carefully and try to understand each and every point of it,
PLOTTING A CONTINUOUS TIME COMPLEX EXPONENTIAL SIGNAL
 



c=10;
f=5;
ph=pi/2;
T=1/f;
t=0:T/40:10*T; %10 cycles
x=c*cos(2*pi*f*t+ph);
a=input('enter 1,0 or -1;a='); %enter either positive, negative, zero
y=exp(a*t);
z=y.*x;
plot(t,z),
xlabel('t'),
ylabel('z'),
title('PLOTTING A CONTINUOUS TIME COMPLEX EXPONENTIAL SIGNAL'),
grid on



 
   







TASK-3:
TO PLOT A PIECE-WISE SIGNAL





%for first segment
t0=-2:0.01:0;
x0=t0;
%for second segment
t1=0:0.01:3;
x1=4*ones(size(t1));
%for third segment
t2=3:0.01:4;
x2=-3*t2+4;
x=[x0 x1 x2 ];
t=[t0 t1 t2 ];
plot(t,x,'red','linewidth',3);
xlabel('time');
ylabel('x(t)');
title('PLOTTING PIECEWISE CONTINUOUS TIME SIGNAL');
axis([-3 4 -10 5]);
grid on;

 
TASK-4:
TIME TRANSFORMATION OFF A PIECE WISE SIGNAL

%for first segment
t0=-2:0.01:0;
x0=t0;
%for second segment
t1=0:0.01:3;
x1=4*ones(size(t1));
%for third segment
t2=3:0.01:4;
x2=-3*t2+4;
x=[x0 x1 x2 ];
t=[t0 t1 t2 ];
subplot(2,1,1),
plot(t,x,'red','linewidth',5), 
xlabel('time'),
ylabel('x(t)'),
title('TIME TRANSFORMATION OF CT SIGNAL(ORIGINAL SIGNAL)'),
grid on,
axis([-4 8 -10 5]);
%TIME TRANSFORMATION (Shifting, Scaling, Reversal)
t4=-2*t+4;
subplot(2,1,2),
plot(t4,x,'black','linewidth',5),
xlabel('time'),
ylabel('x(t)'),
title('TIME TRANSFORMATION OF CT SIGNAL(TRANSFORMED SIGNAL)'),
grid on,
axis([-4 8 -10 5]);
 

TASK-5:
3D PLOT OF  CONTINUOUS TIME EXPONENTIAL SIGNAL

t=0:0.0001:10;
x=20*exp(i*5*t);
subplot(2,2,1),
plot(t,real(x)),
xlabel('time'),
ylabel('real part'),
title('PLOTTING REAL PART');
subplot(2,2,2),
plot(t,imag(x)),
xlabel('time'),
ylabel('imag part'),
title('PLOTTING IMAGINARY PART');
subplot(2,2,3),
plot(t,x),
xlabel('time'),
ylabel('x'),
title('PLOTTING(X)');
subplot(2,2,4),
plot3(real(x),imag(x),t),
xlabel('real part'),
ylabel('imag part'),
zlabel('time'),
title('3D PLOT OF A CONTINUOUS TIME SIGNAL');

 TASK-6:
A)PLOTTING  CONTINUOUS TIME SQUARE WAVE USING FOURIER SERIES
B)PLOTTING CONTINUOUS TIME TRIANGULAR WAVE USING FOURIER SERIES






% Square Wave
t=-10:0.001:10;
dc=1/2;
sum=0;
for k=1:2:200;
    sum=sum+(3/(k*pi))*sin(k*t);
end
x=sum+dc;
subplot(2,1,1),
plot(t,x),
xlabel('time'),
ylabel('magnitude'),
title('square wave'),
 
%Triangular Wave
t=0:0.01:5;
sum=0;
for k=1:2:200;
    sum=sum+(-16/(k^2*pi^2))*cos(6*pi*k*t);
end
subplot(2,1,2),
plot(t,sum),
xlabel('time'),
ylabel('magnitude'),
title('triangular wave')
 
 

 












TASK-7:
SOLVING DERIVATIVES, INTEGRALS & DIFFERENTIAL EQUATIONS

% DERIVATIVE
syms x a b c f 
f=3*x^3-4*x^2+6*x-3;
a=diff(f,x)
b=diff(a,x)
c=diff(b,x)
RESULT:
%a = 9*x^2 - 8*x + 6
%b = 18*x - 8
%c = 18

% PARTIAL DERIVATIVE
f=x^2*y+x*y^3-x^3+y^2;
a=diff(f,x)
b=diff(f,y)
RESULT:
%a = - 3*x^2 + 2*x*y + y^3
%b =  x^2 + 3*x*y^2 + 2*y




%INTEGRALS
PART-A:
f=3*x^3-4*x^2+5*x-3;
a=int(f,x)
b=int(f,x,0,5)
RESULT:
% a = (3*x^4)/4 - (4*x^3)/3 + (5*x^2)/2 - 3*x
% b = 4195/12
PART-B:
f=cos(x)*sin(y);
a=int(f,x,pi/2,pi)
b=int(a,y,0,2*pi)
RESULT:
 % a = -sin(y)
 % b = 0

%DIFFERENTIAL EQUATION
PART-A:
d=dsolve('D2y+3*Dy+2=sin(t)','t')
RESULT:
% d =C18 - (2*t)/3 - (3*cos(t))/10 - sin(t)/10 + C19/exp(3*t) + 2/9
PART-B:
f=dsolve('3*D2y+4*Dy=sin(t)','t')
RESULT:
%f =C25 - (4*cos(t))/25 - (3*sin(t))/25 + C26/exp((4*t)/3)
PART-C:
g=dsolve('D2y+2*Dy+3=sin(x)','y(0)=0','x')
RESULT:
%g =C30/exp(2*x) - (3*x)/2 - (2*cos(x))/5 - sin(x)/5 - C30 + 2/5
TASK-8:
EVALUATING LAPLACE TRANSFORM AND Z-TRANSFORMS
% LAPLACE TRANSFORM
>> syms f a t F  C b
f=exp(-a*t)*heaviside(t); 
// heaviside(t), 0 for t<0, 1 for t>0 and 0.5 for t==0
F=laplace(f)
RESULT:
% F = 1/(a + s)
>> syms x h t X H Y y
x=sin(t);
h=10*exp(10*t);
X=laplace(x)
H=laplace(h)
Y=H*X;
y=ilaplace(Y) // inverse laplace 
RESULT:
% X = 1/(s^2 + 1)
%H  = 10/(s - 10)
%y  =(10*exp(10*t))/101 - (10*cos(t))/101 - (100*sin(t))/101
% z-transform
>>syms x h X H Y y a n
x=a^n;
h=b^(-n);
X=ztrans(x)
RESULT:
% X = -z/(a - z)
TASK-9:
DISCRETE TIME SIGNAL 
PLOTTING

 % DISCRETE TIME SEQUENCE
n=-4:4;
x=[9 1 -2 4 -5 6 -1 7 -8];
stem(n,x);
xlabel('time(sampled)');
ylabel('magnitude');
title('DT Sequence');
axis([-5 5 -10 10]);

 



% DISCRETE TIME EXPONENTIAL SIGNAL (n>=0)
% case 01(a>1)
c=2;
a=5;
n=0:15;
x=c*a.^n;
subplot(3,2,1);
stem(n,x,'linewidth',2);
xlabel('time(sampled)');
ylabel('magnitude');
title('DT exponential signal when a>1');
grid on;
%case 02(0<a<1)
c=2;
a=0.5;
n=0:15;
x=c*a.^n;
subplot(3,2,3);
stem(n,x,'linewidth',2);
xlabel('time(sampled)');
ylabel('magnitude');
title('DT exponential signal when 0<a<1');
grid on;
%case 03(a=1)
c=2;
a=1;
n=0:15;
x=c*a.^n;
subplot(3,2,5)
stem(n,x,'linewidth',2);
xlabel('time(sampled)');
ylabel('magnitude');
title('DT exponential signal when a=1');
grid on;
%case 04(a<-1)
c=4;
a=-2;
n=0:60;
x=c*a.^n;
subplot(3,2,2);
stem(n,x,'linewidth',2);
xlabel('time(sampled)');
ylabel('magnitude');
title('DT exponential signal when a<-1');
grid on;
%case 05(-1<a<0)
c=2;
a=-0.5;
n=0:30;
x=c*a.^n;
subplot(3,2,4);
stem(n,x,'linewidth',2);
xlabel('time(sampled)');
ylabel('magnitude');
title('DT exponential signal when -1<a<0');
grid on;
%case 06(a=-1)
c=2;
a=-1;
n=0:15;
x=c*a.^n;
subplot(3,2,6)
stem(n,x,'linewidth',2);
xlabel('time(sampled)');
ylabel('magnitude');
title('DT exponential signal when a=-1');
grid on;

 

% DISCRETE TIME EXPONENTIAL SIGNAL (n<=0)
% case 01(a>1)
c=2;
a=5;
n=-15:0;
x=c*a.^-n;
subplot(3,2,1);
stem(n,x,'linewidth',2);
xlabel('time(sampled)');
ylabel('magnitude');
title('DT exponential signal when a>1');
grid on;
%case 02(0<a<1)
c=2;
a=0.5;
n=-15:0;
x=c*a.^-n;
subplot(3,2,3);
stem(n,x,'linewidth',2);
xlabel('time(sampled)');
ylabel('magnitude');
title('DT exponential signal when 0<a<1');
grid on;
%case 03(a=1)
c=2;
a=1;
n=-15:0;
x=c*a.^-n;
subplot(3,2,5)
stem(n,x,'linewidth',2);
xlabel('time(sampled)');
ylabel('magnitude');
title('DT exponential signal when a=1');
grid on;
%case 04(a<-1)
c=4;
a=-2;
n=-60:0;
x=c*a.^-n;
subplot(3,2,2);
stem(n,x,'linewidth',2);
xlabel('time(sampled)');
ylabel('magnitude');
title('DT exponential signal when a<-1');
grid on;
%case 05(-1<a<0)
c=2;
a=-0.5;
n=-30:0;
x=c*a.^-n;
subplot(3,2,4);
stem(n,x,'linewidth',2);
xlabel('time(sampled)');
ylabel('magnitude');
title('DT exponential signal when -1<a<0');
grid on;
%case 06(a=-1)
c=2;
a=-1;
n=-15:0;
x=c*a.^-n;
subplot(3,2,6)
stem(n,x,'linewidth',2);
xlabel('time(sampled)');
ylabel('magnitude');
title('DT exponential signal when a=-1');
grid on;


 
TASK-10:
DISCRETE TIME CONVOLUTION  
n0=0:20;
x=5*(-0.5).^n0;
n1=-10:5;
h=(2/3).^n;
n2=-20:10;
y1=conv(x,h);
y=y1(1:length(n2));
subplot(3,1,1);
stem(n0,x);
xlabel('time(sampled)');
ylabel('magnitude x(n)');
title('input signal');
grid on;
subplot(3,1,2);
stem(n1,h);
xlabel('time(sampled)');
ylabel('magnitude h(n)');
title('transfer function h(n)');
grid on;
subplot(3,1,3);
stem(n2,y);
xlabel('time(sampled)');
ylabel('magnitude y(n)');
title('output signal');
grid on;

 

TASK-11:
CONTINUOUS TIME CONVOLUTION  
t=0:0.01:10;
x=ones(1,length(t));
h=2*exp(-1*t);
y1=conv(x,h)*0.01; //multiplying with step size
y=y1(1:length(t));
figure(1);
subplot(3,1,1);
plot(t,x);
xlabel('time');
ylabel('x(t)');
title('convolution of continuous time signal');
subplot(3,1,2);
plot(t,h);
xlabel('time');
ylabel('h(t)');
subplot(3,1,3);
plot(t,y);
xlabel('time');
ylabel('y(t)');
title('convolved signal')
grid on;

 

No comments: