Average Two waves with code

Hello everyone,

  I very new to coding Igor pro, I am trying to take two waves and get the y average of them.

   I've tried:

Averaging 2 waves: wave0, wave1

 and I've tried:

fWaveAverage(WaveList("N_*", "", ";", ""), 1, 3, "NAve", "Nsd")

but I'm not sure how to make a WaveList, I have been coding Igor for only three days, so a lot of the functions are very new to me.

Thanks

Hi,

To get an average of two waves at some point you will need to combine them into a single wave.  

One way to do it is in two steps

concatenate the waves of interest and then take the mean.

function avg2Waves(wave1,wave2)
    wave wave1, wave2
   
    concatenate/Free {wave1,wave2}, tempwave
    return mean(tempwave)
end

In this code the function avg2waves takes two waves and concatenates to a temporary wave (using /FREE) and then returns the mean.

If you want both the mean and sd, this will take a bit more work since in general you can return only one value.

A technique to return more than one value is to make the function return a wave, but this is something for a bit more experienced programming.

In the short run if you want the sd also, I would create a second function that returns that.

function SD2Waves(wave1,wave2)
    wave wave1, wave2
   
    concatenate/Free {wave1,wave2}, tempwave
    return sqrt(variance(tempwave))
end

Andy

Andy's answer is to get the average value of both waves. I wonder if the OP wants a row-by-row average of the points in two waves (their attempt at fWaveAverage suggests so). This works for two waves.

function avg2Waves(wave1,wave2)
    wave wave1, wave2
   
    concatenate/Free {wave1,wave2}, tempwave
    MatrixOp/O resultwave = sumRows(tempwave) / numCols(tempWave)
end

To expand this for any number of waves that are the same length

function avg2Waves(wList)
    String wList // semi-colon separated list of waves
   
    Concatenate/Free wList, tempwave
    MatrixOp/O resultwave = sumRows(tempwave) / numCols(tempWave)
end

A wave list can be generated using the wavelist function and fed into avg2waves as an argument e.g. avg2Waves(wavelist("wave*",";",""))

But instead of going down this route, fWaveAverage has a nice GUI which you can get to from Analysis > Packages > Waves Average.

Hi guys,

I found this spread very useful, and I'd like to have a relating question: how would this code change if there were more than two waves (let's say around 3-10)? Is there a similar function to avg2waves which could be used the same way?

 

Thanks,
Levente