Rei Hoxha Laborator3
Rei Hoxha Laborator3
Laborator nr.3
Lënda: “Perpunim Numerik i Thelluar i Sinjaleve”
Punoi Pranoi
The LZW algorithm is commonly used to compress GIF and TIFF image files and occasionally
for PDF and TXT files. It is part of the Unix operating system's file compression utility. The
method is simple to implement, versatile and capable of high throughput in hardware
implementations. Consequently, LZW is often used for general-purpose data compression in
many PC utilities.
How LZW compression works
The LZW compression algorithm reads a sequence of symbols, groups those symbols into strings
and then converts each string into codes. It takes each input sequence of bits of a given length --
say, 12 bits -- and creates an entry in a table for that particular bit pattern, consisting of the
pattern itself and a shorter code. The table is also called a dictionary or codebook. It stores
character sequences chosen dynamically from the input text and maintains correspondence
between the longest encountered words and a list of code values.
As the input is read, any repetitive results are substituted with the shorter code, effectively
compressing the total amount of input. The shorter code takes up less space than the string it
replaces, resulting in a smaller file. As the number of long, repetitive words increases in the input
data, the algorithm's efficiency also increases. Compression occurs when the output is a single
code instead of a longer string of characters. This code can be of any length and always has more
bits than a single character.
The LZW algorithm does not analyze the incoming text. It simply adds every new string of
characters it sees into a code table. Since it tries to recognize increasingly longer and repetitive
phrases and encode them, LZW is referred to as a greedy algorithm.
lzwInput = uint8('/WED/WE/WEE/WEB/WET');
[lzwOutput, lzwTable] = norm2lzw(lzwInput);
fprintf('\n');
fprintf('Input: ');
fprintf('%02x ', lzwInput);
fprintf('\n');
fprintf('Output: ');
fprintf('%04x ', lzwOutput);
fprintf('\n');
for ii = 257:length(lzwTable.codes)
fprintf('Code: %04x, LastCode %04x+%02x Length %3d\n', ii,
lzwTable.codes(ii).lastCode, lzwTable.codes(ii).c,
lzwTable.codes(ii).codeLength)
end;
% Tests the special decoder case
lzwInput = uint8('abcAAAzAAAAaAAAAAdAAAAAefAAAAAAAAAAAAAAacdc');
[lzwOutput, lzwTable] = norm2lzw(lzwInput);
fprintf('\n');
fprintf('Input: ');
fprintf('%02x ', lzwInput);
fprintf('\n');
fprintf('Output: ');
fprintf('%04x ', lzwOutput);
fprintf('\n');
for ii = 257:length(lzwTable.codes)
fprintf('Code: %04x, LastCode %04x+%02x Length %3d\n', ii,
lzwTable.codes(ii).lastCode, lzwTable.codes(ii).c,
lzwTable.codes(ii).codeLength)
end;
[lzwOutputd, lzwTabled] = lzw2norm(lzwOutput);
fprintf('\n');
fprintf('Input: ');
fprintf('%04x ', lzwOutput);
fprintf('\n');
fprintf('Output: ');
fprintf('%02x ', lzwOutputd);
fprintf('\n');
for ii = 257:length(lzwTabled.codes)
fprintf('Code: %04x, LastCode %04x+%02x Length %3d\n', ii,
lzwTabled.codes(ii).lastCode, lzwTabled.codes(ii).c,
lzwTabled.codes(ii).codeLength)
end;
Input: 2f 57 45 44 2f 57 45 2f 57 45 45 2f 57 45 42 2f 57 45 54
Output: 0030 0058 0046 0045 0101 0046 0105 0106 0102 0043 0105 0055
Code: 0101, LastCode 0030+57 Length 2
Code: 0102, LastCode 0058+45 Length 2
Code: 0103, LastCode 0046+44 Length 2
Code: 0104, LastCode 0045+2f Length 2
Code: 0105, LastCode 0101+45 Length 3
Code: 0106, LastCode 0046+2f Length 2
Code: 0107, LastCode 0105+45 Length 4
Code: 0108, LastCode 0106+57 Length 3
Code: 0109, LastCode 0102+42 Length 3
Code: 010a, LastCode 0043+2f Length 2
Code: 010b, LastCode 0105+54 Length 4
Input: 61 62 63 41 41 41 7a 41 41 41 41 61 41 41 41 41 41 64 41 41 41 41 41 65 66 41 41 41 41 41 41 41 41 41 41 41 41 41 41
61 63 64 63
Output: 0062 0063 0064 0042 0104 007b 0104 0104 0062 0107 0104 0065 010a 0042 0066 0067 010d 0111 0107 0062 0064
0065 0064
Code: 0101, LastCode 0062+62 Length 2
Code: 0102, LastCode 0063+63 Length 2
Code: 0103, LastCode 0064+41 Length 2
Code: 0104, LastCode 0042+41 Length 2
Code: 0105, LastCode 0104+7a Length 3
Code: 0106, LastCode 007b+41 Length 2
Code: 0107, LastCode 0104+41 Length 3
Code: 0108, LastCode 0104+61 Length 3
Code: 0109, LastCode 0062+41 Length 2
Code: 010a, LastCode 0107+41 Length 4
Code: 010b, LastCode 0104+64 Length 3
Code: 010c, LastCode 0065+41 Length 2
Code: 010d, LastCode 010a+41 Length 5
Code: 010e, LastCode 0042+65 Length 2
Code: 010f, LastCode 0066+66 Length 2
Code: 0110, LastCode 0067+41 Length 2
Code: 0111, LastCode 010d+41 Length 6
Code: 0112, LastCode 0111+41 Length 7
Code: 0113, LastCode 0107+61 Length 4
Code: 0114, LastCode 0062+63 Length 2
Code: 0115, LastCode 0064+64 Length 2
Code: 0116, LastCode 0065+63 Length 2
Input: 0062 0063 0064 0042 0104 007b 0104 0104 0062 0107 0104 0065 010a 0042 0066 0067 010d 0111 0107 0062 0064
0065 0064
Output: 61 62 63 41 41 41 7a 41 41 41 41 61 41 41 41 41 41 64 41 41 41 41 41 65 66 41 41 41 41 41 41 41 41 41 41 41 41 41 41
61 63 64 63
Code: 0101, LastCode 0062+62 Length 2
Code: 0102, LastCode 0063+63 Length 2
Code: 0103, LastCode 0064+41 Length 2
Code: 0104, LastCode 0042+41 Length 2
Code: 0105, LastCode 0104+7a Length 3
Code: 0106, LastCode 007b+41 Length 2
Code: 0107, LastCode 0104+41 Length 3
Code: 0108, LastCode 0104+61 Length 3
Code: 0109, LastCode 0062+41 Length 2
Code: 010a, LastCode 0107+41 Length 4
Code: 010b, LastCode 0104+64 Length 3
Code: 010c, LastCode 0065+41 Length 2
Code: 010d, LastCode 010a+41 Length 5
Code: 010e, LastCode 0042+65 Length 2
Code: 010f, LastCode 0066+66 Length 2
Code: 0110, LastCode 0067+41 Length 2
Code: 0111, LastCode 010d+41 Length 6
Code: 0112, LastCode 0111+41 Length 7
Code: 0113, LastCode 0107+61 Length 4
Code: 0114, LastCode 0062+63 Length 2
Code: 0115, LastCode 0064+64 Length 2
Code: 0116, LastCode 0065+63 Length 2