DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

How to determine max value of the last N points in channel data?

What is the easiest way to display in DIAdem report the max value of last 10 points for each channel? I already have the maximum of the whole signal data but I want the max of the last n points.

0 Kudos
Message 1 of 8
(5,339 Views)

Hello mbr,

 

This may not be the most elegant solution, but it seems to work.

 

Some background on what I did here.

 

The "Descriptive Statistics" function in DIAdem ANALYSIS has the ability to calculate 23 characteristics for rows or channels. When you work with channels, you can select what rows of a channel are being analyzed by the function. I simply look at the length of a channel (using the variable CL for Channel Length) and subtract 10 from that value, then use that as the range of rows I want to analyze. I show the dialog settings and the result below. I my example I used the "Speed" channel from group 1 in the DataPortal (using the default DIAdem example data set).

 

Statistics.png

 

 

Option Explicit 

Dim i

For i = 1 to 23
 StatSel(i)       = "No"
 StatSel(5)       = "Yes"
Next 
Call StatBlockCalc("Channel",Str(CL("[1]/speed")-10)+"-"+Str(CL("[1]/speed")),"[1]/Speed")
msgbox(StatTxt2(5))

The For .. Next loop is used to select the statistics I want to calculate (5 = maximum), the variable StatTxt2(x) contains the result of the calculation, you can embed that into your report, or assign it to a better named variable before embedding the result into a report.

 

Please let us know if that example helped you out,

 

      Otmar

 

 

Otmar D. Foehner
0 Kudos
Message 2 of 8
(5,290 Views)

Hello Otmar,

 

your solution works perfectly, thanks for help.

 

Just need to add some kind of loop structure to run it for all channels insde all groups.

 

One more question about digits of precision in a report legend table;

 

