Date:2011-03-03 21:46:41 (8 years 9 months ago)
Author:Werner Almesberger
Commit:c98b91656ab1fe28a01da138044005bf7d84853a
Message:usrp/fft.c: added support for averaging of FFT results before dumping

- fft.c (usage, main): added optional averaging parameter for -d (dump)
- fft.c (do_fft): split the sample into several parts, FFT each of them
individually, and average the FFT results
Files: usrp/fft.c (5 diffs)

Change Details

usrp/fft.c
9191}
9292
9393
94static void do_fft(int skip, int dump, int low, int high, double threshold)
94static void do_fft(int skip, int dump, int low, int high, double threshold,
95    int split)
9596{
9697    float c[2];
9798    float *re = NULL, *im = NULL;
98    double *res;
99    double *out, *res;
99100    int e = 0, n = 0;
100    int i;
101    int i, j, off;
101102    double a;
102103
103104    while (1) {
...... 
138139    im += skip;
139140    n -= skip;
140141
141    res = malloc(n*sizeof(double));
142    out = malloc(n/split*sizeof(double));
143    if (!out) {
144        perror("malloc");
145        exit(1);
146    }
147
148    res = malloc(n/split*sizeof(double));
142149    if (!res) {
143150        perror("malloc");
144151        exit(1);
145152    }
146153
147    switch (alg) {
148    case 0:
149        fft_complex(n, re, im, res);
150        break;
151    case 1:
152        fft_real(n, re, im, res);
153        break;
154    default:
155        abort();
154    for (i = 0; i != n/split; i++)
155        res[i] = 0;
156
157    off = 0;
158    for (i = 0; i != split; i++) {
159        switch (alg) {
160        case 0:
161            fft_complex(n/split, re+off, im+off, out);
162            break;
163        case 1:
164            fft_real(n/split, re+off, im+off, out);
165            break;
166        default:
167            abort();
168        }
169        for (j = 0; j != n/split; j++)
170            res[j] += out[j];
171        off += n/split;
156172    }
173    for (i = 0; i != n/split; i++)
174        res[i] /= split;
157175
158176    if (dump) {
159        for (i = 0; i < n; i += 1)
177        for (i = 0; i != n/split; i++)
160178            printf("%d %g\n", i,
161                10*log(res[(i+n/2) % n])/log(10));
179                10*log(res[(i+(n/split)/2) % (n/split)])/log(10));
162180    } else {
181        /* @@@ need to think about supporting averaged FFT here later */
163182        double s = 0;
164183        double db;
165184
...... 
189208{
190209    fprintf(stderr,
191210"usage: %s [-s skip] [-w window] low high [threshold]\n"
192" %s [-s skip] [-w window] -d\n\n"
211" %s [-s skip] [-w window] -d [split]\n\n"
193212" threshold only use frequency bins with at least this power, in - dB.\n"
194213" E.g., a threshold value of 60 would be -60 dB. (default: %d\n"
195214" dB)\n"
196" -d dump frequency-domain \n"
215" -d [split] dump frequency-domain, optionally splitting the samples into\n"
216" several parts and averaging over them.\n"
197217" -s skip skip this number of samples from the beginning (default: 0)\n"
198218" -w window use the specified window function. Available: blackman, hann,\n"
199219" hamming, rectangle. Default is rectangle.\n"
...... 
240260    case 0:
241261        if (!dump)
242262            usage(*argv);
243        do_fft(skip, 1, 0, 0, 0);
263        do_fft(skip, 1, 0, 0, 0, 1);
264        break;
265    case 1:
266        if (!dump)
267            usage(*argv);
268        do_fft(skip, 1, 0, 0, 0, atoi(argv[optind]));
244269        break;
245270    case 3:
246271        threshold = -atof(argv[optind+2]);
...... 
252277        high = atoi(argv[optind+1]);
253278        if (low > high)
254279            usage(*argv);
255        do_fft(skip, 0, low, high, threshold);
280        do_fft(skip, 0, low, high, threshold, 1);
256281        break;
257282    default:
258283        usage(*argv);

Archive Download the corresponding diff file



interactive