cancel
Showing results for
Did you mean:

Solved!
Go to solution

## Averaging data on condition

Hi, I would like to ask for help with creating averages over specified time periods. One channel contains velocity data with a frequency of 6000 Hz and another contains data on whether the probe is in position or not (returns 1 if probe is in position and returns 0 otherwise) with frequency of 10 Hz. Start time is available for both channels.

So if the position value is 1 for example between 10:00:00 and 10:00:03 I want to generate averaged value of velocity in this period and write it in a new channel. I would also like to create a channel where start time of each average is saved. How do i do that?

Message 1 of 3
(773 Views)
Solution
Accepted by topic author mjedrz

## Re: Averaging data on condition

I made an example file attached and script that fits that file.  Load the file, look at the file (simulated 10 seconds of your data), run the script, examine the results, examine the script, modify the script or ask questions.

```Option Explicit  'Forces the explicit declaration of all the variables in a script.

' Input Setup
Dim inputDataGroup, velocityChnl, probeChnl, startTime, inTimeChnl, endTime
Set inputDataGroup = Data.Root.ChannelGroups("Example Data")
Set velocityChnl = inputDataGroup.Channels("Velocity")
Set probeChnl = inputDataGroup.Channels("Probe On_Off")
startTime = inputDataGroup.Properties("Start_Time").Value
Set inTimeChnl = ChnGenTime("/TimeGenerated","millisecond",startTime,endTime,1,"StartStepNo",10000).Item(1)

' Dilute time channel to 10Hz (it's in 100Hz)
Dim x
For x = 1 To 1000 Step 1
Call inTimeChnl.RemoveValues(x, 9)
Next

' Output Setup
Dim resultDataGroup, averagesChnl, timeStartChnl, timeEndChnl
Set averagesChnl = resultDataGroup.Channels.Add("Averages Probe On", DataTypeChnFloat64)
Set timeStartChnl = resultDataGroup.Channels.Add("ProbeOn Start Times", DataTypeChnDate)
Set timeEndChnl = resultDataGroup.Channels.Add("ProbeOn End Times", DataTypeChnDate)

' Loop to gather averages/times
Dim i, j
Dim velocitySum: velocitySum = 0
Dim velocityCount: velocityCount = 0
Dim recStartTime: recStartTime = startTime
For i = 2 To probeChnl.Size Step 1
If probeChnl.Values(i) = 1 Then ' PROBE = 1
If probeChnl.Values(i - 1) = 0 Then
recStartTime = inTimeChnl.Values(i)
End If
For j = (((i - 1) * 600) + 1) To (((i - 1) * 600) + 1) + 599 Step 1
velocitySum = velocitySum + velocityChnl.Values(j)
velocityCount = velocityCount + 1
Next
Else ' PROBE = 0
If velocityCount > 0 Then
averagesChnl.Values(averagesChnl.Size + 1) = velocitySum / velocityCount
velocitySum = 0
timeStartChnl.Values(timeStartChnl.Size + 1) = recStartTime
timeEndChnl.Values(timeEndChnl.Size + 1) = inTimeChnl.Values(i - 1)
velocityCount = 0
End If
End If
Next```
Message 2 of 3
(702 Views)

## Re: Averaging data on condition

It works, thanks gsklyr!

In my version the line Set inTimeChnl = ChnGenTime("/TimeGenerated","millisecond",startTime,endTime,1,"StartStepNo",10000).Item(1) gives error but I was able to replace it by:

Call ChnGenTime("/TimeGenerated","millisecond",startTime,endTime,1,"StartStepNo",10000)
Set inTimeChnl = inputDataGroup.Channels("TimeGenerated")

Message 3 of 3
(684 Views)