-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculateFano.m
More file actions
110 lines (95 loc) · 2.62 KB
/
calculateFano.m
File metadata and controls
110 lines (95 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
function [L tal eficiencia Resultado fano]=calculateFano(mensaje)
mensaje = char(mensaje);
mensaje = strrep(mensaje,' ','_'); %Se reemplaza el espacio por un "_" para una mejor visualizacion
disp(mensaje)
simbolo = unique(mensaje); %Obtengo todos los distintos caracteres presentes
lM = length(mensaje); %Número total de caracteres
lS = length(simbolo); %Número total de caracteres
for i=1:lS %Identifico cuantas veces se repite cada caracter distinto
cont = 0; %Variables auxiliar para contar el número de repeticiones de un caracter
codigo{i,1}='';
for j=1:lM
if simbolo(i)==mensaje(j)
cont=cont+1;
end
end
prob(i,1)=cont/lM; %matriz para almacenar las probabilidades de cada simbolo
end
respaldo=[];
%
% %Se ordena la matriz de probabilidades de forma descendente
[prob,o]=sort(prob,'descend');
simbolo=simbolo(o);
inicio=1; fin=length(prob); cont=0;
while cont ~= length(prob)
P = prob(inicio:fin);
ref= sum(P)/2;
acum=0;resta1=1;resta2=1;i=1;k=1;
while(resta1==resta2)
acum=acum+P(i);
resta2=abs(ref-acum);
if resta2<resta1
k=i+inicio-1;
resta1=resta2;
end
i=i+1;
end
%Agrego un 0 al codigo desde el punto inicial hasta el punto medio
%y agrego un 1 al codigo desde el punto medio hasta el fin
for i=inicio:k
codigo{i,1}=strcat(codigo{i,1},'0');
end
for i=k+1:fin
codigo{i,1}=strcat(codigo{i,1},'1');
end
%************************************
if inicio == k
cont=cont+1;
if fin == k+1
cont=cont+1;
L=length(respaldo);
if cont ~= length(prob)
inicio = respaldo(L-1);
fin = respaldo(L);
if L > 2
respaldo = respaldo(1:L-2);
else
respaldo = [];
end
end
else
inicio = k+1;
end
else
if fin == k+1
cont=cont+1;
else
respaldo =[respaldo k+1 fin];
end
fin=k;
end
end
fano=mensaje;
%Se crea el vector de bits de la codificacion Huffman
for j=1:length(simbolo)
fano = strrep(fano, simbolo(j), codigo{j,1});
end
%creamos un cellarray para observar las variables, probabilidad y codigo
for j=1:1:lS
Resultado{j,1} = simbolo(j);
Resultado{j,2} = num2str(prob(j));
Resultado{j,3} = codigo{j,1};
end
%Longitud Media
L=0;
for j=1:1:(length(simbolo))
Efc=prob(j,1)*length(codigo{j,1});
L=L+Efc;
end
%Se presenta el resultado
%Cálculo de la Entropia
my_str=simbolo;
tal = sum(prob.*(log2(1./prob)));
%Cálculo de la Eficiencia
eficiencia=(tal/L)*100;
end