I am trying to learn writing automated test setup Matlab codes for keysight instruments. I wrote some simple codes for different instruments and they run perfectly. As a next step, I am trying to generate a signal from Agilent E4422b signal generator and reading it in Matlab from the Agilent E4405B spectrum analyzer. After readings several keysight/Matlab help files and white papers, I wrote the following code. But, the plot that I get in Matlab (please see below) is much different from what I see on the spectrum analyzer (a narrowband signal). Can anyone kindly tell me where my code has mistakes?
Thanks for your time.
Matlab Code:
clc; clear all; close all;
%define signal generator power level
pwr=-10;
%clear instruemnts
instrument1=instrfind
if ~isempty(instrument1)
fclose(instrument1);
delete(instrument1);
clear instrument1
end
%initialize and turn on the signal generator
fprintf('\nConnecting to Instrument E4422B...\n');
e4422bsg1 = gpib('agilent', 7, 4);
e4422bsg1.InputBufferSize = 1e8;
e4422bsg1.OutputBufferSize = 1e9;
e4422bsg1.ByteOrder = 'littleEndian';
fopen(e4422bsg1);
identity_sg1 = query(e4422bsg1, '*IDN?');
fprintf('Hello from signal generator %s', identity_sg1);
%fprintf(e4422bsg1, '*RST');
%fprintf(e4422bsg1, '*CLS');
fprintf(e4422bsg1, ':FREQuency:CW 2 GHz');
fprintf(e4422bsg1, ':POWer:LEVel:IMMediate:AMPLitude %f DBM', pwr);
fprintf(e4422bsg1, ':OUTPUT ON');
%query if the previous commands are complete
opcComp1 = sscanf(query(e4422bsg1, '*OPC?'), '%d');
while opcComp1 ~= 1
opcComp1 = sscanf(query(e4422bsg1, '*OPC?'), '%d');
end
%initialize and turn on the signal analyzer
fprintf('\nConnecting to Instrument E4405B ...\n');
e4405bsa1 = gpib('agilent', 7, 18);
e4405bsa1.Timeout = 60;
e4405bsa1.InputBufferSize = 1e8;
e4405bsa1.OutputBufferSize = 1e9;
e4405bsa1.ByteOrder = 'littleEndian';
fopen(e4405bsa1);
identity_sa1 = query(e4405bsa1, '*IDN?');
fprintf('Hello from signal analyzer %s', identity_sa1);
%fprintf(e4405bsa1, '*RST');
%fprintf(e4405bsa1, '*CLS');
fprintf(e4405bsa1,':FREQ:CENT 2 GHz');
fprintf(e4405bsa1,':FREQ:SPAN 20 MHz');
fprintf(e4405bsa1,':BAND:RES 10 KHz');
fprintf(e4405bsa1,':BAND:VIDeo 10 KHz');
% Set the data trace format to REAL, 32 bits
% Get the nr of trace points
fprintf(e4405bsa1,':FORM:DATA REAL,32');
nr_points = str2double(query(e4405bsa1,':SWE:POIN?'));
% Get the reference level
ref_lev = str2num(query(e4405bsa1,'DISP:WIND:TRAC:Y:RLEV?'));
% wait until it completes
fprintf(e4405bsa1,':INIT:IMM;*WAI')
fprintf(e4405bsa1,':TRAC? TRACE1');
data = binblockread(e4405bsa1,'float32');
fscanf(e4405bsa1); %removes the terminator character
figure(1)
% Plot trace data vs sweep point index
plot(1:nr_points,data)
% Adjust the x limits to the nr of points
% and the y limits for 100 dB of dynamic range
xlim([1 nr_points])
ylim([ref_lev-100 ref_lev])
% activate the grid lines
grid on
title('Swept SA trace')
xlabel('Point index')
ylabel('Amplitude (dBm)')
% close the object, and clear the workspace of the object
fprintf('Disconnecting from Instruments ...\n');
fclose(e4422bsg1);
fclose(e4405bsa1);
delete(e4405bsa1);
delete(e4422bsg1);
clear e4422bsg1;
clear e4405bsa1;
I was able to fix this issue by adding a data format swap statement: fprintf(e4405bsa1,':FORM:BORD SWAP');
after initializing the spectrum analyzer. SWAP and Normal mode have to do with the byte order as per the manual and swapping may not be needed in UNIX based systems.
"SWAP VS Normal: Normal mode is when the byte sequence begins with the most significant byte
(MSB) first, and ends with the least significant byte (LSB) last in the sequence:
1|2|3|4. Swapped mode is when the byte sequence begins with the LSB first, and
ends with the MSB last in the sequence: 4|3|2|1 "