-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhuffman_encoding.m
70 lines (58 loc) · 2.07 KB
/
huffman_encoding.m
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
function [dictionary,avglen] = huffman_encoding(symbol,p)
m = length(symbol);
dictionary = [];
cache = [];
for i=1:m
dictionary = [dictionary;[{symbol(i)},{[]}]];
cache = [cache;[{[symbol(i)]}]];
end
%map = containers.Map(p,linspace(1,m,m));
map = [p linspace(1,m,m)'];
index_map = zeros(256,1);
for i=1:length(symbol)
index_map(symbol(i)) = i;
end
count = 0;
while(length(map))>1
%disp(length(map));
%fprintf('\n');
count = count+1;
map = sortrows(map);
dict_one = cache{map(1,2)};
dict_two = cache{map(2,2)};
for i=1:length(cache{map(1,2)})
%dictionary{index_map(dict_one(i)),2} = [dictionary{index_map(dict_one(i)),2} 1];
dictionary{index_map(dict_one(i)),2} = [1 dictionary{index_map(dict_one(i)),2}];
end
for i=1:length(cache{map(2,2)})
%dictionary{index_map(dict_two(i)),2} = [dictionary{index_map(dict_two(i)),2} 0];
dictionary{index_map(dict_two(i)),2} = [0 dictionary{index_map(dict_two(i)),2}];
end
cache{map(2,2)} = [cache{map(2,2)} cache{map(1,2)}];
map = [map;[map(1,1)+map(2,1) map(2,2)]];
map = map(3:length(map),:);
%break;
if(length(map)==2)
map = sortrows(map);
dict_one = cache{map(1,2)};
dict_two = cache{map(2,2)};
for i=1:length(cache{map(1,2)})
%dictionary{index_map(dict_one(i)),2} = [dictionary{index_map(dict_one(i)),2} 1];
dictionary{index_map(dict_one(i)),2} = [1 dictionary{index_map(dict_one(i)),2}];
end
for i=1:length(cache{map(2,2)})
%dictionary{index_map(dict_two(i)),2} = [dictionary{index_map(dict_two(i)),2} 0];
dictionary{index_map(dict_two(i)),2} = [0 dictionary{index_map(dict_two(i)),2}];
end
cache{map(2,2)} = [cache{map(2,2)} cache{map(1,2)}];
map = [map;[map(1,1)+map(2,1) map(2,2)]];
map = map(3:length(map),:);
break;
end
end
len = zeros(length(symbol),1);
for i=1:length(symbol)
len(i) = length(dictionary{i,2});
end
avglen = sum(p.*len);
end