Pushbutton IQ Recorder with descriptive filenames: Difference between revisions

From GNU Radio
Jump to navigation Jump to search
No edit summary
No edit summary
Line 49: Line 49:
In the following example we will:
In the following example we will:
Use some Pythonic syntax to leverage the Runtime Callbacks in our flowgraph which will allow the filenames to change dynamically based on timestamp and radio parameters.
Use some Pythonic syntax to leverage the Runtime Callbacks in our flowgraph which will allow the filenames to change dynamically based on timestamp and radio parameters.
[[File:Variables_and_imports.png]]
We will need to import the appropriate modules to get the user's home directory and create a date/timestamp
We will need to import the appropriate modules to get the user's home directory and create a date/timestamp
Then we will use a few different variables to define the top-level directory (<code>/user/home</code>) and the appropriate subdirectory <code>data/iq_captures</code>
<code>filename+str(datetime.fromtimestamp(time.time()).strftime('%Y_%m_%d_%H:%M:%S'))+".cfile" if rec_button == 1 else "/dev/null"</code>


[[File:.png]]
Then we will use two different variable blocks to define first, the top-level directory (<code>/home/<username></code>) and second, the appropriate subdirectory <code>data/iq_captures</code> for the recordings. This can be changed to your preference.
 
Then we create the first part of the filename which contains the SDR Frontend parameters and a user note to describe the capture. These will be updated at runtime in the filename if they are changed.
 
The filesink contains the filename variable and appends the properly formatted date/timestamp. Since we may want to trigger multiple recordings after starting the flowgraph, the date/timestamp needs to be generated each time the user triggers a recording, so the syntax must be expressed inside block. If we didn't do this, the date/timestamp would only be set once during the initial setup of the flowgraph.
 
[[File:Variables_and_imports.png]]


This portion of the flowgraph:
This portion of the flowgraph:
Line 63: Line 68:


====Timestamps====
====Timestamps====
If a different format for timestamps is needed the syntax can be modified to suit your needs. The current format is:
<code>time.time()).strftime('%Y_%m_%d_%H_%M_%S')</code>
This can be tested in a python interactive terminal and adjusted to preference. The necessary code to do this is:
<code>
import time
from datetime import datetime
str(datetime.fromtimestamp(time.time()).strftime('%Y_%m_%d_%H_%M_%S'))
</code>


====SDR Frontend Information====
====SDR Frontend Information====
Line 69: Line 82:
[[File:.png]]
[[File:.png]]


The parameters for the File Sink are shown here:
The below python syntax includes the radio parameters with the date/timestamp. This is incomplete however, as it would record constantly from the time the flowgraph starts. When recording raw IQ we need to be mindful of disk space as raw IQ can take up space fast. In the next section we will show how to trigger the recording with a momentary pushbutton as a safety for our disk space and to keep our recordings limited to only the signals we want to record.
 
<code>filename+str(datetime.fromtimestamp(time.time()).strftime('%Y_%m_%d_%H_%M_%S'))+".cfile"</code>
in our current example, using the initial parameters, the filename would be:
"RECORDING_NOTE_915000000Hz_1000000sps_50dB_2023_01_04_01_02_39.cfile"


[[File:.png]]
[[File:.png]]
[[File:.png]]


====User Input Note to Describe the Capture====
====User Input Note to Describe the Capture====
*  
*We use the QT Entry Widget block to put an editable text field in our flowgraph's GUI
*  
*NOTE: once the text is changed, the user must hit the ENTER key to update the note
*
 
 


This part of the flowgraph is shown here:
This part of the flowgraph is shown here:
Line 95: Line 106:
==Only Trigger Recording on User Input==
==Only Trigger Recording on User Input==
Using python conditional statement, we can send the I/Q samples to <code>/dev/null</code> until the record button is pressed (and held). When the record button is pressed, the I/Q samples will begin streaming to a file with a timestamp and radio parameters of the flowgraph's state at the time the button was pressed. When released it will send the samples back to <code>/dev/null</code>
Using python conditional statement, we can send the I/Q samples to <code>/dev/null</code> until the record button is pressed (and held). When the record button is pressed, the I/Q samples will begin streaming to a file with a timestamp and radio parameters of the flowgraph's state at the time the button was pressed. When released it will send the samples back to <code>/dev/null</code>
We will also add a QT GUI LED Indicator to turn red when we are recording to disk, it will be green when sending samples to <code>/dev/null</code>.


====/dev/Null or Filename with Python Conditional Statement====
====/dev/Null or Filename with Python Conditional Statement====


*
*We will send the IQ samples to <code>/dev/null</code> until the trigger event occurs, at that time the expression will be evaluated to switch the path to the predefined path from the flowgraph.
 
Below is the full python expression to insert in the 'filename' parameter of the file sink block. Note that we simply add a conditional at the end, which is based on the momentary pushbutton's state.
<code>filename+str(datetime.fromtimestamp(time.time()).strftime('%Y_%m_%d_%H_%M_%S'))+".cfile" if rec_button == 1 else "/dev/null"</code>


Here is a brief demo of the flowgraph in action:
Here is a brief demo of the flowgraph in action showing the subdirectory adding new timestamped files as we click and hold the momentary pushbutton:


[[File:.gif]]
[[File:.gif]]

Revision as of 06:14, 4 January 2023

Application

