DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

Calculating average of specific data set

I want to calculate the average Y Value of all the data points with a y value above, and below, that of the regression curve at a specific x value (in this case x = -0.1, or thereabouts).

 

This is the code I wrote in order to try and calculate it in the most simple way but it only ever returns one data value.

 

Option Explicit  'Forces the explicit declaration of all the variables in a script.

Dim Reg_at_neg_1_G, iMaxLen, Upr_Value, Upr_Count, Lwr_Value, Lwr_Count, iLoop 

Reg_at_neg_1_G = (Chd(1,"Strg_Angle_Steering_Sensitivity_Approx")) 'returns the first value of the blue regression curve

iMaxLen = ChnPropValGet("Y_Accel", "length")

Upr_Value = 0
Upr_Count = 0
Lwr_Value = 0
Lwr_Value = 0

for iLoop = 1 to iMaxLen 'loop over the channel
  if Chd(iLoop, "Y_Accel") > -0.105 AND Chd(iLoop, "Y_Accel") < -0.095 then
    if Chd(iLoop, "Strg_Angle") > Reg_at_neg_1_G then
      Upr_Value =+ Chd(iLoop, "Strg_Angle")
      Upr_Count =+ 1
    else
      Lwr_Value =+ Chd(iLoop, "Strg_Angle")
      Lwr_Count =+ 1
    end if
  end if
next

msgboxdisp Upr_Value
msgboxdisp Upr_Count
msgboxdisp Lwr_Value
msgboxdisp Lwr_Count

 

 

I have also written the code in a different way which copies the values to a new channel if they meet the requirements and this returns 4 or 5 pieces of data for both above and below the regression curve.

 

In the interest of cutting down unnecessary data channels and learning, can anyone tell me why the above code doesn't work?

 

This is a plot of my data so you can get an idea of what I mean

 

Data.PNG

 

Many thanks

0 Kudos
Message 1 of 3
(5,193 Views)

Hi James,

 

It looks to me like your code is not giving you the results you desire because of an assignment syntax error.  You want to add all the Upr_Values together and then divide by the Upr_Count to get the overall average for the upper lobe.  Your code, though, just overwrites the previous Upr_Value with the current one and assigns the value +1 to Upr_Count each time:

      Upr_Value =+ Chd(iLoop, "Strg_Angle")
      Upr_Count =+ 1

Here's what you need instead to calculate the overall average:

      Upr_Value = Upr_Value + Chd(iLoop, "Strg_Angle")
      Upr_Count = Upr_Count + 1

If you want to get the average values for each X value below the blue line, then I recommend using the Calculate() command to create two new sets of X and Y channels, one set for the upper lobe and another set for the lower lobe.  The Calculate() command can also set all unwanted data to 0 or Null so that you can then calculate the per-X value average in each lobe with the linear approximation function.

 

If you want help with this, post or email me a sample data set,

Brad Turpin

DIAdem Product Support Engineer

National Instruments

brad.turpin@ni.com

0 Kudos
Message 2 of 3
(5,149 Views)

Of course, yeah. Was trying to be too clever and use the += which I don't even know if it exists in diadem as well as getting it the wrong way round. Was onbiously having an off day. Thanks Brad

0 Kudos
Message 3 of 3
(5,138 Views)