Search code examples
pythongnuradio

Export signal generated from GNU Radio Companion to a .csv file


I am trying to generate a random signal using GNU Radio Companion and then export it to a .csv file. I have used a signal source to generate a signal. Then I have connected it to the throttle box. The output has then been connected to the "file sink" block to save the signal into a .csv file. From my understanding the sink block is used to export the data to a file, which in this case is an excel file. However, when I open the .csv file the output is shown in weird characters rather than a number. What is the reason behind this?


Solution

  • From my understanding the sink block is used to export the data to a file, which in this case is an excel file.

    Your understanding is wrong, sadly! The file extension says literally nothing about the contents and format of a file.

    What the file sink writes is simply the values you put in, byte for byte, as they would appear in RAM. That's rather well-documented on the official wiki page of the File Sink block.

    What is the reason behind this?

    Simple: it's not a text file as you expect it to be (CSV is not an Excel file, but "comma separated values in a text file").

    Export signal generated from GNU Radio Companion to a .csv file

    GNU Radio does not come with a block that writes values comma-separatedly. And that's not really by accident: CSV is among the least suitable formats for numerical data, especially complex data. Beyond a couple thousand values, it gets hard to manage data in a text editor or with tools like excel, and typical GNU Radio applications produce millions of values per second! Not to mention that writing a CSV is highly space-inefficient and takes a lot of CPU power, since the nice numerical values that are in a precision-preserving format (for example, 16bit integers, or 32 bit floats interleaving I and Q) have to be "rendered" to a textual representation – which can be both a lossy process and takes litte data and makes much data out of it.

    So, in short, maybe don't do CSV.

    • If you have "a few values" (say, < 5000), OK, Excel might be able to deal with it. In that case, write to a binary file using your File Sink as present, and use the python code I supplied on that wiki page to load the file, and generate an actual Excel file from that (you could go through CSV, but again, that is a lossy process!), using for example using xlwt.
    • But honestly, if you have but a few values, use Pandas to work with that, after loading it in Python (see wiki page). Use its & matplotlib's visualization tools.
    • If you have "many values", Excel will not be the right tool, anyways, and you'll have to do your analysis in a programming language (like Python, Julia, Rust, C++…) or statistical / mass data visualization environment anyways, which nearly without exception can load binary data directly, so you can directly work with the files saved by File Sink.