10
1/10 www.ni.com Working with .m Files in LabVIEW for Text-Based Signal Processing, Analysis, and Math 1. 2. 3. 4. 5. 6. 7. 1. 1. Overview This tutorial provides several step-by-step exercises that introduce you to using NI LabVIEW software to work with .m file scripts for text-based math, signal processing, and analysis. This tutorial also explores LabVIEW MathScript, the LabVIEW feature that offers this capability. LabVIEW MathScript is available in the Full and Professional Development Systems in LabVIEW 8.0 through 8.6. In LabVIEW 2009, LabVIEW MathScript becomes the LabVIEW MathScript Note: RT Module. You cannot run VIs from previous versions of LabVIEW that contain MathScript Nodes until you install and activate the MathScript RT Module. This tutorial does not provide an extensive introduction to the LabVIEW development environment. Refer to the tutorial (linked below) for a general Getting Started with LabVIEW Virtual Instruments overview of LabVIEW. Table of Contents Working with .m Files in LabVIEW Working in the LabVIEW MathScript Window MathScript Tips Trying Out .m File Scripts Using the MathScript Node, Part I Using the MathScript Node, Part II Related Links  Working with .m Files in LabVIEW  Although L abVIEW is a de velopmen t environmen t built aroun d a graphic al programming langu age, LabVIEW also allow s you to create files and w ork with text-ba sed math. One way to work w ith .m text-based math in LabVIEW is through a script node interface. Script nodes combine text-based files with traditional LabVIEW graphical programming. Script nodes are resizable text-entry .m regions you can add to LabVIEW block diagrams. One type of script node, the MATLAB script node, calls the MATLAB software to execute scripts. You must have a license d copy of the MATLAB software installed on your compu ter to use ® MATLAB script nodes because the script nodes invoke the MATLAB software script server to execute scripts written in the MATLAB language syntax. You also can create scripts in LabVIEW with LabVIEW MathScript. LabVIEW MathScript is a text-based language you can use to write functions and scripts for use in the LabVIEW MathScript or MathScript Node. MathScript Nodes can process many of your text-based scripts created in a MATLAB or compatible environment. However, because the MathScript RT Module Window engine does not support all functions supported by the MATLAB software, some functions in your existing scripts might not be supported. You can implement such functions with a Formula Node or another script node. You can use the LabVIEW Application Builder and MathScript to build stand-alone applications and shared libraries that include functionalit y defined in files. .m You can interact with MathScript in several different ways. Use the for an interactive interface in which you can load, save, develop, and execute file scripts. Use LabVIEW MathScript Window .m the MathScript Node to utilize the graphical programming environment in LabVIEW and to deploy file scripts as part of a stand-alone application. .m The following section offers a brief walk-through of the . Later sections in this tutorial discuss the MathScript Node. LabVIEW MathScript Window MATLAB is a registered trademar k of The MathWorks, Inc. All other trademarks are the property of their respectiv e owners ® Working in the LabVIEW MathScript Window The window appears when you launch LabVIEW. Use this window to create new VIs and projects, select among the most recently opened LabVIEW files, find examples, and Getting Started launch the . You also can access information and resources to help you learn about LabVIEW, such as specific manuals, help topics, and resources on the National Instrument s Web LabVIEW Help site. Opening the LabVIEW MathScript Window Select to display the . Tools»MathScript Window LabVIEW MathScript Window The includes a in the lower left corner. Enter the following command in the and press the <Enter> key: LabVIEW MathScript Window Command Window Command Window t = 1:10 : Document Type Tutorial : Yes NI Supported : Jul 31, 2009 Publish Date

NI Tutorial 4854 En

Embed Size (px)

Citation preview

Page 1: NI Tutorial 4854 En

7/31/2019 NI Tutorial 4854 En

http://slidepdf.com/reader/full/ni-tutorial-4854-en 1/101/10 www.ni.c

Working with .m Files in LabVIEW for Text-BasedSignal Processing, Analysis, and Math

1.

2.

3.

4.

5.

6.

7.

1.

1.

Overview

This tutorial provides several step-by-step exercises that introduce you to using NI LabVIEW software to work with .m file scripts for text-based math, signal processing, and analysis. This tutoriaalso explores LabVIEW MathScript, the LabVIEW feature that offers this capability.

