10-28-2015 09:24 AM
Big picture - I am finding the zero crossings on a data set so that I can calculate channel to channel phase shift in my data.
Small picture - I am starting by finding the 1st zero cross in the data, once I find that I will use the index to find the next and so on
Problem
The script posted is just looking for the 1st zero crossing then moving on to the next channel. The problem is that I can not figure out have to make my group and channel vary with for loops.
Option Explicit 'Forces the explicit declaration of all the variables in a script. Dim intCount, intChan 'loop variables Dim z For intCount = 2 to GroupCount-1 'groups Call GROUPDEFAULTSET(intCount) 'change current group For intChan = 1 to ChnNoMax 'iterate through channels Z = ChnFind("Ch(""[intCount]/[intchan]"")<0") 'This does not work, but I'm not sure how to fix it msgbox(z) next next
I'm sure I'm missing something obvious.
MK
Solved! Go to Solution.
10-29-2015 01:32 PM
Figured it out.
You have to turn the variable into a string using the str function and the fun use of &'s
change the "Z=..." line below to
Z = ChnFind("Ch(""[" & str(intCount) & "]/[" & str(intChan) & "]"")>0")
10-30-2015 07:39 AM - edited 10-30-2015 07:41 AM
Hi MK,
I prefer to use an actual variable-- I think it makes the formula much easier to read and the code much easier to debug. The following approach will work in all recent versions of DIAdem:
iMax = Data.Root.ChannelGroups.Count FOR i = 2 TO iMax-1 Set Group = Data.Root.ChannelGroups(i) FOR Each Channel In Group.Channels T1 = Channel.GetReference(eRefTypeNameName) Row = ChnFind("Ch(T1) = 0", 1) LogFileWrite Group.Name & "/" & Channel.Name & " = " & Row NEXT ' Channel NEXT ' i, Group
In DIAdem 2015 the ChnFind() function has two new parameters at the end that enable you to specify both the symbols in the formula and the channel each symbol refers to. Even more useful for your task, in DIAdem 2015 there are ChnEvent...() functions to find every instance of an event (such as a zero crossing) and return them all to you in an array from just one function call.
I hope you have access to DIAdem 2015,
Brad Turpin
DIAdem Product Support Engineer
National Instruments
10-30-2015 09:06 AM
Great, thanks Brad. This will really help simplify my code. A couple of clarification questions:
What is the advantage to using T1 instead of "dim"ing my own variable (say S1)?
Channel.GetReference(eRefTypeNameName) returns the "Group Name/ Channel Name" Is there something available that allows me to get just the Channel Name or just the group name? Group.GetReference(eRefTypeNameName) seems to work to get the group name, but I'm not sure if it is the correct way to do so.
10-30-2015 01:45 PM
I just realized the answer to my second question (Group.Name and Channel.Name) were staring me in the face...
10-31-2015 02:16 PM
Hi MK,
You can use your own variables, but you have to GlobalDim them, not Dim them. You have to use a global variable in ChnFind() and Calculate() expressions. The only advange of T1, R1, etc. is that they are automatically declared as global variables every time DIAdem launches.
You can assign T1 = Channel.Name, then the ChnFind() expression will use the first matching channel name it finds in the Data Portal. You can not just assign T1 = Group.Name, because the ChnFind() function only works on channels, not groups.
Brad Turpin
DIAdem Product Support Engineer
National Instruments
11-02-2015 08:42 AM
My reason for asking stems from the fact that I have this elsewhere in the code.
CurrChan = Data.GetChannel("[" & str(intCount) & "]/[" & str(intChan) & "]").Name Set Rising = Data.Root.ChannelGroups("Index " & str(CurrGroup) &"").Channels.Add(""& str(CurrChan) & " Rising",DataTypeChnFloat64)'Add new channel for each channel in group Set Falling = Data.Root.ChannelGroups("Index " & str(CurrGroup) &"").Channels.Add(""& str(CurrChan) & " Falling",DataTypeChnFloat64)'Add new channel for each channel in group
I use this code to grap the current channel name based on the loops iteration and convert it to a string. The group has already been created at this point and I am creating a channel called "Current Channel Rising".
I'm not sure if I could use the methods you described to simplify this.
Michael
11-03-2015 07:12 AM
Hi Michael,
You don't need global variables to simplify this, but I would definitely use object variables to simplify it. When indexing Groups or Channels, it's much easier to use the index variable with the Data.Root.ChannelGroups collect or Group.Channels collection directly. I also prefer to store the Group object and Channel object in a variable. For instance, you could then use Channel.DataType to Add exactly the same data type channel to the new group that came from the old group. You can also easily get the name, unit, and all sorts of other properties directly from the object variable. I prefer to use a separate Group object variable to reduce clutter in the colde, even though it adds 2 lines to your code snippet example.
Set FromGroup = Data.Root.ChannelGroups(intCount) Set FromChannel = FromGroup.Channels(intChan) Set ToGroup = Data.Root.ChannelGroups("Index " & FromGroup.Name) Set Rising = ToGroup.Channels.Add(FromChannel.Name & " Rising", DataTypeChnFloat64) Set Falling = ToGroup.Channels.Add(FromChannel.Name & " Falling", DataTypeChnFloat64)
Brad Turpin
DIAdem Product Support Engineer
National Instruments