Fast averaging
Sayellow
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
////////////////////////
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
////////////////////////
John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
July 24, 2014 at 04:46 pm - Permalink
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.
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
July 25, 2014 at 05:04 am - Permalink
Resample
quite good. And it is really damn fast.July 25, 2014 at 06:56 am - Permalink