11-12-2019 06:48 AM
Hi.
I'm using Diadem to display a value at a specific data point, using this:
Flow [lpm]:
@@str(Data.Root.ChannelGroups.Item(3).Channels.Item("Flow1").Values(Pno(Data.Root.ChannelGroups(3).Channels("Time\ai0"),5)),"AutoAdj")@@
@@str(Data.Root.ChannelGroups.Item(3).Channels.Item("Flow1").Values(Pno(Data.Root.ChannelGroups(3).Channels("Time\ai0"),15)),"AutoAdj")@@
@@str(Data.Root.ChannelGroups.Item(3).Channels.Item("Flow1").Values(Pno(Data.Root.ChannelGroups(3).Channels("Time\ai0"),25)),"AutoAdj")@@
@@str(Data.Root.ChannelGroups.Item(3).Channels.Item("Flow1").Values(Pno(Data.Root.ChannelGroups(3).Channels("Time\ai0"),35)),"AutoAdj")@@
@@str(Data.Root.ChannelGroups.Item(3).Channels.Item("Flow1").Values(Pno(Data.Root.ChannelGroups(3).Channels("Time\ai0"),45)),"AutoAdj")@@
@@str(Data.Root.ChannelGroups.Item(3).Channels.Item("Flow1").Values(Pno(Data.Root.ChannelGroups(3).Channels("Time\ai0"),55)),"AutoAdj")@@
@@str(Data.Root.ChannelGroups.Item(3).Channels.Item("Flow1").Values(Pno(Data.Root.ChannelGroups(3).Channels("Time\ai0"),65)),"AutoAdj")@@
@@str(Data.Root.ChannelGroups.Item(3).Channels.Item("Flow1").Values(Pno(Data.Root.ChannelGroups(3).Channels("Time\ai0"),75)),"AutoAdj")@@
@@str(Data.Root.ChannelGroups.Item(3).Channels.Item("Flow1").Values(Pno(Data.Root.ChannelGroups(3).Channels("Time\ai0"),85)),"AutoAdj")@@
@@str(Data.Root.ChannelGroups.Item(3).Channels.Item("Flow1").Values(Pno(Data.Root.ChannelGroups(3).Channels("Time\ai0"),95)),"AutoAdj")@@
@@str(Data.Root.ChannelGroups.Item(3).Channels.Item("Flow1").Values(Pno(Data.Root.ChannelGroups(3).Channels("Time\ai0"),105)),"AutoAdj")@@
But I would like to see the average of 120 data points/ samples at the 5, 15, 25, etc marker.
Can anyone please help me here 🙂
Brian
Solved! Go to Solution.
11-12-2019 09:58 AM
Here is a chunk of code I used for that.
'-------------------------------------------------------------------------------
'-- VBS script file
'-- Created on 09/28/2017 13:00:56
'-- Author: G
'-- Comment: Function to average a few data points in a channel with N data points distance among averages
'-------------------------------------------------------------------------------
Option Explicit 'Forces the explicit declaration of all the variables in a script.
DIM SUB_NAME
' AVERAGE POINTS N POINTS APART
' ARG 1: Input channel for calculating average values
' ARG 2: Name for the output channel
' ARG 3: Name for the output group
' ARG 4: Distance (in data points) between each two calculated averages
' ARG 5: Number of data points to average
' RETURN: Writes an output channel to the output group with averaged values
' taken in even separation in the input channel
' NOTES: Output group will be created if needed. Make sure input channel is long enough
Function avgEveryNpts(inputChannel, outputChannelName, outputGroupName, numOfPtsApart, numOfPtsToAvg)
SUB_NAME = "avgEveryNpts" & ": "
Call LogFileWrite(SUB_NAME & "Started exeution")
' START
On Error Resume Next ' Ignore Errors
Call LogFileWrite(SUB_NAME & inputChannel.Properties("Name").Value & " Size: " & inputChannel.Properties("length").Value) ' Check if input channel argument is not a channel
If Not Err.Number <> 0 Then ' If input channel arg is of channel object type
Dim output, inChannelSize, outGroup, i, j, sum, outCounter
i = 1
If Not Data.Root.ChannelGroups.Exists(outputGroupName) Then ' Add the outgroup if doesn't exist
Call Data.Root.ChannelGroups.Add(outputGroupName)
End If
Set outGroup = Data.Root.ChannelGroups(outputGroupName)
Call outGroup.Channels.Add(outputChannelName, DataTypeChnFloat64)
Set output = outGroup.Channels(outputChannelName)
inChannelSize = inputChannel.Properties("length").Value
If numOfPtsApart >= (numOfPtsToAvg * 2) Then ' Check appropriate numeric arguments followed by type checking
If TypeName(outputChannelName) = "String" And TypeName(outputGroupName) = "String" And (TypeName(numOfPtsApart) = "Integer" Or TypeName(numOfPtsApart) = "Long") And (TypeName(numOfPtsToAvg) = "Integer" Or TypeName(numOfPtsToAvg) = "Long") Then
If numOfPtsApart > 0 And numOfPtsToAvg > 0 Then
If inChannelSize > numOfPtsApart Then
outCounter = 1
Do While TRUE ' Endlessly loop
sum = 0
If i > inChannelSize Or i = inChannelSize Or (i + numOfPtsApart) > inChannelSize Then ' If it is the last value to calculate
For j = inChannelSize To inChannelSize - numOfPtsToAvg + 1 Step - 1
sum = sum + inputChannel.Values(j)
Next
Call output.SetValues((sum / numOfPtsToAvg), outCounter, 1, eValueBlockValueOverwrite)
Exit Do
ElseIf i = 1 Then ' If it is the first value to calculate
For j = 1 To numOfPtsToAvg Step 1
sum = sum + inputChannel.Values(j)
Next
Call output.SetValues((sum / numOfPtsToAvg), outCounter, 1, eValueBlockValueOverwrite)
Else ' Any point in the middle
For j = i - CInt(numOfPtsToAvg / 2) To (i - CInt(numOfPtsToAvg / 2)) + numOfPtsToAvg - 1 Step 1
sum = sum + inputChannel.Values(j)
Next
Call output.SetValues((sum / numOfPtsToAvg), outCounter, 1, eValueBlockValueOverwrite)
End If
i = i + numOfPtsApart
outCounter = outCounter + 1
Loop
Else
Call LogFileWrite(SUB_NAME & "the input channel size is too small for the distance argument (4th arg)... Aborting")
End If
Else
Call LogFileWrite(SUB_NAME & "numerical arguments must be above 0... Aborting")
End If
Else
Call LogFileWrite(SUB_NAME & "argument type wrong. Reqquired: 1-channel, 2-String, 3-String, 4-Integer, 5-Integer... Aborting")
End If
Else
Call LogFileWrite(SUB_NAME & "number of points to average must be at least half the distance between each point... Aborting")
End If
Else
Call LogFileWrite(SUB_NAME & "Input channel argument must be of channel object... Aborting")
End If
On Error GoTo 0
' END
Call LogFileWrite(SUB_NAME & "Ended exeution")
End Function
11-12-2019 03:26 PM
Hi Brian,
You could keep those impressive @@expressions if you like, and just point them to a new channel which has been smoothed with ChnSmooth() using a window size (SmoothWidth) of 110. That would technically show the average of 121 points, but that's pretty close to what you want and also very close to what you have.
Brad Turpin
DIAdem Product Support Engineer
National Instruments
11-20-2019 12:50 AM
Thank you for the fine solution. I'll look into it when I get more time 🙂