# Investigation of Energy Cost of Data Compression Algorithms in WSN for IoT Applications
## 4 Hybrid Model for Run Length Encoding with Adaptive Huffman Encoding
### Hybrid Algorithm  
The proposed hybrid algorithm works in two phases: Initially the data is compressed using RLE algorithm, then in the second phase, AHE algorithm is applied on the compressed data received from RLE. The parameters used in the proposed H-RLEAHE algorithm are given in Table 1. Further, the compression technique using the proposed algorithm and decompression is discussed thereafter. \begin{table}
\begin{tabular}{l l} \hline \hline
**Parameter** & **Description** \\ \hline D\({}_{\text{in}}\) & A sequence of sensor data \\ P\({}_{\text{k}}\) & Packet size \\ NYT & Not Yet Transmitted \\ R\({}_{\text{node}}\) & Repeat count in RLE \\ Length & Length of the stream \\ Unique & Unique data in the stream \\ Final stream & Output stream packet \\ Loc & Location of the Pointer \\ Info \(0\) & Information about the node \\ \hline \hline \end{tabular}
\end{table} 
Table 1: Parameters that were used in the data compression algorithm. ```
1:Input
2:Packet Size= Px
3:Input Data = Din: A sequence of sensor data of length Px
4:Start
5:Index = 1
6:R\({}_{\text{Code}}\)(Index)= D\({}_{\text{m}}\)(index)
7:Info (Index) = 1
8:for i = 2 to Px:
9:if Dim(i-1) == Din(i):
10:Info (Index)= Info (Index) + 1
11:else:
12:Index = Index + 1
13:R\({}_{\text{Code}}\)(Index) = Din(i)
14:Info (Index) = 1
15:End
16:End
17:NYT = unique(RCode)
18:N = length(RCode)
19:Loc = 1
20:for i = 1 to N
21:Temp = R\({}_{\text{Code}}\)(i)
22:if (NYT = = Temp)
23:Old_Code = NYT_Code(NYTlocation-1)
24:New_Node = Weight_Increment for Temp, OldCode
25:else
26:Code = NYT_Code()
27:Number = get_Number(Code)
28:end
29:Step:A
30:if max (Number) in Block
31:New_Node = Weight_Increment()
32:else
33:switch_Node to max Numbered Node
34:New_Node = Weight_Increment()
35:end
36:ifNew_Node in Root Node:
37:Final_Stream(Loc) = New_Node
38:Loc ++
39:exit ()
40:else
41:Go to parent node in step A
42:end
43:end
44:Output:Final_StreamIn the H-RLEAHE algorithm, in step 1, data packet is initialized in bits. From step 2 to step 5, RLE algorithm is initialized. During step 6, the _for_-loop is setup and examines to see whether there are any characters that are identical to those in the next index. The count increments to 1 if the characters are identical. If not, the count and character are concatenated. Step 10 checks for unique code and matches and stores it in the location. In step 11, Adaptive Huffman encoding is applied. The Algorithm 2 for decompression is given below. ```
1: loop = 1
2: While (loop < length (Final_Stream))
3: Temp= Final_Stream(loop)
4: If (Temp == NYT)
5: Val = Freq_increment(Temp)
6: Update (Tree)
7: else
8: Readbits_(NewNYT)
9: Val = Get_NYT()
10: Add_NYT to tree
11: end
12: AHcode(Loc) = Val
13: loop ++
14: end
15: Final_Data = empty array
16: Count, AHcode =SeperateCountData(AHcode)
17: For i 1 to length(AHcode)
18: Final_Data(i)= AHcode(i)^ones(Count(i))
19: End
20: Decompressed_Data = Final_Data
```

**Algorithm 2**Decompression algorithm for H-RLEAHE

## 5 Performance Measures and Analysis

Analyses of data compression techniques are carried out using the following metrics. * Compression Ratio (\(CR\)): The compression algorithms factor is the ratio of the size of the uncompressed data in bits (\(b_{uc}\)) to the size of the compressed data in bits (\(b_{c}\)) [43]. \[CR=\frac{b_{uc}}{b_{c}}\] (1)
* Compression Time (\(CT\)): The time it takes to compress the original data [43]. * Consumption of CPU resources or power used during data compression: An investigation on data compression's impact on energy use. * Transmission cost: The amount of energy consumed to transmit compressed data. _Analysis of RLE, AHE, H-RLEAHE and H-AHERLE Algorithm_

In this section we have used real world data [44] to analyze the compression ratio of different algorithms with varying data size. Suppose the length of original data is \(L\) and the length of compressed data is \(K\). We have formulated the energy consumption of different algorithms and used the TIMSP430 [45] microcontroller which has a 16-bit CPU designed for systems with minimal resources.