typestar

analysis_pipeline.ipf in Igor Pro

A whole small analysis: load, clean, fit, and report, in the order you would actually run it.

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

// A decay experiment, start to finish.

Function/WAVE MakeTestData(Variable n)
    Make/O/N=(n)/D root:raw
    WAVE raw = root:raw
    SetScale/P x, 0, 0.001, "s", raw
    raw = 0.2 + 1.5 * exp(-x / 0.05) + gnoise(0.03)
    return raw
End

Function/WAVE Clean(WAVE src)
    Duplicate/O src, $(NameOfWave(src) + "_clean")
    WAVE cleaned = $(NameOfWave(src) + "_clean")

    WaveStats/Q cleaned
    Variable cut = V_avg + 4 * V_sdev
    cleaned = cleaned > cut ? NaN : cleaned

    Smooth/B 5, cleaned
    return cleaned
End

Function FitDecay(WAVE y, Variable &tau, Variable &error)
    Make/O/D/N=3 coefs = {0.2, 1.5, 0.05}
    FuncFit/Q SingleExp, coefs, y /D

    WAVE W_sigma
    tau = coefs[2]
    error = W_sigma[2]
    return V_chisq
End

Function SingleExp(WAVE w, Variable t) : FitFunc
    return w[0] + w[1] * exp(-t / w[2])
End

Function RunPipeline()
    DFREF saved = GetDataFolderDFR()
    SetDataFolder root:

    WAVE raw = MakeTestData(2000)
    WAVE cleaned = Clean(raw)

    Variable tau, error
    Variable chisq = FitDecay(cleaned, tau, error)

    DoWindow/K DecayGraph
    Display/N=DecayGraph cleaned
    WAVE/Z fit_raw_clean
    if (WaveExists(fit_raw_clean))
        AppendToGraph fit_raw_clean
        ModifyGraph rgb(fit_raw_clean)=(65535, 16000, 16000), lsize=1.5
    endif
    Label left, "Signal (V)"
    Label bottom, "Time (s)"
    TextBox/C/N=fitInfo/A=RT "tau = " + num2str(tau) + " s"

    Printf "tau %.4g +/- %.4g s, chi squared %g\r", tau, error, chisq
    SetDataFolder saved
End

How it works

  1. Each stage is its own function, so any of them can be run alone.
  2. The pipeline keeps its work in a data folder and restores the caller's.
  3. Every wave it makes is named after the stage that made it.

Keywords and builtins used here

The run, in numbers

Lines
63
Characters to type
1419
Tokens
327
Three-star pace
90 tpm

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

Type this snippet

Step 1 of 2 in Encore, step 15 of 16 in Waves & analysis.

← Previous Next →