13 331 understanding the example code the following

Info icon This preview shows pages 13–16. Sign up to view the full content.

View Full Document Right Arrow Icon
13
Image of page 13

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
3.3.1 Understanding the Example Code The following steps teach you how the sample code works and how to build your own custom blocks. 1. Download the zip file of example code from the cource website. It contains three files: maxdetect.py - Python code for the custom block. maxdetect.xml - XML file required for instantiating the custom block in GRC designs. top.grc - An example top-level GRC file that demonstrates using the custom block. 2. Save “maxdetect.py” and “top.grc” into your working directory. 3. Save “maxdetect.xml” into the directory ~ /.grc_gnuradio so that GRC can find it. 4. Quickly read through “maxdetect.py” and “maxdetect.xml”. You will notice the following things: The “maxdetect” block takes a vector of floats as input (the parameter “vlen” specifies the length of this vector). The “maxdetect” block produces two floats as outputs: the frequency of the peak and the magnitude of the peak. The “maxdetect” block also takes two other paramters: “samp rate” and “alpha.” More about these later. The file “maxdetect.py” defines a class called “maxdetect”. The constructor init () is where the input/output port signatures ( gr.iosignature ) are defined. The constructor also creates some message sources, sinks, and queues, and connects them together. More about this later. The “maxdetect” class also contains some callback functions ( set samp rate , set alpha ), which are executed when the top-level code changes the values of the “maxdetect” block’s parameters. The “maxdetect” class also contains a method called main loop . This is where the pro- cessing is actually performed. This loop repeats indefinitely in its own thread, processing samples as they arrive. 5. The trickiest part of the “maxdetect” block is getting the data samples in and out of the flow graph so that you can manipulate them. Although it is more straightforward to do this in C++ code, it is still possible in Python. The trick is to use a message sink and a message queue to receive the incoming samples. Since messages are just packets of data samples, the Python code can obtain the data samples by unpacking the bytes of each packet. After processing, the Python code places the data samples into another queue, which sends them out of the “maxdetect” block via a message source object. This approach may seem a bit roundabout, but according to Josh Blum (creator of GRC), this is the best way to do it (see [1], [2], and [3]). Here is a summary of the flow of data inside our custom “maxdetect” block: < input > [message sink] (process in the thread) [message source] < output > 14
Image of page 14
6. The message queues are thread-safe. Thus, we do not have to worry about any synchronization problems between our “main loop” thread and the rest of the flow graph; the queues automat- ically handle this synchronization.
Image of page 15

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 16
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern