Sunday, November 18, 2018

Saving Selected GNURadio Data using an External Trigger

When using GNURadio Companion (GRC) software package with a software defined radio (SDR) to capture radio signals, the usual approach is to save the incoming data stream into a file so that they can be processed later other other tools. However, the fast sample rate of SDR devices such as HackRF causes GRC script to generate large IQ data files which are difficult to handle on computers unless there's a big memory and a fast processor. It would be a wiser decision to save IQ data only when there's some useful signal is coming through the data stream. This article describes a way to give an external trigger to a GRC script in order to save selected segments of the incoming IQ data stream into a file. The two key elements of this trick are Burst Tagger and Tagged File Sink.

The Burst Tagger is a block which tag the incoming data stream by inserting a special tag with a value. The tagging work is one based on an input given to it through a second channel. When the value of this second channel goes above 0, a tag is inserted into the data string and when the value of the second channel goes below 0 again, another tag is inserted into the data. These two tags specify the region of the data stream which we needs to be saved. This output of the Burst Tagger is sent to the Tagged File Sink block. This block identify the tagged regions of the data stream and save each of those regions into separate data files.

Let's start.

(1) First of all, we need a GNURadio Companion flowchart like the one shown in the following figure. Once the flowchart was created, generate the Python script by clicking the button to "Generate the script". The generated file is named as top_block.py by default.




(2) Write the following Python script in a separate file and name as test.py. This script updates the "trigger" variable which causes the GRC script to perform the tagging and saving data files.


 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
import top_block
import time

print("Creating top_block class...")
tb=top_block.top_block()

print("Starting top_block...")
tb.start()
print("Started...")

#######################################
# Testing the trigger

print("saving...")
tb.set_trigger(1)
time.sleep(2)
tb.set_trigger(-1)
print("stopping...")

time.sleep(2)

print("saving...")
tb.set_trigger(1)
time.sleep(0.5)
tb.set_trigger(-1)
print("stopping...")


(3) Now, the script can be run as follows. You should see that two IQ data files get's saved into the current working directory.

python test.py

That's it!

~*************~

7 comments:

  1. Hi, it seems that there should be a pic in (3), '...runs as follow'.
    I still dont get how to use these two blocks. could you please tell more?
    thanks!!!

    ReplyDelete
  2. Hi, it seems that there should be a pic in (3), '...runs as follow'.
    I still dont get how to use these two blocks. could you please tell more?
    thanks!!!

    ReplyDelete
    Replies
    1. "Run as follows" means just running that Python script.

      Delete
  3. Hello Asanka. Nice job. However, the flowgraph & the python code doesn't work for me. When I run the python script "test.py" the program freezes and nothing is saved in the folder.
    I also want to average many captured signals (the same thing an oscilloscope does). Do you know how can I achieve that?
    Thanks,
    Lucas.

    ReplyDelete
  4. For me the following modifications worked
    Default trigger setting
    Variable ID: trigger Value: 0

    To place a burst into the data stream I have modified the python script as follows:
    Start recording:
    tb.set_trigger(1)
    time.sleep(0.01)
    tb.set_trigger(0)
    Stop recording:
    tb.set_trigger(-1)
    time.sleep(0.01)
    tb.set_trigger(0)

    ReplyDelete
  5. For all the ones that can't find the saved files, like me. They are saved in the home folder of your user.

    ReplyDelete