%% Generate AM Waveforms on Keysight(R) 3352x Waveform Generator using the IVI-C Driver
% This example shows how to initialize the driver, read a few properties of
% the driver, generate waveforms using Agilent Technologies 3352x waveform
% generator and output the result in MATLAB(R).

%%
% Copyright 2015 The MathWorks, Inc.

%% Requirements
% This example requires the following:
%
% * Keysight IO libraries version 17.1 or newer
% * Keysight 335XX / 336XX Function / Arbitrary Waveform Generator IVI
% driver version 2.2.1.0 or newer
%

%% Enumerate Available IVI-C Drivers on the Computer
% This enumerates the IVI drivers that have been installed on the computer.
IviInfo = instrhwinfo('ivi');
IviInfo.Modules

%% Create MATLAB Instrument Driver And Connect to the Instrument

% Create the MATLAB instrument driver
makemid('Ag3352x','Ag3352x.mdd')

% Use icdevice with the MATLAB instrument driver name and instrument's
% resource name to create a device object. In this example the instrument
% is connected by GPIB at board index 0 and primary address 1.
myInstrument = icdevice('Ag3352x.mdd', 'GPIB0::01::INSTR','optionstring','simulate=true');

% Connect driver instance
connect(myInstrument);

%% Attributes and Variables Definition

% These values are defined in the driver's header file 'Ag3352x.h'
IVI_ATTR_BASE = 1000000;
IVI_SPECIFIC_ATTR_BASE = IVI_ATTR_BASE + 150000;
IVI_CLASS_ATTR_BASE = IVI_ATTR_BASE + 250000;
AG3352X_ATTR_CHANNEL_AM_MODE = IVI_SPECIFIC_ATTR_BASE + 46;
AG3352X_ATTR_AM_INTERNAL_DEPTH = IVI_CLASS_ATTR_BASE + 403;
AG3352X_ATTR_AM_INTERNAL_FREQUENCY = IVI_CLASS_ATTR_BASE + 405;
AG3352X_VAL_AM_AMPLITUDE_MODULATION_MODE_ON = 0;
AG3352X_VAL_WFM_SINE = 1;
AG3352X_VAL_AM_INTERNAL = 0;
AG3352X_VAL_AM_INTERNAL_SINE = 1;

%% Get General Device Properties
% Query information about the driver and instrument
DriverIdentification = get(myInstrument,'Inherentiviattributesdriveridentification');
InstrumentIdentification = get(myInstrument,'Inherentiviattributesinstrumentidentification');
Utility = get(myInstrument, 'Utility');
Revision = invoke(Utility, 'revisionquery');
Vendor = get(DriverIdentification, 'Specific_Driver_Vendor');
Description = get(DriverIdentification, 'Specific_Driver_Description');
InstrumentModel = get(InstrumentIdentification, 'Instrument_Model');
FirmwareRev = get(InstrumentIdentification, 'Instrument_Firmware_Revision');

% Print the queried driver properties
fprintf('Revision:        %s\n', Revision);
fprintf('Vendor:          %s\n', Vendor);
fprintf('Description:     %s\n', Description);
fprintf('InstrumentModel: %s\n', InstrumentModel);
fprintf('FirmwareRev:     %s\n', FirmwareRev);
fprintf(' \n');

%% Create AM Waveform

% Clears all event registers and error queue
DriverStatus = get(myInstrument, 'Driverstatus');
invoke(DriverStatus, 'statusclear');
%Places the instrument in a known state
Utility = get(myInstrument, 'Utility');
invoke(Utility, 'reset');
% Configures standard waveform generation
% AMPLITUDE is set to 5 V,DCOFFSET is set to 0 V,FREQUENCY is set to 1 MHz,
% STARTPHASE is set to 0 degree
ConfigurationStandardWaveform = get(myInstrument, 'Configurationstandardwaveform');
invoke(ConfigurationStandardWaveform, 'configurestandardwaveform', 'Channel1',AG3352X_VAL_WFM_SINE,5,0,1000000,0);
% Enable amplitude modulation
ConfigurationAmplitudeModulation = get(myInstrument, 'Configurationamplitudemodulation');
invoke(ConfigurationAmplitudeModulation, 'configureamenabled', 'Channel1',true);
% Configures the source of the AM modulating waveform to be internal
invoke(ConfigurationAmplitudeModulation, 'configureamsource', 'Channel1',AG3352X_VAL_AM_INTERNAL);
% Configures internal amplitude modulating waveform source
DEPTH = 50;
AM_FREQUENCY = 10000;
invoke(ConfigurationAmplitudeModulation, 'configureaminternal',DEPTH,AG3352X_VAL_AM_INTERNAL_SINE,AM_FREQUENCY);
% Enable signal output
Enabled = true;
ConfigurationGeneral = get(myInstrument, 'Configurationgeneral');
invoke(ConfigurationGeneral, 'configureoutputenabled', 'Channel1',Enabled);

%% Display Any Errors from the Driver

% If there are any errors, query the driver to retrieve and display them.
ErrorNum = 1;
while (ErrorNum ~= 0)
    [ErrorNum, ErrorMsg] = invoke(Utility, 'errorquery');
    fprintf('ErrorQuery: %d, %s\n', ErrorNum, ErrorMsg);
end

%% Disconnect Device Object and Clean Up
disconnect(myInstrument);
% Remove instrument objects from memory.
delete(myInstrument);

%%



%% Additional Information:
% This example shows the setup and generating of waveforms from a waveform
% generator using the IVI driver. Using Instrument Control Toolbox(TM), 
% it is possible to automate control of instruments, and, build test 
% systems that use MATLAB to perform analyses that may not be possible 
% using the built-in capability of the hardware.
