Introduction

Here you can find some (hopefully) useful Matlab stuff.

Index


Huffman coding

I needed a Matlab function for Huffman coding/decoding to be used in a course of mine. Since I was not able to find anything I liked, I wrote it by myself. Here you can find both a "pure .m" version and two C versions: a MEX file one and a stand-alone program to be called via the system call. A warning: since I wrote these functions for my students, help text and comments are in italian. I plan to translate them in english as soon as possible.
  • Zip file (approx 19K) with pure m-file version (Octave compatible) You will find the following functions
    1. huffman_code to be called as follows [BITS, TABLE]=huffman_code(X, FREQS) where
      X Array of non-negative integers to be coded
      FREQS Array of probabilities to be used in the construction of the Huffman table. If omitted, the histogram of X is used.
      BITS A string of '0' and '1' characters. Each character represents a bit of the final bitstream.
      TABLE The Huffman code necessary to decode BITS.
      Arrays BITS and TABLE can be directly used as parameters to huffman_decode as follows X = HUFFMAN_DECODE(BITS, TABLE)

      huffman_code can also use a pre-generated Huffman code. See the function help for details.

    2. huffman_decode to be called as follows X = HUFFMAN_DECODE(BITS, TABLE) where BITS and TABLE are the output values of huffman_code. This function is quite slow. If you need something faster, try considering the C version.
    3. huffman_aggiungi_foglia Used by huffman_decode (In case you are wondering, "aggiungi_foglia" means "add a leaf")
    4. huffman_test a test script
    5. huffman_decode_old an old version of huffman_decode
  • MEX version of huffman_decode This version is much faster, but, unfortunately, is for Matlab only. If you use Octave, consider the stand-alone version.

    In order to install it, do:

    1. Download the C source into a directory searched by Matlab
    2. Start Matlab and go to the directory which contains the C source
    3. Do mex do_huffman_decode.c
    Now you should be able to use the function do_huffman_decode. The syntax is the same as huffman_decode, but with a different name (a do_ is prepended to the function name).
  • Zip archive with a stand-alone version of huffman_decode. This version is composed of a stand-alone C program together an m-file that runs the C program via the system function. This version is Octave compatible. You need a C compiler to compile it, of course.

Arithmetic coding (Matlab and Octave. 1.4.2 May 20, 2008)

Index

What is this?

Here you can find a .zip file with a MEX interface to an arithmetic coder in C. This version (1.4.2) works both with Octave (tested with 2.9.10) and Matlab.

A major improvement of version 1.3 is that now it is possible to read/write compressed data from/to a Matlab/Octave variable (with the previous versions it was necessary to read/write data from/to a file).

The interface to some mid-level functions in version 1.3.2 is slightly different

  • Function names encode, decode and close now are prefixed by arith_ prefix.
  • New coders and decoders are now created by new_arithmetic_encoder and new_arithmetic_decoder.

I know, I know... It is not nice to change the user interface between different versions, but the changes were necessary since Octave 2.1.73 does not have user-level classes and function close would have clashed with the graphical close function.

If you do not work with Octave, the interface change is a big problem for you and you are not interested in reading/writing compressed data from/to Matlab variables, Here you can find a .zip file with the 1.2 version (which works with Matlab only, but it is not mantained anymore).

How do I install it?

To install the library, download the archive and unzip it in a directory searched by Octave/Matlab (e.g. ~/octave or ~/matlab). The MEX files will be automagically compiled the first time you use the library.

How do I use it?

  1. Download it and decompress it in a directory searched by Matlab (or Octave)
  2. File I/O version...
    1. To encode
      • Create an encoder with h=new_arithmetic_encoder(filename, ab_size) where filename is the output file and ab_size is the alphabet size.
      • Encode with arith_encode(h, x) where x is a matrix of integers between 0 and ab_size-1.
      • Finish encoding with arith_close(h)
    2. To decode
      • Create a decoder with h=new_arithmetic_decoder(filename, ab_size) where filename is the input file and ab_size is the alphabet size.
      • Decode with y=arith_decode(h, dims)
      • Finish decoding with arith_close(h)
  3. Memory I/O version...
    1. To encode
      • Create an encoder with h=new_arithmetic_encoder(ab_size) where ab_size is the alphabet size.
      • Encode with arith_encode(h, x) where x is a matrix of integers between 0 and ab_size-1.
      • Finish encoding with bitstream=arith_close(h) Variable bitstream is a struct representing the bitstream generated by the encoder and it can be given directly to new_arithmetic_decoder.

        bitstream will have at least the two fields ab_size (the alphabet dimension given to new_arithmetic_encoder) and buffer, a double array with integer entries in the range [-128, 127], representing the bitstream generated by the encoder.

    2. To decode
      • Create a decoder with h=new_arithmetic_decoder(u) or with h=new_arithmetic_decoder(buffer, ab_size) where u is the value returned by arith_close (see above)
      • Decode with y=arith_decode(h, dims)
      • Finish decoding with arith_close(h)
  4. Higher-level functions
    • Use the two "high level" functions (save_arith_compressed() and load_arith_compressed()) to save and load in compressed form an integer matrix to a file.
See the online help and the README file for more informations. You could also want to look at the Changes file.

DCT II, III and IV (1.0 September 23, 2005)

flexdct.m (FLEXible DCT) can compute DCT II, III and IV (DCT I is not implemented yet); iflexdct.m computes the inverse DCT. Just call them as y=flexdct(x,type) x=iflexdct(y,type) where type is 2, 3 or 4 (or "ii", "iii" or "iv"). y has the same size of x. If x is a matrix, flexdct operates columnwise.

Install MEX on first call (1.0 September 30, 2005)

Suppose you wrote a nice matlab library which uses MEX functions. If someone else wants to use it, they need to compile and install the MEX file and this can be intimidating for the novice (although it is not really difficult).

M-file install_mex_if_needed can help you to make MEX installation automatic. Just copy the file in some directory searched by matlab and call

install_mex_if_needed(MEX_FILENAME);

in the M-file which uses the MEX file MEX_FILENAME. The function will check if the MEX file is already installed and if it is not, it will install it.

With the call above the source file is a file with name MEX_FILENAME.c in the same directory of the caller. If you want to specify a different directory or different filenames, you can use the "full" call

install_mex_if_needed(MEX_FILENAME, SRCDIR, SOURCES);

See the command help for details.


Search for the closest lattice point

Archive closest-lattice-point.zip contains a matlab library which searches in a lattice for the element which is closest to a given point.

In order to use it, do the following steps

  1. If you do not already have it, download install_mex_if_needed and save it in a directory searched by matlab.
  2. Download closest-lattice-point.zip and unpack it in a directory searched by matlab
  3. Call Y=CLOSEST_LATTICE_POINT(X, M)

    where M is the lattice basis. The n-th column of Y will be the element of the lattice of basis M closest to n-th column of X.

    See the online help for details