MATLAB News & Notes - November 2003
Building Analysis Applications For PC-Based Instruments
by Tom Gaudette, Loren Dean, and Jane Price
The shift to PC-based instruments is a recent trend in the test and measurement market that is making it easier to integrate measurement with analysis.
Companies such as Tektronix, LeCroy, and Agilent Technologies now provide PC-based oscilloscopes that run MATLAB directly on the measurement device. You can then apply MATLAB analysis and visualization capabilities directly to the data as it streams into the instrument.
This article describes an application, ScopeMath, built for Tektronix Open Windows oscilloscopes. ScopeMath provides a MATLAB interface for visualization and analysis to oscilloscope data. It enables you to easily connect to the device, select and apply analysis algorithms, and view the results. You can also use the application on other instruments or with data or image acquisition boards.
ScopeMath uses MATLAB and the Instrument Control Toolbox to create a simple measurement system that lets you:
- Create an interface for collecting and analyzing test data
- Make a standard connection to the instruments, independent of the device protocol
- Work with a variety of instruments and data-capture devices
- Run a series of measurements with minimal changes to the code
Collecting and Analyzing Data
The ScopeMath graphical user interface (GUI) built using MATLAB GUIDE (Figure 1) has the same appearance whether the application is running on the instrument or on your desktop.
The axis plot in the upper left portion of the GUI shows the data captured from the instrument. The lower left plot shows the resulting analysis on the data. A bus interface control panel for controlling I/O is in the upper right of the GUI. The analysis selection panel is shown below it. The I/O control panel lets you select the appropriate resource to communicate with the instrument without knowing whether VISA is installed using a real GPIB cable, Ethernet from an external computer, or on the instrument.
ScopeMath also provides tools for viewing and displaying the collected data—zooming, data labeling, and cursors. These tools let you study the data and explore the results using mouse button callbacks on the graph.
Connecting to the Device—Independent of Protocol
With the standard set of MATLAB commands in the Instrument Control Toolbox, ScopeMath can communicate with devices independent of protocol. The toolbox supports instrument communication protocols, such as TCP/IP, UDP, RS-232 serial, GPIB, and VISA.
Figure 1: The ScopeMath GUI showing captured data and analysis results.
Click on image to see enlarged view.
The VISA instrument driver used with Tektronix oscilloscopes can be installed on the instrument or on your desktop. If used on the instrument, it communicates directly to the instrument through its internal bus. If used off the instrument, it communicates from your desktop machine over a GPIB cable or the Ethernet to the instrument. This type of interface can help you generate the application using your desktop PC while communicating remotely with the instrument in the lab over Ethernet.When the application has been developed and tested, it can then be loaded and run directly on the instrument without changing any code.
Finding Hardware at Run-Time
Because we want ScopeMath to be used on a variety of devices without needing to program specific device information, we need ScopeMath to identify the hardware at run-time. The Instrument Control Toolbox can programmatically search for hardware that is connected to the computer at run-time.With a series of commands we can find out if VISA was installed, which vendor’s VISA implementation is installed, and which hardware resources are available through the VISA interface. You can install different vendors’ VISA implementations and set up their hardware with different resources, but you must know what they are at run-time. The following ScopeMath code
| Figure 2:
The ScopeMath bus interface control panel. Click on image to see enlarged view. |
h = instrhwinfo('visa');
installedVISA = h.InstalledAdaptors{1};
resources = instrhwinfo('visa',installedVISA);finds all the hardware connected to your computer.
The resource list now contains all the GPIB resources that VISA knows about on the system. You only need to select the instrument resource with which you want to communicate. After selecting the resource you want to use, press Connect to connect to the hardware.
Working With Various Instruments and Data Capture Devices
Because each instrument model may use a different set of commands, we need a way to send commands to the instrument that are specific to its manufacturer and model.
To do this in ScopeMath, we create a series of functions with the same base name but with different endings, depending on the make and model of the instrument.
| Instrument | Function Name |
|---|---|
| TDS 5000 | getwaveformtds5000 |
| TDS 7000 | getwaveformtds7000 |
Each function is now instrument-specific. Once you connect to
the instrument, the ScopeMath code queries the instrument and obtains
the information about its manufacturer and model and uses this
information at run-time to call the appropriate functions.
To generate the different functions for each model, we use a getwaveform
command that takes an instrument control object and, based on the
instrument with which it is communicating, calls the correct instrument-specific
command. The instrument-specific commands are either in a getwaveformdefault
or getwaveform<ModelName> function. The getwaveform command calls
the correct code for the instrument by concatenating the getwaveform
string with part of the string that identifies the model of the
instrument and evaluates the concatenated string at run-time.
The following code shows how we did this.
function [data, time] = getwaveform(obj, DataSource)
…
model = get(obj,'Name');
functionfilename = lower(['getwaveform',model]);
if(exist([functionfilename],'file')==0|isempty(model))
functionfilename = 'getwaveformdefault';
end;
[data,time,scale] = feval(functionfilename,obj,
DataSource);
The variable obj is an instrument control object that holds the
instrument model name in its Name property. DataSource holds a
string that indicates the channel you use to get the data.
Now, when we call getwaveform, we will call the function that
has the specific commands for the instrument to which we are connected.
We can also add new instruments just by adding the correct getwaveform<ModelName> function
without changing any other code.
ScopeMath can now run on a computer connected to the instrument
or directly on the instrument itself. To use ScopeMath for another
manufacturer, you just need to generate the getwaveform for your
specific instrument and place it on the MATLAB path.
| Figure 3:
The analysis list box can be expanded to include
aditional MATLAB functions. Click on image to see enlarged view. |
Running a Series of Measurements With Minimal Code Changes
Because we want to use a wide variety of customizable and extensible analysis functions, we use a framework for adding functions that is very similar to the way the MATLAB demo system works. This framework lets us add new functions without changing any code.When we start up ScopeMath, it scans a default directory for the list of available analysis functions and populates the list box with them (see below). When we select an analysis function from the list box we can evaluate the string contained in the list box and process the data. The following code sample shows how simple it is to analyze the data captured from the getwaveform command defined above.
…
flist = get(handles.AnalysisFunction,'String');
fname = flist{get(handles.AnalysisFunction,'Value')};
[x,y]=feval(fname, data, time);
…
In this case, fname in the third line of code is the function that contains the algorithm against which to evaluate the data. That function takes the array data and the array time and uses them to return the x and y values that are displayed in ScopeMath. ScopeMath is now shipping with the Tektronix OpenChoiceTM oscilloscopes and can also be downloaded from MATLAB Central.
| For more information visit: |
|


