Introduction
Here you can find some (hopefully) useful Matlab stuff.Index
- Huffman coding
- Arithmetic coding (1.4.2, May 20, 2008)
- DCT II, III and IV
- Autoinstall for MEXes
- Search for the closest lattice point
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
-
huffman_codeto be called as follows[BITS, TABLE]=huffman_code(X, FREQS)where Arrays BITS and TABLE can be directly used as parameters toX 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. huffman_decodeas followsX = HUFFMAN_DECODE(BITS, TABLE)huffman_codecan also use a pre-generated Huffman code. See the function help for details. huffman_decodeto be called as followsX = HUFFMAN_DECODE(BITS, TABLE)where BITS and TABLE are the output values ofhuffman_code. This function is quite slow. If you need something faster, try considering the C version.-
huffman_aggiungi_fogliaUsed byhuffman_decode(In case you are wondering, "aggiungi_foglia" means "add a leaf") huffman_testa test scripthuffman_decode_oldan old version ofhuffman_decode
-
-
MEX version of
huffman_decodeThis 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:
- Download the C source into a directory searched by Matlab
- Start Matlab and go to the directory which contains the C source
- Do
mex do_huffman_decode.c
do_huffman_decode. The syntax is the same ashuffman_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 thesystemfunction. 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,decodeandclosenow are prefixed byarith_prefix. -
New coders and decoders are now created by
new_arithmetic_encoderandnew_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?
- Download it and decompress it in a directory searched by Matlab (or Octave)
- File I/O version...
- To encode
- Create an encoder with
h=new_arithmetic_encoder(filename, ab_size)wherefilenameis the output file andab_sizeis the alphabet size. - Encode with
arith_encode(h, x)wherexis a matrix of integers between 0 andab_size-1. - Finish encoding with
arith_close(h)
- Create an encoder with
- To decode
- Create a decoder with
h=new_arithmetic_decoder(filename, ab_size)wherefilenameis the input file andab_sizeis the alphabet size. - Decode with
y=arith_decode(h, dims) - Finish decoding with
arith_close(h)
- Create a decoder with
- To encode
- Memory I/O version...
- To encode
- Create an encoder with
h=new_arithmetic_encoder(ab_size)whereab_sizeis the alphabet size. - Encode with
arith_encode(h, x)wherexis a matrix of integers between 0 andab_size-1. - Finish encoding with
bitstream=arith_close(h)Variablebitstreamis astructrepresenting the bitstream generated by the encoder and it can be given directly tonew_arithmetic_decoder.bitstreamwill have at least the two fieldsab_size(the alphabet dimension given tonew_arithmetic_encoder) andbuffer, a double array with integer entries in the range [-128, 127], representing the bitstream generated by the encoder.
- Create an encoder with
- To decode
- Create a decoder with
h=new_arithmetic_decoder(u)or withh=new_arithmetic_decoder(buffer, ab_size)whereuis the value returned byarith_close(see above) - Decode with
y=arith_decode(h, dims) - Finish decoding with
arith_close(h)
- Create a decoder with
- To encode
- Higher-level functions
- Use the two "high level" functions
(
save_arith_compressed()andload_arith_compressed()) to save and load in compressed form an integer matrix to a file.
- Use the two "high level" functions
(
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
- If you do not already have it, download install_mex_if_needed and save it in a directory searched by matlab.
- Download closest-lattice-point.zip and unpack it in a directory searched by matlab
-
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