Sunday, January 12, 2020

MATHLAB

1. Generate Noisy signal using std

-------------------------------------Script Start Hear ------------------------------------
clear all;
sig=1.;mu=0.; dc= 0.5;
for i=1:1000;
  x(i)=i;
  y(i)= dc + normrnd(mu,sig);
end;
plot(x,y,'b');hold on;
sig=.2; mu=0; dc=3.;
for i=1:1000;
  x(i)= i;
  y(i)= dc + normrnd(mu,sig);
 end;
 plot(x,y,'r'); hold off;

---------------------------------------End Hear---------------------------------------------


2.


---------------------------------------Script Start Hear ------------------------------------
clear all;
sig= 0.2; mu=0.; dc = 3.0;
for i=1:1000;
  y(i)= dc +normrnd(mu,sig);
end;
 n=10; hist(y,n);
 [h,z]=hist(y,n);
 sum=0;sig=0;
 for i=a:n;
   sum=sum+z(i)*h(i);
 end
sum=sum/1000;
for i=1:n;
  sig=sig+((z(i)-sum)^2)*h(i);
 end;
sig=sqrt(sig/9999); 

-------------------------------------End Hear-----------------------------------------------



3. 

---------------------------------------Script Start Hear ------------------------------------
clear all;
for i=1:1000;
  z = pi*i/180.;
  x(i)=i;
  y1(i)=10*sin(z)+1*(rand-0.5);
  y2(i)=10*sin(z)+5*(rand-0.5);
  t(i)=10*sin(z);
end;
figure(1);
plot(x,y1,'.');line(x,t);
figure(2);
plot(x,y2,'.'); line(x,t);

-------------------------------------End Hear-----------------------------------------------



4.


---------------------------------------Script Start Hear ------------------------------------
clear all;
for i=1:1000;
  z = pi*i/180.;
  x(i)=i;
  y1(i)=10*sin(z)+1*(rand-0.5);
  y2(i)=10*sin(z)+5*(rand-0.5);
  t(i)=10*sin(z);
end;
figure(1);
plot(x,y1,'.');line(x,t);
figure(2);
plot(x,y2,'.'); line(x,t);

-------------------------------------End Hear-----------------------------------------------






5.

---------------------------------------Script Start Hear ------------------------------------

clear all;
[x,y] = meshgrid(-1:.25:1);
z=sin(x.^2+y.^2);
[xi,yi]=meshgrid(-1:.04:1);
zi=interp2(x,y,z,xi,yi,'linear');
surf(xi,yi,zi)

-------------------------------------End Hear-----------------------------------------------






















6.

---------------------------------------Script Start Hear ------------------------------------

[x,y]=meshgrid(-3:3);
z=peaks(7);
figure;
surf(x,y,z);
title('original Sampling');
[xq,yq]=meshgrid(-3:0.25:3);
zq=interp2(x,y,z,xq,yq,'cubic');
figure;
surf(xq,yq,zq);
title('Over a Finer Grid'
-------------------------------------End Hear-----------------------------------------------











7.  Linear Equation

---------------------------------------Script Start Hear ------------------------------------
clear all;
% x=[3,2,-1] = 10;
% y=[-1,3,2]  = 5;
% z=[1,-1,-1] = -1;

% Ax = b
% x=b/A

A=[3 2 -1; -1,3,2; 1 -1 -1];
b=[10;5;-1];
x=inv(A)*b

-------------------------------------End Hear-----------------------------------------------
you may get this answer,

x =

   -2.0000
    5.0000
   -6.0000

8.

---------------------------------------Script Start Hear ------------------------------------

clear all;
vs=10; rs=10e3; % parameters
k=0;            % counter
for x=0:1e3:50e3;
    k=k+1;
    rl(k)=x;    %load resister
    pl(k)=((vs/(rs+rl(k)))^2)*rl(k); % power dessipation
end;
plot(rl,pl,'.-')
title('power Dileverd to Load');
xlabel('Load Resistance in Ohms');
ylabel('Power in Watts');

-------------------------------------End Hear-----------------------------------------------


 9. R-C Network

---------------------------------------Script Start Hear ------------------------------------
clear all;
% funtion rceval is used
c=10.0e-6;r1=2500; r2=10000;
[v1,t1]=rceval(r1,c);
[v2,t2]=rceval(r2,c);
% plot the voltage
plot(t1,v1,t2,v2)
axis([ 0 1 0 6 ]);
text(0.55,5.5,'*is for 2500 Ohms')
text(0.55,5.0,'+is for 10000 Ohms'


-------------------------------------End Hear-----------------------------------------------



 10. Sine Wave with Noice

---------------------------------------Script Start Hear ------------------------------------

clear all;
t=linspace(0,10,512);
s=sin(2*pi/5*t);
n=0.1*randn(size(t));
x=s+n;
plot(t,s,'-r'); hold on;
plot(t,x,'-b'); hold off;
snr=20*log10(std(s)/std(n));
disp(snr);

-------------------------------------End Hear-----------------------------------------------








11.  Smoothing Electrical Signal

---------------------------------------Script Start Hear ------------------------------------
clear all;
t=linspace(0,10,512);
s=sin(2*pi/5*t);
n=0.1*randn(size(t));
x=s+n;
plot(t,s,'-r'); hold on;
plot(t,x,'-b'); hold off;
snr=20*log10(std(s)/std(n));
disp(snr);
subplot(2,1,1);
plot(t,x,'b')
snr=20*log10(std(s)/std(n));
subplot(2,1,2);
y=smooth(x,10);
plot(t,y,'r')
-------------------------------------End Hear-----------------------------------------------


Search This Blog