-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLZ.m
60 lines (51 loc) · 1.65 KB
/
LZ.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
clc
clear
close all
img_bitstream = '011001011010'
dictionary = solve(img_bitstream)
enc = prefix_locations(dictionary)
compression_ratio = length(img_bitstream)/length(enc)
function enc = prefix_locations(dictionary)
n = length(dictionary);
num_bits = ceil(log2(n));%changed
enc = [];%changed
result = zeros(1, n);
for i = 1:n
if length(dictionary{i}) == 1
result(i) = 0;
enc = [enc dec2bin(result(i), num_bits) dictionary{i}(end)];%changed
else
prefix = dictionary{i}(1:end-1);
matches = strcmp(prefix, dictionary);
match_locs = find(matches);
match_lengths = cellfun(@length, dictionary(matches));
[~, idx] = max(match_lengths);
result(i) = match_locs(idx);
enc = [enc dec2bin(result(i), num_bits) dictionary{i}(end)];
end
end
end
function dictionary = solve(s)
dictionary = {};
n = length(s);
start = 1;
end_ = 1;
while end_ <= n
if ismember(s(start:end_), dictionary)
end_ = end_ + 1;
continue
end
match = find(strfind(s(start:end_), s(end_)), 1);
if ~isempty(match) && match > 1
loc = dec2bin(match-1, ceil(log2(end_-start+1))); % binary location of repeated digit
bit = num2str(str2double(s(end_)) - str2double(s(end_-1))); % change in bit
dictionary{end+1} = [loc bit]; % add new term to dictionary
start = end_ + 1;
end_ = start;
else
dictionary{end+1} = s(start:end_);
start = end_ + 1;
end_ = start;
end
end
end