When doing field work, capturing Raw I/Q for post processing is a helpful way to get the best signal quality of a capture while spending minimal time in a non-lab environment. Changing filenames manually for every capture can be laborious and subject to user error. Furthermore, starting and stopping a flowgraph can be tricky if we are tuning the SDR frontend parameters to find optimal level/frequency/sample rate. If a non-descriptive filename is used such as capture_file.cfile it does nothing to describe the situation to the user during the analysis stage as it doesn't include any information about the RF samples we've captured. This is critical to the post-analysis process.

  • What was the sample rate?
  • What was the Center Frequency?
  • When was it recorded?
  • What gain setting did we use?
  • What were we even trying to capture?

These are all questions we ask after the fact, especially if a lot of time has gone by since we did the recordings.

One way to address this is to include those pieces of information in the filename

Introduction

Assumptions

  • GNURadio 3.10+
  • directory /home/<username>/data/iq_captures is present on filesystem (the flowgraph will automatically find your /home/<username> path).

Goals

Create a File Sink with Dynamic Information in the Filename

  • timestamp of recording
  • radio parameters
  • User Input Note for clarity

Only Record the file on an User Input Trigger

  • Define User Input as Momentary Switch in GUI
  • Set Conditional Statement in File Sink Block

Content

The flowgraph for this tutorial is shown below along with the GRC file needed if you would like to test it out. This flowgraph is intended for use by anyone with GNURadio 3.10+ installed. It does not use an actual SDR frontend which allows users to test without hardware, but that also means that the values for center frequency and gain are merely representative so, when changed at runtime they won't be reflected in the synthetic spectrum when running the flowgraph. The aim of this tutorial is merely to demonstrate the mechanism. For an example that uses real hardware, try https://github.com/muaddib1984/wavetrap

Pushbutton iq recorder whole graph.png

Pushbutton IQ Recorder


Create Synthetic Spectrum for the Flowgraph

This screenshot shows the blocks used to generate some synthetic spectrum with intermittent narrowband carriers:

Pushbutton synth signal.png

This is what the simulated spectrum looks like when running in GNURadio:

Synth spectrum plot.gif

Create a File Sink with Dynamic Information in the Filename

In the following example we will: Use some Pythonic syntax to leverage the Runtime Callbacks in our flowgraph which will allow the filenames to change dynamically based on timestamp and radio parameters. Variables and imports.png We will need to import the appropriate modules to get the user's home directory and create a date/timestamp

Then we will use two different variable blocks to define first, the top-level directory (/home/<username>) and second, the appropriate subdirectory data/iq_captures for the recordings. This can be changed to your preference.

Then we create the first part of the filename which contains the SDR Frontend parameters and a user note to describe the capture. These will be updated at runtime in the filename if they are changed.

The filesink contains the filename variable and appends the properly formatted date/timestamp. Since we may want to trigger multiple recordings after starting the flowgraph, the date/timestamp needs to be generated each time the user triggers a recording, so the syntax must be expressed inside block. If we didn't do this, the date/timestamp would only be set once during the initial setup of the flowgraph.

Variables and imports.png

This portion of the flowgraph:

  • one
  • two
  • three


Timestamps

If a different format for timestamps is needed the syntax can be modified to suit your needs. The current format is: time.time()).strftime('%Y_%m_%d_%H_%M_%S') This can be tested in a python interactive terminal and adjusted to preference. The necessary code to do this is: import time from datetime import datetime str(datetime.fromtimestamp(time.time()).strftime('%Y_%m_%d_%H_%M_%S'))

SDR Frontend Information

This section of the flowgraph is shown here:

File:.png

The below python syntax includes the radio parameters with the date/timestamp. This is incomplete however, as it would record constantly from the time the flowgraph starts. When recording raw IQ we need to be mindful of disk space as raw IQ can take up space fast. In the next section we will show how to trigger the recording with a momentary pushbutton as a safety for our disk space and to keep our recordings limited to only the signals we want to record.

filename+str(datetime.fromtimestamp(time.time()).strftime('%Y_%m_%d_%H_%M_%S'))+".cfile" in our current example, using the initial parameters, the filename would be: "RECORDING_NOTE_915000000Hz_1000000sps_50dB_2023_01_04_01_02_39.cfile"


User Input Note to Describe the Capture

  • We use the QT Entry Widget block to put an editable text field in our flowgraph's GUI
  • NOTE: once the text is changed, the user must hit the ENTER key to update the note

This part of the flowgraph is shown here:


File:.png


[[File:]]


Example:

Only Trigger Recording on User Input

Using python conditional statement, we can send the I/Q samples to /dev/null until the record button is pressed (and held). When the record button is pressed, the I/Q samples will begin streaming to a file with a timestamp and radio parameters of the flowgraph's state at the time the button was pressed. When released it will send the samples back to /dev/null We will also add a QT GUI LED Indicator to turn red when we are recording to disk, it will be green when sending samples to /dev/null.

/dev/Null or Filename with Python Conditional Statement

  • We will send the IQ samples to /dev/null until the trigger event occurs, at that time the expression will be evaluated to switch the path to the predefined path from the flowgraph.

Below is the full python expression to insert in the 'filename' parameter of the file sink block. Note that we simply add a conditional at the end, which is based on the momentary pushbutton's state. filename+str(datetime.fromtimestamp(time.time()).strftime('%Y_%m_%d_%H_%M_%S'))+".cfile" if rec_button == 1 else "/dev/null"

Here is a brief demo of the flowgraph in action showing the subdirectory adding new timestamped files as we click and hold the momentary pushbutton:

File:.gif

Prerequisites