Code covered by the BSD License  

Highlights from
Agilent Spectrum Analyzer Seminar Examples

image thumbnail
from Agilent Spectrum Analyzer Seminar Examples by Richard Overdorf
Agilent MXA, PSA Seminar Examples

example8
function example8
% MATLAB/MXA example 8
% TOI automated measurement and surface plot

% Version: 1.0
% Date: Sep 11, 2006  
% 2006 Agilent Technologies, Inc.

% Clean up any unclosed instrument object

oldobjs=instrfind;
if ~isempty(oldobjs)
disp('Cleaning up ...')
delete(oldobjs);
clear oldobjs;
end 

% TOI measurement parameters
f_cent = 1e9; % center frequency
pow = -30:1:-15; % input power range
sep = logspace(5,7,7); % tone separation range
pk_threshold = -80; % peak_threshold criterium
pk_excursion = 6; % peak_excursion criterium
num_ave = 4; % number of trace averages for each measurement


n_pow = length(pow);
n_sep = length(sep);
TOI = zeros(n_sep,n_pow,2);

% Initial setup
%localhost=
mxa_ip = '192.168.100.2';
mxa_port = 5025;
esg_ip = '192.168.100.1';
esg_port = 5025;

% MXA Interface creation and connection opening
fprintf('\nConnecting to MXA ...\n');
mxa = tcpip(mxa_ip,mxa_port);

% ESG Interface creation and connection opening
fprintf('\nConnecting to ESG ...\n');
esg = tcpip(esg_ip,esg_port);


disp(' ')
disp('Connecting to MXA/ESG ...');
set(mxa,'InputBufferSize',2000);
set(mxa,'Timeout',10);
fopen(mxa)
fopen(esg)

% MXA initial setup

fprintf(mxa,[':FREQ:CENT ' num2str(f_cent)]);
fprintf(mxa,[':FREQ:SPAN ' num2str(5*sep(1))]);
fprintf(mxa,'DISP:WIND:TRAC:Y:RLEV 10');
fprintf(mxa,'INIT:IMM;*OPC?');
fscanf(mxa);
fprintf(mxa,'FORM:DATA REAL,32');
fprintf(mxa,['CALC:MARK:PEAK:THR ' num2str(pk_threshold)]);
fprintf(mxa,['CALC:MARK:PEAK:EXC ' num2str(pk_excursion)]);
fprintf(mxa,'CALC:MARK:PEAK:TABLE:STAT ON');
fprintf(mxa,['AVER:COUNT ' num2str(num_ave)]);
fprintf(mxa,'TRAC:TYPE AVER');
fprintf(mxa,'INIT:IMM;*OPC?');
fscanf(mxa);

% ESG/MXG initial setup
fprintf(esg,[':FREQ ' num2str(f_cent)]);
fprintf(esg,':POW -20');
fprintf(esg,':RADIO:MTONE:ARB:SETUP:TABLE:NTONES 2');
fprintf(esg,':RADIO:MTONE:ARB:SETUP:TABLE:FSPACING 1e5');
fprintf(esg,':RADIO:MTONE:ARB on');
fprintf(esg,':OUTPUT on');
fprintf(esg,':OUTPUT:MOD on');

fprintf('\nStarting toi measurement:');
fprintf('\nPower sweep range: %d dBm to %d dBm in %d dB steps', min(pow), max(pow), pow(2)-pow(1));

fprintf(mxa,':INIT:CONT OFF');


for x = 1:n_sep
   
    fprintf(esg,[':RADIO:MTONE:ARB:SETUP:TABLE:FSPACING ' num2str(sep(x))]);
    
    fprintf(esg,'*OPC?');
    fscanf(esg);
    
    fprintf(mxa,[':FREQ:SPAN ' num2str(5*sep(x))]);
    fprintf(mxa,'*OPC?');
    fscanf(mxa);
    fprintf(['\nFrequency separation: ' num2str(round(sep(x)/1e3)) ' kHz '])
    for y = 1:n_pow
        fprintf(esg,[':POW ' num2str(pow(y))]');
        fprintf(esg,'*OPC?');
        fscanf(esg);
        toi = toi_meas(mxa,pk_threshold,pk_excursion);
        TOI(x,y,1) = toi(1);
        TOI(x,y,2) = toi(2);
        fprintf('.')        
    end
    %pause(4)
end

fprintf(mxa,'CALC:MARK:PEAK:TABLE:STAT OFF');
fprintf(mxa,':INIT:CONT ON');


% Surface plot
fprintf('\nPlotting results ...\n')


surf(log10(sep),pow,((TOI(:,:,1)+TOI(:,:,2))/2)',...
    'FaceColor','interp',...
    'EdgeColor','k',...
    'Linewidth',.2) % plot the average of toi lower and right

colormap(jet(256))

xlabel('Tone Separation (log_{10}(f) Hz)')
ylabel('Input Power Leval (dBm)')
zlabel('TOI (dBm)')
title(['TOI vs Input Level and Tone Separation (Center = ' num2str(f_cent/1e6) ' MHz)'])
colorbar

%Disconnect an clean up
disp(' ')
disp('Disconnecting from MXA/ESG ...')
fclose(mxa)
fclose(esg)
delete([mxa esg]);
clear mxa esg


    function toi = toi_meas(mxa,pk_thr,pk_exc)

        fprintf(mxa,':INIT:IMM;*OPC?');
        fscanf(mxa);
        fprintf(mxa,[':CALC:DATA1:PEAK? ' num2str(pk_thr) ',' num2str(pk_exc) ',AMPL,ALL']);

        peak_data = binblockread(mxa,'float32');
        peak_num = peak_data(1);
        peak_amp = peak_data(2:2:end);
        peak_freq = peak_data(3:2:end);

        toi = [-999.0 -999.0];

        if peak_num <= 2 % no 3rd order products found
            return
        end

        if peak_freq(1) < peak_freq(2)
            f1 = peak_freq(1); y1 = peak_amp(1);
            f2 = peak_freq(2); y2 = peak_amp(2);
        else
            f1 = peak_freq(2); y1 = peak_amp(2);
            f2 = peak_freq(1); y2 = peak_amp(1);
        end

        peak_sep = f2-f1;

        f3_idx = find(abs(peak_freq - (f1 - peak_sep)) < peak_sep/25);
        if ~isempty(f3_idx)
            f3 = peak_freq(f3_idx(1)); y3 = peak_amp(f3_idx(1));
            toi(1) = (min(y1,y2)-y3)/2+y1;
        end

        f4_idx = find(abs(peak_freq - (f2 + peak_sep)) < peak_sep/25);
        if ~isempty(f4_idx)
            f4 = peak_freq(f4_idx(1)); y4 = peak_amp(f4_idx(1));
            toi(2) = (min(y1,y2)-y4)/2+y2;
        end
    end

end

Contact us at files@mathworks.com