Average of all points in a bunch of 1D waves

Average rating
(1 vote)

Hopefully this will be useful to somebody. This function will get a bunch of 1D waves (number of points not relevant) and find the average value (mean or median) of all points in each wave and put the average into a point in a new wave.

//This function will find the average of all points for each 1D wave in a batch of waves and then put the result
//into a new wave. So n waves will give a wave n points.
//It requires a sensible root naming convention, e.g. cell1_veh_0, cell1_veh_1...
//Use "opt" to specific "mean" or "median".
Function AvgWaves(root,opt) //call example AvgWaves("cell1_veh","mean")
	String root
	String opt
 
	String rootstar=root + "*"
	String wavenames=wavelist(rootstar,";","") //gets the wavelist of batch of waves
 
	String ResultWave="W_" + opt + "_" + root	
	String LabelWave="T_" + opt + "_" + root
	Variable nwaves =ItemsInList(wavenames)
	Make /O/N=(nwaves) $ResultWave	//average values will go in this wave
	Make /O/T/N=(nwaves) $LabelWave	//wave names will go in this textwave
	Wave w0 = $ResultWave
	Wave/T wT = $LabelWave
 
	Variable i
	Variable tempvar
	String name
 
	if (stringmatch(opt,"mean")==1)
 
	For (i = 0; i < nwaves; i += 1)
		name = StringFromList(i,wavenames)
		Wave /z w1 = $name
		Duplicate /o w1 tempwave			//not required
		WaveTransform zapnans, tempwave	//not required
		tempvar=mean(tempwave)
		w0[i]=tempvar
		wT[i]=name
		Killwaves tempwave
	endfor
 
	elseif (stringmatch(opt,"median")==1)
 
	For (i = 0; i < nwaves; i += 1)
		name = StringFromList(i,wavenames)
		Wave /z w1 = $name
		Duplicate /o w1 tempwave
		WaveTransform zapnans, tempwave
		tempvar=StatsMedian(tempwave)
		w0[i]=tempvar
		wT[i]=name
		Killwaves tempwave
	endfor
 
	else	//error message
		Print "Select mean or median (in quotes) for opt"
		return 0
	Endif
End

Back to top