IEEE 802.15.4 subsystem
Sign in or create your account | Project List | Help
IEEE 802.15.4 subsystem Commit Details
Date: | 2011-03-03 20:00:28 (13 years 24 days 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 | ||
---|---|---|
1 | 1 | #include <stdlib.h> |
2 | 2 | #include <stdio.h> |
3 | 3 | #include <unistd.h> |
4 | #include <string.h> | |
4 | 5 | #include <math.h> |
5 | 6 | #include <sys/types.h> |
6 | 7 | |
... | ... | |
12 | 13 | static int alg = 0; |
13 | 14 | |
14 | 15 | |
15 | static double window(int i, int n) | |
16 | static double window_rectangle(int i, int n) | |
17 | { | |
18 | return 1; | |
19 | } | |
20 | ||
21 | ||
22 | static double window_hann(int i, int n) | |
23 | { | |
24 | return 0.5-0.5*cos(M_PI*2*i/(n-1)); | |
25 | } | |
26 | ||
27 | ||
28 | static double window_hamming(int i, int n) | |
16 | 29 | { |
17 | 30 | return 0.54-0.46*cos(M_PI*2*i/(n-1)); |
18 | 31 | } |
19 | 32 | |
20 | 33 | |
34 | static 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 | ||
40 | static double (*window)(int i, int n) = window_rectangle; | |
41 | ||
42 | ||
21 | 43 | static void fft_complex(int n, const float *re, const float *im, double *res) |
22 | 44 | { |
23 | 45 | fftw_complex *in, *out; |
... | ... | |
30 | 52 | |
31 | 53 | for (i = 0; i != n; i++) { |
32 | 54 | double w = window(i, n); |
55 | ||
33 | 56 | in[i][0] = re[i]*w; |
34 | 57 | in[i][1] = im[i]*w; |
35 | 58 | } |
... | ... | |
165 | 188 | static void usage(const char *name) |
166 | 189 | { |
167 | 190 | 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" | |
170 | 193 | " threshold only use frequency bins with at least this power, in - dB.\n" |
171 | 194 | " E.g., a threshold value of 60 would be -60 dB. (default: %d\n" |
172 | 195 | " dB)\n" |
173 | 196 | " -d dump frequency-domain \n" |
174 | 197 | " -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" | |
175 | 200 | , name, name, -DEFAULT_THRESHOLD); |
176 | 201 | exit(1); |
177 | 202 | } |
... | ... | |
184 | 209 | double threshold = DEFAULT_THRESHOLD; |
185 | 210 | int c; |
186 | 211 | |
187 | while ((c = getopt(argc, argv, "a:ds:")) != EOF) | |
212 | while ((c = getopt(argc, argv, "a:ds:w:")) != EOF) | |
188 | 213 | switch (c) { |
189 | 214 | case 'a': |
190 | 215 | alg = atoi(optarg); |
... | ... | |
195 | 220 | case 's': |
196 | 221 | skip = atoi(optarg); |
197 | 222 | 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; | |
198 | 235 | default: |
199 | 236 | usage(*argv); |
200 | 237 | } |