Fast averaging

Hi,

I have few (7) very long waves (~ 300 000 rows each), where the rows represent time. TIme varies from 1 to 5 seconds. I'd like to average every 5 min (300 seconds) each wave. I'm having some trouble to write a piece of code for that because it seems that logic operator "IF" takes forever, or maybe the "wavestats"(?). In reality, the problem here related to my lousy programming skills :(
Also I don't know how many rows I'll have in the end (in the averaged wave). Any better suggestion of how I could speed up the averaging?
I uploaded the data (just 2 waves due to size) and the Macro I wrote.
Thanks in advance!

Macro avg2()
PauseUpdate; Silent 1  //building window...

variable i
variable j=0
make/o/n=3000 C1_5min //in this example only C1, but I could use an index k=0-6. I don't know how long the output wave will be (C1 varies between 1 and 5 seconds), so I'm just guessing something large.


do
i=0

make/o/n=3000 tmp1  /// just a temporary wave for averaging
tmp1=nan

    do
        if (root:avgs:min_numb(i)==j) /// external wave containing only the minute number. This wave contains the same lengh as the original wave C1
        tmp1(i)=root:C1(i)
        endif
       
        i=i+1
    while (i<=328163) // number of rows in the wave C1
   
wavestats/q tmp1
C1_5min(j)=V_avg


j=j+1
while (j<=3000)

EndMacro

////////////////////////
Example_C0++.txt (8.63 MB)
How about using the Resample operation with a suitable setting for /DOWN? Be sure to read the documentation carefully- it's kind of complicated. But it should do what you want, and it should do it fast.

John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
I hacked this function together which might or might not work, but if it works it should be faster. Never use good old macros for these heavy load jobs.
There are still many many problems in here (like hard-coding all names and so on), but it should do the job if a quick solution is required.

Function Average()

Variable AveragingTime = 5 //5 minutes
Wave minwave    = root:avgs:min_numb
Wave C1     = root:C1
make/o/n=3000 C1_5min = NaN

Variable i, count = 1, pos = 0, average = 0, currmin = minwave[0]
For (i = 0; i < NumPnts(minwave); i +=1)
    if (minwave[i] <= currmin + AveragingTime)
        average += C1[i]
        count +=1
    else
        C1_5min[pos] = average/count
        currmin = minwave[i]
        average = C1[i]
        count = 1
        pos +=1
    endif
Endfor
C1_5min[pos+1] = average/count
End