From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.
We appreciate your patience as we improve our online experience.
From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.
We appreciate your patience as we improve our online experience.
09-23-2019 04:23 AM
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?
Solved! Go to Solution.
09-23-2019 03:37 PM
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 endTime = DateAdd("s", 100, startTime) 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 resultDataGroup = Data.Root.ChannelGroups.Add("Result") 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
09-24-2019 02:45 AM
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")