# An Energy Aware Adaptive Sampling Algorithm for Energy Harvesting WSN with Energy Hungry Sensors
## 3 Energy Aware Adaptive Sampling Algorithm  
Typically ASA algorithms evaluate the maximum frequency (\(f_{Max}\)) of the signal and decide the sampling frequency (\(f_{Sampling}\)), which is usually at least twice \(f_{Max}\) to avoid aliasing. An example of ASA algorithm (Algorithm 1) is presented in the pseudo code below and more details can be found in [6; 20]:  
```
1. Store an initial number of sensor samples (\(W\));
2. Estimate the \(f_{Max}\) on the acquired \(W\) samples;
3. Based on \(f_{Max}\) set the sampling frequency to a value \(f_{Sampling}=cf_{Max}\) where \(c\) is a confidence parameter that has to be \(\geq 2\) to avoid aliasing;
4. Define upper and lower frequency borders to detect the change in the maximum frequency \(f_{Up}\) and \(f_{Down}\) as: \(f_{Up}=min\ ((1+\delta)f_{Max},f_{Sampling}/2);f_{Down}=(1-\delta)f_{Max}\);
5. \(h_{1}=0\); \(h_{2}=0\); \(i=W+1\);
6. while(1){
7. Acquire a sample and add to the dataset;
8. Estimation of the current maximum frequency \(f_{Curr}\) on the sequence (\(i-W+1\), \(i\));
9. Count the consecutive number of events when the frequency was higher or lower than previously
10. If \((\ |f_{Curr}-f_{Up}|<|f_{Curr}-f_{Max}|)\)
11. \(h_{1}++;h_{2}=0\);
12. else if \((\ |f_{Curr}-f_{Down}|<|f_{Curr}-f_{Max}|)\)
13. \(h_{2}++;h_{1}=0\);
14. if \(((h_{1}>h)\ |\ |\ (h_{2}>h))(\)
15. \(f_{Sampling}=c\ f_{Curr};f_{Max}=f_{Curr}\);
16. \(f_{Up}=min[(1+\delta)f_{Max},f_{Sampling}/2];f_{Down}=(1-\delta)f_{Max}]\)
```  
**Algorithm 1** An example of ASA algorithm. The relevant parameters are as follows:  
* \(h\) is the consecutive number of samples after which the algorithm detects a change. A low value of \(h\) indicates variation in the maximum frequency of the detected signal. As a consequence, false detections are more frequent, which finally results in a very frequent update of the sampling rate--\(f_{Sampling}\). * \(\delta\) is the minimum percentage change in the maximum frequency that must be detected by ASA. Using the ASA algorithm can drastically reduce the number of acquired samples with respect to the traditional fixed sampling rate approach, hence saving energy [6; 20]. However, to guarantee self-sustainability when the node is equipped with energy harvesters, the ASA needs to monitor the energy level of the node and manage the node activity based on the available energy. Therefore, to further increase the life span of a node and ultimately achieve self-sustainability, we propose an approach in which each of the nodes in the network will have the ability to further adapt the sampling rate of the sensors according to the energy available in the system. This approach can be used as standalone extension for every ASA algorithm. The sampling rate of the sensors refers to how often the sensors are sampled and how often the measured data are sent to the base station (BS). It is also assumed that each of the nodes in the network is able to monitor its own energy level, and is able to harvest energy from the environment. Based on their energy levels, the nodes can adjust the sampling rate received from the BS following the equations below:  
\[f_{EASA}=f_{Sampling}K \tag{1}\]  
\[\left\{ \begin{array}{c}K=1\,\ if\ E_{batt}\geq X\\ K=1-\left(\frac{X_{level}\ -\ E_{batt}}{100}\right)^{m},if\ E_{batt}<X\end{array} \right. \tag{2}\]  
where \(K\) is the function cost for the energy awareness; \(E_{batt}\) is the battery level expressed in percentage (0-100); and \(X_{level}\) is the selected critical level of the battery in percentage, where the sample frequency starts to decrease from \(f_{Sampling}\) to \(f_{EASA}\) and the system starts to conserve energy. The rate of saving is also dictated by the parameter \(m\), which in our simulations was fixed to 1, 0.5, and 0.33. The proposed algorithm follows \(f_{Sampling}\) until the energy in the battery drops below a level \(X_{level}\). Afterwards, \(f_{Sampling}\) is decreased following the trend defined by Equation (2).