MATLAB Examples

Read an Infiniium waveform

The Keysight Infiniium application can save waveform data to a MATLAB® .mat file, so that you can work with it directly in MATLAB. One way to read the data is to use the MATLAB load function to import the .mat file. However, there are some additional steps that need to be taken. For example, Infiniium stores waveform data as integers (to reduce file size), and these must be converted to double precision using the other parameters in the file. The InfiniiumWaveform toolbox provides functions for automating this process, making it easy to work with Infiniium .mat files.

There are two ways to read data from an Infiniium waveform .mat file. The simplest way is to use the infiniiumWfmRead utility function. This function can read all .mat files, but for files that contain more than one waveform, it can only return one of those waveforms. The second way is to use the infiniiumWfm.InfiniiumMatFile class, which can return all waveforms, and allows for more low-level control.

Contents

Path

In order to use the InfiniiumWaveform functions and the example files in this demo, they need to be on the MATLAB path. See the instructions in 'Setting the MATLAB Path' in either the InfiniiumWaveform help documents, or the UserGuide in help\html.

Set up the files

This demo uses some files which are included in the toolbox.

inputFilename = 'channel_1_Demo.mat';     % file has one waveform
multiFilename = 'multiChannel_Demo.mat';  % file has two waveforms

Read a waveform using infiniiumWfmRead

Read basic data from a waveform file. y is a vector of waveform samples, x0 is the time value of the first sample, and dx is the sample time. The sample time is the amount of time between samples, and is the inverse of the sample rate.

[y,x0,dx] = infiniiumWfmRead(inputFilename);  %#ok<*ASGLU>

Read the waveform data, and x, which is the full vector of time samples. Plot the output.

[y,~,~,x] = infiniiumWfmRead(inputFilename);
figure(1), hold off, plot(x,y)
title(sprintf('Data from %s',inputFilename),'Interpreter','none');

Read a file that has more than one waveform.

If a .mat file has more than one waveform, infiniiumWfmRead returns only one of the waveforms, and by default, it is the first one. A warning is displayed when this happens.

[y,x0,dx,x] = infiniiumWfmRead(multiFilename);
WARNING: file contains more than one waveform, returning Channel_1

Use the infiniiumWfmInfo utility to get a list of all waveforms in this file.

wfms = infiniiumWfmInfo(multiFilename) %#ok<*NOPTS,*NASGU>
wfms =

  1×2 cell array

    'Channel_1'    'Channel_2'

Instruct infiniiumWfmRead to return the second waveform labeled 'Channel_2'

[y,x0,dx,x] = infiniiumWfmRead(multiFilename,'Source','Channel_2');

The InfiniiumMatFile class

The infiniiumWfmRead function uses the InfiniiumMatFile class to read Keysight Infiniium .mat files. Advanced users can use this class directly when dealing with files that have more than one waveform. It will return all of the waveforms in a file, and it allows for more low-level control.

Waveform data is stored in the class as an array of WaveformData objects. The WaveformData class is a simple class for holding data for a single waveform, and can do some basic tasks such as plotting the data.

Note: the InfiniiumMatFile is located in the +infiniiumWfm package directory. This prefix must be used when accessing the class and its methods, as demonstrated in the following examples. Also, the class is a handle class, which means that the class constructor returns a handle, or reference, to the object. Any function that modifies a handle object, passed as an input argument, does not need to return the object. See the MATLAB help files under Advanced Software Development for more on packages and handle versus value classes.

Read waveforms using the InfiniiumMatFile class

Initialize a class object by reading an Infiniium .mat file.

obj = infiniiumWfm.InfiniiumMatFile(multiFilename)   % create object using infiniiumWfm prefix
obj = 

  InfiniiumMatFile with properties:

              waveform: [1×2 infiniiumWfm.WaveformData]
              filename: 'multiChannel_Demo.mat'
                 frame: [1×1 struct]
          numWaveforms: 2.00000000000000e+000
    waveformSourceList: {'Channel_1'  'Channel_2'}

The frame property holds the struct containing information about the frame (scope), or application (such as MATLAB) that created the file.

obj.frame
ans = 

  struct with fields:

     Model: 'N8900A'
    Serial: 'No Serial'
      Date: '21-Sep-2017 08:53:31'

The waveform property contains the waveform data. Since there is more than one waveform, select the first one. Here you can see the raw data properties y, x0, and dx, as well as the waveform source label.

obj.waveform(1)
ans = 

  WaveformData with properties:

      source: 'Channel_1'
           y: [400×1 double]
          x0: -24.9877749341677e-009
          dx: 625.000000000000e-012
           x: [400×1 double]
           N: 400.000000000000e+000
    fileData: [1×1 struct]
    unitList: {1×43 cell}

Examine the contents of the raw file data, which is stored in the fileData property. Note that the Data field would have contained the original integer data stored in the file, but to conserve memory, that data is cleared after it's converted to double precision.

obj.waveform(1).fileData
ans = 

  struct with fields:

       NumPoints: 400.000000000000e+000
     NumSegments: 0.00000000000000e+000
    WaveformType: 'NORMAL'
     XDispOrigin: -24.9877749341677e-009
      XDispRange: 250.000000000000e-009
            XInc: 625.000000000000e-012
            XOrg: -24.9877749341677e-009
          XUnits: 'Second'
     YDispOrigin: -1.20561098332758e-003
      YDispRange: 858.997825620910e-003
            YInc: 11.6511802189981e-006
            YOrg: -1.19978539321808e-003
          YUnits: 'Volt'
            Data: []

Plot the waveform object using a class method for plotting.

figure(2), hold off, plot(obj.waveform(1));

Get the raw waveform data.

[y,x0,dx] = getData(obj.waveform(1));

Get a different waveform using the source label.

wfm = getWaveform(obj,'Channel_2');

Change the data, and give it a new source label.

wfm.y = wfm.y + .1*rand(size(wfm.y));
wfm.source = 'Channel_2_PlusNoise';
wfm
wfm = 

  WaveformData with properties:

      source: 'Channel_2_PlusNoise'
           y: [400×1 double]
          x0: -24.9877749341677e-009
          dx: 625.000000000000e-012
           x: [400×1 double]
           N: 400.000000000000e+000
    fileData: [1×1 struct]
    unitList: {1×43 cell}