Our full technical support staff does not monitor this forum. If you need assistance from a member of our staff, please submit your question from the Ask a Question page.


Log in or register to post/reply in the forum.

Computing Mode (Most Frequently Occurring) In CR Basic


komalia Apr 3, 2015 04:55 PM

I have a WS600 from Lufft. It has a precipitation sensor on it and reports type of precipitation. It gives a 0 if no precipitation, 40 for undetermined precipitation, 60 for rain and 70 for snow. The WS600 is attached to a CR1000.

In my CRBasic program I query the sensor every minute for these values. I then call a table that runs every 10 minutes. In this table I would like to take the Mode (most frequently occurring) value in the measurements of precipitation type. I see no way to do this. I can take the average, the median, the standard deviation, etc., but there is no mode function. I could do a histogram of the data, but this forces me to use linearly spaced bins. Since my values can be 0, 40, 60 or 70 I have no way to bin the data in such a way to place each of these reports into a unique bin (from which I could take the max of the histogram and get the mode). Do you have any suggestions how to compute the Mode in CRBasic? I am using CRBasic 3.2. The CR1000 has the newest OS (OS 28).


Dana Apr 3, 2015 07:19 PM

I think the following will work. Please note that I haven't written the code all out -- I am attempting to provide direction on how I think it can be done. Thus, I haven't done any testing to see if it will actually work! But here's the concept:

'WS600Msmt is the variable that holds the result from the sensor
Public WS600Msmt
Public Mode(4)
Public DestMaxSpa(2)
Public WS600Mode

'count the occurrences of each mode and put the counts into
'an array
If WS600Msmt = 0 then Mode(1) = Mode(1) + 1
If WS600Msmt = 40 then Mode(2) = Mode(2) + 1
If WS600Msmt = 60 then Mode(3) = Mode(3) + 1
If WS600Msmt = 70 then Mode(4) = Mode(4) + 1

'use MaxSpa to find the maximum of the Mode array
'the second destination variable is the Location
'within the array where the Max occurs
MaxSpa (DestMaxSpa, 4, Mode(1))

'Return a mode, based on which index in Mode() holds the max
If DestMaxSpa(2)= 1 Then WS600Mode = 0
If DestMaxSpa(2)= 2 Then WS600Mode = 40
If DestMaxSpa(2)= 3 Then WS600Mode = 60
If DestMaxSpa(2)= 4 Then WS600Mode = 70

You would need to clear out all the variables used for the calculation every 10 minutes, *after* the CallTable which stores the mode.

There may be another way to accomplish this which is more efficient -- this is just off the top of my head. Let me know if it works!

Dana


JDavis Apr 6, 2015 05:20 PM

Select/Case statements are another way to do this.

Expressions with the Disable Variable parameter of the Totalize is yet another way to accomplish the goal with the data table itself. The example program in our manual for the Windsonic1 shows how to do this.


It is mostly a matter of preference of the programmer which method to use.

Log in or register to post/reply in the forum.