From 38e5b8a3a9e798f76a88c271562a80e395ee7367 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Fri, 12 Nov 2010 11:45:20 -0300 Subject: [PATCH] usrp/d: cleanup and allow number of samples to be set on command line --- usrp/d.c | 42 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/usrp/d.c b/usrp/d.c index f77b769..b76b855 100644 --- a/usrp/d.c +++ b/usrp/d.c @@ -1,11 +1,12 @@ +#include #include #include -#define N 100 +#define SAMPLES_DEFAULT 100 -int main(int argc, char **argv) +static void average(int samples) { float c[2]; int n = 0; @@ -20,14 +21,45 @@ int main(int argc, char **argv) break; if (s < 0) { perror("read"); - return 1; + exit(1); } } sum += hypot(c[0], c[1]); - if (n++ % N) + if (n++ % samples) continue; - printf("%f\n", sum/N); + printf("%f\n", sum/samples); sum = 0; } +} + + +static void usage(const char *name) +{ + fprintf(stderr, +"usage: %s [samples]\n\n" +" samples samples to average over (default: %d)\n" + , name, SAMPLES_DEFAULT); + exit(1); +} + + +int main(int argc, char **argv) +{ + int n = SAMPLES_DEFAULT; + + switch (argc) { + case 1: + break; + case 2: + n = atoi(argv[1]); + if (n <= 0) + usage(*argv); + break; + default: + usage(*argv); + } + + average(n); + return 0; }