-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathss_energy.py
176 lines (129 loc) · 4.64 KB
/
ss_energy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
'''
NEYMANN-PEARSON SIGNAL DETECTOR FOR
SPECTRUM SAMPLING IN COGNITIVE RADIO
BASED ON SIGNAL ENERGY
AUTHOR: ABIJITH J. KAMATH
abijithj@iisc.ac.in, kamath-abhijith.github.io
'''
# %% LOAD LIBRARIES
import os
import numpy as np
from tqdm import tqdm
from scipy.stats import chi2
from matplotlib import style
from matplotlib import rcParams
from matplotlib import pyplot as plt
import utils
# %% PLOT SETTINGS
plt.style.use(['science','ieee'])
plt.rcParams.update({
"font.family": "serif",
"font.serif": ["cm"],
"mathtext.fontset": "cm",
"font.size": 24})
# %% PARAMETERS
Nd = 32
Nc = 8
K = 50
N = (K+1)*(Nc+Nd)
NUM_STATS = 1000
PFA = 0.05
SNR_MIN = -20
SNR_MAX = 6
SNR_STEP = 2
# %% MONTE CARLO SIMULATIONS // CLEAN PARAMETERS
SNRS = np.arange(SNR_MIN, SNR_MAX, SNR_STEP)
true_PFA = np.zeros(len(SNRS))
true_PD = np.zeros(len(SNRS))
est_PFA = np.zeros(len(SNRS))
est_PD = np.zeros(len(SNRS))
for itr, SNR in tqdm(enumerate(SNRS)):
noise_var = 1 / 10**(SNR/10)
threshold = chi2.isf(q=PFA, df=N) * noise_var
stats_H0 = utils.energy_stat_H0(NUM_STATS, Nd, Nc, K, noise_var)
stats_H1 = utils.energy_stat_H1(NUM_STATS, Nd, Nc, K, noise_var)
false_alarms = sum(stats_H0 > threshold)
detections = sum(stats_H1 > threshold)
est_PFA[itr] = false_alarms / NUM_STATS
est_PD[itr] = detections / NUM_STATS
true_PFA[itr] = PFA
true_PD[itr] = chi2.sf(x=threshold / (1 + noise_var), df=N)
# %% PLOTS // CLEAN PARAMETERS
os.makedirs('./results/', exist_ok=True)
path = './results/'
plt.figure(figsize=(12,6))
ax = plt.gca()
utils.plot_signal(SNRS, true_PD, ax=ax, plot_colour='green',
legend_label=r'TRUE $P_D$', show=False)
utils.plot_signal(SNRS, est_PD, ax=ax, plot_colour='blue',
legend_label=r'ESTIMATED $P_{D}$', yaxis_label=r'$P_{D}$',
xaxis_label=r'$\mathrm{SNR}$', show=True,
save=path+'eneProb_PD')
plt.figure(figsize=(12,6))
ax = plt.gca()
utils.plot_signal(SNRS, true_PFA, ax=ax, plot_colour='green',
legend_label=r'TRUE $P_{FA}$', show=False)
utils.plot_signal(SNRS, est_PFA, ax=ax, plot_colour='blue',
legend_label=r'ESTIMATED $P_{FA}$', yaxis_label=r'$P_{FA}$',
xaxis_label=r'$\mathrm{SNR}$', ylimits=[0,2*PFA], show=True,
save=path+'eneProb_PFA')
# %% MONTE CARLO SIMULATIONS // NOISY PARAMETERS
SNRS = np.arange(SNR_MIN, SNR_MAX, SNR_STEP)
true_PFA = np.zeros(len(SNRS))
true_PD = np.zeros(len(SNRS))
est_PFA = np.zeros(len(SNRS))
est_PD = np.zeros(len(SNRS))
for itr, SNR in tqdm(enumerate(SNRS)):
noise_var = 1 / 10**(SNR/10)
noise_var = noise_var * 10**(1/10)
threshold = chi2.isf(q=PFA, df=N) * noise_var
stats_H0 = utils.energy_stat_H0(NUM_STATS, Nd, Nc, K, noise_var)
stats_H1 = utils.energy_stat_H1(NUM_STATS, Nd, Nc, K, noise_var)
false_alarms = sum(stats_H0 > threshold)
detections = sum(stats_H1 > threshold)
est_PFA[itr] = false_alarms / NUM_STATS
est_PD[itr] = detections / NUM_STATS
true_PFA[itr] = PFA
true_PD[itr] = chi2.sf(x=threshold / (1 + noise_var), df=N)
# %% PLOTS // NOISY PARAMETERS
os.makedirs('./results/', exist_ok=True)
path = './results/'
plt.figure(figsize=(12,6))
ax = plt.gca()
utils.plot_signal(SNRS, true_PD, ax=ax, plot_colour='green',
legend_label=r'TRUE $P_D$', show=False)
utils.plot_signal(SNRS, est_PD, ax=ax, plot_colour='blue',
legend_label=r'ESTIMATED $P_{D}$', yaxis_label=r'$P_{D}$',
xaxis_label=r'$\mathrm{SNR}$', show=True,
save=path+'eneProb_PD_Noisy')
plt.figure(figsize=(12,6))
ax = plt.gca()
utils.plot_signal(SNRS, true_PFA, ax=ax, plot_colour='green',
legend_label=r'TRUE $P_{FA}$', show=False)
utils.plot_signal(SNRS, est_PFA, ax=ax, plot_colour='blue',
legend_label=r'ESTIMATED $P_{FA}$', yaxis_label=r'$P_{FA}$',
xaxis_label=r'$\mathrm{SNR}$', ylimits=[0,2*PFA], show=True,
save=path+'eneProb_PFA_Noisy')
# %% THRESHOLD COMPARISONS
prior1 = 0.2
prior0 = 1-prior1
SNRS = np.arange(SNR_MIN, SNR_MAX, SNR_STEP)
threshold_NP = np.zeros(len(SNRS))
threshold_BD = np.zeros(len(SNRS))
for itr, SNR in tqdm(enumerate(SNRS)):
noise_var = 1 / 10**(SNR/10)
noise_var = noise_var * 10**(1/10)
threshold_NP[itr] = chi2.isf(q=PFA, df=N) * noise_var
threshold_BD[itr] = 2 * (1+noise_var) * noise_var * \
(N/2 * np.log((1+noise_var)/noise_var) + np.log(prior0/prior1))
# %% PLOTS :: THRESHOLD COMPARISON
os.makedirs('./results/', exist_ok=True)
path = './results/'
plt.figure(figsize=(12,6))
ax = plt.gca()
utils.plot_signal(SNRS, threshold_NP, ax=ax,
legend_label=r'Neymann-Pearson Detector', show=False)
utils.plot_signal(SNRS, threshold_BD, ax=ax, plot_colour='green',
ylimits=[0,0.5*1e6], legend_label=r'Bayes Detector', show=True,
save=path+'thresholds')
# %%