# Lesson 2. Creating a Pre-Emphasis User Measurement Script

This MATLAB example creates a pre-emphasis measurement that runs in the Infiniium / MATLAB environment. The measurement takes one source waveform as an input and runs two dependent measurements. Dependent measurements are automatically run before the script. 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.

Color has been added to the following example lines of the XML and script code to make them easier to read. After you finish this lesson, continue with Lesson 3 to convert this operator into a MATLAB standalone application.

## Procedure

1. Complete Lesson 1, if you have not already done so.
2. Display a single-valued waveform.
3. Copy the following XML listing into a text editor. Name the XML file PreEmphasis.xml and save the file in Infiniium's user measurements folder (\Documents\Keysight\Infiniium\User Measurements).

- Later, when this XML file is imported into the User Measurement Setup dialog box, it identifies the PreEmphasis.m script, populates the dialog box, identifies two dependent measurements, and creates the user measurement button as shown in these figures.

HelloWorld.xml
Drag the mouse over this listing, enter Ctrl-C to copy, and paste it into your text editor.
<?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.m</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> <ValidType>Wave</ValidType> <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

4. Copy the following script into MATLAB's editor and save the script as PreEmphasis.m in Infiniium's user measurements folder (\Documents\Keysight\Infiniium\User Measurements).

PreEmphasis.m
Drag the mouse over this listing, enter Ctrl-C to copy, and paste it into the MATLAB editor.
    newline=double(sprintf('\n'));

    % These values need to be set by the script.
    % Set Status to Correct only when Result is computed.
    Result = 0.0;
    Units = 'dB';
    Status = 'Invalid';
    ErrorMsg = '';

    % Verify MeasurementData exists.
    if isempty(MeasurementData)
        ErrorMsg = ['Unable to retrieve any measurement data.' newline...
                    'Measurement Data is required, please load the XML configuration file '...
                    'rather than loading the MATLAB executable directly.'];
        return;
    end

    % Do nothing if there is no SrcData.
    if isempty(SrcData)
        ErrorMsg = 'No data';
        return;
    end

    % Variables get cleared only when a MATLAB measurement is installed.
    % So, clear dependent measurement results to prevent another MATLAB
    % measurement from creating them and confusing the script.
    clear Vpp Vamptd;

    % Iterate through MeasurementData to find V p-p and V amptd
    for i=1:length(MeasurementData)
        if strcmp(MeasurementData(i).Source1, Source)
            if strcmp(MeasurementData(i).Name, 'Peak-Peak')
                Vpp.Result = MeasurementData(i).Result;
                Vpp.Status = MeasurementData(i).Status;
                Vpp.Source = MeasurementData(i).Source1;
            elseif strcmp(MeasurementData(i).Name, 'Amplitude')
                Vamptd.Result = MeasurementData(i).Result;
                Vamptd.Status = MeasurementData(i).Status;
                Vamptd.Source = MeasurementData(i).Source1;
            end
        end
    end

    % Verify required measurements have been made.
    if (~exist('Vpp') || ~exist('Vamptd'))
        ErrorMsg = ['Unable to retrieve "Peak-Peak" and "Amplitude" measurements.' newline...
                    'Please include Dependents in your XML configuration file.'];
        return;
    end

    % Need to have status of V p-p and V amptd carry through
    if (strcmp(Vpp.Status, 'Correct') && strcmp(Vamptd.Status, 'Correct'))
        Status = 'Correct';
    elseif (strcmp(Vpp.Status, 'Invalid') || strcmp(Vamptd.Status, 'Invalid'))
        Status = 'Invalid';
        ErrorMsg = 'Dependent measurements have invalid statuses.';

        % Return now before possibly setting to Questionable later.
        return;
    else
        Status = 'Questionable';
        ErrorMsg = 'Dependent measurements have questionable statuses.';
    end

    % Compute Result.
    Result = 20 * log10(Vpp.Result/Vamptd.Result);

Do not use spaces or hyphens when selecting a filename for your script.

5. Make a Single acquisition.
6. Choose Measure > Measurements....
7. At the bottom of the Add Measurements dialog box, click User Defined Wave Measurements....
8. In the User Measurement Setup dialog box, select an available tab.
9. Click Browse and select your XML configuration file, PreEmphasis.xml and close the dialog box.
10. Add the measurement. Click on your new Pre-Emphasis measurement button.
11. In the User Meas dialog box, select the Signal and measurement Region; then, click Apply.
12. View the results in the Results panel. Notice that the results of the two dependent measurements are not listed.

You may have to wait a few seconds for the measurement to appear.