LabVIEW MathScript is available in the Full and Professional Development Systems in LabVIEW 8.0 through 8.6. In LabVIEW 2009, LabVIEW MathScript becomes the LabVIEW MathScripNote:

RT Module. You cannot run VIs from previous versions of LabVIEW that contain MathScript Nodes until you install and activate the MathScript RT Module.

This tutorial does not provide an extensive introduction to the LabVIEW development environment. Refer to the tutorial (linked below) for a genGetting Started with LabVIEW Virtual Instruments

overview of LabVIEW.

Table of Contents

Working with .m Files in LabVIEW

Working in the LabVIEW MathScript Window

MathScript Tips

Trying Out .m File Scripts

Using the MathScript Node, Part I

Using the MathScript Node, Part II

Related Links

 

Working with .m Files in LabVIEW

 Although LabVIEW is a development environment built around a graphical programming language, LabVIEW also allows you to create files and work with text-based math. One way to work w.m

text-based math in LabVIEW is through a script node interface. Script nodes combine text-based files with traditional LabVIEW graphical programming. Script nodes are resizable text-entry.m

regions you can add to LabVIEW block diagrams.

One type of script node, the MATLAB script node, calls the MATLAB software to execute scripts. You must have a licensed copy of the MATLAB software installed on your computer to use®

MATLAB script nodes because the script nodes invoke the MATLAB software script server to execute scripts written in the MATLAB language syntax.

You also can create scripts in LabVIEW with LabVIEW MathScript. LabVIEW MathScript is a text-based language you can use to write functions and scripts for use in the LabVIEW MathScript

or MathScript Node. MathScript Nodes can process many of your text-based scripts created in a MATLAB or compatible environment. However, because the MathScript RT ModuleWindow

engine does not support all functions supported by the MATLAB software, some functions in your existing scripts might not be supported. You can implement such functions with a Formula Node

another script node. You can use the LabVIEW Application Builder and MathScript to build stand-alone applications and shared libraries that include functionality defined in files..m

You can interact with MathScript in several different ways. Use the for an interactive interface in which you can load, save, develop, and execute file scripts. ULabVIEW MathScript Window .mthe MathScript Node to utilize the graphical programming environment in LabVIEW and to deploy file scripts as part of a stand-alone application..m

The following section offers a brief walk-through of the . Later sections in this tutorial discuss the MathScript Node.LabVIEW MathScript Window

MATLAB is a registered trademark of The MathWorks, Inc. All other trademarks are the property of their respective owners®

Working in the LabVIEW MathScript Window

The window appears when you launch LabVIEW. Use this window to create new VIs and projects, select among the most recently opened LabVIEW files, find examples, andGetting Started

launch the . You also can access information and resources to help you learn about LabVIEW, such as specific manuals, help topics, and resources on the National Instruments WLabVIEW Help

site.

Opening the LabVIEW MathScript Window

Select to display the .Tools»MathScript Window LabVIEW MathScript Window

The includes a in the lower left corner. Enter the following command in the and press the <Enter> key:LabVIEW MathScript Window Command Window Command Window

t = 1:10

:Document Type Tutorial

: YesNI Supported

: Jul 31, 2009Publish Date

Page 2: NI Tutorial 4854 En

7/31/2019 NI Tutorial 4854 En

http://slidepdf.com/reader/full/ni-tutorial-4854-en 2/102/10 www.ni.c

1.

2.

3.

1.

2.

1.

Notice that the result of the command you entered appears in the , located just above the . The result of this command is a vector of ten elements, whOutput Window Command Window

the elements start at 1 and end at 10 with a step size of 1.

Locate the tabs labeled , , and at the top right of the . Click the tab to display the page. This page displays a Variables Script History LabVIEW MathScript Window Variables Variables

of all variables you define and previews variables that you select.

Notice that the variable list contains an entry for the vector you defined in step 2. Click the in the variable list to display the contents of in the located in the lower right cot t t  Preview Pane

of the .LabVIEW MathScript Window

Enter the following commands in the . Press the <Enter> key at the end of each line.Command Window

t=0:.1:2*pi;

