在网上看了好多 , 都是对香农进行编码的案例 , 却没有 进行解码的操作 , 今天就来补齐这个欠缺
text = '你好'; % 待编码的文本
[encoded, decoded, avgCodeLength, efficiency] = shannonCoding(text);
disp('编码结果:');
disp(encoded);
disp('解码结果:');
disp(decoded);
disp('平均码长:');
disp(avgCodeLength);
disp('编码效率:');
disp(efficiency);
function [encoded, decoded, avgCodeLength, efficiency] = shannonCoding(text)
symbols = unique(text);
freq = zeros(size(symbols));
for i = 1:length(symbols)
freq(i) = sum(text == symbols(i));
end
freq = freq / numel(text);
cumProb = cumsum(freq);
codeTable = cell(length(symbols), 2);
for i = 1:length(symbols)
codeTable{i, 1} = symbols(i);
codeTable{i, 2} = ''; % 初始化编码为空
end
codeTable = buildCodeTable(codeTable, cumProb, 1, '');
encoded = '';
for i = 1:numel(text)
symbol = text(i);
index = find(strcmp(codeTable(:, 1), symbol));
code = codeTable{index, 2};
encoded = [encoded, code];
end
decoded = '';
code = '';
for i = 1:length(encoded)
code = [code, encoded(i)];
index = -1;
for j = 1:length(codeTable)
if strcmp(codeTable{j, 2}, code)
index = j;
break;
end
end
if index >= 0
decoded = [decoded, codeTable{index, 1}];
code = '';
end
end
codeLengths = cellfun(@length, codeTable(:, 2));
avgCodeLength = sum(codeLengths .* freq);
efficiency = 1 ./ avgCodeLength;
end
text = '你好'; % 待编码的文本
[encoded, decoded, avgCodeLength, efficiency] = shannonCoding(text);
disp('编码结果:');
disp(encoded);
disp('解码结果:');
disp(decoded);
disp('平均码长:');
disp(avgCodeLength);
disp('编码效率:');
disp(efficiency);
function [encoded, decoded, avgCodeLength, efficiency] = shannonCoding(text)
% 计算字符频率
symbols = unique(text);
freq = zeros(size(symbols));
for i = 1:length(symbols)
freq(i) = sum(text == symbols(i));
end
freq = freq / numel(text);
% 计算累积概率
cumProb = cumsum(freq);
% 构建编码表
codeTable = cell(length(symbols), 2);
for i = 1:length(symbols)
codeTable{i, 1} = symbols(i);
codeTable{i, 2} = ''; % 初始化编码为空
end
% 递归构建编码表
codeTable = buildCodeTable(codeTable, cumProb, 1, '');
% 编码
encoded = '';
for i = 1:numel(text)
symbol = text(i);
index = find(strcmp(codeTable(:, 1), symbol));
code = codeTable{index, 2};
encoded = [encoded, code];
end
% 解码
decoded = '';
code = '';
for i = 1:length(encoded)
code = [code, encoded(i)];
index = -1;
for j = 1:length(codeTable)
if strcmp(codeTable{j, 2}, code)
index = j;
break;
end
end
if index >= 0
decoded = [decoded, codeTable{index, 1}];
code = '';
end
end
% 计算平均码长
codeLengths = cellfun(@length, codeTable(:, 2));
avgCodeLength = sum(codeLengths .* freq);
% 计算编码效率
efficiency = 1 ./ avgCodeLength;
end
% 递归构建编码表
function codeTable = buildCodeTable(codeTable, cumProb, index, code)
if index > length(codeTable)
return;
end
if cumProb(index) <= 0.5
codeTable{index, 2} = [code, '0'+' '];
codeTable = buildCodeTable(codeTable, cumProb, index+1, [code, '0']);
else
codeTable{index, 2} = [code, '1'+' '];
codeTable = buildCodeTable(codeTable, cumProb, index+1, [code, '1']);
end
end