## 4 Design and Implementation
### Proposed HFSPDM Modulation
#### 4.3.2 Decoding in HFS-PDM Modulation  
Decoding of HFSPDM Modulated signal requires two thresholding steps. First thresholding is performed to identify the frequencies, while the second thresholding is performed to identify the duration of frequencies. The first level of thresholding is performed similar to BFSOOK as discussed in Section 4.2.2, which provides the largest stripe width from each frames. The widest stripe from sample frames should be four times the width of the smallest stripe due to the preset relation between frequencies. However, as the practical system is prone to external noise, an error margin of 10% of the widest stripe is set. Thus, the relation between the widths of different frequencies is given as follows, where \(W1\), \(W2\), and \(W3\) are widths of stripes in the frames for frequencies \(F1\), \(F2\), and \(F3\), respectively.  
\[W3=4\times W1\pm\frac{W1}{10} \tag{11}\]  
Stripe width of second frequency can be estimated from \(W3\) and \(W1\) as shown in Equation (12)  
\[W2=\frac{W3}{2}\pm\frac{W1}{10} \tag{12}\]  
After finding frequency from the frames, second thresholding is applied based on Percentage Frequency Frame Ratio(PFFR). In this method, the percentage occurrence of any frequency frame out off \(F2\) and \(F3\) can be considered. For example Equation (13) shows \(P_{F2}\) as percentage of \(F2\) frequency frames. Where, \(T_{F2}\) is total number of frames between successive start frames containing \(F2\) frequency and \(N_{Frames}\) is total number of frame between successive start frames.  
\[P_{F2}=\frac{T_{F2}}{N_{Frames}}\times 100 \tag{13}\]  
The algorithmic implementation of HFSPDM is as shown in Algorithm 7. The duration of frequencies should be more than twice the frame rate of camera for sampling. Here, assuming pre-processing is performed, a sample set of stripe widths from the video stream is created. The frames can be identified in \(F1\), \(F2\), and \(F3\) frequency frames from the obtained widths. Finally, the bit pairs are decoded by counting the number of \(F2\) and \(F3\) frequency frames and comparing it with the threshold values. Here two boolean flags are used to track start bit frequency \(F1\) and count the number of \(F2\) and \(F3\) frequency frames. \(Fr2\) and \(Fr3\) consist of the count of respective frequency frames and percentage of \(F2\) or \(F3\) frequency is estimated. The obtained percentage is then used to decode the bit pair using percentage mapping applied during encoding.  
```
1:functionCountFrequencyFrame(CroppedImage)
2:W=CountWidth(CroppedImage)
3:ifW==F1 and Flag1==0 and Flag2=0 then
4: Set Flag1=1
5:elseifW==F2 and Flag1==1 then
6: Set Flag2=1
7: Increment Counter for F2
8:elseifW==F3 and Flag1==1 then
9: Set Flag2=1
10: Increment Counter for F3
11:elseifW==F1 and Flag1==1 and Flag2==1 then
12: Set Flag1=0
13: Set Flag2=0
14: Return Count of F2 and F3 Frequency containing frames
15:endif
16:endfunction
17:functionMain(video)
18:for each frame in the video do
19:Preprocessing(image) \(\triangleright\) As shown in Algorithm 1
20:CountWidth(CroppedImage)
21: Append the obtained value in a buffer
22:if buffer size is full then
23: F1=min(buffer)
24: F3=4\(\times\)F1+F1/10
25: F2=F3/2+F1/10
26:endif
27:endfor
28:for each frame in the video do
29:Fr2,Fr3=CountFrequencyframes(CroppedImage)
30: Compare with the threshold value
31: Decode bit pair
32:endfor
33:endfunction
```  
**Algorithm 7** HFSPDM Demodulation for VLC-OCC Receiver.