Date:2010-11-12 15:48:20 (13 years 4 months ago)
Author:Werner Almesberger
Commit:417ef7b8c16e713d53a912efa3053968a7d5881f
Message:usrp/p: cleanup and more command-line control

- usrp/p.c (find_peak, main): moved data processing to separate function
- usrp/p.c (find_peak, usage, main): new option -s skip to set the number
of samples to skip (default: 0)
- usrp/p.c (find_peak, usage, main): new option -d to dump the histogram
instead of picking the peak
- usrp/p.c (find_peak, usage, main): the percentile can now be set on the
command line (default: 0.9)
Files: usrp/p.c (4 diffs)

Change Details

usrp/p.c
11#include <stdlib.h>
22#include <stdio.h>
3#include <unistd.h>
34#include <math.h>
45#include <sys/types.h>
56
67
7#define PERC 0.9
8#define SKIP 1000000
8#define PERC_DEFAULT 0.9
99
1010
1111static int comp(const void *_a, const void *_b)
...... 
1717}
1818
1919
20int main(int argc, char **argv)
20static void find_peak(int skip, float percentile, int dump)
2121{
2222    float max = 0;
2323    float c[2], a;
2424    float *rec = NULL;
25    int e = 0, n = 0, skip = SKIP;
25    int e = 0, n = 0;
2626
2727    while (1) {
2828        size_t s;
...... 
3333                break;
3434            if (s < 0) {
3535                perror("read");
36                return 1;
36                exit(1);
3737            }
3838        }
3939        if (skip) {
...... 
5454        rec[n] = a;
5555        n++;
5656    }
57
58    if (skip >= n) {
59        fprintf(stderr, "cannot skip %d of %d entries\n", skip, n);
60        exit(1);
61    }
62    rec += skip;
63    n -= skip;
64
5765    qsort(rec, n, sizeof(float), comp);
58    printf("%f %f\n", max, rec[(int) (PERC*n)]);
59#if 0
60int i;
6166
62    for (i = 0; i < n; i += 1000)
63        printf("%f %f\n", (double) i/n, rec[i]);
64#endif
67    if (!dump)
68        printf("%f %f\n", max, rec[(int) (percentile*n)]);
69    else {
70        int i;
71
72        for (i = 0; i < n; i += 1000)
73            printf("%f %f\n", (double) i/n, rec[i]);
74    }
75}
76
77
78static void usage(const char *name)
79{
80    fprintf(stderr,
81"usage: %s [-s skip] [percentile]\n"
82" %s [-s skip] -d\n\n"
83" percentile select the specified percentile (default: %g)\n\n"
84" -d dump the histogram\n"
85" -s skip skip this number of samples from the beginning (default: 0)\n"
86    , name, name, PERC_DEFAULT);
87    exit(1);
88}
89
90
91int main(int argc, char **argv)
92{
93    int dump = 0, skip = 0;
94    float perc = PERC_DEFAULT;
95    int c;
96
97    while ((c = getopt(argc, argv, "ds:")) != EOF)
98        switch (c) {
99        case 'd':
100            dump = 1;
101            break;
102        case 's':
103            skip = atoi(optarg);
104            break;
105        default:
106            usage(*argv);
107        }
108
109    switch (argc-optind) {
110    case 0:
111        break;
112    case 1:
113        if (dump)
114            usage(*argv);
115        perc = atof(argv[optind]);
116        break;
117    default:
118        usage(*argv);
119    }
120
121    find_peak(skip, perc, dump);
122
65123    return 0;
66124}

Archive Download the corresponding diff file



interactive