"Block averaging" function?

Hi everyone,

I have actually written a simple function myself to do this, but it is rather slow and I was wondering if a function existed within Igor that would allow me to do this quickly.

I am attempting to "compress" a wave by taking the first 5 points of it, taking the average value of these points, and making this average one point in a new wave. I would then go to the next 5 points, take the average, and so on; the resulting wave is 5 times smaller than the initial. I could then make it 5 times smaller still by calling the function again using the output of the first call.

As I mentioned, it is very simple to program conceptually, but I was hoping someone might be able to point out a function that could do this and would be less computationally intensive (since I hear that many built-in Igor functions are quite optimized). I've looked through the Igor documentation and wasn't able to find anything, but it's entirely possible I missed something.

Thanks in advance.
Consider lossless compression:

ImageTransform compress myWave

A.G.
WaveMetrics, Inc.
You haven't shown us the Igor function you're using, but here's what I would use:

Function BlockAverage(inputWave, outputWave, blockSize)
    WAVE inputWave
    WAVE outputWave
    Variable blockSize
   
    Variable inputPoints = numpnts(inputWave)
    if (!WaveExists(inputWave))
        print "The input wave does not exist."
        return -1
    endif
    if (!WaveExists(outputWave))
        print "The output wave does not exist."
        return -2
    endif
    if (blocksize < 1 || blocksize > inputPoints)
        print "The block size parameter is not within an appropriate range."
        return -3
    endif
    Redimension/N=(inputPoints/blockSize) outputWave
    outputWave = sum(inputWave, pnt2x(inputWave, blockSize * p), pnt2x(inputWave, (blockSize * (p + 1)) - 1))/blockSize
    return 0
End

Put this in your procedure window:

#include <Decimation>


Then choose Analysis->Decimate.

To see what the is going on under the hood, choose Windows->Procedure Windows->Decimation.ipf. Basically, it creates a destination wave and then executes a wave assignment statement that calls the mean function.
Thanks a lot for the replies. The decimate function is interesting, but Adam's code is more what I was looking for. I tried it out and it is much faster than my own code (I was looping through the entire wave with a for loop and increasing the index by the block size each time).

Thanks again.
Hi, Can anyone help me amend the code to work for a 2D matrix? I am trying to reduce the number of columns by averaging values in each (for example) 5 columns to obtain an "average" matrix with less columns, while the number of rows remains unchanged.