Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 0:1237894bf4b5, committed 2009-11-03
- Comitter:
- simon
- Date:
- Tue Nov 03 05:46:18 2009 +0000
- Commit message:
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
| mbed.bld | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 1237894bf4b5 main.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Tue Nov 03 05:46:18 2009 +0000
@@ -0,0 +1,46 @@
+// mean and median averaging examples, sford
+
+#include "mbed.h"
+
+AnalogIn input(p20);
+
+int compare(const void *a, const void *b) {
+ return (int)(*(float*)a - *(float*)b);
+}
+
+float median(float *samples, int n) {
+ qsort(samples, n, sizeof(float), compare);
+ if(n % 2) { // odd number of samples
+ return samples[(n - 1)/2];
+ } else { // even number of samples
+ return (samples[(n/2)-1] + samples[n/2]) / 2.0;
+ }
+}
+
+float mean(float *samples, int n) {
+ float sum = 0.0;
+ for (int i=0; i<n; i++) {
+ sum += samples[i];
+ }
+ return sum / (float)n;
+}
+
+int main() {
+
+ float samples[9];
+
+ // take some samples
+ for(int i=0; i<9; i++) {
+ samples[i] = input;
+ wait(0.01);
+ }
+
+ // show the samples
+ for(int i=0; i<9; i++) {
+ printf("%f\n", samples[i]);
+ }
+
+ // show the averages
+ printf("mean = %f\n", mean(samples, 9));
+ printf("median = %f\n", median(samples, 9));
+}
diff -r 000000000000 -r 1237894bf4b5 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue Nov 03 05:46:18 2009 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/20a79241b4a0