y=sin(t);

 After you press the <Enter> key, LabVIEW displays each command in the . LabVIEW does not display the output for each command because the semicolon at the end of Output Window

each line directs LabVIEW to suppresses the output.

On the page, click the local variable . Then modify the view options on the page to view the contents of the variable as a graph or as numerical elements, as showVariables y  Variables y 

the following figure. You also can use these different views to modify the contents of the variable.

Working with Multi-Line Scripts

The previous exercise demonstrated how to use the to enter commands and view the output for these commands. The exercise also showed how to view and modify theCommand Windowcontents of variables. The following exercise shows how to use the to enter multi-line scripts and create larger, more involved scripts. Complete the following steps to create aScript Editor 

multi-line script, run the script, and then display the results in several different display formats.

Click the tab to display the page.Script Script

Page 3: NI Tutorial 4854 En

7/31/2019 NI Tutorial 4854 En

http://slidepdf.com/reader/full/ni-tutorial-4854-en 3/103/10 www.ni.c

1.

2.

Copy and paste the following file script into the on the page. Notice the script includes percent signs (%) that precede lines of comments. Use these comments to inclScript Editor Script

help documentation for the script.

%This example calculates and graphs the theoretical

% BER plots for two types of communication

% systems (BPSK and QPSK)

Eb_over_No_in_dB = [0:14];

% Purposes of the x-axis of the plot

Eb_No=10.^(Eb_over_No_in_dB./10);

% PSK is antipodal

xant = sqrt(2.*Eb_No);

% QPSK is orthogonal

xorth = sqrt(Eb_No);

% Use the erfc function as equivalent to

% Q function

Qant = 0.5*erfc(xant/sqrt(2));

Qorth = 0.5*erfc(xorth/sqrt(2));

% Plot the first result

semilogy(Eb_over_No_in_dB,Qant);

% Keep the same plot in graph

% plot the second one

grid on; hold on;

semilogy(Eb_over_No_in_dB,Qorth,'r--');

v = axis;

axis([v(1:2) 10^-6 .1])

xlabel('Eb/N0 in dB');

ylabel('Probability of bit error')

hold off;

legend('Antipodal','Orthogonal')

Click the button to execute the script, as shown in the following figure.Run

The example script plots the theoretical value of bit error rate (BER) from a digital communication system with two different digital modulation schemes.

Page 4: NI Tutorial 4854 En

7/31/2019 NI Tutorial 4854 En

http://slidepdf.com/reader/full/ni-tutorial-4854-en 4/104/10 www.ni.c

1.

1.

2.

This example invokes several built-in functions for math, graph formatting, and other tasks. MathScript includes built-in functions for a variety of tasks, including categories such as advanced mat

approximation, audio, basic math, bitwise operations, Boolean operations, utility commands, relational operators, data acquisition, digital signal processing, digital filter design, digital filter 

implementation, combinatorial geometry, interfacing with shared libraries (DLLs), linear algebra, linear systems, matrix operations, set membership, modeling/prediction, ODE, optimization, plotti

polynomial operations, programming constructs, resampling functions, set operations, string manipulation, support functions, time/date functions, timing functions, transforms, trigonometric

functions, vector analysis, waveform generation, and window generation.

In addition to the built-in MathScript functions, you also can define functions in files. Complete the following steps to define a function in a file..m .m

Delete all commands on the page. Then copy and paste the following script into the . The script defines a function named . This function converts anScript Editor Script Editor   rad2deg

input parameter to degrees and returns the result in the output.rad deg

function deg = rad2deg(rad)

% This is a Comment

% This function converts from radians to degrees.

deg = rad.*180./pi;

Click the button to save the function as in the directory. By default, LabVIEW searches the directory for user-defined functions andSave rad2deg.m LabVIEW Data LabVIEW Data

user-defined scripts.

NOTE: Use the page to configure the default search path list for MathScript. Select to display theMathScript: Search Paths Options File»LabVIEW MathScript Properties LabVIEW

dialog box and select from the list to display this page.MathScript Properties MathScript: Search Paths Category

In the , enter the following commands and press the <Enter> key after each command.Command Window

The user-defined function nowrad2deg

available as a function you can call from

scripts.

