[CODE, L_ave, yita, H] = HuffmanEncoding(probabilities);
disp(['对应概率:',num2str(probabilities)]); disp(['对应码字:',CODE]); disp(['平均码长:',num2str(L_ave)]); disp(['信源熵:',num2str(H)]); disp(['编码效率:',num2str(yita)]);
file_path = 'data.txt'; encodedFilePath = 'encoded_data.txt'; decodedFilePath = 'decoded_data.txt'; EncodeAndSave(file_path, symbols, CODE, encodedFilePath); HuffmanDecoding(encodedFilePath, symbols, CODE, file_path, decodedFilePath);
function EncodeAndSave(file_path, symbols, CODE, encodedFilePath) fileID = fopen(file_path, 'r'); originalContent = fscanf(fileID, '%c'); fclose(fileID); encodedContent = ''; for i = 1:numel(originalContent) symbolIndex = strfind(symbols, originalContent(i)); if ~isempty(symbolIndex) encodedContent = strcat(encodedContent, CODE{symbolIndex}); else error(['未在符号集中找到字符:', originalContent(i)]); end end fileID = fopen(encodedFilePath, 'w'); fprintf(fileID, '%s', encodedContent); fclose(fileID); disp(['编码内容已保存至:', encodedFilePath]); end
function [CODE, L_ave, yita, H] = HuffmanEncoding(probabilities) p = probabilities; N = length(p); code = strings(N-1,N); reflect = zeros(N-1,N); p_SD = p; for i=1:N-1 M = length(p_SD); [p_SD,reflect(i,1:M)] = sort(p_SD,'descend'); code(i,M) = '1'; code(i,M-1) = '0'; p_SD(M-1) = p_SD(M-1) + p_SD(M); p_SD(M) = []; end CODE = strings(1,N); for i = 1:N column = i; for m = 1:N-1 [~, column] = find(reflect(m,:) == column); CODE(1,i) = strcat(CODE(1,i),code(m,column)); if column == N+1-m column = column-1; end end end CODE = reverse(CODE); for i=1:N L(i) = size(char(CODE(1,i)),2); end L_ave = sum(L.*p); H = sum(-p.*log2(p)); yita = H/L_ave; end
function HuffmanDecoding(encodedFilePath, symbols, CODE, originalFilePath, decodedFilePath) fileID = fopen(encodedFilePath, 'r'); encodedContent = fscanf(fileID, '%s'); fclose(fileID); inv_CODE = cell(size(CODE)); for i = 1:numel(CODE) inv_CODE{strcmp(CODE, CODE{i})} = symbols(i); end decodedContent = ''; currentCode = ''; for i = 1:numel(encodedContent) currentCode = [currentCode, encodedContent(i)]; foundSymbol = false; for j = 1:numel(CODE) if strcmp(currentCode, CODE{j}) decodedContent = strcat(decodedContent, inv_CODE{j}); currentCode = ''; foundSymbol = true; break; end end end fileID = fopen(decodedFilePath, 'w'); fprintf(fileID, '%s', decodedContent); fclose(fileID); disp(['解码内容已保存至:', decodedFilePath]); originalFileContent = fileread(originalFilePath); errors = sum(strcmp(decodedContent, originalFileContent) == 0); totalChars = numel(decodedContent); errorRate = errors / totalChars; disp(['误码率:', num2str(errorRate)]); end
|