found that digits can be limited by "d.dd" but this worked only when applying template manually @@str(CMax(#), "d.dd")@@


but not when trying by script

D2LegTxtFreeA(2) = "Max: @@str(CMax(#), "d.dd")@@"    ?

0 Kudos
Message 3 of 8
(5,256 Views)

Hello mbr,

 

I have cleaned up and extended the code with the help of Dr. Turpin, our Senior Support Engineer, to make things a little easier to read and use more up to date commands and variables that make the Script more robust:

 

Option Explicit 

Dim i, Group, Channel, RowRange
FOR i = 1 to 23
 StatSel(i) = "No"
NEXT
StatSel(5) = "Yes"
StatResChn = 0
Set Group = Data.Root.ChannelGroups(1)
'Set Group = Data.Root.ActiveChannelGroup
FOR Each Channel In Group.Channels
  RowRange = CL(Channel)-10 & "-" & CL(Channel)
  Call StatBlockCalc("Channel", RowRange, Channel)
  Channel.Properties.Add "LastMax", StatMax
NEXT ' Channel

' CL(Channel) can be replaced by Channel.Size in newer versions of DIAdem

The result of this Script is that each channel will get an additional property called "LastMax" (you can change the name of the property in the Script if you prefer something else.

 

LastMax is a floating point variable.

 

OK, so now we have an additional property for every channel in the group we analyzed (in this example group 1, or alternatively the "active" group, i.e. the one highlighted in the DataPortal.

 

To add this to the legend, I did the following steps:

 

  1. In REPORT, I added "Speed" to the 2D layout of the "EXAMPLE" template, after removing the empty table box in the upper right.
  2. I activated the "Legend" in the 2D report, and moved the legend to the upper right side
  3. I dragged the new "LastMax" property into the legend.

    REPORT 1.png
  4. I then double clicked into the legend, and changed the "AutoAdj" setting to the "d.dd" setting.
    REPORT2.png

 

Any additional curves added to the axis system will automatically be added to the legend, using the same format.

 

I hope that's what you were looking for. Let us know if that's helpful.

 

      Otmar

Otmar D. Foehner
0 Kudos
Message 4 of 8
(5,248 Views)

Hi Otmar and Brad,

 

this was absolutely helpful, thank you.

 

Still one more question about formatting legend column type. It seems that even if third column for last max values is added it is not formatted as "Free text". The first two columns are working by default but added columns are not formatted correctly. So I would like to set the third column type as "Free text"  to display these previously searched max values:

...

Call GraphObjOpen("Area : 1")
Call GraphObjOpen(D2AxisYObj(1))
D2AxisYTxt       =     "Y-SCALE"
D2AxisYTxtRelPos = "centric"
 D2AxisYSize = 1.6
 D2AxisYTxtSize = 1.6
D2LegPositionType = "SystemRelated"
D2LegPosSysRelatedX = 32
D2LegPosSysRelatedY = 98
D2LegNo = 3
D2LegTxtFreeA(2) = "Max: @@CMax(#)@@"
D2LegTxtFreeA(3) = "LastMax: @@Str(Data.GetChannel(#).Properties("LastMax").Value, "d.dd")@@"
Call GraphObjClose(D2AxisYObj(1))
Call GraphObjOpen(D2AxisXObj(1))
D2AxisXTxt       =     "X-SCALE"
D2AxisXTxtRelPos = "centric"
 D2AxisXSize = 1.6
 D2AxisXTxtSize = 1.6
D2AxisXFormat = "#hh:nn"
Call GraphObjClose(D2AxisXObj(1))
Call GraphObjClose("Area : 1")

...

 

The third column was formatted as "curve-related text" by default despite of D2LegTxtFreeA(3) so it does not show values properly..

Tried D2LegTxtType = "Free text" but this formats only the first column which I want to keep as it is by default (channel names) ?

 

Also D2LegTxtFreeA(2) returns "expected end of statement" if trying to limit digits by "d.dd" so had to format it as "Max: @@CMax(#)@@" ?

 

All these formats works fine when set manually in a dialog window but I would like to perform these formattings by script..

 

0 Kudos
Message 5 of 8
(5,236 Views)

Hello mbr,

 

I haven't used the "old" REPORT parameters in ages, as we have completely converted REPORT to be object oriented some years ago, so I'm not familiar with the old "GraphObjOpen" style parameters anymore.

 

D2LegTxtType should be the correct variable according to the help system index, so I am not quite sure why your attempt to change the type there has failed. Also, when I drag a variable from the DataPortal to the legend, it is automatically set to "Free Text".

 

Can you share the layout file (*.TDR) with me? Also, what time zone are you in, we may be able to quickly resolve this in a call rather than here in the forum. I assume you are somewhere in Europe based on the time stamp of your posts (very early morning here in TX), but between the support team in Europe and myself we should find a time slot the suits you and the NI support person that can help out.

 

You can email me at otmar DOT foehner AT ni DOT com and we can proceed from there,

 

      Otmar

Otmar D. Foehner
0 Kudos
Message 6 of 8
(5,198 Views)

pm sent

0 Kudos
Message 7 of 8
(5,192 Views)

Just in case someone visits this thread in the future, here is the code that seems to work for the application in question:

 

Call GraphObjOpen("Area : 1")
Call GraphObjOpen(D2AxisYObj(1))
D2AxisYTxt       =     "TEMPERATURE"
D2AxisYTxtRelPos = "centric"
D2AxisYSize = 1.6
D2AxisYTxtSize = 1.6
D2LegPositionType = "SystemRelated"
D2LegPosSysRelatedX = 32
D2LegPosSysRelatedY = 98

  D2LegNo = 3
  D2LegTxtTypeA(2) = "Free text"
  D2LegTxtFreeA(2) = "Max: @@CMax(#)@@"
  D2LegTxtTypeA(3) = "Free text"
  D2LegTxtFreeA(3) = "LastMax: @@Str(Data.GetChannel(#).Properties(""LastMax"").Value, ""d.dd"")@@"
Call GraphObjClose(D2AxisYObj(1))
Call GraphObjOpen(D2AxisXObj(1))
D2AxisXTxt       =     "ELAPSED TIME"
D2AxisXTxtRelPos = "centric"
D2AxisXSize = 1.6
D2AxisXTxtSize = 1.6
D2AxisXFormat = "#hh:nn"
Call GraphObjClose(D2AxisXObj(1))
Call GraphObjClose("Area : 1")

The recommendation in general is to change the code to object oriented programming, something along these lines:

Spoiler
'------------------- CurveLegendColumn ------------------------------
'Set oCurveLegendColumn = Report.ActiveSheet.Objects.Item("Area : 1").CurveLegend.Columns.Item(3)
Set oCurveLegendColumn = oCurveLegendColumns.Item(3)
oCurveLegendColumn.RelativeWidth  = 10
oCurveLegendColumn.Text           = "LastMax: @@Str(Data.GetChannel(#).Properties(""LastMax"").Value, ""d.dd"")@@"
oCurveLegendColumn.Title          = ""
oCurveLegendColumn.Type           = eLegendTextCustomText

This is to make sure the code will run in future versions of DIAdem.

 

      Otmar

Otmar D. Foehner
0 Kudos
Message 8 of 8
(5,168 Views)