You are on page 1of 2

Music 422/EE 367C Winter 2006/07

Assignment #2
Due January 26, 2007

1) Working with bits (extra credit):

Text is normally stored as 8 bit ASCII codes (of which only the lowest 7 bits are
used to store the basic character set). For this exercise, you will create a lossy text
codec that stores text using only 5 bits per character. In doing so, you will gain
experience in dealing with reading/writing binary coded files.
a) Write a function that takes the M lowest bits from an unsigned integer and writes
them starting at the Nth bit location in an array of BYTES (unsigned character
b) Define a mapping from the basic ASCII character set onto only 5 bits.
(Obviously, you will need to sometimes map multiple ASCII characters onto the
same 5-bit code. For example, you will need to map both capital and small letters
onto the same code.)
c) Write a text encoder/decoder that allows you to read in ASCII text files (e.g., .txt
files from Notepad), map the text into 5 bit codes, pack the coded text into arrays
of BYTES, write the packed arrays into a coded file, read in your coded files,
decode your coded files back into ASCII codes, and write out your decoded text
d) Test your 5 bit text codec on several sample text files. Check file sizes to see
what compression ratio you achieved. How readable is your decoded file?
e) Change your mapping to use two of your codes as control characters. Let one
code signify that the next character is capitalized. Let the other code signify that
the next character comes from a different set of mappings onto 5 bit codes (i.e.
include some important characters that weren’t included in the basic mapping).
How does this change impact compression ratio? How does this change impact
2) Quantization:
In this exercise you will develop quantization routines that you will use when
developing audio coders in later exercises. Program functions to
quantize/dequantize using:
a) R-bit midtread
b) R-bit midrise
c) Rs, Rm-bit floating point midtread.
Use these functions to fill in the table below.
Output = Input
Q-1(Q(input)) -1.0 -0.97 -0.24 0.0 0.007 0.48 0.78 0.999 1.0
8 bit midtread

8 bit midrise

3 scale bit, 5
mantissa bit FP

12 bit midtread
In Chapter 2 Figure 10 (page 38) of the textbook, a random signal was utilized to
simulate values for the signal to quantization noise ratio defined as
where the quantization error is the difference between the quantized-dequantized
signal and the input signal (i.e., error[n] = Q-1(Q(x[n]))-x[n]).
Graph the equivalent SNR vs. average input signal power over the range from -50
dB to 0 dB for a 1.7 kHz sine wave sampled at 16 kHz using the following
d) 8 bits midtread
e) 8 bits midrise
f) 3 scale bits, 5 mantissa bits floating point midtread
g) 12 bit midtread.
3) A first audio coder revisited:
In this exercise you will develop a simple audio coder that allows you to test the
effects of different quantization routines. You should build upon the basic routines
for reading and writing audio files that you put in place in Assignment #1. These
core routines will continue to be useful in later exercises.
a) Write an audio encoder/decoder that reads in 16 bit PCM audio files, dequantizes
the audio samples to floating point values from –1.0 to 1.0, quantizes the samples
using the quantization functions you prepared for the prior exercise, packs the
quantized samples into arrays of BYTES, writes the results into a coded file
format you define, reads in your coded file, converts your data back into 16 bit
PCM codes, and writes out your decoded audio data into an audio file you can
play. (It is highly recommended that you keep the encode routines in separate
modules from the decode routines; this code organization will become extremely
important in later exercises. Also, please make sure that your
quantize/dequantize and packing/unpacking routines can handle variable bit-
length codes). Verify that your coder is bug-free by making sure that files coded
using 16-bit midtread uniform quantization do not sound degraded when decoded.
b) Test your codec on some sound samples using 1) 4-bit midtread uniform
quantization, 3) 8-bit midtread uniform quantization, and 4) 3 scale bits, 5
mantissa bits midtread floating point quantization. What compression ratios do
you get? Describe the quantization noise you hear.
4) Applying Huffman Coding (extra credit):
Estimate symbol probabilities for each of the 16 codes used in 4-bit midtread uniform
quantization in the coder you developed in exercise 3) above using your sound
samples. Use these probabilities to define a set of Huffman codes for your 4-bit
quantization codes. Modify your codec to read/write the coded file format using
these Huffman codes when encoding using 4-bit midtread uniform quantization. How
much do the Huffman codes improve the compression on your sound samples?
5) Reading Assignment:
Chapters 4 and 5 from the textbook, M. Bosi and R. E. Goldberg, “Introduction to
Digital Audio Coding and Standards”, KAP 2003.