Menu
![](/uploads/1/2/7/5/127555234/316493610.jpg)
Permalink
Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
That is, to firstly calculate the uniform quantization noise and uniform quantization SNR in accordance with the uniform quantization, then to calculate the SNR improvement quantities resulted from the compression & expansion, then the sum of them is the non-uniform quantization SNR. Here takes the second method to conduct simulation, that is.
Sign upBranch:master
Find file Copy path
Fetching contributors…
%% Matlab code for Image Compression Using Run Length Encoding |
clc; |
clear; |
close all; |
%% Set Quantization Parameter |
quantizedvalue=100; |
%% Read Input Image |
InputImage=imread('cameraman.tif'); |
[row col p]=size(InputImage); |
%% Wavelet Decomposition |
[LL LH HL HH]=dwt2(InputImage,'haar'); |
WaveletDecomposeImage=[LL,LH;HL,HH]; |
imshow(WaveletDecomposeImage,[]); |
%% Uniform Quantzation |
QuantizedImage= WaveletDecomposeImage/quantizedvalue; |
QuantizedImage= round(QuantizedImage); |
ImageArray=toZigzag(QuantizedImage) |
%% Run Length Encoding |
j=1; |
a=length(ImageArray); |
count=0; |
for n=1:a |
b=ImageArray(n); |
if na |
count=count+1; |
c(j)=count; |
s(j)=ImageArray(n); |
elseif ImageArray(n)ImageArray(n+1) |
count=count+1; |
elseif ImageArray(n)b |
count=count+1; |
c(j)=count; |
s(j)=ImageArray(n); |
j=j+1; |
count=0; |
end |
end |
%% Transmission Variables c contains count s contains symbol |
c; |
s; |
%% Calculation Bit Cost |
InputBitcost=row*col*8; |
InputBitcost=(InputBitcost); |
c1=length(c); |
s1=length(s); |
OutputBitcost= (c1*8)+(s1*8); |
OutputBitcost=(OutputBitcost); |
%% Run Length Decoding |
g=length(s); |
j=1; |
l=1; |
for i=1:g |
v(l)=s(j); |
if c(j)~=0 |
w=l+c(j)-1; |
for p=l:w |
v(l)=s(j); |
l=l+1; |
end |
end |
j=j+1; |
end |
ReconstructedImageArray=v; |
%% Inverse ZigZag |
ReconstructedImage=invZigzag(ReconstructedImageArray) |
%% Inverse Quantization |
ReconstructedImage=ReconstructedImage*quantizedvalue; |
%% Wavelet Reconstruction |
sX = size(ReconstructedImage); |
cA1 = ReconstructedImage(1:(sX(1)/2), 1:(sX(1)/2)); |
cH1 = ReconstructedImage(1:(sX(1)/2), (sX(1)/2+1):sX(1)); |
cV1 = ReconstructedImage((sX(1)/2+1):sX(1), 1:(sX(1)/2)); |
cD1 = ReconstructedImage((sX(1)/2+1):sX(1), (sX(1)/2+1):sX(1)); |
ReconstructedImage = idwt2(cA1,cH1,cV1,cD1,'haar'); |
%% Visualize both Input and Reconstructed Images |
subplot(1,2,1);imshow(InputImage);title('Input Image'); |
subplot(1,2,2);imshow(ReconstructedImage,[]);title('Reconstructed Image'); |
%% Calculating PSNR and MSE |
InputImage=double(InputImage); |
ReconstructedImage=double(ReconstructedImage); |
n=size(InputImage); |
M=n(1); |
N=n(2); |
MSE = sum(sum((InputImage-ReconstructedImage).^2))/(M*N); |
PSNR = 10*log10(256*256/MSE); |
disp('MSE'); |
disp(MSE); |
disp('PSNR'); |
disp(PSNR); |
disp('InputBitcost'); |
disp(InputBitcost); |
disp('OutputBitcost'); |
disp(OutputBitcost); |
Copy lines Copy permalink
Source Coding
Represent Partitions
Where to watch original saint seiya. Scalar quantization is a process that maps all inputs within a specified range to a common value. This process maps inputs in a different range of values to a different common value. In effect, scalar quantization digitizes an analog signal. Two parameters determine a quantization: a partition and a codebook.
A quantization partition defines several contiguous, nonoverlapping ranges of values within the set of real numbers. To specify a partition in the MATLAB® environment, list the distinct endpoints of the different ranges in a vector.
For example, if the partition separates the real number line into the four sets
then you can represent the partition as the three-element vector
The length of the partition vector is one less than the number of partition intervals.
Represent Codebooks
A codebook tells the quantizer which common value to assign to inputs that fall into each range of the partition. Represent a codebook as a vector whose length is the same as the number of partition intervals. For example, the vector
is one possible codebook for the partition
[0,1,3]
.Determine Which Interval Each Input Is In
The
quantiz
function also returns a vector that tells which interval each input is in. For example, the output below says that the input entries lie within the intervals labeled 0, 6, and 5, respectively. Here, the 0th interval consists of real numbers less than or equal to 3; the 6th interval consists of real numbers greater than 8 but less than or equal to 9; and the 5th interval consists of real numbers greater than 7 but less than or equal to 8.The output is
If you continue this example by defining a codebook vector such as
then the equation below relates the vector
index
to the quantized signal quants
.This formula for
quants
is exactly what the quantiz
function uses if you instead phrase the example more concisely as below.Optimize Quantization Parameters
Section Overview
Quantization distorts a signal. You can reduce distortion by choosing appropriate partition and codebook parameters. However, testing and selecting parameters for large signal sets with a fine quantization scheme can be tedious. One way to produce partition and codebook parameters easily is to optimize them according to a set of so-called training data.
Note
The training data you use should be typical of the kinds of signals you will actually be quantizing.
Example: Optimizing Quantization Parameters
The
lloyds
function optimizes the partition and codebook according to the Lloyd algorithm. The code below optimizes the partition and codebook for one period of a sinusoidal signal, starting from a rough initial guess. Then it uses these parameters to quantize the original signal using the initial guess parameters as well as the optimized parameters. The output shows that the mean square distortion after quantizing is much less for the optimized parameters. The quantiz
function automatically computes the mean square distortion and returns it as the third output parameter.The output is
Differential Pulse Code Modulation
Section Overview
The quantization in the section Quantize a Signal requires no a priori knowledge about the transmitted signal. In practice, you can often make educated guesses about the present signal based on past signal transmissions. Using such educated guesses to help quantize a signal is known as predictive quantization. The most common predictive quantization method is differential pulse code modulation (DPCM).
The functions
dpcmenco
, dpcmdeco
, and dpcmopt
can help you implement a DPCM predictive quantizer with a linear predictor.DPCM Terminology
To determine an encoder for such a quantizer, you must supply not only a partition and codebook as described in Represent Partitions and Represent Codebooks, but also a predictor. The predictor is a function that the DPCM encoder uses to produce the educated guess at each step. A linear predictor has the form
where x is the original signal,
y(k)
attempts to predict the value of x(k)
, and p
is an m
-tuple of real numbers. Instead of quantizing x
itself, the DPCM encoder quantizes the predictive error, x-y. The integer m
above is called the predictive order. The special case when m = 1
is called delta modulation.Represent Predictors
If the guess for the
k
th value of the signal x
, based on earlier values of x
, isthen the corresponding predictor vector for toolbox functions is
Note
The initial zero in the predictor vector makes sense if you view the vector as the polynomial transfer function of a finite impulse response (FIR) filter.
Example: DPCM Encoding and Decoding
A simple special case of DPCM quantizes the difference between the signal's current value and its value at the previous step. Thus the predictor is just
y(k) = x (k - 1)
. The code below implements this scheme. It encodes a sawtooth signal, decodes it, and plots both the original and decoded signals. The solid line is the original signal, while the dashed line is the recovered signals. The example also computes the mean square error between the original and decoded signals.The output is
Optimize DPCM Parameters
Section Overview
The section Optimize Quantization Parameters describes how to use training data with the
lloyds
function to help find quantization parameters that will minimize signal distortion.This section describes similar procedures for using the
dpcmopt
function in conjunction with the two functions dpcmenco
and dpcmdeco
, which first appear in the previous section.Note
The training data you use with
dpcmopt
should be typical of the kinds of signals you will actually be quantizing with dpcmenco
.Example: Comparing Optimized and Nonoptimized DPCM Parameters
This example is similar to the one in the last section. However, where the last example created
predictor
, partition
, and codebook
in a straightforward but haphazard way, this example uses the same codebook (now called initcodebook
) as an initial guess for a new optimized codebook parameter. This example also uses the predictive order, 1, as the desired order of the new optimized predictor. The dpcmopt
function creates these optimized parameters, using the sawtooth signal x
as training data. The example goes on to quantize the training data itself; in theory, the optimized parameters are suitable for quantizing other data that is similar to x
. Notice that the mean square distortion here is much less than the distortion in the previous example.The output is
Compand a Signal
Section Overview
In certain applications, such as speech processing, it is common to use a logarithm computation, called a compressor, before quantizing. The inverse operation of a compressor is called an expander. The combination of a compressor and expander is called a compander.
The
compand
function supports two kinds of companders: µ-law and A-law companders. Its reference page lists both compressor laws.Example: µ-Law Compander
The code below quantizes an exponential signal in two ways and compares the resulting mean square distortions. First, it uses the
quantiz
function with a partition consisting of length-one intervals. In the second trial, compand
implements a µ-law compressor, quantiz
quantizes the compressed data, and compand
expands the quantized data. The output shows that the distortion is smaller for the second scheme. This is because equal-length intervals are well suited to the logarithm of sig
, but not well suited to sig
. The figure shows how the compander changes sig
.The output and figure are below.
![Quantization Quantization](http://www.abbreviations.com/images/225361_VULI.png)
Huffman Coding
Section Overview
Huffman coding offers a way to compress data. The average length of a Huffman code depends on the statistical frequency with which the source produces each symbol from its alphabet. A Huffman code dictionary, which associates each data symbol with a codeword, has the property that no codeword in the dictionary is a prefix of any other codeword in the dictionary.
The
huffmandict
, huffmanenco
, and huffmandeco
functions support Huffman coding and decoding.Note
For long sequences from sources having skewed distributions and small alphabets, arithmetic coding compresses better than Huffman coding. To learn how to use arithmetic coding, see Arithmetic Coding.
Create a Huffman Code Dictionary in MATLAB
Huffman coding requires statistical information about the source of the data being encoded. In particular, the
p
input argument in the huffmandict
function lists the probability with which the source produces each symbol in its alphabet.For example, consider a data source that produces 1s with probability 0.1, 2s with probability 0.1, and 3s with probability 0.8. The main computational step in encoding data from this source using a Huffman code is to create a dictionary that associates each data symbol with a codeword. The commands below create such a dictionary and then show the codeword vector associated with a particular value from the data source.
The output below shows that the most probable data symbol, 3, is associated with a one-digit codeword, while less probable data symbols are associated with two-digit codewords. The output also shows, for example, that a Huffman encoder receiving the data symbol 1 should substitute the sequence 11.
Create and Decode a Huffman Code Using MATLAB
The example below performs Huffman encoding and decoding, using a source whose alphabet has three symbols. Notice that the
huffmanenco
and huffmandeco
functions use the dictionary that huffmandict
created.Arithmetic Coding
Section Overview
Arithmetic coding offers a way to compress data and can be useful for data sources having a small alphabet. The length of an arithmetic code, instead of being fixed relative to the number of symbols being encoded, depends on the statistical frequency with which the source produces each symbol from its alphabet. For long sequences from sources having skewed distributions and small alphabets, arithmetic coding compresses better than Huffman coding.
The
arithenco
and arithdeco
functions support arithmetic coding and decoding.Represent Arithmetic Coding Parameters
Arithmetic coding requires statistical information about the source of the data being encoded. In particular, the
counts
input argument in the arithenco
and arithdeco
functions lists the frequency with which the source produces each symbol in its alphabet. You can determine the frequencies by studying a set of test data from the source. The set of test data can have any size you choose, as long as each symbol in the alphabet has a nonzero frequency.For example, before encoding data from a source that produces 10 x's, 10 y's, and 80 z's in a typical 100-symbol set of test data, define
Alternatively, if a larger set of test data from the source contains 22 x's, 23 y's, and 185 z's, then define
Create and Decode an Arithmetic Code Using MATLAB
The example below performs arithmetic encoding and decoding, using a source whose alphabet has three symbols.
Quantize a Signal
Scalar Quantization Example 1
The code below shows how the
quantiz
function uses partition
and codebook
to map a real vector, samp
, to a new vector, quantized
, whose entries are either -1, 0.5, 2, or 3.The output is below.
Scalar Quantization Example 2
This example illustrates the nature of scalar quantization more clearly. After quantizing a sampled sine wave, it plots the original and quantized signals. The plot contrasts the
x
's that make up the sine curve with the dots that make up the quantized signal. The vertical coordinate of each dot is a value in the vector codebook
.![](/uploads/1/2/7/5/127555234/316493610.jpg)