鱼C论坛

 找回密码
 立即注册
查看: 1789|回复: 8

huffman编码实现

[复制链接]
发表于 2023-6-5 17:59:55 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
clc
clear
symbols=1:3;
p=[.45 .35 .2];
sig=randsrc(100,1,[symbols;p]);

tic
dict=huffmandict(symbols,p);
comp=huffmanenco(sig,dict);
toc

dsig=huffmandeco(comp,dict);

if(~isequal(sig,dsig))
    print('Error!');
else
L=sum(cellfun('length',dict(:,2))'.*p);
l=length(comp)/100;
H=-sum(p.*log2(p));
eta=H/L;
    fprintf('Sourse Entropy:%1.2f,\nAverage Huffman code length:%1.2f,\nCoding efficiency:%3.1f.\n',...
        H,L,eta*100);
end

需要自行编写huffmandict和huffmanenco函数,后面比较效率。这两个函数怎么编写,matlab,求助!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-6-5 18:05:33 | 显示全部楼层

好的,我可以帮您解决这个问题。下面是huffmandict和huffmanenco函数的示例代码:
function dict = huffmandict(symbols, p)
% HUFFMANDICT Generate Huffman dictionary.
%
%   dict = HUFFMANDICT(symbols, p) generates a Huffman code dictionary
%   based on the input symbol probabilities p. symbols is a vector of
%   distinct data source symbols. The function returns a cell array
%   containing a two-column lookup table. Column 1 contains the input
%   symbols and column 2 contains their corresponding Huffman codes.

nSymbols = length(p);
dict = cell(nSymbols, 2);

if nSymbols == 1
    dict{1, 1} = symbols;
    dict{1, 2} = '';
else
    % Construct initial binary tree nodes
    counts = zeros(2*nSymbols-1, 1); % Node counts
    counts(1:nSymbols) = p; % Assign leaf node counts
    
    leftChild = zeros(2*nSymbols-1, 1); % Left children
    rightChild = zeros(2*nSymbols-1, 1); % Right children
    parent = zeros(2*nSymbols-1, 1); % Parent nodes
    for i = (nSymbols+1):(2*nSymbols-1)
        [counts, index] = sort(counts);
        leftChild(i) = index(1);
        rightChild(i) = index(2);
        parent(index(1)) = i;
        parent(index(2)) = i;
        counts(i) = counts(1) + counts(2);
    end

    % Encode each symbol using its corresponding path in the binary tree
    codeWord = repmat(' ', nSymbols, nSymbols);
    for i = 1:nSymbols
        node = i;
        while parent(node) ~= 0
            if leftChild(parent(node)) == node
                codeWord(i, nSymbols - ceil(log2(nSymbols) - log2(node+1))) = '0';
            else
                codeWord(i, nSymbols - ceil(log2(nSymbols) - log2(node+1))) = '1';
            end
            node = parent(node);
        end
        dict{i, 1} = symbols(i);
        dict{i, 2} = fliplr(codeWord(i, :));
    end
end


function comp = huffmanenco(sig, dict)
% HUFFMANENCO Perform Huffman encoding.
%
%   comp = HUFFMANENCO(sig, dict) encodes the input signal sig using the
%   provided Huffman dictionary dict. The function returns a binary
%   string containing the encoded message.

nBits = 0;
for i = 1:length(sig)
    index = find(strcmp(dict(:, 1), sig(i)));
    nBits = nBits + length(dict{index, 2});
end

comp = repmat('0', 1, nBits);
k = 1;
for i = 1:length(sig)
    index = find(strcmp(dict(:, 1), sig(i)));
    codeWord = dict{index, 2};
    comp(k:k+length(codeWord)-1) = codeWord;
    k = k + length(codeWord);
end

注意,这只是示例代码,可能并不是完全优化的。为了提高编码效率,您可以使用MATLAB内置的函数huffmandict和huffmanenco来生成哈夫曼码字典和进行哈夫曼编码。

求最佳答案
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-6-5 18:14:05 | 显示全部楼层
sfqxx 发表于 2023-6-5 18:05
好的,我可以帮您解决这个问题。下面是huffmandict和huffmanenco函数的示例代码:

……gpt要是能编出不报错的我也不用来这里问了

点评

不,我坚决不同意楼主的看法!: 2.0 我很赞同!: 5.0
我很赞同!: 5
  发表于 2023-6-5 18:23
不,我坚决不同意楼主的看法!: 2
。  发表于 2023-6-5 18:16
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2023-6-5 18:14:31 | 显示全部楼层
您好!Huffman编码是一种经典的无损数据压缩算法。您的代码似乎需要两个自定义函数: huffmandict 和 huffmanenco 。虽然MATLAB内置了这两个函数,但如果您想自行编写,以下是这两个函数的一种可能实现:

首先是 huffmandict 函数,该函数的任务是根据符号和它们的概率创建一个Huffman编码字典:
function dict = huffmandict(symbols, p)
    % Create a cell array of all Huffman nodes, which initially are all
    % 'leaf' nodes containing symbol-probability pairs.
    nodes = num2cell([symbols(:) p(:)], 2);
    while numel(nodes) > 1
        % Sort all the current nodes by probability.
        [~, order] = sort([nodes{:, 2}]);
        nodes = nodes(order);
        % Combine the two nodes with smallest probability, and build a new
        % node that points to them.
        smallestTwo = nodes(1:2);
        newNode = {[], sum([smallestTwo{:, 2}])};
        nodes = [{newNode}; nodes(3:end)];
        % The new node is a 'parent' node and the two selected nodes are its 'children'.
        newNode{1} = smallestTwo;
    end
    % The single remaining node is the root of the Huffman tree.
    huffTree = nodes{1};
    % Traverse the Huffman tree to produce the symbol-code dictionary.
    dict = createDict(huffTree, []);
end

function dict = createDict(huffNode, prefix)
    if iscell(huffNode{1})
        % huffNode{1} contains two 'children', so we're not at a 'leaf' node.
        dict = [createDict(huffNode{1}{1}, [prefix 0]);
                createDict(huffNode{1}{2}, [prefix 1])];
    else
        % We're at a 'leaf' node, so return the symbol and its code.
        dict = {huffNode{1}, prefix};
    end
end

然后是 huffmanenco 函数,它使用给定的Huffman字典来编码一个信号:
function comp = huffmanenco(sig, dict)
    % Create a map from symbols to their corresponding codes.
    huffLUT = containers.Map(dict(:,1), dict(:,2));
    % Encode the signal.
    comp = cell2mat(huffLUT.values(num2cell(sig)));
end

这两个函数应该能够满足您的需求。需要注意的是,这些实现是为了展示基本的Huffman编码过程,可能没有完全优化。如果您需要处理大数据集或需要更高的性能,可能需要使用更复杂的数据结构或算法。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-6-5 18:17:38 | 显示全部楼层
G`J 发表于 2023-6-5 18:14
……gpt要是能编出不报错的我也不用来这里问了

?你是机器人吗

点评

我很赞同!: 5.0 不,我坚决不同意楼主的看法!: 2.0
不,我坚决不同意楼主的看法!: 2
no  发表于 2023-6-5 18:40
我很赞同!: 5
是的  发表于 2023-6-5 18:25
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-22 23:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表