MATLAB Examples

Using NI-FGEN Instrument Driver To Generate A Sine Wave

This example shows how to generate a sine wave on a function generator using the NI-FGEN software.

Instrument Control Toolbox™ supports communication with instruments through interfaces and drivers.

For more details on the toolbox, visit the Instrument Control Toolbox product page.



In this example, you will learn to generate a sine wave using the NI-FGEN software package version 2.7.2 or higher and a NI PXI-5402 function generator. You can also use any other function generator supported by the NI-FGEN software package version 2.7.2 or higher.

Verify NI-FGEN Installation

Use the instrhwinfo command to check if the NI-FGEN software package is installed correctly.

driverInfo = instrhwinfo ('vxipnp','niFgen');
  HardwareInfo with properties:

     Manufacturer: 'National Instruments Corp.'
            Model: 'National Instruments Function Generator'
    DriverVersion: '1.0'
    DriverDllName: 'C:\Program Files\IVI Foundation\VISA\Win64\bin\nifgen_...'

Create a MATLAB Instrument Object

Use the icdevice function to create an instrument object from the MDD, and establish a connection to the function generator using that object.

icdevice function takes two or more input arguments. The MDD file name, the resource name for the function generator and optionally, setting specific parameters.

You can get the resource name for the function generator from NI Measurement and Automation tool. For example: A resource name of PXI1Slot6 in NI MAX would be DAQ::PXI1Slot6 and Device 2 would be DAQ::2. You can remove the optionstring argument and the corresponding string parameter if you have the actual hardware.

% Specify Resource ID
resourceID = 'DAQ::PXI1Slot6';
ictObj = icdevice('niFgen',resourceID,'optionstring','Simulate=true,DriverSetup=Model:5402');

% Connect driver instance

Attributes and Variables Definition

For the purpose of this example, the function generator is configured to generate a sine wave on Channel 0 with a frequency of 10E6, amplitude of 2, DC Offset of 0 and starting phase of 0.

% These values are defined in the driver's header file 'niFgen.h'

% This value is described in the help file 'NI Signal Generators Help'
ChannelName = '0';

Configure Output Mode and Waveform

invoke(ictObj.Configurationfunctionsstandardfunctionoutput,'configurestandardwaveform',ChannelName, NIFGEN_VAL_WFM_SINE, NIFGEN_ATTR_FUNC_AMPLITUDE, NIFGEN_ATTR_FUNC_DC_OFFSET, NIFGEN_ATTR_FUNC_FREQUENCY, NIFGEN_ATTR_FUNC_START_PHASE);

Initiate the Waveform Generation

Once you configure the function generator with the required settings, use an appropriate function call to initiate the waveform.


Enable the Output

Once the waveform generation begins, enable the output of the function generator.

Enabled = 1;
invoke(ictObj.Configuration,'configureoutputenabled', ChannelName, Enabled);

Clean Up

Delete the MATLAB Instrument Object.

Note: If you delete the MATLAB Instrument Object, it will stop the waveform generation.

clear ictObj;