Newer
Older
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
import sys
import os
import tempfile
import numpy as np
import matplotlib.pyplot as plt
from gnuradio import blocks
from gnuradio import gr
import osmosdr
class NoiseFloorDetect(gr.top_block):
def __init__(self,
samp_rate,
freq,
capture_time,
device=None
):
gr.top_block.__init__(self, "Top Block")
self.samp_rate = samp_rate
self.freq = freq
self.capture_time = int(capture_time)
self.device = device
self.msgq_out = self.blocks_message_sink_0_msgq_out = gr.msg_queue(0)
self._build_blocks()
def detect(self):
"""
"""
self.start()
samples_count = 0
sample_blocks = []
minimum = 999999999.9
maximum = 0.0
while True:
# pull from message queue
msg = self.msgq_out.delete_head()
msg_string = msg.to_string()
# convert string with floats into an numpy array
samples = np.fromstring(msg_string, dtype='float32')
sample_blocks.append(samples)
samples_count += len(samples)
if samples_count >= self.capture_time*self.samp_rate:
break
samples = np.concatenate(sample_blocks)
statistic_results = self._extract_statistic_data_from_samples(samples)
plot_results = self._plot_data(samples, self.samp_rate)
results = statistic_results
results['plot_path'] = plot_results['plot_path']
results['threshold'] = plot_results['threshold']
return results
def get_samp_rate(self):
return self.samp_rate
def set_samp_rate(self, samp_rate):
self.samp_rate = samp_rate
self.blocks_throttle_0.set_sample_rate(self.samp_rate)
def _plot_data(self, samples, samp_rate=2e6):
result = {}
# 1. split samples into 1s sections and extract max value
# 2. threshold is calculated as median + min value
max_sections = []
for i in range(0,int(len(samples)/2e6)-1):
max = np.amax(samples[i*int(samp_rate):(i+1)*int(samp_rate)])
max_sections.append(max)
median = np.median(max_sections)
min = np.amin(max_sections)
result['threshold'] = round(median+min, 6)
plt.plot(samples)
plt.plot([0, len(samples)-1], \
[result['threshold'], result['threshold']], \
label="threshold " + str(result['threshold']), \
linewidth=1.5, linestyle="-", color="red")
plt.legend(loc='upper right')
plt.xlabel("Samples")
plt.ylabel("Magnitude")
result['plot_path'] = self._create_image_tmp_file()
plt.savefig(result['plot_path'].name)
plt.close()
return result
def _create_image_tmp_file(self):
return tempfile.NamedTemporaryFile()
def _extract_statistic_data_from_samples(self, samples):
measurements = {}
measurements['min'] = np.amin(samples)
measurements['max'] = np.amax(samples)
measurements['average'] = np.average(samples)
measurements['median'] = np.median(samples)
measurements['std'] = np.std(samples)
measurements['seconds'] = len(samples)/self.samp_rate
return measurements
def _build_blocks(self):
self.osmosdr_source_0 = osmosdr.source( args="numchan=" + str(1) + " " + "osmosdr=0" )
self.osmosdr_source_0.set_sample_rate(self.samp_rate)
self.osmosdr_source_0.set_center_freq(self.freq, 0)
self.osmosdr_source_0.set_freq_corr(0, 0)
self.osmosdr_source_0.set_dc_offset_mode(0, 0)
self.osmosdr_source_0.set_iq_balance_mode(0, 0)
self.osmosdr_source_0.set_gain_mode(False, 0)
self.osmosdr_source_0.set_gain(0, 0)
self.osmosdr_source_0.set_if_gain(20, 0)
self.osmosdr_source_0.set_bb_gain(20, 0)
self.osmosdr_source_0.set_antenna("", 0)
self.osmosdr_source_0.set_bandwidth(0, 0)
self.blocks_head_0 = blocks.head(gr.sizeof_gr_complex*1, \
int(self.samp_rate)*int(self.capture_time))
self.blocks_throttle_0 = blocks.throttle(gr.sizeof_gr_complex*1, \
10*self.samp_rate, \
True)
self.blocks_message_sink_0 = blocks.message_sink(gr.sizeof_float*1, \
self.blocks_message_sink_0_msgq_out, \
False)
self.blocks_file_source_0 = self.osmosdr_source_0
self.blocks_complex_to_mag_squared_0 = blocks.complex_to_mag_squared(1)
##################################################
# Connections
##################################################
self.connect((self.blocks_file_source_0, 0), (self.blocks_throttle_0, 0))
self.connect((self.blocks_throttle_0, 0), (self.blocks_head_0, 0))
self.connect((self.blocks_head_0, 0), (self.blocks_complex_to_mag_squared_0, 0))
self.connect((self.blocks_complex_to_mag_squared_0, 0), \
(self.blocks_message_sink_0, 0))