## 6 Energy Detection Algorithm  
The algorithm developed for simulating ED of OFDM signals is presented in this section. The algorithm enables the simulation of the detection process for PU signals with and without DT adaptation and NU variations. Matlab (R2016) was used to model the SS process and to simulate the ED of the signals transmitted within different OFDM system designs (RA, MA and combined RA and MA). The pseudocode of the algorithm used for the SS of signals based on the ED method is presented as Algorithm 1. The 1 st line of Algorithm 1 sets the input simulation parameters, which include the OFDM signal (_ofdm_signal_), the sample quantity (\(N\)), the length of the OFDM data (_len_ofdm_data_) obtained after conversion of signals from parallel to serial (as shown in Figure 1), the SNR, the NU factor (\(\rho\)), the DT factor (\(\rho\)), the noise variances (\(\sigma_{n_{i}}^{2}\)), the range of \(P_{fa_{i}}\) values (_length_ (\(P_{fa_{i}}\))) and the number of Monte Carlo simulations (_kk_). Table 3 shows the exact values of the parameters used in the simulation. The selected values are based on the parameters characteristic of the real OFDM systems. The OFDM signal (_ofdm_signal_) in line 3 of Algorithm 1 is generated by setting the initial parameters, which include the OFDM modulation type, the size of each OFDM block, the FFT/IFFT points, the length of CP, the interference constellation and the normalisation type and amount of PU Tx power. The parameters for performing the Monte Carlo simulations, such as the length of the false alarm probability (\(P_{fa_{i}}\)) and the number of simulations, are defined and executed in Algorithm 1 lines 4 to 8. Lines 9 to 10 show the pseudocode expressing generation of AWGN with variance \(\sigma_{n_{i}}^{2}\) and zero mean. The selected values of noise variances used for the simulation are set to realistic levels characteristic for real wireless channels (Table 3). The OFDM signal (_final_ofdm_signal_) is created by multiplying the amount of SNR and the values of the OFDM signal in line 11. In lines 12-13 of Algorithm 1, two versions of the received signal are shown. The _Received_signal_1_ indicates the OFDM signal without the noise variation (\(\rho=1\)) detected with the DT adaptation (\(\rho^{\prime}>1\)). The _Received_signal_2_ indicates the OFDM signal in the scenario of NU variation (\(\rho>1\)) detected with DT adaptation (\(\rho^{\prime}>1\)). The energy calculation for each of the received signals (_energy_calc_1 and _energy_calc_2_) is presented in lines 14-15 of Algorithm 1. In pseudocode lines 16-17, the average signal received for a number of \(N\) samples is represented as a calculation of test statistics for two scenarios: the test statistics for signals received with DT adaptation (_test_stat_1_) and for signals received with NU variation and DT adaptation (_test_stat_2_). The calculation of the test statistics expressed as average received signal energy is performed according to the relation (2). Pseudocode lines 18-19 present the assessment of the received signal threshold. _Thresh1(\(p\))_ expresses the first scenario where the received signal is detected by means of DT adaptation. _Thresh12(\(p\))_ expresses the second scenario where the received signal is impacted by the NU variation and received based on DT adaptation. Mathematical expressions for the first and second scenario are presented by relations (17) and (23), respectively.