Skip to content

Commit

Permalink
SM: Aula 09
Browse files Browse the repository at this point in the history
  • Loading branch information
JCapucho committed Dec 13, 2023
1 parent 327a337 commit 3a21147
Show file tree
Hide file tree
Showing 19 changed files with 11,678 additions and 0 deletions.
3 changes: 3 additions & 0 deletions 2ano1/SM/aula09/Alfabeto1.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function [Simbolos] = Alfabeto1(Texto)
Simbolos = unique(Texto);
end
4 changes: 4 additions & 0 deletions 2ano1/SM/aula09/Alfabeto2.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function [Simbolos, Frequencia] = Alfabeto2(Texto)
[Frequencia, Simbolos] = groupcounts(Texto');
Frequencia = Frequencia / sum(Frequencia);
end
6 changes: 6 additions & 0 deletions 2ano1/SM/aula09/Entropia.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function [H] = Entropia(Texto)
[~, Frequencia] = Alfabeto2(Texto);

H = -sum(Frequencia .* log2(Frequencia));
end

12 changes: 12 additions & 0 deletions 2ano1/SM/aula09/GeraMensagem.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function [NumBits, NumBPS] = GeraMensagem(f, CompMesg, nBits)
% Utilizamos os índices dos símbolos como o alfabeto
alfabeto = 1:length(f);
% Geramos a mensagem aleatória como uma matriz (CompMesg x 1) que
% utiliza as probabilidades que nos foram passadas e o alfabeto que
% criamos.
msg = randsrc(CompMesg, 1, [alfabeto; f]);

NumBits = sum(nBits(msg));
NumBPS = NumBits / CompMesg;
end

5 changes: 5 additions & 0 deletions 2ano1/SM/aula09/HuffmanCoding.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function [SimbolosOrdenados, Codigos] = HuffmanCoding(Simbolos, Frequencia)
[~, idxs] = sort(Frequencia);
SimbolosOrdenados = Simbolos(idxs);
end

11 changes: 11 additions & 0 deletions 2ano1/SM/aula09/MakeCodes.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function [Codigos] = MakeCodes(Texto)
[Simbolos, ~] = Alfabeto2(Texto);

N = length(Simbolos);

Codigos = cell(N, 1);

for k=1:length(Simbolos)
Codigos{k} = [repmat('1', 1, k - 1) '0'];
end
end
14 changes: 14 additions & 0 deletions 2ano1/SM/aula09/NumeroBitsCodigo2.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function [NumBits, mensagem] = NumeroBitsCodigo2(Texto)
[Simbolos, ~] = Alfabeto2(Texto);

Codigos = MakeCodes(Texto);

[~, indexes] = ismember(Texto, Simbolos);

mensagem = [];
for idx=indexes
mensagem = [mensagem Codigos{idx}];
end

NumBits = length(mensagem);
end
Binary file added 2ano1/SM/aula09/assets/HistrogramaSimbolos.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 3a21147

Please sign in to comment.