# Lesson 2. Creating a Butterworth User Operator Script

In this MATLAB example, you'll learn how to create a Butterworth filter operator in the Infiniium / MATLAB environment. 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. Copy the following XML listing into a text editor. Name the file Butterworth.xml and save it in Infiniium's user functions folder (\Documents\Keysight\Infiniium\User Functions). Notice how the XML <Control> elements will be used to create the User-Defined Controls shown in the following picture of the User Operator Setup dialog box.

Butterworth.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"?> <Function xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Script>Butterworth.m</Script> <Name>Butterworth</Name> <Abbreviation>Butter</Abbreviation> <Comments>Creates a Butterworth filter</Comments> <FunctionType>1 Source</FunctionType> <Control Name = "FilterType"> <Enumeration> <Default>low</Default> <Selection>low</Selection> <Selection>high</Selection> </Enumeration> </Control> <Control Name = "FilterOrder"> <Integer> <Min>1</Min> <Max>4</Max> <Units>None</Units> <Default>2</Default> </Integer> </Control> <Control Name = "FilterCutoff"> <Double> <Min>10e6</Min> <Max>1000e9</Max> <Resolution>1000</Resolution> <Units>Hertz</Units> <Default>7.5e9</Default> </Double> </Control> <Control Name = "IsPeriodic"> <Integer> <Min>0</Min> <Max>1</Max> <Units>None</Units> <Default>0</Default> </Integer> </Control> </Function> |

3. Copy the following script into MATLAB's editor. Name your script file Butterworth.m and save it in Infiniium's user measurements folder (\Documents\Keysight\Infiniium\User Functions).

Butterworth.m
Drag the mouse over this listing, enter Ctrl-C to copy, and paste it into the MATLAB editor.
    % Initialize output variables
    FiltData = [];

    % Output waveform
    Gain = 1.0;
    XUnits = 'Same';
    YUnits = 'Same';
    ErrorMsg = '';

    % Gather input data
    SrcData = SrcData; % Input waveform
    XOrg = XOrg;
    XInc = XInc;

    % Gather user-defined controls
    N = FilterOrder;
    Fc = FilterCutoff;
    FType = FilterType;
    IsPeriodic = IsPeriodic;

    % Constant output variables
    FilterDelay = N * 0.112 / Fc;
    FilterWidth = N / Fc;

    if ~isempty(SrcData)
        % Create the filter
        Wn = 2 * XInc * Fc;
        [B,A] = butter(N, Wn, FType);
        numPts = length(SrcData);
        if IsPeriodic
            % Perform the filtering circularly using ffts.
            fftSrcData = fft(SrcData);
            h = freqz(B, A, numPts, 'whole').';
            FiltData = real(ifft(fftSrcData .* h));

            % Rotate to remove FilterDelay.
            shift = round(FilterDelay / XInc);
            FiltData = circshift(FiltData, [0, -shift]);
        else
            startIndex = ceil(FilterWidth / XInc) + 1;
            if (startIndex > numPts)
                ErrorMsg = 'Too few points. Pipeline eats up entire waveform.';
            else
                % Perform the filtering
                FiltData = filter(B, A, SrcData);

                % Remove filter delay and pipeline
                FiltData = FiltData(startIndex:end);
                leadPipeline = FilterWidth - FilterDelay;
                XOrg = XOrg + leadPipeline;
            end
        end
    end

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

4. Display a single waveform on the display.
5. Click the button on the lower-right corner of the screen.
6. At the top of the Functions dialog box, select the User tab and drag a single-input User operator icon into the operator construction area as shown in the following picture.
7. Click on the user operator to open the User Operator Setup dialog box.
8. In the dialog box, click Browse and select your XML configuration file, Butterworth.xml. The dialog box is populated with settings from the XML file.
9. In the Functions dialog box, drag a Function Color to the User Operator as shown in the following picture. This will display the output waveform of the function.
10. Click Auto Scale, and the input and output waveforms should be displayed. Of course, your waveforms will be different from those shown here. The important thing is that both the input and output waveforms are displayed. Notice that the Signals legend identifies the operator's output waveform.

You may have to wait a few seconds for the waveform to appear and the waring icon () to be removed.