typestar

wave_toolkit.ipf in Igor Pro

The small utility functions every Igor project ends up writing, in one file.

#pragma TextEncoding = "UTF-8"
#pragma rtGlobals = 3

// Utilities. Nothing here touches a global or leaves a wave behind.

Function/WAVE Normalize(WAVE src)
    Duplicate/FREE src, out
    WaveStats/Q out
    if (V_max == V_min)
        out = 0
        return out
    endif
    out = (out - V_min) / (V_max - V_min)
    return out
End

Function/WAVE RemoveOutliers(WAVE src, Variable sigmas)
    Duplicate/FREE src, out
    WaveStats/Q out
    Variable low = V_avg - sigmas * V_sdev
    Variable high = V_avg + sigmas * V_sdev
    out = (out < low || out > high) ? NaN : out
    return out
End

Function/WAVE MovingAverage(WAVE src, Variable window)
    Duplicate/FREE src, out
    Smooth/B window, out
    return out
End

Function Rms(WAVE src)
    Duplicate/FREE src, squared
    squared = src[p]^2
    return sqrt(sum(squared) / numpnts(squared))
End

Function/S DescribeWave(WAVE w)
    WaveStats/Q w
    String out
    sprintf out, "%s: %d points, %g to %g", NameOfWave(w), V_npnts, V_min, V_max
    return out
End

Function TestToolkit()
    Make/FREE/N=100/D probe = p
    WAVE scaled = Normalize(probe)

    WaveStats/Q scaled
    if (abs(V_min) > 1e-9 || abs(V_max - 1) > 1e-9)
        Print "Normalize is wrong"
        return -1
    endif

    Make/FREE/N=4/D flat = 3
    if (abs(Rms(flat) - 3) > 1e-9)
        Print "Rms is wrong"
        return -1
    endif

    Print "toolkit ok:", DescribeWave(probe)
    return 0
End

How it works

  1. Each one takes a wave reference and returns something, so they compose.
  2. Free waves keep the scratch work out of the experiment.
  3. The self-test at the bottom is what makes a toolkit trustworthy.

Keywords and builtins used here

The run, in numbers

Lines
63
Characters to type
1259
Tokens
276
Three-star pace
85 tpm

At the three-star pace of 85 tokens a minute, this run takes about 195 seconds.

Type this snippet

Step 1 of 1 in Encore, step 18 of 18 in Language basics.

← Previous