LabVIEW MathScript开发算法:第二部分——MathScript 交互式窗口
概览
本系列文章为您提供熟悉LabVIEW MathScript的练习。本文讲述了一个使用LabVIEW MathScript 窗口的示例。
MathScript 含义
无论您是要开发算法、研究信号处理概念还是分析结果,利用LabVIEW 您可以选择最有效的技术计算语法。您可以结合LabVIEW 图形化编程方式和LabVIEW MathScript ,它是面向数学的文本化编程语言,兼容广泛使用的m文件脚本语法。
参见:
LabVIEW MathScript 内幕:了解是什么特性使LabVIEW 非常适合于文本化算法开发和原型化
LabVIEW MahtScript示例库:本站点包含了大量LabVIEW MathScript示例。
LabVIEW MathScript 窗口
您可以利用两种接口来使用LabVIEW MathScript——LabVIEW MathScript 交互式窗口和MathScript 节点。参考本系列文章的第一篇来了解有关LabVIEW MathScript节点的更多信息。
您可以使用交互式MathScript 窗口来一次性输入命令,如下图所示。您也可以在简单的文本编辑窗口中输入批量脚本,从文本文件加载脚本或从独立的文本编辑器中输入。MathScript 窗口具有多种形式的回馈信息,如图形和文本。

使用MathScript 窗口进行算法开发
- 在Getting Started窗口中,选择Tools»MathScript Window来激活MathScript 窗口。
- 在Command Window文本框中输入4+6+1并按<Enter>键。命令的结果将在Output Window中显示。您可以一次性输入命令来及时计算出结果。
- 点击Variables标签。LabVIEW 会更新变量、ans,在位于Partition/Variable树形条的Local下方,可以看到上次命令的执行结果。
- 点击Script标签来输入下列命令
x=linspace(0,2*pi, 30);
b=sin(x)./(cos(x));
plot (x,b);axis([0 2*pi -20 20]);
注意x = linspace(0, 2*pi, 30);命令生成一个新的x变量并在0到2*pi内均匀取30个值来填入变量。
- 在Script页面上点击Run按钮,将出现Plot 1窗口并显示x相对于b的XY曲线。您可以通过点击Plot 1窗口的右上角的x来关闭窗口。
- 点击Variables标签来显示您所建立的变量,如x和b
- 在Partition/Variable树形条中选择b,将出现一个代表数值的表。选中Graphical First?复选框,您将首先看到变量值的图形显示,而不是缺省的数值显示。
- 点击Script标签并点击 Load按钮。选择Mitra P2_1.m(Mitra, Sanjit and Kaiser, James H., 数字信号处理手册Handbook for Digital Signal Processing [New York: John Wiley and Sons, 1993], 21)。该脚本生成一个测试信号并在该信号上应用滑动平均滤波器。
- 点击Run按钮来运行该脚本,将出现Prompt User for Input对话框。在Desired length of the filter=文本框中输入正值并点击OK按钮。MathScript 脚本可以包含交互式对话框来提示用户输入。
- The Mitra P2_1.m script, shown below, uses subplot commands to specify one of the four sub-plots on the Plot window. After you specify a sub-plot, subsequent commands affect that sub-plot. For example, a plot command, following a subplot command, will populate the sub-plot specified in the subplot command with the plot specified in the plot command.
如下图所示,Mitra P2_1.m脚本使用子曲线图命令在曲线图窗口中指定四个子曲线图中的一个。在您指定子曲线图后,后续的命令将影响该子曲线图。例如,一个绘图命令在子曲线图命令后,将对子曲线命令所指定的子曲线图上使用绘图命令。
% Program P2_1
% Simulation of an M-point Moving Average Filter
% Generate the input signal
n = 0:100;
s1 = cos(2*pi*0.05*n); % A low-frequency sinusoid
s2 = cos(2*pi*0.47*n); % A high frequency sinusoid
x = s1+s2;
% Implementation of the moving average filter
M = input('Desired length of the filter = ');
num = ones(1,M);
y = filter(num,1,x)/M;
% Display the input and output signals
clf;
subplot(2,2,1);
plot(n, s1);
axis([0, 100, -2, 2]);
xlabel('Time index n'); ylabel('Amplitude');
title('Signal #1');
subplot(2,2,2);
plot(n, s2);
axis([0, 100, -2, 2]);
xlabel('Time index n'); ylabel('Amplitude');
title('Signal #2');
subplot(2,2,3);
plot(n, x);
axis([0, 100, -2, 2]);
xlabel('Time index n'); ylabel('Amplitude');
title('Input Signal');
subplot(2,2,4);
plot(n, y);
axis([0, 100, -2, 2]);
xlabel('Time index n'); ylabel('Amplitude');
title('Output Signal');
axis;
您可以使用Command Window文本框来找出更多命令信息。利用在Command Window 文本框中键入help subplot,该命令的描述以及语法信息、输入、输出和示例将会出现在Output Window中。
相关链接
利用LabVIEW 进行算法开发和原型化——第一部分——使用MathScript 节点
法律条款
本教程由National Instruments公司(简称“NI”)开发。 尽管National Instruments可为该程序提供技术支持,但是该指南的内容并非完全通过测试和验证,NI不以任何方式保证其质量,也不保证相关产品或驱动程序的新版本出现时继续为其提供技术支持。本教程仅以其“现状”向用户提供,教程没有任何担保。教程使用受ni.com网站上《使用条款》的约束。 (http://ni.com/legal/termsofuse/unitedstates/us/)
