Can't get rid of the error "Ambiguous wave point number"


I have got a problem when a call a function from another function. The function I'm calling should return a wave, but I only get the error massage "Ambiguous wave point number". I don't really understand whats wrong because the called function is a  Function/WAVE, function. Somehow I can see where the problem is so if anyone have an idea it would be much appreciated.



Call function

Function/WAVE get_BG_slope(theYwave, theXwave, from, to)
    Wave  theYwave, theXwave
        Variable from, to
        Make/O/N=(2,2) A
        Make/O/N=(2) B
        Variable i
       A[0][0] = theXwave[from]
       A[0][1] = 1
       A[1][0] = theXwave[to]
       A[1][1] = 1
       B[0] = theYwave[from]
       B[1] = theYwave[to]
        MatrixInverse/O A
        MatrixOp/O C = A x B
        Make/O/N=(numpnts(theXwave)) line
            line[i] = theXwave[i]*C[0]+C[1]
        return line

Main function

Function finalWaves(theOffWave, theOnWave, theXwave, endnumber)
    Wave theOffWave, theOnWave, theXwave
    Variable endnumber
    String finWaveName, offWNameShift
    Variable i, OffBase, OnBase
    Variable to = numpnts(theXwave)-2
    Variable from = 1
    finWaveName = "finalSiganl_"+NameOfWave(theOffWave)[strsearch(NameOfWave(theOffWave),"_",0),strlen(NameOfWave(theOffWave))]+"norm"
    offWNameShift = NameOfWave(theOffWave) + "shift"
    CurveFit/Q/H="00"/TBOX=768 line theOffWave /X=theXwave /D
    Wave W_coef
    OffBase = W_coef[0]
    CurveFit/Q/H="01"/TBOX=768 line theOnWave[0,endnumber]  /X=theXwave /D
    OnBase = W_coef[0]
    Make/O/N=(numpnts(theOnWave)) $offWNameShift
    Wave w = $offWNameShift
    w = theOnWave
    w -= (OnBase-OffBase)
    Make/O/N=(numpnts(theOnWave)) $finWaveName
    Wave ww = $finWaveName
    ww = (w-theOffWave)/theOffWave
    Make/O/N=(numpnts(theOnWave)) driftLine
    driftLine = 0
    driftLine = get_BG_slope(ww, theXwave, from, to)



I think the error is the last line of your main function. You make driftline, set it to 0 and then you need to get the wave back from your wave function and then assign it to driftline, rather than what you have written there which is giving an error because you are getting a wave rather than a point value or range of values. So I think this would work (untested)

    Make/O/N=(numpnts(theOnWave)) driftLine
    driftLine = 0
    Wave myReturnedWave = get_BG_slope(ww, theXwave, from, to)
    driftLine[] = myReturnedWave[p]

Alternatively, since your main function ends there, you could edit the /WAVE Function to make the wave named driftline rather than line and make it a regular function. Then you can just write get_BG_slope(ww,theXwave.from, to) in your main function instead of those final 3 lines.

The solution you have so far creates two output waves. line is created by get_BG_slope and driftLine is created by finalWaves. You probably want only one output wave.

A simple way to do that is to have get_BG_slope create the driftLine wave and create no wave in finalWaves. In that case, I would rename get_BG_slope with a name that better describes what it does, such as CreateBGSlopeOutputWave.

You could make CreateBGSlopeOutputWave more general by not hardcoding the name of the output wave in the function but rather by passing the output wave name from the higher level function. For example:

Function/WAVE CreateBGSlopeOutputWave(theYwave, theXwave, from, to, outputWaveName)
    String outputWaveName

    Make/O/N=(numpnts(theXwave)) $outputWaveName
    WAVE wOut = $outputWaveName // Create wave reference for output wave
        wOut[i] = theXwave[i]*C[0]+C[1]
    return wOut

Function finalWaves(theOffWave, theOnWave, theXwave, endnumber)

    WAVE wOut = CreateBGSlopeOutputWave(ww, theXwave, from, to, "driftLine")

You do not need the "WAVE wOut =" part in the second function unless you want to do something with the output wave after it is returned from CreateBGSlopeOutputWave. But leaving it there does no harm either.



Just some other thoughts to tidy up.

Is the for loop really needed instead of this?

wOut = theXwave[p]*C[0]+C[1]

Would it help to use FREE waves in place of the make/O in those cases where you don't need the waves (e.g. A and B and C).