Date:2011-03-03 20:00:28 (8 years 3 months ago)
Author:Werner Almesberger
Commit:95a9e12e2e0f539c204b8512655a1e6e93f1ef52
Message:usrp/fft.c: made window function user-selectable, added hann, blackman, rect

- fft.c (window, usage, main): added command line option -w window to
select window function
- fft.c (window_rectangle, window_hann, window_hamming, window_blackman):
increased choice of window functions
Files: usrp/fft.c (6 diffs)

Change Details

usrp/fft.c
11#include <stdlib.h>
22#include <stdio.h>
33#include <unistd.h>
4#include <string.h>
45#include <math.h>
56#include <sys/types.h>
67
...... 
1213static int alg = 0;
1314
1415
15static double window(int i, int n)
16static double window_rectangle(int i, int n)
17{
18    return 1;
19}
20
21
22static double window_hann(int i, int n)
23{
24    return 0.5-0.5*cos(M_PI*2*i/(n-1));
25}
26
27
28static double window_hamming(int i, int n)
1629{
1730    return 0.54-0.46*cos(M_PI*2*i/(n-1));
1831}
1932
2033
34static double window_blackman(int i, int n)
35{
36    return 0.42-0.5*cos(M_PI*2*i/(n-1))+0.08*cos(M_PI*4*i/(n-1));
37}
38
39
40static double (*window)(int i, int n) = window_rectangle;
41
42
2143static void fft_complex(int n, const float *re, const float *im, double *res)
2244{
2345    fftw_complex *in, *out;
...... 
3052
3153    for (i = 0; i != n; i++) {
3254        double w = window(i, n);
55
3356        in[i][0] = re[i]*w;
3457        in[i][1] = im[i]*w;
3558    }
...... 
165188static void usage(const char *name)
166189{
167190    fprintf(stderr,
168"usage: %s [-s skip] low high [threshold]\n"
169" %s [-s skip] -d\n\n"
191"usage: %s [-s skip] [-w window] low high [threshold]\n"
192" %s [-s skip] [-w window] -d\n\n"
170193" threshold only use frequency bins with at least this power, in - dB.\n"
171194" E.g., a threshold value of 60 would be -60 dB. (default: %d\n"
172195" dB)\n"
173196" -d dump frequency-domain \n"
174197" -s skip skip this number of samples from the beginning (default: 0)\n"
198" -w window use the specified window function. Available: blackman, hann,\n"
199" hamming, rectangle. Default is rectangle.\n"
175200    , name, name, -DEFAULT_THRESHOLD);
176201    exit(1);
177202}
...... 
184209    double threshold = DEFAULT_THRESHOLD;
185210    int c;
186211
187    while ((c = getopt(argc, argv, "a:ds:")) != EOF)
212    while ((c = getopt(argc, argv, "a:ds:w:")) != EOF)
188213        switch (c) {
189214        case 'a':
190215            alg = atoi(optarg);
...... 
195220        case 's':
196221            skip = atoi(optarg);
197222            break;
223        case 'w':
224            if (!strcmp(optarg, "blackman"))
225                window = window_blackman;
226            else if (!strcmp(optarg, "hann"))
227                window = window_hann;
228            else if (!strcmp(optarg, "hamming"))
229                window = window_hamming;
230            else if (!strcmp(optarg, "rectangle"))
231                window = window_rectangle;
232            else
233                usage(*argv);
234            break;
198235        default:
199236            usage(*argv);
200237        }

Archive Download the corresponding diff file



interactive