AnsweredAssumed Answered

Reading spectrum analyzer plot in Matlab

Question asked by makCalgary on Nov 8, 2017
Latest reply on Nov 22, 2017 by RMALRT

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
%clear instruemnts
if ~isempty(instrument1)
clear instrument1
%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';
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');
%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';
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,':TRAC? TRACE1');
data = binblockread(e4405bsa1,'float32');
fscanf(e4405bsa1); %removes the terminator character
% Plot trace data vs sweep point index
% 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');
clear e4422bsg1;
clear e4405bsa1;


Matlab Plot: ESA spectrum captured by Matlab