Refer to the MathScript Function Syntax

topic and the Calling User-Defined 

topFunctions from LabVIEW MathScript 

the (linked below) for moLabVIEW Help

information about working with user-defined functions.

MathScript Tips

This section contains a few tips for using the . The following tips might be useful as you work in the .LabVIEW MathScript Window LabVIEW MathScript Window

Place the cursor in the and use the Up and Down arrow keys on the keyboard to scroll through the command history.Command Window

Right click the on the page and select from the shortcut menu to display the variable in a separate window that you can resize.Preview Pane Variables Undock Window

 

Trying Out .m File Scripts

There are several online and local options that allow you to test your user-defined scripts in MathScript. Refer to the Developer Zone documentUse Fully Featured LabVIEW MathScript Today 

(linked below) for more information about these options.

Using the MathScript Node, Part I

Have you ever wanted to interactively change the value of a parameter and immediately see the response? Have you ever wanted to test the algorithm in a user-defined script with real acquired

data?

With LabVIEW, you can choose the most effective syntax for technical computing whether you are developing algorithms, exploring signal processing concepts, or analyzing results. You can

combine LabVIEW graphical programming with LabVIEW MathScript.

You can use the MathScript Node to combine textual algorithms with LabVIEW graphical programming. You then can use knobs, slides, buttons, graphics, and other controls and indicators

to instrument file scripts..m

Command Result of the Command

rad2deg(pi) Invokes the user-defined function you previously defined.

help rad2deg Returns the first commented paragraph as the help documentation for the user-defined

function.

rad2deg(linspace(0,pi,10)) Invokes the user-defined function that you previously defined with a function call (“linspace”)

as a parameter.

Page 5: NI Tutorial 4854 En

7/31/2019 NI Tutorial 4854 En

http://slidepdf.com/reader/full/ni-tutorial-4854-en 5/10

Page 6: NI Tutorial 4854 En

7/31/2019 NI Tutorial 4854 En

http://slidepdf.com/reader/full/ni-tutorial-4854-en 6/106/10 www.ni.c

1.

2.

1.

2.

3.

1.

1.

2.

3.

Timing

Samples per second (Hz) = 10000

 Automatic = checked

Connect the output from the Simulate Signal Express VI to the graph to display the synthesized signal.data Signal Samples

 Add a Convert from Dynamic Data Express VI to the block diagram. Click the button to choose the default settings for the VI and close the configuration dialog box.OK

The Convert from Dynamic Data Express VI converts data from the dynamic data type to a 1D array of scalars, which is a data type that the MathScript Node supports.

Wire the output from the Simulate Signal Express VI to the input of the Convert from Dynamic Data Express VI.

Wire the output of the Convert from Dynamic Data VI to the input of the MathScript Node.signalin

Wire the output on the right side of the MathScript Node to the graph terminal. The block diagram should resemble the following screenshot.fftresult FFT Result

Switch to the front panel and click the button to run the VI. LabVIEW displays the live signal in the graph on the left side of the front panel and the results of the analysis in the graph oRun

the right side of the front panel.

Click the button and then switch to the block diagram.STOP

 Add a Filter Express VI to the block diagram inside the While Loop.

Choose the following selections in the dialog box and click the button.Filter OK

Filter type = smoothing

Moving average = Triangular 

Half-width moving average = 7

Page 7: NI Tutorial 4854 En

7/31/2019 NI Tutorial 4854 En

http://slidepdf.com/reader/full/ni-tutorial-4854-en 7/107/10 www.ni.c

1.

2.

1.

Wire the output of the Simulate Signal Express VI to the input of the Filter VI.Signal

Right click the output of the Filter VI and select from the shortcut menu to create a waveform graph. This graph displays the filtered signal. The bFiltered Signal Create»Graph Indicator  

diagram should resemble the following screenshot.

Switch to the front panel and click the button to run the VI.Run

Using the MathScript Node, Part II

This section includes an example of building a more complicated VI that displays an RF antenna pattern for a dish antenna in linear (XY) and polar plots. The example shows how to build a custom

user interface to add interactivi ty to an algorithm you define in a script in a file using a MathScript Node. The example adds a control input to set the amplitude/lambda input parameter of the.m

