CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Counting number of samples

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
delene



Joined: 13 Oct 2003
Posts: 32

View user's profile Send private message Send e-mail

Counting number of samples
PostPosted: Fri Jun 24, 2011 8:51 am     Reply with quote

Hi..

I am reading in 200 samples of data, and want to count which value appears the most number of times in the list.
I haven't got a clue on how to start or do this, and am hoping that someone can take pity on me and tell me how.

Thanks
Delene
delene



Joined: 13 Oct 2003
Posts: 32

View user's profile Send private message Send e-mail

PostPosted: Fri Jun 24, 2011 8:53 am     Reply with quote

Just wanted to add, I can either count them as I read them or store them in an array and count them, which ever is easiest
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri Jun 24, 2011 9:31 am     Reply with quote

What is the range of values for the incoming data, i.e. what is the lowest and highest possible value?
delene



Joined: 13 Oct 2003
Posts: 32

View user's profile Send private message Send e-mail

PostPosted: Fri Jun 24, 2011 9:34 am     Reply with quote

anywhere between -19999 and + 19999
Ttelmah



Joined: 11 Mar 2010
Posts: 19496

View user's profile Send private message

PostPosted: Fri Jun 24, 2011 9:40 am     Reply with quote

Counting while reading, would depend on the size of the samples. If these are int8 values, then it is easy. Just have a 256 element int8 array, set all entries to zero before you start, and when each value arrives, increment the corresponding entry (so if a value of 23 arrives increment array[23]). When finished, look for the largest value in the array.
This gets harder for an int16 array, and basically impossible for float values. You would need to specify a 'margin' between which values were deemed to be identical, or it'll probably never have two identical values.

Have a look at:

<http://discuss.joelonsoftware.com/default.asp?interview.11.352366.6>

Which gives a reasonably efficient algorithm, which needs a little tweaking....

Try searching for 'finding the mode of an array'. This is the mathematical 'name' for what you want.

Best Wishes
delene



Joined: 13 Oct 2003
Posts: 32

View user's profile Send private message Send e-mail

PostPosted: Fri Jun 24, 2011 9:54 am     Reply with quote

Thank you Ttelmah for your help and for getting back to me so quickly.

The name of the function I am looking for is particularly helpful!

Will do as you have suggested THANK YOU
Smile
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri Jun 24, 2011 11:17 am     Reply with quote

delene wrote:
anywhere between -19999 and + 19999
This is a very wide range for 200 samples. I mean, assuming the input signal is random there will be less than 1% chance for two samples to have the same value.

You could use the banding technique proposed by Ttelmah, i.e. choosing a margin over which values are considered to be the same. But I wonder, are you really looking for the statistical mode? The value occuring most?
In most signal processing operations it is much more common to look for the median value or the average value.

What is the input signal you are processing and what goal are you trying to achieve?
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Fri Jun 24, 2011 2:01 pm     Reply with quote

The probabilities depend on what the numbers represent. If they are part numbers you could easily have 1 pipe, 2 flanges, 12 bolts, and 24 washers with the washers winning out.

I think this is a problem for a "sparse array". You have a real array of 200 that indexes into a virtual array of -19999 to + 19999. Every time you get a new sample you see if it is in the array. If it is there you increment the quantity. If it is not there then you put it in and give it a quantity of 1. It is a little slow, but I don't see any other way. It may on may not be worth periodically sorting the array.
_________________
The search for better is endless. Instead simply find very good and get the job done.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group