# What is Variable – Length Coding & How Does it's Works

#### Cyber Security : Go from Zero to Hero

Most Popular

60 Lectures 5.5 hours

#### Master C and Embedded C Programming- Learn as you go

Best Seller

66 Lectures 5.5 hours

#### C Programming from scratch- Master C Programming

Best Seller

60 Lectures 8 hours

### Introduction

Variable Length Coding (VLC) is a technique used in digital communication and data compression, which is designed to represent information using fewer bits than their original representation. This is achieved by assigning shorter codes to more frequent data symbols, and longer codes to less frequent symbols. The technique has been widely used in digital communication and data compression, particularly in image and video coding, where the high redundancy of data requires efficient coding techniques to reduce the data rate.

### How Variable Length Coding Works

Variable Length Coding works by assigning a unique code to each data symbol based on its frequency. In the simplest form, the code can be a binary string, where the most common symbols are assigned shorter codes, and less frequent symbols are assigned longer codes. This is called Huffman coding, which is a widely used technique for data compression.

The basic steps involved in the Huffman coding algorithm are as follows:

1. Frequency Calculation: The frequency of occurrence of each symbol in the input data stream is calculated.

2. Symbol Sorting: The symbols are sorted based on their frequency, with the most frequent symbol placed at the top.

3. Tree Creation: A binary tree is created, where each leaf node represents a symbol and its frequency, and each internal node represents the sum of the frequencies of its child nodes.

4. Code Assignment: The codes are assigned to the symbols based on their position in the binary tree. The code assigned to a symbol is obtained by traversing the tree from the root to the leaf node representing the symbol, and appending a 0 or 1 depending on whether the left or right branch is taken at each internal node.

5. Data Encoding: The input data is then encoded using the assigned codes, where each symbol is replaced by its corresponding code.

### Example of Variable Length Coding

Let's consider an example where we want to encode the following message using Huffman coding:

"ABBCCCDDDD"

Step 1: Frequency Calculation

 Symbol Frequency A 1 B 2 C 3 D 4

Step 2: Symbol Sorting

 Symbol Frequency D 4 C 3 B 2 A 1

Step 3: Tree Creation

The tree is created by repeatedly combining the two lowest frequency symbols until a single root node is obtained.

Step 4: Code Assignment

The codes are assigned to the symbols based on their position in the binary tree:

 Symbol Code D 0 C 10 B 110 A 111

Step 5: Data Encoding

The input message is then encoded using the assigned codes:

ABBCCCDDDD11011110100

### Advantages of Variable Length Coding

1. Efficient Data Compression: Variable length coding is an efficient data compression technique that can significantly reduce the data rate without loss of information.

2. Tailored Codes: Variable length coding can tailor codes to the statistical properties of the data, resulting in shorter codes for more frequent symbols and longer codes for less frequent symbols.

3. Error Resilience: Variable length coding is more resilient to transmission errors than fixed length coding. In case of a transmission error, only a few bits are affected, resulting in a smaller error impact compared to fixed length coding.

### Disadvantages of Variable Length Coding

1. Complexity: The process of constructing the Huffman tree and assigning codes can be computationally expensive for large datasets, making it impractical for real-time applications.

2. Overhead: Variable length coding introduces additional overhead in the form of codebook or dictionary, which is required to decode the data.

3. Variable Length Codes: Variable length codes may not be suitable for all applications, as they may not be compatible with certain hardware or software systems that require fixed length codes.

4. Sensitivity to Data: Variable length coding is highly sensitive to the statistical properties of the data, and may not perform well for data with highly variable or unpredictable distributions.

### Applications of Variable Length Coding

1. Image and Video Compression: Variable length coding is widely used in image and video compression standards such as JPEG, MPEG, and H.264 to reduce the data rate and improve the storage and transmission efficiency.

2. Voice and Speech Coding: Variable length coding is also used in voice and speech coding standards such as G.711, G.729, and AMR to compress audio data and improve the bandwidth efficiency of communication systems.

3. Text Compression: Variable length coding is used in text compression applications such as ZIP and gzip to compress textual data and reduce the storage and transmission requirements.

4. Network Protocols: Variable length coding is used in network protocols such as HTTP, SMTP, and FTP to encode headers and control information, which reduces the overhead and improves the efficiency of data transfer.

### Conclusion

Variable Length Coding is an efficient data compression technique that assigns shorter codes to more frequent data symbols, and longer codes to less frequent symbols. The technique has been widely used in digital communication and data compression, particularly in image and video coding, where the high redundancy of data requires efficient coding techniques to reduce the data rate. Despite its advantages, variable length coding also has some limitations, such as its complexity, overhead, and sensitivity to data. Nevertheless, the technique has proven to be a valuable tool for improving the efficiency of data storage and transmission in a variety of applications.