#!/usr/bin/env python2
# -*- coding: utf-8 -*-

import sys

from gnuradio import blocks
from gnuradio import gr

import numpy as np


class top_block(gr.top_block):

    def __init__(self, samp_rate=2e6, file_path=sys.argv[1]):
        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_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))
        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))

    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):
    """
    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()

    # 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)

    count_zero  = 0
    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()

        # convert string with floats into an numpy array
        samples = np.fromstring(msg_string, dtype='float32')
        # 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("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