## 3 Materials and Methods
### Jamming Detection Algorithm
```
Begin Start; Set initial conditions for a Network; for each Network Scheme (collaborative, cooperative)do for each number of nodes (100, 200, 300, 400, 500)do for each area (100, 300, 500, 1000, 5000, 10000)do  Get tables from simulations and experiments;  Get data and data labels from tables; for each scenario (normal operation, reactive jamming, random jamming, constant jamming, deceptive jamming)do for each Nodedo  Measure Energy;  Calculate PDR;  Calculate RSSI;  end for  end for  end for for every nodedo  Detect signal;  Measure Energy in steady state and its maximum value \((Data_{1})\);  Calculate PDR in steady state and its minimum value \((Data_{2})\);  Calculate RSSI in steady state and its maximum value \((Data_{3})\);  Initialize counters \((C_{C_{O}}=0,C_{D_{E}}=0,C_{R_{A}}=0,C_{R_{E}}=0,C_{N_{I}}=0)\);  Sort data from smallest to largest \((A,B,C,D,E)\);  Determine decision thresholds \((Th_{A_{B}n},Th_{B_{C}n},Th_{C_{D}n},Th_{D_{E}n})\) where for Energy (\(n=1\)), for PDR (\(n=2\)) and for RSSI (\(n=3\)). end
```  
**Algorithm 1**Proposed jamming detection algorithm for collaborative and cooperative WSN. Part 1. ```
for every Metric: For Energy (n = 1), for PDR (n = 2) and for RSSI (n = 3)do for each nodedo  Assign Data (1, 2, or 3) to Data; ifData \(\leq\) MetricTh\({}_{A_{B^{n}}}\)then  temporarily save the \"data label\" associated with Data in this rhomb; else ifMetricTh\({}_{A_{B^{n}}}<Data\leq\)MetricTh\({}_{B_{C^{n}}}\)then  temporarily save the \"data label\" associated with Data in this rhomb; else ifMetricTh\({}_{B_{C^{n}}}<Data\leq\)MetricTh\({}_{D_{C^{n}}}\)then  temporarily save the \"data label\" associated with Data in this rhomb; else ifMetricTh\({}_{D_{C^{n}}}<Data\leq\)MetricTh\({}_{D_{C^{n}}}\)then  temporarily save the \"data label\" associated with Data in this last step;  end if  end if  end if  end if  end for
```  
**Algorithm 2**Proposed jamming detection algorithm for collaborative and cooperative WSN. Part 2. ```
for every data labeldo ifdata label associated with Data selected \(=\)\(C_{O}\)then \(C_{C_{O}}=C_{C_{O}}+1\) else ifdata label associated with Data selected \(=\)\(D_{E}\)then \(C_{D_{E}}=C_{D_{E}}+1\) else ifdata label associated with Data selected \(=\)\(R_{A}\)then \(C_{R_{A}}=C_{R_{A}}+1\) else ifdata label associated with Data selected \(=\)\(R_{E}\)then \(C_{R_{E}}=C_{R_{E}}+1\) else  data label associated with Data selected \(=\)\(N_{I}\); \(C_{N_{I}}=C_{N_{I}}+1\)  end if  end if  end for  end for
```  
**Algorithm**Proposed jamming detection algorithm for collaborative and cooperative WSN. Part 3. ```
for every data labeldo ifdata label associated with Data selected \(=\)\(C_{O}\)then \(C_{C_{O}}=C_{C_{O}}+1\) else ifdata label associated with Data selected \(=\)\(D_{E}\)then \(C_{D_{E}}=C_{D_{E}}+1\) else ifdata label associated with Data selected \(=\)\(R_{A}\)then \(C_{R_{A}}=C_{R_{A}}+1\) else ifdata label associated with Data selected \(=\)\(R_{E}\)then \(C_{R_{E}}=C_{R_{E}}+1\) else  data label associated with Data selected \(=\)\(N_{I}\); \(C_{N_{I}}=C_{N_{I}}+1\)  end if  end for  end for  end for
```  
**Algorithm**Proposed jamming detection algorithm for collaborative and cooperative WSN. Part 3. ```
To take a decision, display results; if\(C_{C_{O}}==3\)then  Constant jamming aggression; \"Jamming attack\" message;  Save Data 1, Data 2 and Data 3. else if\(C_{D_{L}}==3\)then  Deceptive jamming aggression; \"Jamming attack\" message;  Save Data 1, Data 2 and Data 3. else if\(C_{R_{A}}==3\)then  Random jamming aggression; \"Jamming attack\" message;  Save Data 1, Data 2 and Data 3. else if\(C_{R_{E}}==3\)then  Reactive jamming aggression; \"Jamming attack\" message;  Save Data 1, Data 2 and Data 3. else if\(C_{N_{J}}==3\)then  No jamming aggression;  NO Jamming attack\" message;  Save Data 1, Data 2 and Data 3. else  Repeat the process  end if  end if  end if ifDoes the user want to repeat the process?then  Repeat the process else  End process  end if
```  
**Algorithm 4**Proposed jamming detection algorithm for collaborative and cooperative WSN.