0% found this document useful (0 votes)
71 views7 pages

Constanta Depi Matlab

The document contains code examples for introducing MATLAB code to perform various tasks. It includes examples of: 1. Plotting functions and calculating derivatives and integrals 2. Performing operations on vectors and matrices 3. Generating and plotting random variables and distributions 4. Modeling and analyzing signals and systems 5. Introductory digital communication techniques like signal detection in noise The code examples cover fundamental concepts in mathematics, statistics, signal processing, and communications including plotting functions, linear algebra operations, probability distributions, signal modeling and analysis, and basic digital communication methods.

Uploaded by

Adrian Hurmuz
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views7 pages

Constanta Depi Matlab

The document contains code examples for introducing MATLAB code to perform various tasks. It includes examples of: 1. Plotting functions and calculating derivatives and integrals 2. Performing operations on vectors and matrices 3. Generating and plotting random variables and distributions 4. Modeling and analyzing signals and systems 5. Introductory digital communication techniques like signal detection in noise The code examples cover fundamental concepts in mathematics, statistics, signal processing, and communications including plotting functions, linear algebra operations, probability distributions, signal modeling and analysis, and basic digital communication methods.

Uploaded by

Adrian Hurmuz
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Introduceti in Matlab urmatoarele linii de cod:

Lucrarea 1:
Ex.1
t=-10:0.05:10;
f=cos(t);
plot(t,f,'.k' );

Ex.2
v1=1:2:9;
v2 = [7 3 8 1 -2]
v_adun = v1+v2
v_scad = v1-v2
v_prod = v1*v2
v_prodscalar = v1.*v2
Ex.3
A=[zeros(1,3); (-2)*ones(1,3)]
Ex.4
t1 = -2*pi:0.1:2*pi;
f1 = cos(t1);
df1 = diff(f1)./0.1; %aproximarea derivatei - viteza de variatie
Integf1 = cumsum(f1).*0.1; %aproximarea integralei - aria subgraficului
plot(t1, f1, 'k' ); %functia
hold on; plot(t1(1:end-1), df1, 'b'); % derivata - nu se poate calcula
% in ultimul pct
plot(t1, Integf1, 'r'); % integrala
zero_cross1 = [ ]; %initializam sirul indicilor pentru trecerile prin
%zero
for i = 1:length(df1)-1
if or(and(df1(i)>0, df1(i+1)<0), and(df1(i)<0, df1(i+1)>0))
zero_cross1 = [zero_cross1 i];
end;
end
plot( t1(zero_cross1), zeros(size(zero_cross1)), '*g'), hold off;

Ex.5.
function [w]=gauss_var(mu,sigma,dom_min,dom_max);
x=dom_min:1:dom_max;
w=(1/(sigma*sqrt(2*pi)))*exp(-(((x-mu).^2)/(2* sigma^2)));
F=cumsum(w); %functia de repartitie

subplot(1,2,1);plot(x , w, 'k');
subplot(1,2,2);plot(x , F, 'b');

Lucrarea 2:
Ex.1
x=rand(1,50);
plot(x,.k);
m_x=mean(x)
std_x = std(x)
Ex.2
x=randn(1,50);
plot(x,.r);

Ex.3
X=rand(1,100); %cele 100 de realizari particulare ale lui X
figure(1);plot(X,'.');
mX=mean(X) % media lui X
sX=std(X)%dispersia
[hX,absX]=hist(X,20);%histograma
figure(2);bar(absX,hX/100);% densitatea de probabilitate
HX=cumsum(hX/100);
figure(3);plot(absX,HX);
a=1;b=2;
Y=a*X+b;
figure(1);hold on;plot(Y,'k');
mY=mean(Y) %media lui Y
sY=std(Y)%dispersia
[hY,absY]=hist(Y,20);%histograma
figure(2);hold on;bar(absY,hY/100,'k');% densitatea de probabilitate
HY=cumsum(hY/100);
figure(3); hold on; plot(absY,HY,'k');

Lucrarea 3:
Ex.1
x=-50:50;
y=-50:50;
mx=0;my=0;
sx=2;sy=3;
rxy=0.1;
const1=1/(2*pi*sx*sy*sqrt(1-rxy));
const2=1/(2*(1-rxy));
sx2=2*sx^2 ;
sy2=2*sy^2;
rsxsy=2*rxy/(sx*sy);
for i=1:length(x)

