Float To Char: Difference between revisions

From GNU Radio
Jump to navigation Jump to search
No edit summary
m (Clarified the role of the 'Scale' value in the block.)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:Block Docs]]
[[Category:Block Docs]]
Convert stream of floats to a stream of char, with an optional scaling factor.
This block will convert a stream of signed 32-bit floating point values (floats) to a stream of signed 8-bit integers (char), with an optional scaling factor. The ''Float to Char'' block will quantize the input values, after being multiplied by the ''Scale'' factor, to integer values between -128 to +127. Values are rounded to the nearest integer. Any values less than -128 or greater than +127 will be clipped to these limits.


== Parameters ==
== Parameters ==
Line 10: Line 10:
== Example Flowgraph ==
== Example Flowgraph ==


Insert description of flowgraph here, then show a screenshot of the flowgraph and the output if there is an interesting GUI. Currently we have no standard method of uploading the actual flowgraph to the wiki or git repo, unfortunately. The plan is to have an example flowgraph showing how the block might be used, for every block, and the flowgraphs will live in the git repo.
An example flowgraph showing the affect that the ''Float to Char'' block has on an input signal.
 
[[File:Float-to-Char-flowgraph.png|800px]]
 
The [[Signal Source]] block provides a sinusoid with an amplitude of 1. Using a ''Scale'' value of the ''Float to Char'' block of 1, the input signal will be quantized to values of -1, 0, and 1. Note that the input is multiplied by the ''Scale'' value before being quantized into the ''char'' data type.
 
[[File:Float-to-Char-scale-1.png|800px]]
 
Using a ''Scale'' value of 127 for the ''Float to Char'', and 1 for the ''Char to Float'', the sinusoid looks more natural.
 
[[File:Float-to-Char-scale-127.png|800px]]
 
However, zooming in on the sinusoid that has passed through the ''Float to Char'' shows that the amplitudes have been quantized to integer values.
 
[[File:Float-to-Char-scale-127-1-zoomed.png|800px]]
 
If the amplitude of the input signal is higher than 127 or less than -128, the signal amplitudes will be clipped at these limits. For example, here's an example using a ''Scale'' value of 150 for the ''Float to Char'' block.
 
[[File:Float-to-Char-scale-150-1.png|800px]]


== Source Files ==
== Source Files ==


; C++ fileshttps://wiki.gnuradio.org/index.php/SISO
; C++ files
 
: [https://github.com/gnuradio/gnuradio/blob/main/gr-blocks/lib/float_to_char_impl.cc float_to_char_impl.cc]
: [https://github.com/gnuradio/gnuradio TODO]


; Header files
; Header files
: [https://github.com/gnuradio/gnuradio TODO]
: [https://github.com/gnuradio/gnuradio/blob/main/gr-blocks/lib/float_to_char_impl.h float_to_char_impl.h]
 
; Public header files
: [https://github.com/gnuradio/gnuradio TODO]


; Block definition
; Block Definition
: [https://github.com/gnuradio/gnuradio TODO]
: [https://github.com/gnuradio/gnuradio/blob/main/gr-blocks/grc/blocks_float_to_char.block.yml blocks_float_to_char.block.yml]

Latest revision as of 02:12, 23 March 2025

This block will convert a stream of signed 32-bit floating point values (floats) to a stream of signed 8-bit integers (char), with an optional scaling factor. The Float to Char block will quantize the input values, after being multiplied by the Scale factor, to integer values between -128 to +127. Values are rounded to the nearest integer. Any values less than -128 or greater than +127 will be clipped to these limits.

Parameters

(R): Run-time adjustable

Scale (R)
Scaling factor applied to input stream.

Example Flowgraph

An example flowgraph showing the affect that the Float to Char block has on an input signal.

Float-to-Char-flowgraph.png

The Signal Source block provides a sinusoid with an amplitude of 1. Using a Scale value of the Float to Char block of 1, the input signal will be quantized to values of -1, 0, and 1. Note that the input is multiplied by the Scale value before being quantized into the char data type.

Float-to-Char-scale-1.png

Using a Scale value of 127 for the Float to Char, and 1 for the Char to Float, the sinusoid looks more natural.

Float-to-Char-scale-127.png

However, zooming in on the sinusoid that has passed through the Float to Char shows that the amplitudes have been quantized to integer values.

Float-to-Char-scale-127-1-zoomed.png

If the amplitude of the input signal is higher than 127 or less than -128, the signal amplitudes will be clipped at these limits. For example, here's an example using a Scale value of 150 for the Float to Char block.

Float-to-Char-scale-150-1.png

Source Files

C++ files
float_to_char_impl.cc
Header files
float_to_char_impl.h
Block Definition
blocks_float_to_char.block.yml