Introduction_to_G_Programming
Introduction_to_G_Programming
© Eduardo Perez
Preface to an "Introduction to G
Programming"
Available under Creative Commons-ShareAlike 4.0 International License (http://creativecommon
s.org/licenses/by-sa/4.0/).
The Internet, personal devices and multicore computers have greatly changed and
enhanced our lives by allowing us to access information and entertainment on-
demand anytime, anywhere. While these technologies are great on their own merit,
the reality is that in order to reap the benefits, someone has to program these devices
to develop useful applications.
The book was written to help the user learn the G programming syntax and begin
developing G programs quickly and easily. Although familiarity with programming
concepts could help learning G, the book assumes the user has had no previous
exposure to any programming languages. Therefore, to avoid confusion, no pseudo-
code or syntax comparisons are made with text-based programming languages. All
examples in this book are working graphical examples and have been tested
thoroughly. Chapter 1 is an introductory tutorial providing a reference for beginners
and seasoned programmers alike. Subsequent chapters provide more details on the G
syntax building up to the development of parallel programs that run on multicore
platforms.
Dr. Eduardo "Lalo" Perez is one of the original LabVIEW development team members
responsible for the design, deployment and optimization of the Digital Signal
Processing and Data Analysis Libraries still being used today.
Dr. Perez has nearly 30 years of engineering programming experience and nearly 20
years designing and implementing digital signal processing software architectures for
the deployment of multimedia, communications and biomedical real-time
applications. He has extensive experience in large enterprises -where he successfully
deployed global multimedia networks and services for AT&T and Ernst & Young -as
well as startups where he forged strategic alliances with Intel, Microsoft, Samsung and
Texas Instruments to accelerate adoption of audio-visual services over IP. Dr. Perez'
other accomplishments include: member of the first ever live video webcast team over
ISDN in 1994, first commercial DSL live IP broadcast in 1999, provided nearly 3 years
of webcasting services for Broadcast.com(now Yahoo! Broadcasting Services)with
100% success rate, team member at SBC Communications that launched SBC Internet
Services and DSL services and provided guidance for deployment of early Internet
multimedia communities.
Dr. Perez holds a variety of patents on high quality video encoding, compressive data
acquisition, multirate media processing, remote computing and streaming delivery
mechanisms.
A native from Mexico, he immigrated to the U.S. at the age of 18 to pursue higher
education. He received his B.S., M.S., and Ph.D. in Electrical and Computer Engineering
in the areas of Telecommunications, Image Processing, and Real-Time Digital Imaging
Systems, respectively, all from the University of Texas at Austin.
Dr. Perez was a member of the 1976 Mexican Olympic Swimming team.
4
Chapter 1 Introduction to G
Programming
Available under Creative Commons-ShareAlike 4.0 International License (http://creativecommon
s.org/licenses/by-sa/4.0/).
• Interactive
• Execute in Parallel
• Multicore
The program is a block diagram edited in the Block Diagram programming window.
The program input data and results are manipulated and displayed in the Front Panel
window.
The first program is to display the text "Hello graphical interactive parallel multicore
world" in the Front Panel window.
Right click on the Block Diagram window and select String Constant from the
Functions » Programming » String menu.
5
Drag and drop the String Constant onto the Block Diagram window as show in the
following Figure 1.3.
Type in "Hello graphical interactive parallel multicore world." in the String Constant.
Right click in the Front Panel window and select a String Indicator from the Controls
»Modern » String & Path menu.
Click the "Hello graphical interactive parallel multicore world" terminal and then
click on the String Indicator triangular terminal to wire the terminals.
Save your program as Hello, World.vi. Return to the Front Panel window. Click the run
button ([U+27AF]). You have successfully completed and executed your first G
program.
The next program converts degrees from Fahrenheit to Celsius using the formula
In the Block Diagram window, select the subtract, multiply and divide from the
Functions » Mathematics » Numeric menu
Wire the subtract, multiply and divide functions as shown in Figure 3.11.
Right click on the upper left terminal of the subtract function and select Create »
Control from the pop-up menu.
Re-labelx as Fahrenheit and wire the terminal as shown in Fahrenheit Input Control.
7
Right click on the lower left terminal of the subtract function and select Create
»Constant and type 32.0.
Repeat the process to generate numeric constants for the multiply and divide function
with 5.0 and 9.0 respectively.
To complete the program, right click on the right terminal of the divide function and
select Create »Indicator. Re-label x/y as Celsius. The fnal diagram is shown in
Fahrenheit to Celsius G Diagram
Switch to the Front Panel window to run the program. Save the program as Celsius.vi.
Try various Fahrenheit values to see the corresponding Celsius values. You have
successfully finished a Fahrenheit to Celsius calculator.
1.3 Functions
Available under Creative Commons-ShareAlike 4.0 International License (http://creativecommon
s.org/licenses/by-sa/4.0/).
From the Edit menu select Create SubVI to create a G function. The resulting diagram
is shown in Creating a Function .
From the File menu select Save All and save the Untitled function as Fahrenheit to
Celsius.vi.
Open the Fahrenheit to Celsius.vi by double clicking on the icon. Right click on the
icon editor (upper right corner) and select Edit Icon...
After editing the icon, the function's icon is shown in the upper right corner of the
Front Panel window. Save the function, plug in various input values and run the
function. Save the function.
Close theFahrenheit to Celsius function and return to the Celsius Block Diagram
windows. The Celsius diagram reflects the updated Fahrenheit to Celsius icon
This program determines if a year is a leap year or not. A leap year is divisible by 4 but
not by 100, except when it is divisible by 400. A number x is divisible by a number y if
the remainder of x/y is identical to zero, i.e. Rem(x/y}=O is true therefore
Leap Year = {Rem (Year/4) = 0 And Not (Rem (Year/100) = 0)} Or Rem (Year/400) = 0
(3.1)
For example:
Start a new G program and right click on the Block Diagram window. Go to the
Functions » Programming » Numeric menu in the Block Diagram window.
Select three copies of the Quotient & Remainder function and three numeric
constants. Type in 4, 100 and 400 for the numeric constants and wire these constants
to the lower input terminal (corresponding to the dividend) of the Quotient &
Remainder function.
From the Functions » Programming » Boolean menu select the AND and OR
operators
Place the Boolean operators as shown in Q&R, Comparison & Boolean Functions.
From the Functions » Programming » Structures menu, click on the Case Structure.
Click and drag on the Block Diagram window to create the Case Structure.
The True diagra m option is indicated at the top of the case structure.
Click on the down arrowhead next to the True label and select the False option .
Drop another string constant and type "Is not a Leap Year".
Go to the Front Panel window and place a numeric input and an output string. Re-
label the numeric input to Year and the output string to Message.
Right click on Year and select Representation » I32 from the numeric pop-up menu.
Arrange the Year and Message terminals in the Block Diagram window as shown in
the figure.
13
Wire the OR operator is to the "7" in the case structure and the string constant "Is not
a Leap Year" is wired to Message.
Select the True option and Wire the "Is a Leap Year" string constant to the output
terminal of the Case Structure.
Save the program as Leap Year.vi, enterYear values and run the program to
determine whether the value of Year is that of a leap year or not.
1.5 Arrays
Available under Creative Commons-ShareAlike 4.0 International License (http://creativecommon
s.org/licenses/by-sa/4.0/).
Right click on the Front Panel window and selectArray from the Controls » Modern »
Arrays, Matrix & Cluster menu, and drop an array onto the Front Panel window. The
array structure consists of an index or element offset (left portion of the structure)
and the array elements (right portion of the structure). When the array structure is
placed on the Front Panel window, the data type of the array is undefned as indicated
by the grayed out portion of the array.
14
To define the array data type, drag and drop a data type onto the array structure. For
instance, to create an input array of numbers, place Numeric Control into the array
structure.
At this point, the numeric array is an Empty or Null array because no elements of the
array have been defned. This is indicated by the grayed out numeric control within the
array structure.
Define elements of an input array by selecting the offset and entering its value. For
instance, at ofset 4, enter the value 0.0. This defines Numeric Input Array as {0, 0, 0,
0, 0}.
An output array is created similarly to an input array with the exception that an output
data type needs to be dropped into the array structure.
This program converts an array of Fahrenheit values to Celsius. Create numeric input
and output arrays and label them Fahrenheit and Celsius respectively. In the
Fahrenheit array enter the values 0, 20, 40, 60, 80, 100, 120, 140, 160, 180 and 200 at
ofsets 0 through 10 as shown in Numeric Input and Output Arrays.
Right click in the Block Diagram window, navigate to Programming » Structures and
click on For Loop.
Click and drag to create the For Loop as shown in Creating For Loops and For Loop.
Right click inside the For Loop and select Select a VI... from the pop-up menu. Find
the Fahrenheit to Celsius.vi and clickOK. Drop the function inside the For Loop.
To complete the program, wire the Fahrenheit input array to the input terminal of the
Fahrenheit
to Celsius function and wire the output terminal of the Fahrenheit to Celsius
function to the Celsius output array.
This program uses the For Loop to select each element in the Fahrenheit input array,
converts that value to Celsius and saves the results in the Celsius output array. Save
the program as Fahrenheit to Celsius For Loop.vi and run the program.
The Celsius output array contains: Celsius {-17.7778, -6.6667, 4.44444, 15.5556,
26.6667, 37.7778, 48.8889, 60, 71.1111, 82.2222, 93.3333}
The next program will generate Fahrenheit values and convert them to Celsius until a
condition is met to stop the iterations in a While Loop. In the Block Diagram window,
select the While Loop structure by clicking on it from the Functions » Programming »
Structures menu.
In the Front Panel window, create two numeric output arrays. Label them Fahrenheit
and Celsius.
From the Functions menu, select Multiply function and a couple of numeric
constants. Type in 20.0 and 300.0 for the numeric constants. Select the Fahrenheit to
Celsius.vi and drop it inside the While Loop. Re-arrange the diagram to look like
Generating Fahrenheit Values
18
Wire the While Loop components as shown in Generating Fahrenheit Values & Stop
Condition.
Wire the output of the Multiply operation to theFahrenheit and the output of the
Fahrenheit to Celsius function to the Celsius numeric output arrays. The
connections between the While Loop and the Fahrenheit andCelsius arrays are
broken (see Broken Wires).
To repair the broken connections, roll over the mouse pointer to the Loop Tunnel.
19
Right click on the Loop Tunnel and select Enable Indexing from the pop-up menu.
Figure 1.66 [/topic/body/fig/title/title {"- topic/title "}) Enable Loop Indexing (title]
This enables values to accumulate and store the results into an array. Repeat for the
Celsius array.
Each iteration of the While Loop in this program generates an i ×20 Fahrenheit value
and converts it to Celsius. The While Loop stops iterating when the generated
Fahrenheit value is greater than or equal to 300. The resulting arrays are stored in the
Fahrenheit and Celsius numeric output arrays.
Save the program as Fahrenheit to Celsius While Loop.vi and run it. The program
generates the following results:
Fahrenheit {0, 20, 40, 60, 80, 100, 120, 140, 160, 180, 200, 240, 260, 280, 300}
Celsius {-17.7778, -6.6667, 4.44444, 15.5556, 26.6667, 37.7778, 48.8889, 60, 71.1111,
82.2222, 93.3333, 104.444, 115.556, 126.667, 137.778, 148.889}
1.8 Graphs
Available under Creative Commons-ShareAlike 4.0 International License (http://creativecommon
s.org/licenses/by-sa/4.0/).
Using the previous G program example, we will now visualize the results by adding a
graph to the Front Panel windows. Right click on the Front Panel window. Select XY
Graph from the Controls » Modern » Graph menu.
Drop the XY Graph in the Front Panel window. Double click on the x and y axis labels
and renameTime to Fahrenheit and Amplitude to Celsius.
Select Bundle from the Functions » Programming » Cluster, Class & Variant menu
21
Wire the Fahrenheit and Celsius results to the input Bundle terminals and the
output Bundle terminal to the XY Graph.
Save the program and run it. The resulting graph is shown in the fgure below.
1.9 Interactivity
Available under Creative Commons-ShareAlike 4.0 International License (http://creativecommon
s.org/licenses/by-sa/4.0/).
In the Front Panel window, from the Functions » Modern » Numeric select the
vertical pointer slide. From the Functions » Modern » Graph select Waveform Chart.
Re-label the vertical pointer slide as Amplitude and the waveform chart as Sine
Wave. Re-arrange to GUI to look like the figure below.
Right click on Sine Wave and selectProperties from the pop-up menu.
23
Select the Scales tab and change Maximum to 1023. Sine Wave will display 1024
samples. Click on the down arrow located to the right of Time (XAxis) and select
Amplitude (YAxis).
De-select Autoscale and change the Minimum and Maximum values to -10 and 10.
Click OK.
De-Selecting Autoscale
24
In the Block Diagram window, re-arrange the Amplitude and Sine Wave terminals
and finish the program as shown in Interactive Sine Wave Diagram.
And right click on theLoop Control and from the pop-up menu selectCreate Control.
A stop terminal is created...
While the program is running, change the Amplitude and watch the graph update to
refect the interactive changes.
26
To end the G program, simply click on the stop button. Congratulations. You have
successfully completed and executed your first interactive G program.
Create a copy of the while loop and its contents by selecting Edit » Paste. Organize
the diagram as shown in the figure below.
Go the Front Panel window and organize the input and output controls as shown in
the figure below.
Congratulations!!! You have just completed your frst parallel interactive program using
G. Save the program, run it and interact with it. To end this program click on stop and
stop 2.
1.12 Polymorphism
Available under Creative Commons-ShareAlike 4.0 International License (http://creativecommon
s.org/licenses/by-sa/4.0/).
This program shows the polymorphic properties of G. Create the G program shown
below. Notice that the Subtract and Multiply operations allow arrays to be wired in
the G program.
Figure 2.1
30
Figure 2.2
Figure 2.3
31
Chapter 3 Operators
3.1 Numeric
Available under Creative Commons-ShareAlike 4.0 International License (http://creativecommon
s.org/licenses/by-sa/4.0/).
3.2 Boolean
Available under Creative Commons-ShareAlike 4.0 International License (http://creativecommon
s.org/licenses/by-sa/4.0/).
3.3 Comparison
Available under Creative Commons-ShareAlike 4.0 International License (http://creativecommon
s.org/licenses/by-sa/4.0/).
3.4 Math
To create an array in G, right click on the Front Panel window and select Array from
the Controls » Modern » Arrays, Matrix & Cluster menu, and drop the array
structure onto the Front Panel window to create an array.
The array structure consists of an index or element offset (highlighted left portion of
the array structure) and the array elements (right portion of the structure). When the
array structure is placed on the Front Panel window, the data type of the array is
undefined as indicated by the grayed out portion of the array.
To define the array data type, drag and drop any data type, such as numeric, Boolean,
string or cluster structure, onto the elements portion of the array structure.
At this point, the newly defined array is anEmpty orNullArray because no elements of
the array have been defined. This is indicated by the grayed out data type within
theelements array structure.
To define elements of an input array, select the element's index and enter the
appropriate value. Figure 6.5 defines a numeric array with one element at index 0.
36
G arrays are zero-based. The last element index of an N element array is N1. Last
Array Element and Undefined Nth Element are those of a 10 element array.
An output array is created similarly to an input array with the exception that an output
data type needs to be dropped into the array structure.
To create multidimensional arrays, click on the array's index and select Add
Dimension from the menu. Multidimensional Array shows a 2-dimensional array.
4.3 Clusters
Available under Creative Commons-ShareAlike 4.0 International License (http://creativecommon
s.org/licenses/by-sa/4.0/).
Clusters allow users to create compound data types by aggregating various and
different data types into a single unit.
Select the various data types and drag them onto the cluster structure. Figure Figure
4.13shows an Error Cluster consisting of a Boolean Error, a numeric ID and a string
Message data types.
The case structure allows data to flow based on a integer, Boolean or string matching
condition. The case executed is selected based on the data wired to theCase Selector.
In the Front Panel window, select a Boolean control and an output string.
To select the False case, click on the selector label down arrow and select False from
the pop-up menu. You can also cycle through the cases by clicking the next (right) or
previous (left) arrows.
Wire the string constant in the case structure to the output string terminal.
Select theTrue case and wire the string constant to the case structure tunnel.
Complete the diagram as shown in Completed Case Diagram.
It is important to note that all instances in a case structure must be wired to enable
data to flow from thecase structure.
In the Front Panel window, toggle the Boolean input control and run the program.
Select an Integer 32 numeric input and an Integer 32 numeric output and label them
Selector and Case respectively.
In the Block Diagram window, create a case structure, select the False case and
arrange the terminals as shown in Multicase.
41
Wire the Selector numeric control to the case selector on the case structure. The
selector label reflects the diagram update.
In the0, Default case, add a numeric constant and leave its value as 0.
Using the selector label, select case 1. Add a numeric constant, enter 1 and wire it to
the case tunnel. The resulting diagram is shown in Case 1.
Right click anywhere in the case structure and selectAdd Case After from the pop-up
menu.
Case 2 is added after case 1. Add a numeric constant, enter2 and wire it to the case
structure tunnel.
42
Multicase Selection Program shows the results of running this simple case selection
programs for Selector set to 0, 1, 2 and 3 respectively.
The For Loop structure repeatedly executes the diagram within the structure. The
Loop Count specifies the number of times the loop contents must be executed and
the Loop Iteration indicates which iteration is currently being executed.
The Loop Count and Loop Iteration are of Integer 32 data types. If the Loop Count is
set to N, then the Loop Iteration value range is from 0 toN1. This is illustrated in Loop
Count and Final Loop Iteration.
To add a Shift Register, right click on the For Loop structure and select Add Shift
Register from the pop-up menu.
To add elements to the shift register, right click on the shift register and select Add
Element from the pop-up menu.
To illustrate the use of the shift registers, the following example computes the
Fibonacci number Fib(n).
In the Front Panel window, select an integer 32 numeric input and output controls and
labeled them n and Fib(n) respectively. Arrange the diagram as shown in Shift
Register Example.
Add a 0 and 1 numeric constants to initialize the elements of the shift register and
wire them to the i-1 and i-2 elements respectively. Add the add operator in the for
loop and complete the program wiring as shown in Fibonacci G Program.
For n=0, the for loop iterates 0 times and passes 0 to Fib(n), therefore Fib(0) 0. For n 1,
the for loop the values ini-1 and i-2 shift register elements are added (0+1) and saved
in the i shift register element (1). Since the loop iterates once only, the resulting value
is passed toFib(n), therefore Fib(1) 1. For n= 2, the first iteration produces the value of
1. Prior to the next and final iteration, the values are shifted in the register as follows:
The value in the i-1 shift register element is shifted to the i-2 shift register element
The value in the i shift register element is shifted to thei-1 shift register element
nd
To start the 2 and final iteration, thei-1 shift register element contains 1 and the i-2
shift register element contains 0. These are added to produce 1, which is passed to
Fib(n) and, therefore, Fib(2) 1. This process is repeated for values of n > 2.
Save this program as Fibonacci.vi. Figure 7.29 shows the result of Fib(8).
5.2.2 Auto-Indexing
Available under Creative Commons-ShareAlike 4.0 International License (http://creativecommon
s.org/licenses/by-sa/4.0/).
To disable auto-indexing, right click on the target Auto-Indexed Tunnel and select
Disable Indexing from the pop-up menu.
The final diagram with the Auto-Indexed Tunnel disabled is shown in Disabled Auto-
Indexing.
The While Loop conditionally iterates executing the statements within the structure.
The Loop Condition establishes whether the loop iterates or terminates. TheLoop
Iteration is a zero-based iteration execution reference similar to the For Loop.
46
The default loop condition is to continue if the Boolean condition is False (or stop if
True). The while loop in the following Figure 5.35 will iterate while Iterations is less
than Loop Iteration is False or, equivalently, will stop iterating whenIterations is less
than the value in Loop Iteration.
At times it is more convenient to let the while loop iterate while the condition is True.
To change the loop condition, right click on the loop condition icon and select
Continue if True from the pop-up menu.
Programmatically, while loop shift registers are identical to for loop shift registers.
Refer to Section Shift Registers for the discussion. However, an example is provided to
illustrate the use of shift registers in while loops.
Notice that two shift registers keep track of the factorial and the sum. Also notice the
dot in the multiplication. This is because the loop iteration is an integer 32 data type
and the input from one of the shift registers is double precision numeric. The dot
represents that the integer 32 data type has been coerced into a double precision
number.
Save the program as e.vi. The result of running this program is shown in Computed e
to 5 Digits.
By default, while loops are auto-indexed disabled. In order for while loops to process
and generate arrays, the loop tunnel must be enabled to auto-indexed arrays.
48
To enable auto-indexing, right click on the loop tunnel and select Enable Indexing
from the pop-up menu.
In this example the while loop appropriately generates a 1,000 element numeric array
with random numbers.
5.4 Sequence
Available under Creative Commons-ShareAlike 4.0 International License (http://creativecommon
s.org/licenses/by-sa/4.0/).
The following examples time in milliseconds (ms) the execution of a G diagram. The
sequence of events is get a start time stamp, execute the diagram, get stop time
stamp and take the difference between the stop and start times to determine the
execution time.
Flat Sequences always execute left to right. A Flat Sequence structure starts with a
single frame and allows a user to visualize the diagram sequences.
49
To add frames to a sequence, right click on the sequence structure and select either
Add Frame After or Add Frame Before from the pop-up menu according to the
program's needs.
Add two more frames to the sequence structure to get a three frame sequence as
shown in Three Frame Sequence.
From the Functions » Programming » Timing menu select Tick Count (ms) function.
Drop the Tick Count (ms) function in the frst (left most) frame of this sequence. Make
a copy of the Tick Count function and place it on the third (right most) frame as
shown in Start and Stop Tick Counts.
Add a For Loop that iterates 5,000 times to the second frame. Add a subtract
operator, an unsigned integer 32 output and complete the program as shown in
Timing G Program. The execution of this program shows the time in milliseconds it
nd
took for the 2 sequential frame to execute.
For this timing example, start with a Stacked Sequence and add 3 more frames. The
sequence frames are labeled 0, 1, 2 and 3 and will execute in that order.
Go to the first frame (frame 0) and add a Tick Count (ms) function. Right click on the
sequence structure and select Add Sequence Local from the pop-up menu.
The Sequence Local is shown as an undefined tunnel. Wire the Tick Count (ms)
function to the Sequence Local to define the tunnel data type and data flow. Data can
now flow from frame 0 to the other frames as needed.
Go to the next frame sequence (frame 1) and enter the program to be timed.
Go to the third frame of the sequence (frame 2), add a Tick Count (ms) function, add
another Sequence Local and wire the Tick Count (ms) to the new Sequence Local.
The wired sequence frame is shown in Stop Time Stamp.
Go to the last frame (frame 3) and add aSubtract function. Wire the Sequence Locals
from frame 2 and frame 0 to the Subtract function as shown in Stacked Timing G
Program. To complete the diagram, wire the output of the Subtract function to the
unsigned integer 32 output.
It is important to note that the programs in Timing G Program and Stacked Timing G
Program are programmatically identical.
52
Chapter 6 Functions
Available under Creative Commons-ShareAlike 4.0 International License (http://creativecommon
s.org/licenses/by-sa/4.0/).
6.1 Connectors
Available under Creative Commons-ShareAlike 4.0 International License (http://creativecommon
s.org/licenses/by-sa/4.0/).
On the Front Panel window, right click on the icon located on the right upper corner of
the window and select Show Connector.
Right click on the connector pane and select Patterns. A menu with connector
patterns is presented from which you can select the appropriate pattern. For this
example select the pattern highlighted in Select Connector Pattern.
Click on the connector terminal followed by a click on the input or output control to
which the terminal is to be associated. In Associating Terminals, the left connector
terminal is associated with the numeric input control n.
Repeat for all the input and output controls that are to be associated to the terminals.
For the Fibonacci.vi, Connected Terminals shows the right connector terminal
associated with the Fib(n) output terminal.
Right click on the connector pane and selectEdit Icon... from the pop-up menu. This
will bring the icon editor (Figure: Icon Editor). Edit the icon for black and white,
16-color and 256-color displays and click OK when completed. Save the G program to
complete the function.
To invoke functions, right click on the Block Diagram window and selectSelect a VI...
from the pop-up menu. This will bring a file dialog box. Find the desired function to be
part of the program and click OK.
In the example shown in Fibonacci Series, the Fibonacci series of the first 20 Fibonacci
numbers is stored in an array. The numbers are computed by invoking the
Fibonacci.vi function.
Chapter 7 Graphs
7.1 Waveform Chart
Available under Creative Commons-ShareAlike 4.0 International License (http://creativecommon
s.org/licenses/by-sa/4.0/).
The following example will build a simple G program that will allow you to chart a sine
wave as it is being generated on a point-by-point basis using the equation:
Start with a while loop and add into it a Multiply and Sine functions, a numeric
constant with value 0.2 and a Boolean control to stop the loop when its value is True.
Arrange the diagram to look as in the following Figure Figure 7.2.
To select a waveform chart, right click on the Front Panel window and select
Waveform Chart from the Controls »Modern »Graph menu.
Figure 7.4
In the Block Diagram window, make sure that the Waveform Chart terminal is inside
the while loop. Wire the output of the Sine function to this terminal.
Most modern computers will run this program too fast. Thus, before this program is
executed, a delay of 125 milliseconds will be inserted in the while loop. This will allow
users to see how the Waveform Chart operates as data samples are plotted in the
chard.
Drop the Wait Until Next ms Multiple function inside the loop and wire a constant to it
with the value
125. This will delay the loop for 125 milliseconds. The final Waveform Chart program is
shown in Figure Waveform Chart Program.
The default graphing mode of the Waveform Chart is autoscaling. You will notice the
auto-scaling property when the program frst begins to run and the y-axis, labeled
Amplitude, updates automatically as new numerical values are aggregated and
displayed on the chart.
As the program continues to run, the graph continues to build as per the values
associated with the x-axis, labeled Time, which correspond to the index value of the
equations.
As the program continues to run, the autoscaling property also applies to the x-axis.
Noticed the updated x-axis. For this example, the x-axis will continue updating so as
long as the program is running. This gives the appearance of a scrolling strip chart.
58
Stopping and restarting the G program retains the numeric history and continues to
aggregate the values for display.
The Waveform Chart options can be easily updated by right clicking on the Waveform
Chart and selecting the appropriate option to update from the pop-up menu.
Selecting Properties from this pop-up menu brings up the Waveform Chart dialog
window (Figure Figure 7.14).
59
The Waveform Graph allows numeric arrays to be displayed graphically in the Front
Panel window. Similar to the previous example, we will build a simple G program that
will allow you to graph a sine wave using the equation:
60
Figure 7.15
Start by building the following program shown in Figure For Loop Sine Wave.
Right click on the Front Panel window, select Waveform Graph from the Modern
»Graph pop-up menu, and drop it on the Front Panel window.
In the Block Diagram window you will see the Waveform Graph terminal. Wire the
Sine function output to the Waveform Graph terminal through the For Loop.
Run the program. The resulting graph is shown in Figure Sine Wave Graph.
61
7.2.2 Multiplots
Available under Creative Commons-ShareAlike 4.0 International License (http://creativecommon
s.org/licenses/by-sa/4.0/).
In this example a sine wave and a noisy sine wave will be plotted. Modify the previous
example to add noise to the sine operation as shown in Figure Sine and Noisy Sine
Waveforms.
Add a Build Array operator and wire the output of the Sine function and the multi-
add operator containing the sine value plus some random noise between -0.5 and 0.5
to the Build Array operator. Wire the output of the Build Array operator to the
Waveform Graph terminal.
You can continue adding 1D arrays to be multiplotted into a single Waveform Graph.
Run the program. The multiplot result is shown in Figure Multiplot.
62
7.3 XY Graph
Available under Creative Commons-ShareAlike 4.0 International License (http://creativecommon
s.org/licenses/by-sa/4.0/).
The example shown in Figure Spiral G Program generates the spiral shown in Figure Fi
gure 7.24.
The heart of interactive programming in G is the while loop. Any input control within
the while loop can be modified from the Front Panel window at run time to provide
seamless interaction with the G program.
In the Front Panel window, from the Functions »Modern »Numeric select the vertical
pointer slide. From the Functions »Modern »Graph select Waveform Chart.
Re-label the vertical pointer slide as Amplitude and the waveform chart as Sine
Wave. Re-arrange to GUI to look like the figure below.
64
Right click on Sine Wave and select Properties from the pop-up menu.
Select the Scales tab and change Maximum to 1023. Sine Wave will display 1024
samples.
Click on the down arrow located to the right of Time (XAxis) and select Amplitude
(YAxis).
65
De-selectAutoscale and change the Minimum and Maximum values to-10 and 10.
ClickOK.
Rearrange Amplitude and Sine Wave terminals and finish the program as shown in
Figure Figure 8.9. Scroll the mouse pointer over the Loop Control...
And right click on the Loop Control and from the pop-up menu select Create Control.
A stop terminal is created.
With the corresponding stop Boolean input control. Save the G program
asInteractivity.vi.
While the program is running, change the Amplitude and watch the graph update to
reflect the interactive changes.
67
Interactive Program
Figure 8.16
Congratulations. You have successfully completed and executed your first interactive
G program.
68
In the following example, we will develop a simple program where interactivity and
parallelism are part of the program.
Create a copy of the while loop and its contents by selecting Edit »Paste. Organize the
diagram as shown in the figure below.
Go the Front Panel window and organize the input and output controls as shown in
the figure below.
69
You have just completed your first parallel interactive program using G. Save the
program, run it and interact with it.
The Create Matrix function generates a square matrix based of size indicated by Size
containing random numbers between 0 and 1. The Create Matrix function is shown
in Figure Creating a Square Matrix.
The Split Matrix function determines the number of rows in the matrix and shifts
right the resulting number of rows by one (integer divide by 2). This value is used to
split the input matrix into the top half and bottom half matrices. The Split Matrix
function is shown in Figure Split Matrix into Top & Bottom.
Sequence
Operation Description
Frame
Second Frame Records start time for single core matrix multiply
Fifth Frame Splits the matrix into top and bottom matrices
Seventh
Performs multicore matrix multiply
Frame
The rest of the calculations determine the execution time in milliseconds of the single
core and multi-core matrix multiply operations and the performance improvement of
using data parallelism in a multicore computer.
The program was executed in a dual core 1.83 GHz laptop. The results are shown in
Figure Data Parallelism Performance Improvement. By leveraging data parallelism, the
same operation has nearly a 2x performance improvement. Similar performance
benefts can be obtained with higher multicore processors
72
Adding a shift register to the loop allows tasks to be pipelined and be executed in
parallel in separate cores should they be available. Task pipelining is shown in Figure
Pipelined Tasks.
The program below times the sequential task and the pipelined tasks to establish its
performance improvement when executed in multicore computers.
Figure Pipelining Performance Improvement shows the results of running the above G
program in a dual core 1.8 GHz laptop. Pipelining shows nearly 2x performance
improvement.
Feedback Nodes provide a storage mechanism between loop iterations. They are
programmatically identical to the Shift Registers. Feedback Nodes consist of an
Initializer Terminal and the Feedback Node itself (see Figure Feedback Node).
To add a Feedback Node, right click on the Block Diagram window and select
Feedback Node from the Functions »Programming »Structures pop-up menu. The
direction of the Feedback Node can be changed by right clicking on the node and
selecting Change Direction.
Consider the function in Figure Figure 11.1 where a set of numbers in a one-
dimensional array represents the resulting noisy signal is to be written to a file. This
section will outline the steps required to create files.
Create a new G program, right click in the G programming window and select File
Dialog from the Functions »Programming »File I/O »Advanced Functions menu.
Drag and drop the File Dialog function onto the G programming window.
The Configure File Dialog dialog box automatically appears to configure the function.
Accept the default configuration shown in Figure Figure 11.3 to create a single file by
clicking the OK button.
The resulting diagram after closing the configuration dialog box is shown in .
Optionally, right click on File Dialog and select View As Icon from the pop-up menu.
This will save some real estate in the G programming window.
From the Functions »Programming »File I/O menu select Open/Create File, Write
Binary File and Close File functions.
Right click on the operation (0:open) terminal of the Open/Create File function
(highlighted in Figure File Create Operation).
Click on the down arrow in the operation constant just created and select open or
create from the pop-up menu.
The resulting updated operation constant value is shown in Figure Figure 11.12.
Set the constant to write-only. Re-arrange the block diagram to look like the diagram
shown in Figure Figure 11.14. At this point, the file is set to create a new file for
writing.
78
Get the Noisy Signal function and wire its output data to the Data terminal of the
Write to Binary File function.
Complete the diagram by connecting the Open, Write and Close file operations as
shown in Figure Figure 11.16.
When this G program is executed, the standard file dialog box appears. Name the file
to be written signal.dat.
Once the program completes executing, the signal.dat file is created and located in
the location indicated by the path selected.
The signal.dat file created in the previous example will be used to read data from a
file. As in the previous example, select the File Dialog, Open/Create File, Read from
Binary File and Close File functions.
Create constants by right clicking on the operation (0:open) and access (0:read/
write) terminals of the Open/Create File operation. Set the constants toopen
andread-only respectively (see Figure Figure 11.20).
Similar to creating arrays, drop an array constant in the G diagram, drop a numeric
constant onto the array constant and set the data type representation to double. Wire
this array constant to the data type terminal of the Read from Binary File function
as shown in Figure Figure 11.21 .
80
With the data type specified, wire thedata terminal of the Read from Binary File
function to the Waveform Graph terminal as shown in Figure Figure 11.23 .
Complete the program by wiring refnum and error terminals of the Open/Create
File, Read from Binary File and Close File functions as shown in Figure Figure 11.24 .
When this program is executed, a fle dialog box appears. Select the signal.dat file and
click OK.
81
The binary data in signal.dat is read and plotted in a Waveform Graph. The result is
shown in Figure Figure 11.26.