Python Example 3. Pre-Emphasis

Meas. mode:
Scope
Eye
TDR
Package License:
L-RND
L-SNT

This Python example creates a pre-emphasis measurement that runs FlexDCA's oscilloscope mode. The measurement takes one source waveform as an input and runs two dependent measurements: Amplitude and Peak-to-Peak. Dependent measurements are automatically run before the script runs and the dependent measurement results are passed as variables to your script. Although not required, it is a good practice to give the XML file and script files the same or similar base filename, but using different filename extensions of course! As the number of your measurements grow, you'll appreciate being able to quickly associate the different files at a glance.

Only certain measurements can be dependent measurements. However, all measurements (dependent or otherwise) can always be started from the GUI or programatically before running your measurement script.

Procedure

  1. Place FlexDCA into Oscilloscope mode and display a single-valued waveform.
  2. Copy the following XML listing into a text editor. Save the file as PreEmphasis.xml in FlexDCA's user measurements folder (\Documents\Keysight\FlexDCA\User Measurements). Later, when this XML file is imported into the User Measurement Setup dialog, it identifies the PreEmphasis.py script, populates the dialog, and creates the user measurement button as shown in these figures.

Copy

PreEmphasis.xml

<?xml version="1.0" encoding="utf-8"?>
    <Measurement xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Script>PreEmphasis.py</Script>
    <Name>Pre-Emphasis</Name>
    <Abbreviation>PreEmp</Abbreviation>
    <!-- <Icon>MyPicture.png</Icon> -->
    <Comments>This measures the amount of pre-emphasis on a signal.</Comments>
    <MeasurementType>1 Source</MeasurementType>
    <ValidMode>Scope</ValidMode>
    <Dependent Name = "Amplitude">
        <SourceIndex>1</SourceIndex>
    </Dependent>
    <Dependent Name = "Peak-Peak">
        <SourceIndex>1</SourceIndex>
    </Dependent>
    </Measurement>
            

The <Icon> element is optional. This element allows you to place a picture on your user operator icon.
Example without <Icon> element Example with <Icon> element
  1. Copy the following script into Anaconda's Python editor, Spyder. Save the script as PreEmphasis.py in FlexDCA's user measurements folder (\Documents\Keysight\FlexDCA\User Measurements). In the following script, notice:
    • Output variables that are initialized.
    • Three input variables that are assigned to local function variables.
    • Results from the two dependent measurements (from input dictionary MeasData) thar are assigned to local variables Vpp and Vamptd.
    • Two print statements that send the dependent results to the Show Output dialog box illustrating a technique used for troubleshooting.

    The algorithm function name must be written in lower case. Naming the function Algorithm.m or ALGORITHM, for example, will break the script.

    Copy

    PreEmphasis.py

    # -*- coding: utf-8 -*-
    # Python 2.7

    import math  # Import Python's standard math library.
    # Argument "variables" is a dictionary from
    # FlexDCA that maps from variable name to value.
    def algorithm(variables): 
                                     
        # Initialize the four variables that are returned to FlexDCA.Result = 0.0
        Units = 'dB'
        Status = 'Invalid'  # Set to 'Correct' after Result is computed.ErrorMsg = ''

        # Initialize local function variables with variables from FlexDCA.
        SrcData = variables['SrcData']    # String of Y-data values of input waveform
        Source = variables['Source']      # Source of the input waveform
        MeasData = variables['MeasData']  # A dictionary of dependent measurement results
        # Do nothing if there is no SrcData.
        num_samples = len(SrcData)
        if num_samples == 0:
            return { 'ErrorMsg' : 'No data' }

        # Find dependent measurement results in MeasData: V p-p and V amptd
        i = 1
        print '\nDependent measurement results:'
        for meas in MeasData:
            print 'Meas', i, '=', meas['Name'], ',', meas['Result'], ',', meas['Source1'], ',', meas['Status']
            i = i + 1
            if (meas['Source1'] == Source):
                if (meas['Name'] == 'Peak-Peak'):
                    Vpp = meas   # Peak-Peak measurement result
                elif (meas['Name'] == 'Amplitude'):
                    Vamptd = meas   # Amplitude measurement result# Verify required measurements have been made.
        if (Vpp is None) or (Vamptd is None):
            ErrorMsg = 'Unable to retrieve "Peak-Peak" and "Amplitude" measurements.'
            return { 'ErrorMsg' : ErrorMsg }

        # Need to have status of V p-p and V amptd cary through
        if (Vpp['Status'] == 'Correct') and (Vamptd['Status'] == 'Correct'):
            Status = 'Correct'
        
        elif (Vpp['Status'] == 'Invalid') or (Vamptd['Status'] == 'Invalid'):
            ErrorMsg = 'Dependent measurements have invalid statuses.'

            # Return now before possibly setting to Questionable later.
            return { 'Status' : Status,
                     'ErrorMsg' : ErrorMsg }
        else:
            Status = 'Questionable'
            ErrorMsg = 'Dependent measurements have questionable statuses.'

        # Compute result
        Result = 20 * math.log10(Vpp['Result'] / Vamptd['Result'])

        # Return dictionary of results to FlexDCA
        return { 'Result' : Result,       # Calculated scalar result
                 'Units' : Units,         # Measurement units
                 'Status' : Status,       # Status string 'Correct', 'Questionable', or 'Invalid'
                 'ErrorMsg' : ErrorMsg }  # Error message string                            
                            

  2. In FlexDCA's User measurement tab, click the User Meas Setup button.
  3. In the dialog, select an available tab.
  4. Click Browse and select your XML configuration file, PreEmphasis.xml and close the dialog.
  5. Click on your new Pre-Emphasis measurement button and view the results in the Results panel. Notice that the results of the two dependent measurements are not listed.
  6. Return to the User Meas Setup dialog, and in the Pre-Emphasis measurement tab click the Show Output button. The output of the print statement shows information about the dependent measurements.