## 3 Materials and Methods
### Jamming Detection Algorithm  
The _jamming detection algorithm_ is based on performance metrics and uses as input the cooperative and collaborative schemes as well as the density of nodes, as seen in Figure 4. It is a practical method that consists of a continuous calculation of the PDR and RSSI metrics, which are complemented with Energy to distinguish the type of interference that is present in the network. From among all the metrics presented in Table 3, we chose only three representative metrics that were sufficient to exemplify the performance of a sensor. The selected metrics are RSSI, PDR and Energy. The proposed mechanism uses these metrics according to the actual performance of the sensors that we use in the experiment. First, the sniffer of the sensors with which the experiment was carried out makes it possible to easily measure the  
Figure 3: Proposed detection mechanism. RSSI, from the MAC layer. On the other hand, the PDR, from the network layer, is an easy metric to count with any sensor brand. Finally, the Energy, in all the layers, is programmed with a model that has already been tested in other research works. This makes the model more representative, more random and a bit more reliable. The algorithm begins by requesting as inputs the network scheme, the number of nodes and the area, taking into account that the counters of the four types of jamming and _no jamming_ are at zero. Next, a database provides the data tables obtained with the simulations and the experiment. Next, the signal from WSN in real time is detected, PDR and RSSI are calculated and Energy is measured. All jamming counters are then reset. All the data from tables is organized from smallest (A) to largest (E). The decision _thresholds_ are then determined by _taking the averages_ of each pair of data groups already ordered from lowest to highest. The process is carried out following the statistical technique of _moving average_ in which the average of the \(m\) elements contained in a sample is taken (in our algorithm \(m=2\)) causing an aliasing effect. Figure 4: Proposed jamming detection algorithm. Figure 4 can be written as a pseudo-code (see Algorithms 1-4). The energy comparisons are made first (when \(n=1\)), taking the energy data in real time and seeing between which _decision thresholds_ that data falls, and consequently we associate the _data label_ to the _data selected_. A similar process is carried out with the PDR (when \(n=2\)) and the RSSI (when \(n=3\)), taking into consideration that the order of the data from lowest to highest does not always come from the same type of jamming. Once the iterations with the three performance metrics are completed, the _data label_ (\(C_{O}\), \(D_{E}\), \(R_{A}\), \(R_{E}\), \(N_{I}\)) is associated to the _data selected_ (real-time Energy, PDR and RSSI values), transferring their resulting values to the corresponding jamming type counters, and if the result obtained is equal to three, it indicates the presence of that kind of jamming. In the event that the counter value is one or two, the process must be repeated with other real-time measured data. In the absence of jamming attacks, the _no jamming_ (\(C_{N_{I}}\)) counter will have a value of three. According to the measurements of all the metrics mentioned above, an alarm of the presence of jamming would be produced, indicating what type of jamming the network is facing, so that an algorithm for mitigation can be implemented immediately. In addition, when a type of jamming has been detected, the real-time information that originated this detection can be stored (Step 35) for reference wherein future artificial intelligence or machine learning could be used to improve the algorithm. Through this algorithm, the aggressiveness suffered by the network from jamming can be labeled and the bases for its mitigation can be laid.