# Wave Folding

This small snippet allows one to efficiently fold and average a 1D wave containing periodic data, for instances, a wave of 100 points containing ten cycles of 10-point data.

The end result is a 10-point 1D wave containing the averaged results of the ten cycles originally contained by the 100-point 1D wave.

function foldWave(foldedWave, waveToFold)
wave foldedWave, waveToFold

variable numRowfW = numpnts(foldedWave)
variable numRowwTF = numpnts(waveToFold)

variable numFold = floor(numRowwTF/numRowfw)

duplicate/o/R=[0,numFold*numRowfW] waveToFold, fWMatrix
redimension/N=(numRowfW, numFold,0,0) fWMatrix

end

threadsafe static Function MeanRow (wave2D, whichRow)
wave wave2D
variable whichRow

MatrixOP/O/free w=row(wave2D,whichRow)^t
return mean(w)
End

Two quick suggestions:

1. You may want to add /FREE to your duplicate command.  This should clean out the fWMatrix which you should not really need when execution completes.

2. You could simplify

using:

MatrixOP/O/FREE aa=averageCols(fwMatrix^t)^t

A.G. Forum Support Gallery