05-17-2018 08:27 AM
Hi,
I am trying to calculate the time intervals between when a curve goes above and below a certain threshold value. That is to say, on an XY-plot with X representing time in milliseconds and Y the curve in question, I would like to calculate the time interval X1-X0 where X0 is the time when the Y curve goes above a certain value Yv (30 in my case) and X1 is the time when it goes below it again. All calculated interval times then have to be stored in a channel called IntervalTimes.
I imagine it to be a ChnEvent problem but I am struggling since the update to Diadem 2017 as I still haven't gotten used to the news calculator, its functions and scripts.
Help would be most appreciated.
Thanks,
Tim
Solved! Go to Solution.
05-17-2018 09:11 AM
Hi timpec,
Do you have access to the online courses ? If i remember well and if I understand what you want to do, that is the exact subject of an exercise in the DIAdem Basics course. They use the ChnFind function twice in a row to find both the events and then substract the times.
I don't have DIAdem at the moment but if you can't figure out how to do it, I'll try to send you an example by next week.
05-17-2018 09:12 AM
Option Explicit 'Forces the explicit declaration of all the variables in a script. DIM THRESHOLD: THRESHOLD = 0.01 Dim xChnl, yChnl, resChnl, resDescriptionChnl, i Set xChnl = Data.Root.ChannelGroups(1).Channels("X") Set yChnl = Data.Root.ChannelGroups(1).Channels("Y") Set resChnl = Data.Root.ChannelGroups(1).Channels.Add("IntervalTimes", DataTypeChnFloat64) Set resDescriptionChnl = Data.Root.ChannelGroups(1).Channels.Add("IntervalTimesType", DataTypeChnString) For i = 2 To yChnl.Size Step 1 If yChnl.Values(i) < THRESHOLD And yChnl.Values(i - 1) >= THRESHOLD Then ' If crossed DOWN resChnl.Values(resChnl.Size + 1) = xChnl.Values(i) resDescriptionChnl.Values(resDescriptionChnl.Size + 1) = "DOWN" ElseIf yChnl.Values(i) >= THRESHOLD And yChnl.Values(i - 1) < THRESHOLD Then ' If crossed UP resChnl.Values(resChnl.Size + 1) = xChnl.Values(i) resDescriptionChnl.Values(resDescriptionChnl.Size + 1) = "UP" End If Next
I added the description channel because you did not specify if you have to start with a down or an up crossing so if you are storing the crossing in pairs, you need a way to make sure you know if your initial crossing is above or below threshold. No error checking here either
05-17-2018 10:14 AM
Thank you. Appreciated.
05-29-2018 08:48 AM
Just in case you are interested how to use the ChnEventFunctions
The following script loads the file, finds the events, calculates the time spent in each event and then calculates a channels which allows you to display where the events were found in the data
Option Explicit 'Forces the explicit declaration of all the variables in a script.
Call LogFileDel()
Call Example_EventDuration()
Function Example_EventDuration()
Dim LowerLimit,UpperLimit,oEvents,oChnEventDisplay,K
LowerLimit = 30
UpperLimit = NULL ' Setting it to NULL makes it an open intervall
Call Data.Root.Clear()
Call DataFileLoad(CurrentScriptPath & "timeintervals.tdm")
oEvents = ChnEventDetectionWindow(Data.GetChannel("X"),Data.GetChannel("Y"),LowerLimit,UpperLimit,,,,,,,True)
LogFileWrite("Number of events found: "&ChnEventCount(oEvents))
LogFileWrite("Total time spent in events: "&str(ChnEventDuration(oEvents),"AutoAdj"))
For K = 1 To ChnEventCount(oEvents)
LogFileWrite(K&vbTab&str(ChnEventDuration(oEvents,K),"AutoAdj")&vbTab&oEvents(K-1,0)&vbTab&oEvents(K-1,1))
Next
' Just in case you want to visualize the events found:
Set oChnEventDisplay = Data.Root.ActiveChannelGroup.Channels.Add("EventsDisplay",DataTypeChnFloat64)
Call ChnEventCreateStatusChn(oChnEventDisplay,oEvents,Data.GetChannel("X"))
' You can now use the "oChnEventDisplay" channel to display background segments in VIEW
End Function