Code covered by the BSD License  

Highlights from
Agilent Spectrum Analyzer Seminar Examples

image thumbnail

Agilent Spectrum Analyzer Seminar Examples

by

 

11 Feb 2008 (Updated )

Agilent MXA, PSA Seminar Examples

ADV_DataToBuffer( handle, data )
function ADV_DataToBuffer( handle, data )
%ADV_DataToBuffer Stores spectrum data into the persistence buffer.
%   Use ADV_DataToBuffer to add data acquired from the spectrum
%   analyzer to the persistence data buffer.  If the buffer does
%   not contain any values, the persistence buffer will be initialized
%   using the size of the input array.

global g_ADVTraceFIFO
global g_ADVTraceFIFOSize
global g_ADVDisplayMode
global g_ADVTraceCount
global g_ADVPersistBuffer

%*--------------------------------------------*
%  Check the state of the persistence buffer.
%  If it doesn't exist, it will be created
%  using the size of the input array.  If
%  it already exists and has a different 
%  array size, it will be reinitialized using
%  the new size.
%*--------------------------------------------*
if isempty( g_ADVTraceFIFO ) 
    %*--------------------------------------------*
    %  Trace FIFO has not been initialized
    %*--------------------------------------------*
    ADV_Reset( handle );
     
elseif size( g_ADVTraceFIFO, 1 ) ~= size( data, 1 );
    %*--------------------------------------------*
    %  Trace FIFO existed, but had a different
    %  number of points
    %*--------------------------------------------*
    ADV_Reset( handle );

end

%*--------------------------------------------*
%  Append data to trace FIFO
%*--------------------------------------------*
g_ADVTraceFIFO = [g_ADVTraceFIFO data];
g_ADVTraceCount = g_ADVTraceCount + 1;

%*--------------------------------------------*
%  Calculate the data for the persistence
%  buffer.
%*--------------------------------------------*
if g_ADVDisplayMode <= 1 
    indicies = CalcPersistenceData( data );
    if isempty( indicies ) == 0
    	g_ADVPersistBuffer( indicies ) = g_ADVPersistBuffer( indicies ) + 1;
    end
end

%*--------------------------------------------*
%  Drop the last item from the FIFO
%*--------------------------------------------*
if g_ADVDisplayMode <= 1 && g_ADVTraceCount > g_ADVTraceFIFOSize
    g_ADVTraceCount = g_ADVTraceCount - 1;
    indicies = CalcPersistenceData( g_ADVTraceFIFO( :, 1 ) );
    if isempty( indicies ) == 0 
        g_ADVPersistBuffer( indicies ) = g_ADVPersistBuffer( indicies ) - 1;
    end
end

g_ADVTraceFIFO = g_ADVTraceFIFO( :, 2:(g_ADVTraceFIFOSize + 1) );
   
end % ADV_DataToBuffer

function indicies = CalcPersistenceData( data )
% CalcPersistenceData Examines the trace data and adds it to the
% persistence buffer.

global g_ADVTraceFIFO
global g_ADVPersistAmplPoints
global g_ADVSlope
global g_ADVIntercept
global g_ADVDisplayMode

% Loop through the amplitudes at each frequency and assign them 
% to an amplitude "bin"
binsToChange = [];
for freq=1:size( g_ADVTraceFIFO, 1 )
    % Calculate the amplitude bin for this point
    bin = cast( g_ADVSlope * data(freq) + g_ADVIntercept, 'uint32' );
    
    if bin >=1 && bin <= g_ADVPersistAmplPoints
        if freq == 1 
            % First point is special case.  Add to list of bins to 
            % update.
            binsToChange = bin;
      
        else
            % Calculate the actual bin to change and append to list.
            % The calculated index 'actualBin' allows us to increment all
            % of the bins in the g_ADVPersistBuffer in a single statement.
            actualBin = bin + (freq - 1) * g_ADVPersistAmplPoints;
            binsToChange = [binsToChange actualBin];
            
            % If this is the "advanced" display, connect the dots.
            if g_ADVDisplayMode == 0
                if prevBin < bin && (bin - prevBin) > 1
                    % Previous amplitude bin was less than current bin.
                    % Calculate the points required to draw a line between
                    % the two.
                    if prevBin < 1
                        actualBins = ((bin - 1):-1:1) + (freq - 1) * g_ADVPersistAmplPoints;

                    else
                        midPoint = cast( (bin + prevBin) / 2, 'uint32' );
                        actualBins = ((prevBin + 1):(midPoint + 1)) + (freq - 2) * g_ADVPersistAmplPoints;
                        actualBins = [ actualBins (((bin - 1):-1:midPoint) + (freq - 1) * g_ADVPersistAmplPoints) ];
                    end
                    binsToChange = [binsToChange actualBins];

                elseif prevBin > bin && (prevBin - bin) > 1
                    % Previous amplitude bin was greater than current bin.
                    % Calculate the points required to draw a line between
                    % the two.
                    if prevBin > g_ADVPersistAmplPoints
                        actualBins = ((bin + 1):g_ADVPersistAmplPoints) + (freq - 1) * g_ADVPersistAmplPoints;

                    else
                        midPoint = cast( (prevBin + bin) / 2, 'uint32' );
                        actualBins = (prevBin - 1):-1:(midPoint - 1) + (freq - 2) * g_ADVPersistAmplPoints;
                        actualBins = [ actualBins (((bin+1):midPoint) + (freq - 1) * g_ADVPersistAmplPoints) ];
                    end
                    binsToChange = [binsToChange actualBins];

                end
            end
        end
    end
    prevBin = bin;
end

% Update the persistence buffer
indicies = binsToChange;

end % CalcPersistenceData
    
    

Contact us