algorithm.

Page 8: NI Tutorial 4854 En

7/31/2019 NI Tutorial 4854 En

http://slidepdf.com/reader/full/ni-tutorial-4854-en 8/108/10 www.ni.c

1.

2.

1.

2.

1.

1.

1.

2.

3.

4.

5.

6.

7.

8.

9.

Creating the User Interface

Open a new VI.

 Add a knob control to the front panel. You can use this control to set the size and wavelength of the antenna.

Change the name of the Knob control to .Amplitude/lambda

 Add an XY graph to the front panel. You can display the results of an application by using indicators, charts, and other graphical displays on the front panel of a VI.

 Add a polar plot indicator to the front panel. You can use this indicator to show the RF antenna pattern as a function of the angle.

Switch to the block diagram and locate the controls and indicators you created on the front panel. Move the polar plot indicator and the XY graph to the right side of the block diagram, as

shown in the following screenshot.

Adding a Script to a MathScript Node

 Add a MathScript Node to the block diagram.

Copy and paste the following script into the MathScript Node. This script processes the gain of the antenna at different angles.

% Create angle vector in radians

theta = linspace(-pi/2,pi/2,1000);

u = 2*pi*a*sin(theta);

% initialize matrix

E = ones(size(u));

% Get index of non-zero values

i = find(u);

% Evaluate Antenna pattern equation

E(i) = pi*a^2*abs(2*besselj(1,u(i))./(u(i)));

% change theta to degrees units for polar plot

Out=theta.*180./pi;

Right-click the left border of the MathScript Node and select from the shortcut menu.Add Input

Enter in the input to add an input for the variable in the script.a a

Right-click the right border of the MathScript Node and select from the shortcut menu.Add Output

Enter in the output terminal to add an output for the variable in the script. This variable represents the angle vector for plotting purposes.Out Out 

Right-click the output terminal and select from the shortcut menu to specify the data type of the output variable.Out Choose Data Type»1D-Array»DBL 1D Out

Repeat the steps 5 through 7 for the variable. This variable represents the gain output at different angles.E 

 Add a Bundle function to the block diagram to combine the X and Y components of the regular plot, as shown in the following block diagram.

Page 9: NI Tutorial 4854 En

7/31/2019 NI Tutorial 4854 En

http://slidepdf.com/reader/full/ni-tutorial-4854-en 9/109/10 www.ni.c

1.

2.

3.

4.

5.

1.

2.

1.

 Add a For Loop to the block diagram.

Place a Bundle function inside the For Loop.

Connect the input and output terminals. The squares in the border of the For Loop indicate the loop is set to auto index, or process each input element separately.

Right click the input of the Polar Plot VI and select from the shortcut menu.Polar attributes Create»Control

Switch to the front panel and set the control to 2. Configure the parameters of the polar plot to show a Log scale and display only the right half of the plot, as shown in Amplitude/lambdafollowing front panel.

Run the VI. The graph updates and then the VI automatically stops.

Switch to the block diagram. Add an express While Loop that encircles all elements on the block diagram, as shown in the following screenshot.

Run the VI. The results represent the gain of a dish antenna at different angles.

Related Links

Developer Zone: Getting Started with LabVIEW Virtual Instruments

LabVIEW MathScript Homepage

LabVIEW 2009 Help: MathScript Function Syntax

LabVIEW 2009 Help: Calling User-Defined Functions from LabVIEW MathScript

Developer Zone: Use Fully Featured LabVIEW and MathScript Today

Page 10: NI Tutorial 4854 En

7/31/2019 NI Tutorial 4854 En

http://slidepdf.com/reader/full/ni-tutorial-4854-en 10/10

 

Legal

This tutorial (this "tutorial") was developed by National Instruments ("NI"). Although technical support of this tutorial may be made available by National Instruments, the content in this tutorial ma

not be completely tested and verified, and NI does not guarantee its quality in any way or that NI will continue to support this content with each new revision of related products and drivers. THIS

TUTORIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND AND SUBJECT TO CERTAIN RESTRICTIONS AS MORE SPECIFICALLY SET FORTH IN NI.COM'S TERMS OF US

).http://ni.com/legal/termsofuse/unitedstates/us/