Matlab Huffman code

This Matlab code generates a Huffman codebook for a specified set of symbol probabilities.

Copyright © 2010 Robert G. Maunder. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

34 Responses to “Matlab Huffman code”

  1. hugo Says:

    thank you very much

  2. saman Says:

    i need this realy.
    thx for help

  3. mahmoudawney Says:

    thank you very much.real i want this code.

  4. sandeepreddy Says:

    thank you very much sir

  5. neha Says:

    thnk you :)

  6. Michael Says:

    Thank you

  7. Liangjun Says:

    Thanks for your help

  8. Unknown Says:

    Thank you for the code. It’s awesome!

  9. Pooja shah Says:

    Can you plz tell me how to write Huffman code in matlab with user entering the probabilities and asking the user to enter the base for getting the huffman code.

  10. Rob Says:

    Hi Pooja Shah,

    The Matlab code that you can download from this page will generate a Huffman codebook for you, when you provide it with the symbol probabilities. Here is some code a Huffman encoder and decoder…
    http://users.ecs.soton.ac.uk/rm/wp-content/HuffmanEncoder.m
    http://users.ecs.soton.ac.uk/rm/wp-content/HuffmanDecoder.m

    Take care, Rob.

  11. divya Says:

    repeat accumulate code i need

  12. Rajesh N Says:

    Easy to understand. Very useful. Optimum coding

  13. Andrew Says:

    Hi Rob,

    Your program gives out Huffman Codes which are of a minimum length 2 bits for the highest probability. But the highest probability symbol should have a binary Huffman Code of only 1 bit. Could you please verify this?

  14. Andrew Says:

    Please ignore my previous question. I did not know the Huffman Code\’s algorithm properly. The code is perfect.

  15. kiyeob Says:

    I used this to make an dictionary but it failed the prefix free condition for long text files though it works for short text file.

  16. kiyeob Says:

    Actually I found it is not…I draw a tree from the codewords and found it is prefix-free code but somehow matlab recognizes this as prefix code…
    Thanks,

  17. Rob Says:

    Hi Kiyeob,

    I hope you got this working - I can’t tell from your messages if you solved the problem or not…

    Take care, Rob.

  18. tiha Says:

    please i need a huffman code ^^

  19. Rob Says:

    Hello Tiha,

    You can download the code from…
    http://users.ecs.soton.ac.uk/rm/wp-content/huffman.m

    Take care, Rob.

  20. liberte Says:

    i was no to use matlab very well and traduces all the code in telecommunication. thank you for all you are doing to make the technologies growing

  21. liberte Says:

    please can i have huffman code that return (valeur moyenne, entrepie et l\’efficacit√© ) and binary code, and the user will enter any probability that he need , the probability can be above one . thank you

  22. liberte Says:

    thank you rob for all you are doing for us

  23. Rob Says:

    Hello Liberte,

    You can download my Matlab tools for Huffman codes from…
    http://www.edshare.soton.ac.uk/5293/

    Take care, Rob.

  24. manju singh Says:

    sir plz give me idea of how to store Huffman encoded bits for image in matlab

  25. Rob Says:

    Hi Manju,

    It is easy to store these bits in Matlab by using the ’save’ command.

    Take care, Rob.

  26. abhikalp sharma Says:

    Hello Sir,

    sir please provide me matlab code for huffman image compression with single bit error.

  27. Rob Says:

    Hello Abhikalp,

    I’m afraid that I don’t have any Matlab code for image compression, but it should be possible to apply my Huffman code to this application.

    Take care, Rob.

  28. manju singh Says:

    Thankyou sir,
    Sir I used the command huff_code= huffmanenco(sig,dict) to encode the signal in Huffman jpeg image compression. Sir now I want to calculate Huffman binary file what will be the procedure for this. And one more thing sir will reconstructed image size be the same as compressed image size or it will be different?

  29. manju singh Says:

    Thankyou sir,
    Sir I used the command huff_code= huffmanenco(sig,dict) to encode the signal in Huffman jpeg image compression. Sir now I want to calculate Huffman binary file what will be the procedure for this. And one more thing sir will reconstructed image size be the same as compressed image size or it will be different?

  30. Rob Says:

    Hello Manju,

    I suspect that you can do this by taking eight bits at a time and converting to a decimal number in the range 0 to 255, then using fwrite to write this into a binary file. If you are applying Huffman coding to a file that has already been jpeg encoded, then I would not expect you to achieve any compression. However, I suspect that the resultant binary file would have a different file size to the original jpeg file, albeit only slightly.

    Take care, Rob.

  31. manju singh Says:

    Thankyou sir,
    Sir I applied jpeg compression using Huffman on a image that is in bmp format not in jpeg. Sir I want to know that compressed image or reconstructed image will be same or different.

  32. Rob Says:

    Hi Manju,

    JPEG compression is lossy, so the reconstructed image will be different to the original image.

    Take care, Rob.

  33. abhikalp sharma Says:

    hello sir,
    Sir I want to introduce error in huffman binary file and then decoded this binary file.what will be the procedure for this?Is differnt decoding technique used for this error phenomena or same as we use for huffman without error..

  34. Rob Says:

    Hi Abhikalp,

    If errors are introduced into the Huffman-encoded bit sequence, the decoding procedure is the same, although there is one thing that you need to think about.

    Suppose that we have the codebook 1, 00, 010, 011 and we have the transmitted bit sequence 0001110101001010. If the tenth bit has a transmission error then the received bit sequence will be 0001110100001010. This decodes to the following sequence of codewords, 00, 011, 1, 010, 00, 010, 1, 0. However, that final 0 does not give a complete codeword - you need to write your Huffman decoder so that it doesn’t crash when it encounters this problem. The simple thing you can do is just have the Huffman decoder ignore that final 0 and output only the 7 complete codewords that precede it.

    Take care, Rob.

Leave a Reply

Security Code: