Skip to content
Snippets Groups Projects
Commit d8298244 authored by Daniel Meißner's avatar Daniel Meißner Committed by Daniel Meißner
Browse files

scripts: added script that can detect noise floor

parent b776eba7
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import sys
import numpy as np
from gnuradio import blocks
from gnuradio import gr
class top_block(gr.top_block):
def __init__(self, samp_rate=2e6, file_path=sys.argv[1], measure_time=sys.argv[2]):
gr.top_block.__init__(self, "Top Block")
##################################################
# Variables
##################################################
self.samp_rate = samp_rate
# Queue
self.msgq_out = blocks_message_sink_0_msgq_out = gr.msg_queue(0)
##################################################
# Blocks
##################################################
self.blocks_head_0 = blocks.head(gr.sizeof_gr_complex*1, int(samp_rate)*int(measure_time))
self.blocks_throttle_0 = blocks.throttle(gr.sizeof_gr_complex*1, \
10*samp_rate, \
True)
self.blocks_message_sink_0 = blocks.message_sink(gr.sizeof_float*1, \
blocks_message_sink_0_msgq_out, \
False)
self.blocks_file_source_0 = blocks.file_source(gr.sizeof_gr_complex*1, \
str(file_path), \
False)
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))
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 main(top_block_cls=top_block, options=None):
"""
Examples:
1. USRP spectrum sense /usr/share/gnuradio/examples/uhd/usrp_spectrum_sense.py
"""
tb = top_block_cls()
tb.start()
samples_count = 0
samples_average = np.empty(0, dtype='float32')
samples_median = np.empty(0, dtype='float32')
samples_std = np.empty(0, dtype='float32')
minimum = 999999999.9
maximum = 0.0
while True:
# pull from message queue
msg = tb.msgq_out.delete_head()
msg_string = msg.to_string()
# convert string with floats into an numpy array
samples = np.fromstring(msg_string, dtype='float32')
samples_average = np.append(samples_average, np.average(samples))
samples_median = np.append(samples_median, np.median(samples))
samples_std = np.append(samples_std, np.std(samples))
mi = np.amin(samples)
ma = np.amax(samples)
if mi < minimum:
minimum = mi
if ma > maximum:
maximum = ma
samples_count += len(samples)
if samples_count >= int(sys.argv[2])*2e6:
break
print("\nProcessed %s samples!\n" % (3*int(2e6)))
print("min: %s " % minimum)
print("max: %s \n" % maximum)
print("average: %s" % np.average(samples_average))
print("median: %s" % np.median(samples_median))
print("standard diviation: %s" % np.std(samples_std))
if __name__ == '__main__':
main()
\ No newline at end of file
......@@ -27,7 +27,12 @@ class top_block(gr.top_block):
self.blocks_throttle_0 = blocks.throttle(gr.sizeof_gr_complex*1, \
10*samp_rate, \
True)
self.blocks_threshold_ff_0 = blocks.threshold_ff(0.0006, 0.0006, 0)
#self.threshold = 0.0006
# 0.0000317
# 0.00031
self.threshold = 3.17382e-05*2
self.blocks_threshold_ff_0 = blocks.threshold_ff(self.threshold, self.threshold)
self.blocks_message_sink_0 = blocks.message_sink(gr.sizeof_float*1, \
blocks_message_sink_0_msgq_out, \
False)
......@@ -98,7 +103,7 @@ def main(top_block_cls=top_block, options=None):
count_zero += np.count_nonzero(samples != 1.0)
# Section 5
if ones > 0:
if ones > 0 and np.count_nonzero(samples) > 150:
# skip first message queue sample
if len(arrays_with_ones) == 0:
hash = {}
......@@ -111,10 +116,10 @@ def main(top_block_cls=top_block, options=None):
if (last_recorded_samples['count_before_array'] + \
len(last_recorded_samples['samples'])) == samples_sum:
print("ones in block: %s samples: %s" % (np.count_nonzero(samples), len(samples)))
for sample in samples:
arrays_with_ones[-1]['samples'] = np.append( \
arrays_with_ones[-1]['samples'], sample \
)
arrays_with_ones[-1]['samples'] = np.append \
(arrays_with_ones[-1]['samples'], sample)
else:
hash = {}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment