how to calculate the envelope curve of 4 different waves?

Dear all,

I'm trying to calculate the envelope curve for 4 different waves

Do you know how to do it?

Enclosed to this you will find the .pxp file

Thank you

Best regards

4 waves (13.21 KB)

Here is a crude function that should do the job, taking into account the fact that your wave scaling is negative.

function env4(wave1,wave2,wave3,wave4,minX,maxX,numPoints)
    Wave wave1,wave2,wave3,wave4
    Variable minX,maxX,numPoints
   
    Make/O/N=(numPoints) envWave
    Make/O/FREE/N=(4)/WAVE wp={wave1,wave2,wave3,wave4}
    Make/O/FREE/N=(4) startV,endV
   
    SetScale/I x (minX),(maxX),"", envWave
    Variable i,j,xx,dx,maxValue
   
    for(j=0;j<4;j++)
        startV[j]=DimOffset(wp[j],0)
        endV[j]=DimOffset(wp[j],0)+DimDelta(wp[j],0)*(DimSize(wp[j],0)-1)
    endfor
   
    xx=minX
    dx=DimDelta(envWave,0)
    for(i=0;i<numPoints;i+=1)
        maxValue=-1
        for(j=0;j<4;j+=1)
            wave w=wp[j]
            if(xx>=min(startV[j],endV[j]) && xx<max(startV[j],endV[j]))
                if(w(xx)>maxValue)
                    maxValue=w(xx)
                endif
            endif
        endfor
        envWave[i]=maxValue
        xx+=dx
    endfor
End

You can invoke this on the command line using:

env4('Peak 00','Peak 10','Peak 20','Peak 30',8,-1,500)

Edit:

If you are actually seeking the sum of all the waves then the code is even simpler:

function sum4Waves(wave1,wave2,wave3,wave4,minX,maxX,numPoints)
    Wave wave1,wave2,wave3,wave4
    Variable minX,maxX,numPoints
   
    Make/O/N=(numPoints) sumWave=0
    Make/O/FREE/N=(4)/WAVE wp={wave1,wave2,wave3,wave4}
    Make/O/FREE/N=(4) startV,endV
   
    SetScale/I x (minX),(maxX),"", sumWave
    Variable i,j,xx,dx,maxValue
   
    for(j=0;j<4;j++)
        startV[j]=DimOffset(wp[j],0)
        endV[j]=DimOffset(wp[j],0)+DimDelta(wp[j],0)*(DimSize(wp[j],0)-1)
    endfor
   
    xx=minX
    dx=DimDelta(sumWave,0)
    for(i=0;i<numPoints;i+=1)
        maxValue=0
        for(j=0;j<4;j+=1)
            wave w=wp[j]
            if(xx>=min(startV[j],endV[j]) && xx<max(startV[j],endV[j]))
                maxValue+=w(xx)
            endif
        endfor
        sumWave[i]=maxValue
        xx+=dx
    endfor
End

 

I hope this helps,

A.G.