for j=1:length(y)
% calculam densitatea de probabilitate de ordinul 2
w(i,j)=const1*exp(-const2*(....
((x(i)-mx)^2)/sx2-rsxsy*(x(i)-mx)*(y(j)-my)+((y(j)-my)^2)/sy2));
end
end % calculam densitatile marginale
wx=sum(w);
wy=sum(w');
% afisam graficul densitatii de probabilitate de ordinul 2
subplot(1,3,1);mesh(x,y,w);colormap(gray(32));axis([-50 50 -50 50 0 0.03])
% afisam graficele densitatilor marginale
subplot(1,3,2);plot(x,wx);
subplot(1,3,3);plot(y,wy);

Ex.2
x=randn(1,400);%200 de realizari particulare ale unei gaussiene de medie si
dispersie 1.
y=-2+2*randn(1,400);%medie -2 si dispesie 2
%vom calcula matricea de coocurenta ^n 11x11 puncte
mx=min(x);Mx=max(x)
my=min(y);My=max(y);
absx=mx:(Mx-mx)/10:Mx;
absy=my:(My-my)/10:My;
for i=1:10
for j=1:10
M(i,j)=sum((x<absx(i+1)&x>=absx(i))&(y<absy(j+1)&y>=absy(j)));
end;
end;
figure;surf(absx(1:end-1),absy(1:end-1),M);colormap(gray(256));

Ex.3
N=200;
x=rand(1,N);
%y=rand(1,N);
y=2*x+0.5*rand(1,N);
plot(x,y,'.');
Sx=sum(x);
Sy=sum(y);
Sxx=sum(x.*x);
Syy=sum(y.*y);
Sxy=sum(x.*y);
%panta dreptei de regresie
a=(N*Sxy-Sx*Sy)/(N*Sxx-Sx*Sx);
b=(Sy-a*Sx)/N;
%calculam punctele dreptei
xd=sort(x); yd=a*xd+b;
%trasam dreapta de regresie
hold on; plot(xd,yd,'r'); %eroarea de aproximare
e=((N*Syy-Sy*Sy)/(N*Sxx-Sx*Sx))-a^2;
fprintf('erorea este= %d',e);

Lucrarea 4:
Ex.1
%close all; clear all;
N = 2000; % numarul de realizari particulare
t = -5*pi : 0.5 : 5*pi;
w = 1; % pulsatia
A=1;
P = 2*pi * (rand(1,N)-0.5);
figure;
% reprezentam fiecare realizare particulara pe linia unei matrice
for i = 1:N
x(i,:)=randn(1,1)*sin(w*t);
%x(i,:) = A*sin(w*t+P(i));
plot(t, x(i,:)); hold on;
end
hold off;
% media si dispersia la un moment de timp sunt calculate
% pe coloanele matricei de date
m = mean(x, 1); s = std(x, 0, 1); figure;
subplot(1, 2, 1); plot(t, m);
title('media'); axis([-5*pi, 5*pi, -1, 1]);
subplot(1, 2, 2); plot(t, s); title('dispersia'); axis([-5*pi, 5*pi, -1, 1]);
% functia de autocorelatie
k1 = 0; k2 = 0; lg = length(t);
for k1 = 1:lg;
for k2 = 1:lg
R(k1, k2) = mean(x(:, k1) .* x(:, k2));
end
end
figure;
surf(t, t, R); colormap(gray(256));
title('functia de autocorelatie');

Ex.2
%close all;clear;
N=20;%numarul de realiz'ari
t=-5*pi:0.5:5*pi;
w=1;%pulsa'tia
A=1;
figure;subplot(2,2,1);
%reprezentam fiecare realzare particular'a a semnalului pe linia unei
%matrice
P=2*pi*(rand(1,N)-0.5);
for i=1:N
%x(i,:)=A*sin(w*t+P(i));
x(i,:)=randn(1,1)*sin(w*t);
plot(t,x(i,:));hold on;
end
title('semnalul aleator');
%media si dispersia la un moment de timp sunt pe coloanele matricei de date
m=mean(x,1);

s=std(x,0,1);
subplot(2,2,2);plot(t,m);title('media');axis([-5*pi,5*pi,-1,1]);
subplot(2,2,3);plot(t,s);title('dispersia');axis([-5*pi,5*pi,-1,1]);
%funct'ia de autocorela'tie
k1=0;k2=0;
lg=length(t);
for k1=1:lg;
for k2=1:lg
R(k1,k2)=mean(x(:,k1).*x(:,k2));
end
end
subplot(2,2,4);
surf(t,t,R);colormap(gray(256));title('functia de autocorelatie');
for k=1:lg;
r(k)=R(k,lg-k+1);
end
figure,plot(t,r); title('functia de autocorelatie');
X=abs(fft(x,[],2));
Q1=abs(mean(X.^2)/(10*pi));
figure,subplot(2,1,1);plot(Q1);title('densitatea spectrala de putere');
Q2=abs(fft(r));
subplot(2,1,2),plot(Q2);title('Teorema Wiener-Hincin')

Ex.3
close all;
A=2; w0=5;
t=-5:0.05:5;
x=A*sin(w0*t);
n=randn(1,length(t));
x_n=x+n;
figure;
plot(t,x,'.');
hold on;plot(t,x_n,'k');
XN=fft(x_n);
figure; plot(abs(XN));
FTB=zeros(size(XN));
FTB(6:12)=1;
hold on; plot(100*FTB);
X_F=XN.*FTB;
x_f=real(ifft(X_F));
figure(1);hold on; plot(t,x_f,'*K')

Lucrarea 5:
Ex. 1
%semnalele asociate celor doua simboluri
s0 = zeros(1,100); s1 = ones(1,100);
% zgomotul
medie_n = 0; sigma_n = 0.1;

n = randn(1, 100) * sigma_n + medie_n;


r0 = s0 + n;
r1 = s1 + n;
figure,
subplot(1,3,1);plot(r0,'ok'),hold on, plot(r1,'.k'), hold off;title('a)');
medie_n = 5; sigma_n = 0.1;
n = randn(1, 100) * sigma_n + medie_n;
r0 = s0 + n;
r1 = s1 + n;
subplot(1,3,2);plot(r0,'ok'),hold on, plot(r1,'.k'), hold off;title('b)');
medie_n = 0; sigma_n = 0.5;
n = randn(1, 100) * sigma_n + medie_n;
r0 = s0 + n;
r1 = s1 + n;
subplot(1,3,3); plot(r0,'ok'),hold on, plot(r1,'.k'), hold off;title('c)');

Ex.2
close all;clear all;
% definim t_k
t=5;
% semnalele asociate celor doua simboluri
s0 = zeros(1,100); s1 = ones(1,100);
% parametri zgomotului
medie_n = 0; sigma_n = 0.1;
M=20000;%numarul de repetari ale experimentului
for i=1:M
n = sigma_n * randn(1,100) + medie_n;
r0(i) = n(t) + s0(t);
n = sigma_n * randn(1,100) + medie_n;
r1(i) = n(t) + s1(t);
end
% vizualizam histogramele
[h0, x0] = hist(r0, 100);
m0 = mean(r0); d0 = std(r0);
[h1, x1] = hist(r1, 100);
m1 = mean(r1); d1 = std(r1);
figure,
m0 = mean(r0); d0 = std(r0);
[h1, x1] = hist(r1, 100);
m1 = mean(r1); d1 = std(r1);
subplot(1,3,2);
plot(x0, h0/20000,'k');hold on;
plot(x1, h1/20000,':k');title('b)');
medie_n = 0; sigma_n = 0.5;
for i=1:20000
n = sigma_n * randn(1,100) + medie_n;
r0(i) = n(t) + s0(t);
n = sigma_n * randn(1,100) + medie_n;

r1(i) = n(t) + s1(t);


end
[h0, x0] = hist(r0, 100);
m0 = mean(r0); d0 = std(r0);
[h1, x1] = hist(r1, 100);
m1 = mean(r1); d1 = std(r1);
subplot(1,3,3);
plot(x0, h0/20000,'k');hold on;
plot(x1, h1/20000,':k');title('c)');

Ex/3
% costurile
c00 = 0; c11 = 0; c01 = 1; c10 = 1;
% probabilitatile simbolurilor emise de sursa
P0 = 0.5; P1 = 0.5;
% esantioanele semnalului receptionat
r = 0.5;
% esantioanele semnalelor asociate simbolurilor
s0 = 0; s1 = 1;
% parametri zgomotului
medie n=0; sigma n=0.5;
% calculul pragului testului
ln K = log( (P0*(c01-c00))/(P1*(c10-c11)) )
% calculul raportului de plauzibilitate
ln lambda = sum (r .* (s1-s0))/(sigma n^2) - sum(s1.^2-s0.^2)/(2*sigma n^2)
% luarea deciziei
if (ln lambda < ln K)
fprintf('S-a emis S0');
else
fprintf('S-a emis S1');
end

You might also like