IEEE 802.15.4 subsystem
Sign in or create your account | Project List | Help
IEEE 802.15.4 subsystem Git Source Tree
Root/
| 1 | #include <stdlib.h> |
| 2 | #include <stdio.h> |
| 3 | #include <math.h> |
| 4 | |
| 5 | |
| 6 | #define SAMPLES_DEFAULT 100 |
| 7 | |
| 8 | |
| 9 | static void average(int samples) |
| 10 | { |
| 11 | float c[2]; |
| 12 | int n = 0; |
| 13 | float sum = 0; |
| 14 | size_t s; |
| 15 | |
| 16 | |
| 17 | while (1) { |
| 18 | s = fread(c, sizeof(c), 1, stdin); |
| 19 | if (!s) { |
| 20 | if (!ferror(stdin)) |
| 21 | break; |
| 22 | if (s < 0) { |
| 23 | perror("read"); |
| 24 | exit(1); |
| 25 | } |
| 26 | } |
| 27 | sum += hypot(c[0], c[1]); |
| 28 | if (n++ % samples) |
| 29 | continue; |
| 30 | printf("%f\n", sum/samples); |
| 31 | sum = 0; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | |
| 36 | static void usage(const char *name) |
| 37 | { |
| 38 | fprintf(stderr, |
| 39 | "usage: %s [samples]\n\n" |
| 40 | " samples samples to average over (default: %d)\n" |
| 41 | , name, SAMPLES_DEFAULT); |
| 42 | exit(1); |
| 43 | } |
| 44 | |
| 45 | |
| 46 | int main(int argc, char **argv) |
| 47 | { |
| 48 | int n = SAMPLES_DEFAULT; |
| 49 | |
| 50 | switch (argc) { |
| 51 | case 1: |
| 52 | break; |
| 53 | case 2: |
| 54 | n = atoi(argv[1]); |
| 55 | if (n <= 0) |
| 56 | usage(*argv); |
| 57 | break; |
| 58 | default: |
| 59 | usage(*argv); |
| 60 | } |
| 61 | |
| 62 | average(n); |
| 63 | |
| 64 | return 0; |
| 65 | } |
| 66 |
