02-17-2014 06:39 AM - edited 02-17-2014 06:54 AM
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
Many thanks
02-19-2014 05:38 PM
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
02-20-2014 07:23 AM
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