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

count zero script: added support for signal grouping

Added support to split a big iq sample file into signal blocks.
parent 95412441
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
##################################################
# GNU Radio Python Flow Graph
# Title: Top Block
# Generated: Fri Jan 22 17:58:53 2016
##################################################
import time
import struct
import array
import sys
from gnuradio import blocks
from gnuradio import eng_notation
from gnuradio import gr
from gnuradio.eng_option import eng_option
from gnuradio.filter import firdes
from optparse import OptionParser
import numpy as np
class top_block(gr.top_block):
def __init__(self):
def __init__(self, samp_rate=2e6, file_path=sys.argv[1]):
gr.top_block.__init__(self, "Top Block")
##################################################
# Variables
##################################################
self.samp_rate = samp_rate = 2e6
self.freq = freq = 868e6
self.samp_rate = samp_rate
##################################################
# Message queues (added by grcconvert)
##################################################
# Queue
self.msgq_out = blocks_message_sink_0_msgq_out = gr.msg_queue(0)
##################################################
# Blocks
##################################################
self.blocks_throttle_0 = blocks.throttle(gr.sizeof_gr_complex*1, 10*samp_rate,True)
self.blocks_threshold_ff_0 = blocks.threshold_ff(0.001, 0.001, 0)
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, "/home/meise/tmp/iq_dumps/enocean/868_2m_enocean.iq", False)
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.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
##################################################
# file_source → throttle → complex_to_max_squared → threshold → message_sink
self.connect((self.blocks_complex_to_mag_squared_0, 0), (self.blocks_threshold_ff_0, 0))
self.connect((self.blocks_file_source_0, 0), (self.blocks_throttle_0, 0))
# removed by grcconvert: # self.connect((self.blocks_message_sink_0, 'msg'), (self, 0))
self.connect((self.blocks_threshold_ff_0, 0), (self.blocks_message_sink_0, 0))
self.connect((self.blocks_throttle_0, 0), (self.blocks_complex_to_mag_squared_0, 0))
......@@ -61,18 +53,28 @@ class top_block(gr.top_block):
self.samp_rate = samp_rate
self.blocks_throttle_0.set_sample_rate(self.samp_rate)
def get_freq(self):
return self.freq
def set_freq(self, freq):
self.freq = freq
def main(top_block_cls=top_block, options=None):
"""
What does this function do?
1. Run flowgraph and do energy based signal detection write results into
message queue.
2. Read iq dump file and count samples.
3. Start reading data from flowgraph message queue.
4. Counting some data:
a) 1s and 0s
b) number of all samples
5. If there are ones, save section into a array with hashes and merge depending
sections.
6. Print results.
"""
# Section 1
tb = top_block_cls()
tb.start()
data = np.memmap(open("/home/meise/tmp/iq_dumps/enocean/868_2m_enocean.iq"), mode="r", dtype=np.complex64)
# Section 2
data = np.memmap(open(sys.argv[1]), mode="r", dtype=np.complex64)
samples_in_file = len(data)
print("sum of all samples: %s\n" % samples_in_file)
......@@ -80,33 +82,86 @@ def main(top_block_cls=top_block, options=None):
count_one = 0
samples_sum = 0
arrays_with_ones = []
# Section 3
# just loop until all samples are processed
while True:
# pull from message queue
msg = tb.msgq_out.delete_head()
msg_string = msg.to_string()
# # extract floats from msg string
# # it is slow. very slow! :/
# for i in range(0,len(sample),4):
# # unpack sample to have a tuples
# unpacked = struct.unpack_from('f', sample[i:i+4])
# samples.append(unpacked[0])
# if len(unpacked) != 1:
# print("unpacked length: %s" % (len(unpacked)))
# convert string with floats into an numpy array
samples = np.fromstring(msg_string, dtype='float32')
# do some counting
count_one += np.count_nonzero(samples)
# Section 4: do some counting
ones = np.count_nonzero(samples)
count_one += ones
count_zero += np.count_nonzero(samples != 1.0)
# Section 5
if ones > 0:
if len(arrays_with_ones) == 0:
hash = {}
hash['count_before_array'] = samples_sum
hash['samples'] = samples
arrays_with_ones.append(hash)
else:
last_recorded_samples = arrays_with_ones[-1]
if (last_recorded_samples['count_before_array'] + \
len(last_recorded_samples['samples'])) == samples_sum:
for sample in samples:
arrays_with_ones[-1]['samples'] = np.append( \
arrays_with_ones[-1]['samples'], sample \
)
else:
hash = {}
hash['count_before_array'] = int(samples_sum)
hash['samples'] = samples
arrays_with_ones.append(hash)
samples_sum += len(samples)
if samples_sum == samples_in_file:
break
# Section 6
print("%s of %s sample" % (samples_sum, samples_in_file))
print("one: %s" % count_one)
print("zero: %s" % count_zero)
print("one: %s" % count_one)
print("ones in time (1/2e6)*%s = %ss = %sms\n" % (count_one, 1/2e6*count_one, 1/2e6*count_one*1000))
print("print sections with ones:")
for hash in arrays_with_ones:
string = ""
print("Count: %s" % hash['count_before_array'])
for items in hash['samples']:
string += str(int(items))
print("%s\n" % string)
if __name__ == '__main__':
main()
./count_zeros.py /home/meise/tmp/iq_dumps/enocean/868_2m_10db_rf_enocean_2_-1a_up.iq
Using Volk machine: sse4_1_64_orc
sum of all samples: 22021632
22021632 of 22021632 sample
zero: 22019706
one: 1926
ones in time (1/2e6)*1926 = 0.000963s = 0.963ms
print sections with ones:
Count: 15420758
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111111111111000000000000000001111111111111110000000000000000011111111111111100000000000000000111111111111111000000000000000001111111111111111111111111111111100000000000000001111111111111111000000000000000011111111111111100000000000000000000000000000000001111111111111111111111111111110000000000000000011111111111111111111111111111111111111111111111000000000000000001111111111111111111111111111111111111111111111100000000000000000111111111111111100000000000000000000000000000000111111111111111000000000000000000000000000000000111111111111111100000000000000000000000000000000000000000000000000000000000000001111111111111111111111111111111000000000000000000000000000000000111111111111111100000000000000000000000000000000111111111111111100000000000000000000000000000000000000000000000000000000000000001111111111111111111111111111111000000000000000000000000000000000111111111111111000000000000000001111111111111111111111111111111100000000000000000111111111111111111111111111111111111111111111110000000000000000000000000000000000000000000000000000000000000000111111111111111000000000000000001111111111111111111111111111111100000000000000000111111111111111000000000000000011111111111111110000000000000000000000000000000011111111111111110000000000000000111111111111111111111111111111111111111111111111000000000000000011111111111111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Count: 15428950
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111111111111000000000000000001111111111111110000000000000000011111111111111100000000000000001111111111111111100000000000000001111111111111111111111111111111000000000000000001111111111111110000000000000000011111111111111110000000000000000000000000000000011111111111111111111111111111111000000000000000011111111111111111111111111111111111111111111111100000000000000001111111111111111111111111111111111111111111111110000000000000000111111111111111100000000000000000000000000000000111111111111111000000000000000000000000000000000111111111111111100000000000000000000000000000000000000000000000000000000000000001111111111111111111111111111111000000000000000000000000000000000111111111111111000000000000000000000000000000000111111111111111000000000000000000000000000000000000000000000000000000000000000001111111111111111111111111111111100000000000000000000000000000000111111111111111100000000000000000111111111111111111111111111111100000000000000001111111111111111111111111111111111111111111111100000000000000000000000000000000000000000000000000000000000000000111111111111111000000000000000001111111111111111111111111111111100000000000000001111111111111111000000000000000011111111111111110000000000000000000000000000000001111111111111110000000000000000111111111111111111111111111111111111111111111111000000000000000011111111111111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Count: 15474006
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011111111111111110000000000000000011111111111111100000000000000000011111111111111000000000000000011111111111111110000000000000000011111111111111111111111111111111000000000000000011111111111111110000000000000000111111111111111000000000000000000000000000000001111111111111111111111111111111100000000000000001011111111111111111111111111111111111111111111110000000000000000011111111111111111111111111111111111111111111111000000000000000001111111111111110000000000000000000000000000000001111111111111110000000000000000000000000000000001111111111111110000000000000000000000000000000000000000000000000000000000000000011111111111111111111111111111111000000000000000000000000000000001111111111111110000000000000000000000000000000011111111111111110000000000000000000000000000000000000000000000000000000000000000011111111111111111111111111111110000000000000000000000000000000001111111111111111000000000000000011111111111111111111111111111110000000000000000111111111111111111111111111111111111111111111111000000000000000000000000000000000000000000000000000000000000000001111111111111111000000000000000011111111111111111111111111111111000000000000000111111111111111100000000000000000111111111111111100000000000000000000000000000000111111111111111100000000000000001111111111111111111111111111111111111111111111100000000000000000111111111111111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
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