Converting Data Types: Difference between revisions

From GNU Radio
Jump to navigation Jump to search
No edit summary
(tech writer editing & making easier for ESL)
(40 intermediate revisions by 3 users not shown)
Line 1: Line 1:
<div style="float:right">
{{Template:BeginnerTutorials}}
</div>
This tutorial demonstrates how to convert between data types.


This tutorial will describe how to convert between data types.
The previous tutorial, [[Signal_Data_Types|Signal Data Types]], presents an introduction to the ''Complex Float 32'' and ''Float 32'' data types for representing digitized signals. The next tutorial, [[Packing_Bits|Packing Bits]], describes how to pack and unpack bits 8 bits into the ''char'' or ''byte'' data type.


==Char/Byte Data Type ==
==Char/Byte Data Type ==
The ''Char'' or ''Byte'' data type is another useful data type for representing binary data. The ''Byte'' data type is represented by the magenta color in GRC:
The ''Char'' or ''Byte'' data type is another useful data type for representing binary data. The ''Byte'' data type is represented by the '''<span style="color:purple">purple</span>''' color in GRC, labeled ''Integer 8'':


[[File:Types.png|500px]]
[[File:Types.png|500px]]


Search for the ''Random Source'' block and drag it into the workspace:


* random source
[[File:SearchRandomSourceBlock.png|800px]]
* use in modulator


char/byte data type
 
convert to float
The block defaults to the '''<span style="color:green">green</span>''' ''Integer 32'' data type. Double-click the block to open the properties and modify the data type to ''byte'':
display
 
[[File:SelectByteDataType.png|500px]]
 
 
The ''Random Source'' is now converted to the '''<span style="color:purple">purple</span>''' ''Char'' or ''Byte'' data type.
 
[[File:RandomSourceByteOutput.png|500px]]
 
== Converting ''Byte'' to ''Float 32''==
 
The default parameters of the ''Random Source'' will randomly generate values of 0 and 1. Add the ''QT GUI Time Sink'' and the ''Throttle'' block into the workspace and connect the blocks:
 
[[File:ConnectionErrorCharToComplex.png|700px]]
 
 
The red arrow between the ''Random Source'' and ''Throttle'' blocks shows a data type error that needs to be fixed. Double-click the ''Throttle'' block and change the data type to ''byte'':
 
[[File:ChangeThrottleDataType.png|500px]]
 
 
A new red arrow now shows there is a data type connection between the ''Throttle'' and the ''QT GUI Time Sink'':
 
[[File:ConnectionErrorThrottleTimeSink.png|700px]]
 
 
The ''QT GUI Time Sink'' does not have a char data type. Select ''Float'':
 
[[File:TimeSinkDataTypeOptions.png|500px]]
 
 
The GNU Radio block library comes with a variety of data type converters listed under ''Type Converters''. Search for the ''Char to Float'' block, drag it into the workspace, and connect it into the flowgraph:
 
[[File:SearchCharToFloatBlock.png|700px]]
 
 
All of the red errors have disappeared. Press the ''Play'' button to start the flowgraph:
 
[[File:RunFlowgraphButton.png|250px]]
 
 
The ''QT GUI Time Sink'' will now display the data from the ''Random Source'' block which is randomized 0's and 1's:
 
[[File:RandomSourceTimeSinkOutput.png|700px]]
 
The next tutorial, [[Packing_Bits|Packing Bits]], describes how to pack and unpack bits 8 bits into the ''char'' or ''byte'' data type.

Revision as of 13:48, 15 March 2022

Beginner Tutorials

Introducing GNU Radio

  1. What is GNU Radio?
  2. Installing GNU Radio
  3. Your First Flowgraph

Flowgraph Fundamentals

  1. Python Variables in GRC
  2. Variables in Flowgraphs
  3. Runtime Updating Variables
  4. Signal Data Types
  5. Converting Data Types
  6. Packing Bits
  7. Streams and Vectors
  8. Hier Blocks and Parameters

Creating and Modifying Python Blocks

  1. Creating Your First Block
  2. Python Block With Vectors
  3. Python Block Message Passing
  4. Python Block Tags

DSP Blocks

  1. Low Pass Filter Example
  2. Designing Filter Taps
  3. Sample Rate Change

This tutorial demonstrates how to convert between data types.

The previous tutorial, Signal Data Types, presents an introduction to the Complex Float 32 and Float 32 data types for representing digitized signals. The next tutorial, Packing Bits, describes how to pack and unpack bits 8 bits into the char or byte data type.

Char/Byte Data Type

The Char or Byte data type is another useful data type for representing binary data. The Byte data type is represented by the purple color in GRC, labeled Integer 8:

Types.png

Search for the Random Source block and drag it into the workspace:

SearchRandomSourceBlock.png


The block defaults to the green Integer 32 data type. Double-click the block to open the properties and modify the data type to byte:

SelectByteDataType.png


The Random Source is now converted to the purple Char or Byte data type.

RandomSourceByteOutput.png

Converting Byte to Float 32

The default parameters of the Random Source will randomly generate values of 0 and 1. Add the QT GUI Time Sink and the Throttle block into the workspace and connect the blocks:

ConnectionErrorCharToComplex.png


The red arrow between the Random Source and Throttle blocks shows a data type error that needs to be fixed. Double-click the Throttle block and change the data type to byte:

ChangeThrottleDataType.png


A new red arrow now shows there is a data type connection between the Throttle and the QT GUI Time Sink:

ConnectionErrorThrottleTimeSink.png


The QT GUI Time Sink does not have a char data type. Select Float:

TimeSinkDataTypeOptions.png


The GNU Radio block library comes with a variety of data type converters listed under Type Converters. Search for the Char to Float block, drag it into the workspace, and connect it into the flowgraph:

SearchCharToFloatBlock.png


All of the red errors have disappeared. Press the Play button to start the flowgraph:

RunFlowgraphButton.png


The QT GUI Time Sink will now display the data from the Random Source block which is randomized 0's and 1's:

RandomSourceTimeSinkOutput.png

The next tutorial, Packing Bits, describes how to pack and unpack bits 8 bits into the char or byte data type.