%% Configure Output Signal on Keysight(R) RF Signal Generator Using the IVI-C Driver
% This example shows how to initialize the driver, read a few properties of 
% the driver and configure output signal using Keysight Technologies RF
% Signal Generators and output the result in MATLAB(R).

%%
% Copyright 2015 The MathWorks, Inc.

%% Requirements
% This example requires the following to be installed on the computer:
%
% * Keysight IO libraries version 17.1 or newer 
% * Keysight RF Signal Generators IVI driver version 1.5.0.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 Simulated Instrument

% Create the MATLAB instrument driver
makemid('AgRfSigGen','AgRfSigGen.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('AgRfSigGen.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 'AgRfSigGen.h'
AGRFSIGGEN_VAL_LF_GENERATOR_WAVEFORM_SINE = 0;

%% Get General Device Properties
% Query information about the driver and instrument
DriverIdentification = get(myInstrument,'Inherentiviattributesdriveridentification');
InherentIviAttributesInstrumentIdentification = 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(InherentIviAttributesInstrumentIdentification, 'Instrument_Model');
FirmwareRev = get(InherentIviAttributesInstrumentIdentification, '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');

%% Output 1GHz/0dBm (1 milliwatt) Carrier Signal
fprintf('Carrier Signal:\n\t1GHz/0dBm (1 milliwatt)\n');
invoke(Utility, 'reset');
% Specifies the frequency of the generated RF output signal.    
Rf = get(myInstrument,'Rf');   
set(Rf, 'Frequency', 1E9); % 1 GHz
% Specifies the amplitude (power/level) of the RF output signal.    
set(Rf, 'Power_Level', 0); % 0 dB
% Enable RF output signal.    
set(Rf, 'Output_Enabled', true);

%% Disable AM,FM and PM Modes

% Disable amplitude modulation (AM) of the RF output signal
AnalogModulationAM = get(myInstrument,'Analogmodulationam');
set(AnalogModulationAM, 'AM_Enabled', false);
% Disable frequency modulation (FM) of the RF output signal
AnalogModulationFM = get(myInstrument,'Analogmodulationfm');
set(AnalogModulationFM, 'FM_Enabled', false);
% Disable phase modulation (PM) of the RF output signal
AnalogModulationPM = get(myInstrument,'Analogmodulationpm');
set(AnalogModulationPM, 'PM_Enabled', false);

%% Enable Amplitude Modulation (AM) Output
fprintf('Enable Amplitude Modulation (AM)\n\n');
% Get the analog modulation source name
NameBufferSize = 256;
ConfigurationLfGenerator = get(myInstrument, 'Configurationlfgenerator');
Lfg = invoke(ConfigurationLfGenerator, 'getlfgeneratorname', 1,NameBufferSize);
% Set Lfg as amplitude modulation (AM) source
set(AnalogModulationAM, 'AM_Source', Lfg);
% Set Lfg as active LF generator
LfGenerator = get(myInstrument, 'Lfgenerator');
set(LfGenerator, 'Active_LFGenerator', Lfg);
% Generate a 2 kHz sine waveform modulation signal
invoke(ConfigurationLfGenerator, 'configurelfgenerator', 2000, AGRFSIGGEN_VAL_LF_GENERATOR_WAVEFORM_SINE);
% Enable amplitude modulation (AM) mode
set(AnalogModulationAM, 'AM_Enabled', true);

%% 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 configuring output signal on a RF signal
% 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.
