Frequency Substitution Encoding Discussion and Implementation
by Michael Dipperstein
Frequency Substitution is a term I made up for a transformation that I saw discussed on another web site. I have since lost the link to the site, but if somebody can find it I'll be happy to reference it.
The idea behind Frequency Substitution is simple. The goal is to transform data by replacing the highest frequency symbols with the lowest values. It sounded simple enough, so I thought that I'd give it a try.
Normally this type of transformation doesn't produce useful results, but there are a few compression techniques, such as Rice (Golomb) Coding, that produce smaller code words for lower value symbols. By making the lower valued symbols the most frequent symbols, these encoding schemes may achieve better compression ratios.
As with my other compression implementations, my intent is to publish an easy to follow ANSI C implementation of frequency substitution encoding and decoding. Anyone familiar with ANSI C should be able to follow and learn from my implementation. I'm sure that there's room for improvement of speed and memory usage, but this project is about learning and sharing, not perfection.
Click here for a link to my frequency substitution source. The rest of this page discusses the concepts involved with frequency substitution and my implementation.
The goal of frequency substitution is to transform data by replacing the highest frequency symbols with the lowest values. In a data set contains 256 unique symbols, the most common symbol will be replaced with 0 and the least common symbol will be replaced with 255.
Encoding a data stream using frequency substitution is a two pass process. The first pass is used to count the frequency of the symbols. After the first pass, a table of substitution codes is generated. Then a second pass is made, where an encoded data set is generated using the substitution table.
The first pass should be straight forward, start with a table indicating a frequency of 0 for all possible symbols, then add 1 to a symbol's frequency count every time it is encountered in the data stream. Next sort the substitution table most frequent to least frequent.
On the second pass, write out enough information for the decoder to reconstruct the table, then read in each unencoded symbol and write out the sorted position of that symbol.
Given a symbol set of 0 ... 9, encode the following data stream:
Based on the discussion above, encoding input consists of the following steps:
Step 1. Start with an empty table.
Step 2. Generate a frequency table for the data stream.
Step 3. Sort the symbols by frequency.
Step 4. Write out the encoded data by substituting a
symbol for it's position in the frequency table.
A quick sanity check shows that the sum of the symbols in the initial string (150) is greater than the sum of symbols in the encoded string (96) so we must have lowered the average symbol value.
Decoding data that has been encoded by frequency substitution is a single pass process. Given a table of frequency substitutions, reverse the substitutions preformed during the encoding process.
The following sequence and table of frequency substitutions
Produces the following sequence
If you were paying attention, you're probably wondering how the encoder passed the table of frequency substitutions to the decoder. The next section provides some ideas and discusses my implementations.
The Substitution Key
Before the decoder can do anything with the encoded data, it needs a key that tells it how to undo the substitutions. Since the key is needed immediately, it makes sense for the encoder to output the key before it outputs the encoded data.
So what kind of information do we need to have in the key? In order for the key to be useful, it must contain the original symbol and the value that it was substituted for.
If there are N possible symbols, the simplest thing to do is write out the complete list of all N (symbol, substitution) pairs. Such a list contains 2 × N entries. My goal was to use frequency substitution for data compression, so it would be nice if the list could be made even smaller.
As it turns out there are at least two ways to make the list smaller:
- Use the position of the list entries as the symbol value.
- Only transmit keys for symbols that appear in the data.
Position As Symbol Value
Since the substitution key is the first thing output by the encoder, the key for all N symbols will be output in positions 0 ... (N - 1). Position 0 contains the substitution used for symbol 0, position 1 contains the substitution used for symbol 1, and so on, the symbol number may be omitted from the key. This method requires just N entries for N symbols.
Excluding Unused Symbols
Sometimes the data being encoded is sparse; it only uses a fraction of all of the symbols in the symbol set. In this case there will be a large number of unneeded entries in a position indexed key.
For spare data sets, it is more efficient to transmit (symbol, substitution) pairs omitting symbols that don't appear in the input. To make this method work an end of key or a key length must also be transmitted. Otherwise the decoder would not be able to distinguish the encoded data from the substitution key.
My implementation first outputs the key length - 1, followed by (symbol offset, substitution) pairs. Where the symbol offset is the value of the current symbol minus the value of the previous symbol. The value of the previous symbol is taken as 0 at start up.
(symbol: 30, substitution: 5), (symbol: 31, substitution: 42)
(offset: 30, substitution: 5), (offset: 1, substitution: 42)
Using a symbol offset instead of the actual symbol value has the advantage of producing substitution keys with lower value entries. Normally lower value entries would not be a big deal, but the whole purpose of frequency substitution is to produce data with lower values than the original source.
If you actually read my introduction, you'd notice that I thought frequency substitution might be helpful when used with Rice (Golomb) Coding. In order to test that theory, I used the Calgary Corpus as a test data set.
The table below shows the results of applying frequency substitution to the Calgary Corpus and then using Rice coding (with K = 4) to compress encoded files. Both position indexed and sparse versions of my frequency substitution implemention were used. For reference, the table also provides results for Rice coding alone and Rice coding with BWT and MTF.
The results of my test appear in the following tables:
|File||Orig||Rice||BWT + Rice||FreqSub + Rice||Sparse FreqSub + Rice||Rice||BWT + Rice||FreqSub + Rice||Sparse FreqSub + Rice|
There are three things that you should learn from this table:
- The Calgary Corpus contains data ill suited for Rice encoding.
- Frequency substitution may be applied to the Calgary Corpus data to enable Rice encoding to compress the data.
- Better compression results may be achieved by applying BWT and MFT.
So why use frequency substitution? If your goal is to achieve the best possible compression ratios, most data sets don't provide a compelling reason. However, the computational and memory resources required for frequency substitution is less than that required by BWT and MFT. I didn't provide any proof of that, you'll just have to trust me or see for yourself.
I am releasing my implementations offrequency substitution encoding and decoding under the LGPL. As I add enhancements or fix bugs, I will post them here as newer versions. The larger the version number, the newer the version. I will retain the older versions for historical reasons. I recommend that most people download the newest version unless there is a compelling reason to do otherwise.
Each version is contained in its own zipped archive which includes the source files and brief instructions for building an executable. None of the archives contain executable programs. A copy of the archives may be obtained by clicking on the links below.
|Version 0.1||Initial release.|
All the source code that I have provided is written in strict ANSI C. I would expect it to build correctly on any machine with an ANSI C compiler. I have tested the code compiled with gcc on Linux and mingw on Windows XP.
If you have any further questions or comments, you may contact me by e-mail. My e-mail address is: firstname.lastname@example.org
For more information on compression algorithms, visit DataCompression.info.