How to Fit a Reference Wave to a Source Wave

Sometimes, you may wish to fit a reference wave to a source wave solely by scaling (multiplying by a factor) and offsetting. Here is an example of the fitting functions needed to accomplish this task. The operations rely on duplicating the reference wave as a global. The fit_ref2src function should only be called through the do_fitref2src function or an equivalent. The function leaves wcoeff containing the scaling and offset.

Function do_fitref2src(wave source, wave reference)
    make/N=2/O/D wcoeff = {1,0}
    duplicate/O reference greference
    FuncFit/Q fit_ref2src, wcoeff, source
    killwaves/Z greference
    return 0
end
 
Function fit_ref2src(wave wc, variable xx) : FitFunc
    wave greference
    return (wc[0]*greference(xx) + wc[1])
end

While simple, I post it for the benefit both of the newcomers to Igor Pro as well as the somewhat seasoned folks such as me who tend to forget such simple things exactly when they are needed.

Actually you don't even need the iterative fitting (FuncFit) since the relation is linear, CurveFit will do it faster and does not need any initial guesses :

 

Function do_fitref2src(wave source, wave reference)
    make/N=2/O/D wcoeff
    CurveFit/Q line kwCWave=wcoeff, source /X=reference 
    return 0
end

 

Very interesting indeed. I rather need multiplying and x-offsetting most of the time, which can be easily implemented in JJ's code. But it is good to know that there are these kinds of shorthands for cases where the x offset is known to be zero.

Forum

Support

Gallery

Igor Pro 9

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More