[轉]GNU Radio Companion - GRC
GNU Radio Companion - GRC
注:該文檔適應於捆綁 GNU Radio 的 GRC,它不適應任何獨立發行本的 GRC。如果想使用 GRC 0.70 請參閱 GNU Radio Companion (Old) 。
GNU Radio Companion (GRC) 是一個用來產生信號流程圖及流程圖源代碼的圖形化工具。它目前是由 Josh Blum 構建。
新特性
同穩定版 GRC 0.70 不同的,GRC 目前有哪些新特性?
- 捆綁式發行(Bundled)- GRC 目前是同 GNU Radio 源代碼捆綁在一起。如若所有的依賴關係得以滿足,GRC 便會在 GNU Radio 安裝的同時被安裝。相關內容請參閱安裝及運行部分。
- 桌麵的集成(Desktop Integration)- GRC 可以被完全的集成到支持自由桌麵風格標準元素的桌麵環境(xdg-utils 兼容)中:這些元素包括圖標(icons)、文件擴展(mime type)、及菜單(menu items)等。
- 代碼的生成(Code Generation)- GRC 不再僅僅孤立的通過可執行文件載入 *.grc.xml 並動態地構建流程圖。相應地,GRC 它使用模板(Cheetah templates) 來生成用於構建流程圖的 python 源代碼。GRC 可生成用戶圖形接口(WX GUI) 及非圖形接口(non-GUI)的流程圖的代碼,也可生成階梯形功能模塊(hierarchical blocks)。
- 文檔的生成(Documentation)- GRC 能夠通過對基於 doxygen 生成的 xml 文檔的提取來為 GNU Radio 的功能模塊構建文檔。該功能能夠被使用的前提是在 GNU Radio 的安裝配置過程中需激活 doxygen 功能。
- 變量的處理(Variables)- 早期版本的變量編輯窗口(variable editor window)現在被變量功能模塊(variable blocks)所取代。該變量功能模塊在流程圖的構建時被用到,它同其它功能模塊的行為表現無異,所不同的是它沒有 IO 端口。變量功能模塊將其唯一性的標識(也就是變量的名稱)映射成一個特定的數值。GRC 還有幾個圖形化變量功能模塊用來構建 WX GUI 類型的用戶圖形接口的流程圖,它使得該流程圖具有可視化控製表現形式,滑動條控件(sliders)、文本框(text boxes)、按鈕(buttons)、下拉框(drop downs)、及單選按鈕(radio buttons)。
- 功能的定義(Block Definitions)- GRC 中的每一個模塊都有一個對應的 xml 文件,它包含參數、IO 端口、及用於代碼生成用的模板。每個 xml 文件的關鍵標識(id)及文件名稱同 GNU Radio 的功能塊嚴格對應,其目的是確保其日後的可移植性(portability)。在模塊被執行時 GRC 確認模塊的所有的定義,隻要有定義不能被確認時,該模塊便會報錯並退出其運行過程。
- 文件的格式(File Format) - 假定變量及功能塊的定義有所改動的話,其對應的內部被存儲的流程圖的構架也會相應更改。幸運的是,GRC 會自動將舊有的流程圖轉化為新的內容;不幸的是,轉換並非 100% 的發生。
- 功能的操作(Block Manipulation) - 沒奢望能夠構造發射 / 接收更改的功能模塊,也無需刪除它?(Ever wish that you could take a block out of your transmit/receive change, but not delete it? )。功能模塊具有使能 / 使無效(enabled / disabled)狀態。缺省的,一個功能塊是處於使能狀態(enabled)。如若一個功能塊處於“使無效”(disabled)狀態的話,在流程圖中它便處於無效灰白色(grayed out),它也會因此招致流程圖確認器及代碼生成器的冷遇。功能模塊也能夠被從一個流程圖剪切(cut)、複製(copied)及粘貼(pasted)到另外一個流程圖中。
- 階梯形功能(Hierarchical Blocks) - GRC 能夠基於內置功能模塊來構建階梯形功能模塊。詳情參閱 階梯形功能模塊 部分。
要求
GRC 的要求
在對應的 Linux 的版本管理器中便會發現下麵這些大多數(非全部)的要求。
- [Python 2.5 (或更高版本) https://www.python.org/download/]
- [Python-LXML 2.0 (或更高版本)https://codespeak.net/lxml/installation.html]
- [Cheetah Template Engine 2.0 (或更高版本)https://www.cheetahtemplate.org/download.html]
- [Python-GTK 2.10 (或更高版本)https://www.pygtk.org/downloads.html]
GNU Radio 的要求
在 GNU Radio 版本 3.2 及以後的版本中 GRC 是一捆綁形式發行的。在此強烈建議,使用支持 wx-python, usrp, 及 audio 選項來配置 GNU Radio 的安裝過程。盡管任何配置都是可行的,詳細安裝過程還是敬請參閱 創建指導 。
注:無論安裝與否 GRC 都將會產生構建流程圖所關聯的所有的元素。舉例來說,構建帶有 USRP 信源(usrp source)的流程圖。該流程圖生產的代碼在沒有安裝支持 USRP 的 GNU Radio 的環境中運行將會出錯,除非運行在支持 USRP 的 GNU Radio 環境下。
GRC 安裝
安裝 GRC
GRC 是同 GNU Radio 捆綁發行的。因此延循安裝指導是足以成功安裝 GNU Radio。但是如若缺失任何必須的部件,GRC 的安裝會導致失敗。使用下麵命令來安裝任何缺失的部件及重啟 configure/install:
make
sudo make install
這時 GRC 理應處於被配置部件的清單上;如若意外,請仔細閱讀配置來辨析錯誤所在。
安裝文檔
若想能夠查看 GRC 內部的功能模塊的文檔,安裝 doxygen 以及將 GNU Radio 配置成支持 doxygen 便是必須的:
make
sudo make install
Icons、Mime 類型、及 Menu 的安裝
如果所運行的操作係統支持自由桌麵標準(freedesktop.org standards)元素 (Gnome, KDE, XFCE),或許需要安裝同 GRC 綁定的 icons, mime type, 及 menu items,如若如此請運行如下命令:
sudo grc_setup_freedesktop install
運行 Execution
GRC 通過將數個可被執行的 Python 文件安裝到係統並將其配置到係統路徑上來運行。
運行流程圖編輯器(Flow Graph Editor)
打開終端窗口並鍵入:
gnuradio-companion
使用指南 - Usage Tips
- 添加功能模塊:在功能模塊選擇窗口中雙擊某個功能塊。
- 連接功能模塊:點擊一個功能模塊的端口部分,便可將其同另外一個功能模塊的端口相連。
- 去除連接部分:點擊連接部分,然後選擇刪除,或者將連接部分拖之去除。- Remove a connection: click on the connection, press delete, or drag the connection to remove.
- 編輯模塊參數:雙擊流程圖中功能模塊。
- 選擇一個功能模塊,然後通過向上或向下遍曆便可進行類型變化。
- 更多快捷鍵的了解,請參閱菜單的熱鍵。
- 運行處於完全仿真狀態(也就是沒有 audio 或者 usrp 模塊)流程圖將會占據 100% 的 CPU 資源,這是的 GUI 元素將會因為係統資源的枯竭而無法響應。預防這種情況,模塊 Misc->Throttle 是流程圖的必要模塊,它的功用遏製數據流的流量作用的。
變量 - Variables
變量將符號名(symbolic name)映射為數值。在 GRC 中,一個變量可以用來定義全局常量,也可被用來協調 GUI 對例程圖的控製及運行。
變量塊 - Variable Block
使用變量形式的功能模塊(變量塊)是 GRC 使用變量的最基本表現形式。變量塊的參數 ID 對應“符號名 - symbolic name”。 符號名必須是由字母和數字(也允許下劃線)組成並以字母開頭。使用變量(準確的說,變量功能塊)的簡單的方法是在其它功能塊的參數中使用該變量的符號名便可。
變量控製 - Variable Controls
某些功能模塊具有在運行流程圖的同時更改自身參數功能的回唿方法(callback methods)。 GRC 的變量控製是指使用變量通過回唿方法修改自身參數。如果某個參數具有回唿方法,該參數便會在功能塊屬性的對話窗口(block-properties dialog)便強調。變量滑動條控件(variable slider)、變量文本框(variable text box)、及變量選擇器功能模塊(variable chooser block)便會提供一些圖形小插件(graphical widgets)諸如滑動條控件(sliders)、文本框(text boxes)、按鈕(buttons)、下拉框(drop downs)、及單選按鈕(radio buttons)實施變量控製。除此之外,變量信宿功能模塊采自源於 gnuradio 信號流並將該采樣數據寫入變量之中。
字符串賦值 -String Evaluation
字符串參數是通過二階段賦值。首先,GRC 對其參數自然賦值。如果參數無法使用字符串數據類型被賦值或者賦值故障,便可以理解該參數內含引用(it is understood that the parameter had implied quotation)。在此 GRC 需要使用引號對該參數重新賦值;這樣一來該參數所返回的字符串將嚴格依據參數窗口所規約的類型。String parameters have a two-phase evaluation. First, GRC evaluates the parameter as-is. If the parameter does not evaluate to a string data type or the evaluation fails, then it is understood that the parameter had implied quotation. In this case, GRC will evaluate the parameter again with quotation marks; which will return a string with the exact code that was typed into the parameter window.
使用字符串類型的變量,簡單在參數區域鍵入變量的名稱 parameter: my_var 。 如果該變量數據類型不是字符串,使用 python 的函數 str 對其進行類型轉換:str(my_var)。標準 python 的字符串函數也適用於此:"My Var = " + str(my_var). To use a variable inside a string simply type the name of the variable into the parameter: my_var. If the variable is not a string, cast the variable with python's str function: str(my_var). Standard python string functionality applies: "My Var = " + str(my_var).
注: 字符串參數類型也包括文件打開(file open)及文件存儲(file save)。
濾波器設計 - Filter Design
GNU Radio 的許多功能模塊都接納複數及實數閾值構成的數組作為其參數。它也提供了一個用於產生各種濾波器及窗口閾值的軟件包。有關詳細谘詢請參閱: firdes package 。
FIR 濾波器設計中的閾值生成器 - Firdes Taps Generators
- low_pass(gain, samp_rate, cutoff_freq, width, [window], [beta])
- high_pass(gain, samp_rate, cutoff_freq, width, [window], [beta])
- band_pass(gain, samp_rate, low_cutoff_freq, high_cutoff_freq, width, [window], [beta])
- complex_band_pass(gain, samp_rate, low_cutoff_freq, high_cutoff_freq, width, [window], [beta])
- band_reject(gain, samp_rate, low_cutoff_freq, high_cutoff_freq, width, [window], [beta])
- gaussian(gain, spb, bt, int ntaps)
- hilbert(int ntaps, window, beta)
- root_raised_cosine(gain, samp_rate, symbol_rate, alpha, int ntaps)
- window(window, int ntaps, beta)
FIR 濾波器設計中所用到的窗口類型 - Firdes Window Types
- WIN_HAMMING
- WIN_HANN
- WIN_BLACKMAN
- WIN_RECTANGULAR
- WIN_KAISER
FIR 濾波器設計的一些注解 - Firdes Notes
對於帶通功用而言,其缺省的窗口類型設定為 Hamming window。beta parameter 缺省為 6.76,它僅適用於 Kaiser window 。
FIR 濾波器設計的用法例解 - Firdes Usage Example
構建一個全新的可被導入("import")功能模塊如下:
from gnuradio.gr import firdes
注:帶有閾值參數(taps parameter)的大多數功能模塊將被自動地從 firdes 模塊中被導入(import)。僅在 firdes 不能被賦值時,才需使用該模塊(You only need to use the import block when firdes will not evaluate)。
在濾波器的閾值參數中添加下麵內容便可:
firdes.low_pass(1.0, samp_rate, 1000, 100, firdes.WIN_HAMMING)
使用基於文件格式存儲的濾波器閾值 - Use Taps from a File
如果濾波器的閾值數組以文檔格式被存儲,並欲意於 GRC 中使用此文件:
首先,構建一個全新的可被導入(“import”)功能模塊如下:
import numpy
其次,將下麵參數添加到該濾波器功能模塊的閾值參數中:
numpy.fromfile('taps file path', numpy.complex64)
這意味著將會將整個二進製格式的文件被全部讀入,然後對其依據編譯語法規則以 64 bytes 為基本單元將其描述或解析成為一個複數格式的閾值表現形式。想要將其解析成實數格式的閾值時,需使用 numpy.float32 。更多高級用法請參閱 numpy.fromfile 的幫助文檔:
Help on built-in function fromfile in numpy(用於 numpy 的 fromfile 的內含函數的幫助文檔):numpy.fromfile = fromfile(...)fromfile(file=, dtype=float, count=-1, sep=_) -> array.Required arguments(所需的參數):file -- open file object or string containing file name(所要打開的文檔對象的名稱或包含該文件名稱的字符串).Keyword arguments(主要參數):dtype -- type and order of the returned array (default float) -- 所返回數組(缺省 浮點型)的類型及順序count -- number of items to input (default all) -- 所需輸入(缺省 全部)的內容的大小sep -- separater between items if file is a text file (default "") -- 如果該文件的格式的文本格式(缺省 "")的情況時,其用於隔離其基本單元的格式
該函數返回一組數組,它源自於文本或者二進製碼的文檔,並以給定的數據類型為格式。上麵的參數 'file' 可以是一個打開的文件或包含需要讀取的文件的名稱的字符串。參數 'count' == -1 意味著整個文件將被讀入,否則的話它將會定義以給定格式被讀入的內容(數目)的大小。如果參數 'sep' 表現形式是 "" 時,它便使用 dtype 所規約的格式從二進製文檔中讀取數據,否則它便會對文本文檔的元素之間給出分隔符。參數 'dtype' 用於確定所返回數組(二進製文檔)元素的大小及順序。Return an array of the given data type from a text or binary file. The 'file' argument can be an open file or a string with the name of a file to read from. If 'count' == -1 the entire file is read, otherwise count is the number of items of the given type to read in. If 'sep' is "" it means to read binary data from the file using the specified dtype, otherwise it gives the separator between elements in a text file. The 'dtype' value is also used to determine the size and order of the items in binary files.
網柵格的定位 - Grid Positioning
GRC 提供若幹個圖形信宿及構建 wx-gui 風格的流程圖用的圖形化控製構件(範圍 - scope sink、FFT - fft sink、數目 - number sink、 瀑布圖 - waterfall sink、 星雲圖 - constellation sink、 滑動窗口控件 - slider control, 及選擇器控件 - chooser control)。這些圖形元素都有柵格定位參數用於其精確定位。
柵格定位參數是以 4 個整數數組(行坐標, 列坐標, 行大小, 列大小)為單位的。行坐標和列坐標定義了該圖形元素的左上角的定位坐標。最小數值的位置,(0,0),便是柵格的右上角。
If left blank, the grid parameter specifies that the graphical element will be automatically stacked into a vertical sizer. The vertical sizer is positioned directly above the grid sizer. If you do not want any elements to be added to the vertical sizer, leave no grid parameters blank.
行及列的大小描述網柵的延伸程度,或者圖形元素可以延伸的行數及列數。行大小描述自網柵原點向下延伸的行數,列大小描述網柵向右延伸的列數。這種延伸必須以(1, 1)為單元占據最少一個柵格。
例子 - Example
用戶
The user wishes to place a slider, centered directly above a graphical sink. The slider will be positioned at the 2nd column of the top row and with a column span of 2. The sink will be positioned on the 2nd row, and with a row span of 2 and a column span of 4. Notice the grid parameters below, and the resulting gui layout:
元素 - The Elements:
- Slider Control: (0, 1, 1, 2)
- Graphical Sink: (1, 0, 2, 4)
The Resulting GUI:
0,0 | 0,1 | 0,2 | 0,3 | |||||
1,0 |
1,1 |
1,2 |
1,3 | |||||
2,0 | 2,1 | 2,2 | 2,3 |
階梯形功能塊 - Hierarchical Blocks
GRC can create hierarchical blocks out of the built-in blocks. Hierarchical blocks can be instantiated inside of other grc flow graphs. The python code generated from a hierarchical block can itself be used in non-GRC flow graphs. Four important blocks are used in the creation of a hierarchical block: The options block, parameter blocks, and the pad source and pad sink.
選項功能塊 - The Options Block
In order to make a hierarchical block, the parameters in the options block must be set properly. The id of the options block sets the module name, and must be unique among the entire library of blocks (built-in and custom). The title parameter sets the display name for the block. The generate options must be set to "Hier Block". The category parameter sets the category for the new block. This category can be an existing category in the block selection window or a new category. Categories may be nested by specifying a name with slashes, ex: Custom/Filters. To put blocks into the root category, specify a single slash "/" (a blank category will hide your block).
參數功能塊 - Parameter Blocks
Parameter blocks specify variables in your hierarchical block that should be configurable in the top level block. Parameter blocks work much like variable blocks with a few exceptions: Parameters blocks cannot depend on variable blocks or other parameter blocks. Parameter blocks have a label parameter for display purposes. Parameter blocks take the place of a variable block, do not try to create a variable block with the same id as your parameter block.
Pad Source and Sink Blocks
The pad source and sink blocks create inputs and outputs for the hierarchical block. The pad blocks have configurable data types, vector lengths, and number of ports. A flow graph can have at most, one pad source, and one pad sink. A hierarchical block may have one pad sink and no pad source or no pad sink and one pad source, but it must have at least one pad block.
生成及實例化 - Creating and Instantiating
- Start with a blank slate and create a new (empty) flow graph.
- Setup the options block as described above, with the id, title, generate options, and category.
- Add parameter blocks for all variables you wish to configure/control outside of the block.
- Create at most one pad source and one pad sink to match the IO type and connect them.
- When finished, click the generate button, and close/reopen GRC.
- The hierarchical block will appear in the block selection window.
- Add the hierarchical block to a flow graph as your would any other block.
注解 - Notes
- After making changes to your hierarchical block, make sure to regenerate, and reopen GRC before usage.
- The ID parameter of the block must be unique. If two blocks share the same ID, the last one to be generated will overwrite the other.
- Custom hierarchical blocks may instantiate other custom hierarchical blocks. Just don't have a block instantiate itself!
添加定製功能塊 - Adding Custom Blocks
Every block in GRC corresponds to an XML file that describes the block's parameters, inputs, outputs, and other attributes. Adding a custom block into GRC is simply a matter of creating one of these XML block definition files. A few caveats:
The block should be accessible from the python path. Meaning that the block can be accessed via an import statement.
The block follows the block diagram model: it has parameters, inputs, and outputs. If the block requires some kind of listening thread, or special callback methods to move the data (as in the blks2 packet stuff), it cannot be used in GRC (unless this "special" functionality can be encapsulated into a block that is block-diagram-safe).
If GRC is missing a block definition for a block that is currently in the trunk, or one of the block definitions is missing functionality, please mail the list. The block definitions in the GRC trunk must stay in sync with the actual GNU Radio blocks.
XML 功能塊定義文件的生成 - Creating the XML Block Definition
The best way to learn how to create the xml file is to learn by example. See the block definitions (source:grc/blocks) packaged with GRC, and read through a few files. Essentially, all block definitions are structured as follows:
<?xml version="1.0"?> <block> <name>My Block Name</name> <key>my_package_my_block_ff</key> <category>Filters</category> <import>from gnuradio import my_package</import> <make>my_package.my_block_ff($param1, $param2)</make> <callback>set_param1($param1)</callback> <param> <name>Parameter 1</name> <key>param1</key> <type>real</type> </param> <param> <name>Parameter 2</name> <key>param2</key> <value>1</value> <type>int</type> </param> <sink> <name>in</name> <type>float</type> </sink> <source> <name>out</name> <type>float</type> </source> <source> <name>out</name> <type>float</type> </source> </block>
- The example above will make a block with 2 parameters, 1 input, and 2 outputs.
- The ordering of the tags is important, if tags are not ordered properly, the block will fail validation. See [/trac/browser/gnuradio/trunk/grc/data/platforms/python/block.dtd block.dtd] for specifics.
- The name tags dictate the label text for the block, parameters, and ports.
- The key tags are unique identifiers, they may not contain spaces. The block key must be globally unique among all blocks in GRC. The parameter keys must be unique only within the block.
- The category tag is a unix-style path that represents the location of the block inside the block selection window. The path can be a new category (Custom), or represent a sub-category (Filters/Custom). To put a block into the root category, just use a single slash (/) for the root path.
- The import tag (there can be multiple) must be a valid python import statement to the module containing your block.
- The make tag contains the code necessary to construct your block. This code is essentially a cheetah template nested inside an xml tag. Upon code generation, the template performs a text
substitution on the "
quot; parameters. For more advanced capabilities, see thecheetah template documentation.
- The callback tag registers a set-method from your custom block. Once the set-method is registered, the set-method can be called at runtime when a variable is changed. There can be any number of callback tags, one for each set-method of your block. Or no callback tags if this is not applicable.
- For the param tags, the commonly used values for the type tags are: complex, real, int, complex_vector, real_vector, int_vector, string, and raw. The raw type allows any value to be used without performing type checking. The real type should be used for single and double precision floating point numbers. The int type should be used for longs, ints, shorts, and chars.
- The sink tag represents an input port, and the source tag represents an output port. The allowed values for the type tags are: complex, float, int, short, and byte. For ports with a vector length, specify a vlen tag after the type tag.
一些定義文件的例子 - Some Example Definitions
- Simple Example: "Complex to Real"source:grc/blocks/gr_complex_to_real.xml
- Multiple Callbacks: "Costas Loop"source:grc/blocks/gr_costas_loop_cc.xml
- Vlen Example: "Throttle"source:grc/blocks/gr_throttle.xml
- Advanced Make: "FFT"source:grc/blocks/gr_fft_vxx.xml
安裝 XML 功能定義文件 - Installing the XML Block Definition
有諸多方式可以讓 GRC 知悉最新生成的 xml 文件。下麵是一些可選的途徑之一 ...
方法1: Default Hier Block Location
Create the .xml file inside *~/.grc_gnuradio/ where ~ is your home directory. If the directory does not exist, create it: mkdir ~/.grc_gnuradio/
方法2: 配置文件
構建或者編輯 ~/.gnuradio/config.conf 然後添加下麵內容:
[grc] local_blocks_path=/path/to/my/blocks
The local_blocks_path can contain multiple paths separated by colons: local_blocks_path=/path/to/blocks1:/path/to/blocks2
方法 3: 環境變量
配置
Set the GRC_BLOCKS_PATH environment variable to a path that contains your custom block wrapper. The GRC_BLOCKS_PATH can contain multiple paths separated by colons: GRC_BLOCKS_PATH=/path/to/blocks1:/path/to/blocks2
特別感謝 - Special Thanks
- CER Technology Fellowship: initial funding
- A. Brinton Cooper: starting the project
- Patrick Mulligan: starting the project
- William R. Kenan Jr. Fund: usrp & computers
- Patrick Strasser: the GRC icon
截圖 - Screen Shots
Feel free to submit your own screen shots or flow graphs.
注:GNU Radio Companion(原文出處,翻譯整理僅供參考!)
最後更新:2017-04-03 14:53:50