This example shows the creation of a complex baseband (IQ) signal with MATLAB®, the download of this signal to a vector signal generator, the upconversion of the signal to an RF carrier, and the generation of the RF signal from the instrument. The download of the signal and control of the instrument uses Instrument Control Toolbox™, which supports communication with instruments through interfaces and drivers.
For a complete list of supported hardware, visit the Instrument Control Toolbox product page.
In this example we create a baseband signal and download it over a VISA TCP/IP interface to a Keysight Technologies® (formerly Agilent Technologies®) MXG Vector Signal Generator. We then control the signal generator to upconvert the signal to RF and play it back.
This example was tested with an MXG (N5182A) Signal Generator. The example uses the Signal Processing Toolbox™ to create the waveforms and the Instrument Control Toolbox to interface with the hardware using the instrument-specific Standard Commands for Programmable Instruments (SCPI) command set of the MXG. The example also requires that a supported VISA implementation be installed.
In this example we will create a chirped waveform, or linear frequency modulated (LFM) pulse. Such waveforms may be used to simulate the output of the transmitter module of a pulse-doppler RADAR.
% Define parameters of the LFM pulse. sampleRate = 1e8; % Define sample rate of baseband signal (Hz) pulseWidth = 10e-6; % Define pulse width (seconds) pulseRepititionInterval = 50e-6;% Define pulse repetition interval (seconds) bandWidth = 10e6; % Bandwidth of the pulse (Hz) IPhase = 0; % Initial phase in degrees of IComponent QPhase = 90; % Initial phase in degrees of QComponent tVector = 0:1/sampleRate:pulseWidth; % Vector to generate ideal pulse IComponent = chirp(tVector,-bandWidth/2,tVector(end), bandWidth/2,'linear',IPhase); QComponent = chirp(tVector,-bandWidth/2,tVector(end), bandWidth/2,'linear',QPhase); IQData = IComponent + 1i*QComponent; % Normalize amplitude. scale = max(max(abs(real(IQData))), max(abs(imag(IQData)))); idealPulse = IQData / scale; % Pad the pulse with zeros for the off cycle of the pulse. offPulse = 0:1/sampleRate:((pulseRepititionInterval-pulseWidth)/2); idealSpacedPulse = [offPulse idealPulse offPulse];
Plot one cycle of the LFM pulse displaying the inphase and quadrature(IQ) data, and the envelope of the pulse.
figure(1); hold on; plot((1:length(idealSpacedPulse))/sampleRate, real(idealSpacedPulse),'b') plot((1:length(idealSpacedPulse))/sampleRate, imag(idealSpacedPulse),'g') plot((1:length(idealSpacedPulse))/sampleRate, abs(idealSpacedPulse),':r','linewidth',2) title('One cycle of LFM pulse waveform') xlabel('Time (s)') ylabel('Magnitude (V)') legend('I component','Q component','Envelope') axis tight; axisLimits = axis; axis([axisLimits(1:2) 1.2*(axisLimits(3:4))])
This section defines the connection parameters and initializes the object to transfer data to the instrument.
% Define a filename for the data in the instrument. ArbFileName = 'MATLAB_WFM'; % Define IP address of the instrument. This will need to be updated to % execute this code on your instrument. MXGIPAddress = '172.31.146.123'; % Create a VISA-TCPIP object to connect to the instrument. signalGeneratorObject = visa('agilent',['TCPIP0::' MXGIPAddress '::inst0::INSTR']); % Set up the output buffer size to hold at least the number of bytes we are % transferring. Each point requires 4 bytes (since we are using UINT16 % format to transfer data to the instrument) - 2 bytes for I and 2 bytes % for Q. An additional overhead for the binblock header is also included in % the buffer. signalGeneratorObject.OutputBufferSize = 4*numel(idealSpacedPulse) + 1024; signalGeneratorObject.ByteOrder = 'bigEndian'; % Set the timeout to ensure the entire waveform is downloaded before a % timeout occurs. signalGeneratorObject.Timeout = 10.0; % Set the terminator character to line feed, or LF (i.e., ASCII code 10). signalGeneratorObject.EOSCharCode = 'LF'; % Open a connection to the instrument. fopen(signalGeneratorObject);
The baseband generator on the MXG requires the data to be in the range [0,65536]. Additionally, the I and Q values have to be interleaved before transfer to the instrument. In this section the input array is manipulated as required before downloading it to the MXG.
% Ensure the waveform is a row vector. if ~isequal(size(idealSpacedPulse,1),1) idealSpacedPulse = reshape(idealSpacedPulse,1,numel(idealSpacedPulse)); end % Separate out the real and imaginary data in the baseband signal. wave = [real(idealSpacedPulse);imag(idealSpacedPulse)]; % Transpose the waveform to interleave I & Q data. wave = wave(:)'; % Scale the waveform. Be sure to prevent division by 0. tmp = max(abs([max(wave) min(wave)])); if (tmp == 0) tmp = 1; end scale = 2^15-1; scale = scale/tmp; wave = round(wave * scale); modval = 2^16; % Convert from double to unsigned int datatype. wave = uint16(mod(modval + wave, modval));
This section sets up the instrument to receive the data and queries and displays instrument identification information.
% Empty instrument error queue and event registers and reset the instrument. fprintf(signalGeneratorObject, '*CLS'); fprintf(signalGeneratorObject, '*RST'); % Query the instrument identity and display it. instrumentInfo = query(signalGeneratorObject, '*IDN?'); fprintf('Instrument identification information: %s', instrumentInfo);
Instrument identification information: Agilent Technologies, N5182A, US00000104, A.01.62
This section downloads the waveform to the instrument and initiates signal generation. The SCPI commands used for this task can be found in the instrument programmer's manual.
% Turn off instrument before downloading waveform. fprintf(signalGeneratorObject,':OUTPut:STATe OFF'); fprintf(signalGeneratorObject,':SOURce:RADio:ARB:STATe OFF'); fprintf(signalGeneratorObject,':OUTPut:MODulation:STATe OFF'); % Set the sample rate of the ARB clock to the sample rate of the baseband % signal. fprintf(signalGeneratorObject,[':SOURce:RADio:ARB:CLOCk:SRATe ' num2str(sampleRate)]); % Write the data to the unprotected non-volatile memory of the instrument. n = size(wave); fprintf('Starting Download of %d Points...',n(2)/2) binblockwrite(signalGeneratorObject,wave,'uint16', [':MEM:DATA:UNProtected "WFM1:' ArbFileName '",']); % The signal generator expects an LF character at the end of the binaryblock. % By printing a null string with the EOS character set to LF, this byte will % be sent to the signal generator. fprintf(signalGeneratorObject,''); % Wait for instrument to complete download. % If you see a "Warning: A timeout occurred before the Terminator was reached." % warning you will need to adjust the signalGeneratorObject.Timeout value % appropriately. commandCompleted = query(signalGeneratorObject,'*OPC?'); disp('...done');
Starting Download of 5003 Points......done
Now that the waveform is downloaded onto the non-volatile memory of the instrument, set the instrument to start generating the RF signal.
% Set the carrier frequency of the upconverted signal. fprintf(signalGeneratorObject, ':SOURce:FREQuency 3000000000'); % Set the source power to -30dBm. fprintf(signalGeneratorObject, ':SOURce:POWer -30DBM'); % Select the waveform and initiate generation. fprintf(signalGeneratorObject,[':SOURce:RADio:ARB:WAV "ARBI:' ArbFileName '"']); fprintf(signalGeneratorObject,':SOURce:RADio:ARB:STATe ON'); fprintf(signalGeneratorObject,':OUTPut:MODulation:STATe ON'); fprintf(signalGeneratorObject,':OUTPut:STATe ON');
If any of the preceding SCPI commands generated an error, it would be available in the instrument error queue. Read back the error queue and display errors, if any.
% Read back and display any instrument errors. instrumentError = query(signalGeneratorObject,'SYST:ERR?'); while isempty(strfind(lower(instrumentError),'no error')) fprintf('\tINSTRUMENT ERROR: %s', instrumentError) instrumentError = query(signalGeneratorObject,'SYST:ERR?'); end
% Close the connection to the instrument and delete the object. fclose(signalGeneratorObject); delete(signalGeneratorObject); clear signalGeneratorObject
This example shows the generation of simple LFM pulsed waveforms, and download and playback using an RF Signal Generator. MATLAB can be used to define a wide variety of standard and arbitrary signals using the rich library of functions in the Signal Processing Toolbox and Communications Systems Toolbox™. Additionally, using Instrument Control Toolbox, it is possible to download these signals to instruments such as Function Generators, Arbitrary Waveform Generators and RF Signal Generators, and to automate control of these instruments.