Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I calculate the statistical mode from a set of values obtained from a distance sensor in an Arduino programme?

share|improve this question
    
Err... What do you mean? Do you want to set a variable (mode) according to the distance measured by a distance sensor? If so, simply use a serie of if ... else if ... statements. If this is not what you meant, then explain it better... – frarugi87 Jan 18 '16 at 16:39
2  
@frarugi87 Mode as in mathmatics/statistics. Very ambiguous question, I admit. – CharlieHanson Jan 18 '16 at 16:50
1  
@frarugi87 "Err... What do you mean?" - Ha, pun intended? – CharlieHanson Jan 18 '16 at 16:53
    
@CharlieHanson aaaaaaaaaaah.... ok, now I understood ;) sorry.. And.. The "mean" was not intended :P as for your question, I think you should first divide the values in classes (e.g. from 0 to 10cm, from 10cm (excluded) to 20cm ...), then iterate over the whole data set and fill the classes with a bunch of if statements. At the end iterate over all the frequency classes to find the maximum value – frarugi87 Jan 18 '16 at 17:00
    
I measure the distance to a point for 30 times.But there are some variations in values.I want to get more accurate value by taking statistical mode. – amrutha Jan 18 '16 at 17:28

First, two caveats: (a) The question is fairly general and might be better asked on stackoverflow, except that asking here will tend to focus answers on algorithms with low memory requirements or online methods (methods that continually compute an answer as data streams through). (b) In many cases – eg, with noisy sensors, noisy inputs, changing inputs – the statistical mode is less likely to be relevant than the statistical mean or median.

What computation method is best depends on which model of Arduino you use (because available RAM depends on model), what range of data your distance sensor returns, how many readings are taken, what you want to do with the data, whether an approximate (or nearly-correct) answer is acceptable vs an exact answer, and so forth.

Assuming you take 30 readings and store the data in an array d (that is, in d[0] ... d[29]), you could act as follows:

Set high-value and high-count variables, say hiv and hic, both to 0. For i from 0 to 29, count the number of occurrences ni of the value d[i] in the d array. Whenever ni exceeds hic, set hic to ni and hiv to d[i].

You could make this more elaborate by detecting whether hiv is already equal to d[i] and testing ni ≥ hic rather than ni > hic. This would allow detecting cases where two values are modal.

The method suggested in a comment – counting number of readings in various intervals – may compute an incorrect answer because the interval with the highest total count need not be the interval the mode is in.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.