Change wideband FFT bin filter, clean up code a bit.

This commit is contained in:
Jared Boone 2015-09-02 11:29:38 -07:00
parent c4de9c4423
commit e2bb77d756

View File

@ -31,136 +31,144 @@ using namespace lpc43xx;
#include <array> #include <array>
// numpy.array(numpy.round(scipy.signal.windows.blackmanharris(1024) * 32767), dtype=numpy.int16) /*
static constexpr std::array<int16_t, 1024> window_blackmanharris_1024 { { Employ window-presum technique with a window constructed to act like a bin filter.
2, 2, 2, 2, 2, 2, 3, 3, See: http://www.embedded.com/design/real-time-and-performance/4007611/DSP-Tricks-Building-a-practical-spectrum-analyzer
3, 3, 4, 4, 5, 5, 5, 6,
7, 7, 8, 8, 9, 10, 11, 12, import scipy.signal
13, 14, 15, 16, 17, 18, 19, 20,
22, 23, 24, 26, 27, 29, 31, 32, d = scipy.signal.remez(1024, (0.0, 32000, 48000, 10000000), (1, 0), Hz=20000000)
34, 36, 38, 40, 42, 44, 46, 48, for n in range(0, len(d), 8): print(' '.join(['%5d,' % int(round(v * 32768 * 128)) for v in d[n:n+8]]))
51, 53, 56, 58, 61, 64, 67, 70, */
73, 76, 79, 82, 86, 89, 93, 97, static constexpr std::array<int16_t, 1024> window_wideband_bin_lpf { {
100, 104, 109, 113, 117, 122, 126, 131, 0, -3409, -3284, -3358, -3235, -3312, -3187, -3270,
136, 141, 146, 151, 156, 162, 168, 173, -3143, -3233, -3100, -3202, -3057, -3186, -3009, -3261,
179, 186, 192, 198, 205, 212, 219, 226, -2777, -2904, -3087, -3076, -3058, -3069, -3013, -3037,
233, 241, 249, 256, 265, 273, 281, 290, -2969, -3004, -2931, -2976, -2898, -2956, -2870, -2957,
299, 308, 317, 327, 337, 347, 357, 367, -2871, -2830, -2866, -2914, -2874, -2919, -2864, -2911,
378, 389, 400, 411, 423, 435, 447, 459, -2854, -2905, -2849, -2905, -2851, -2910, -2858, -2917,
472, 485, 498, 512, 526, 540, 554, 569, -2893, -2901, -2866, -2949, -2901, -2970, -2914, -2979,
584, 599, 614, 630, 646, 663, 680, 697, -2923, -2987, -2932, -2997, -2945, -3009, -2960, -3020,
714, 732, 750, 769, 788, 807, 827, 846, -2986, -3043, -2971, -3059, -3005, -3082, -3023, -3094,
867, 887, 908, 930, 952, 974, 996, 1019, -3035, -3105, -3048, -3119, -3064, -3135, -3083, -3153,
1043, 1066, 1091, 1115, 1140, 1166, 1192, 1218, -3102, -3186, -3111, -3193, -3141, -3221, -3166, -3242,
1245, 1272, 1299, 1327, 1356, 1385, 1414, 1444, -3186, -3260, -3204, -3280, -3225, -3302, -3249, -3327,
1475, 1506, 1537, 1569, 1601, 1634, 1667, 1701, -3269, -3358, -3296, -3367, -3318, -3395, -3344, -3418,
1735, 1770, 1805, 1841, 1878, 1915, 1952, 1990, -3364, -3436, -3381, -3455, -3400, -3474, -3420, -3495,
2029, 2068, 2107, 2147, 2188, 2229, 2271, 2314, -3437, -3515, -3464, -3527, -3475, -3546, -3494, -3564,
2357, 2400, 2444, 2489, 2534, 2580, 2627, 2674, -3510, -3578, -3523, -3591, -3535, -3604, -3549, -3618,
2721, 2770, 2819, 2868, 2918, 2969, 3020, 3072, -3563, -3628, -3581, -3641, -3585, -3650, -3596, -3661,
3125, 3178, 3232, 3287, 3342, 3398, 3454, 3511, -3605, -3668, -3610, -3672, -3615, -3676, -3619, -3680,
3569, 3628, 3687, 3746, 3807, 3868, 3930, 3992, -3624, -3680, -3626, -3686, -3622, -3680, -3619, -3677,
4055, 4119, 4183, 4249, 4314, 4381, 4448, 4516, -3616, -3672, -3609, -3663, -3600, -3653, -3590, -3642,
4585, 4654, 4724, 4794, 4866, 4938, 5010, 5084, -3580, -3630, -3564, -3619, -3550, -3599, -3531, -3581,
5158, 5233, 5308, 5385, 5462, 5539, 5618, 5697, -3513, -3561, -3492, -3538, -3468, -3513, -3443, -3487,
5776, 5857, 5938, 6020, 6103, 6186, 6270, 6355, -3417, -3461, -3386, -3432, -3358, -3398, -3323, -3362,
6440, 6526, 6613, 6700, 6789, 6878, 6967, 7058, -3288, -3326, -3251, -3287, -3210, -3245, -3168, -3202,
7149, 7240, 7333, 7426, 7520, 7614, 7710, 7805, -3123, -3158, -3076, -3108, -3030, -3059, -2976, -3004,
7902, 7999, 8097, 8196, 8295, 8395, 8496, 8597, -2922, -2949, -2866, -2892, -2808, -2832, -2746, -2770,
8699, 8802, 8905, 9009, 9114, 9219, 9325, 9431, -2683, -2706, -2619, -2637, -2552, -2570, -2481, -2497,
9539, 9646, 9755, 9864, 9974, 10084, 10195, 10306, -2407, -2423, -2333, -2347, -2256, -2269, -2177, -2188,
10419, 10531, 10645, 10759, 10873, 10988, 11104, 11220, -2095, -2104, -2012, -2018, -1924, -1931, -1835, -1839,
11337, 11454, 11572, 11691, 11810, 11929, 12049, 12170, -1742, -1745, -1648, -1650, -1552, -1552, -1452, -1451,
12291, 12412, 12534, 12657, 12780, 12903, 13027, 13152, -1351, -1347, -1247, -1243, -1139, -1135, -1031, -1024,
13277, 13402, 13528, 13654, 13781, 13908, 14035, 14163, -919, -910, -805, -795, -689, -678, -571, -558,
14291, 14420, 14549, 14678, 14808, 14938, 15068, 15199, -451, -436, -327, -313, -202, -186, -75, -57,
15330, 15461, 15593, 15725, 15857, 15989, 16122, 16255, 55, 75, 187, 208, 320, 342, 456, 480,
16388, 16522, 16655, 16789, 16923, 17058, 17192, 17327, 594, 619, 735, 760, 877, 905, 1021, 1050,
17461, 17596, 17731, 17867, 18002, 18137, 18273, 18408, 1168, 1199, 1317, 1349, 1467, 1500, 1619, 1653,
18544, 18680, 18815, 18951, 19087, 19223, 19358, 19494, 1773, 1808, 1930, 1966, 2086, 2125, 2247, 2285,
19630, 19765, 19901, 20037, 20172, 20308, 20443, 20578, 2407, 2448, 2570, 2611, 2734, 2776, 2899, 2942,
20713, 20848, 20983, 21118, 21252, 21386, 21520, 21654, 3066, 3110, 3234, 3280, 3403, 3450, 3575, 3622,
21788, 21921, 22054, 22187, 22320, 22452, 22584, 22716, 3746, 3795, 3920, 3970, 4095, 4145, 4270, 4321,
22847, 22978, 23109, 23239, 23369, 23499, 23628, 23757, 4446, 4499, 4624, 4678, 4803, 4856, 4983, 5037,
23885, 24013, 24140, 24267, 24394, 24520, 24645, 24770, 5163, 5218, 5344, 5400, 5526, 5582, 5708, 5765,
24894, 25018, 25142, 25264, 25386, 25508, 25629, 25749, 5891, 5948, 6074, 6133, 6259, 6316, 6442, 6502,
25869, 25988, 26106, 26224, 26341, 26457, 26573, 26688, 6627, 6686, 6811, 6871, 6996, 7057, 7181, 7241,
26802, 26915, 27028, 27139, 27251, 27361, 27470, 27579, 7365, 7427, 7550, 7612, 7736, 7797, 7920, 7982,
27687, 27794, 27900, 28005, 28109, 28213, 28315, 28417, 8105, 8167, 8288, 8351, 8473, 8536, 8656, 8719,
28518, 28617, 28716, 28814, 28911, 29007, 29102, 29196, 8839, 8902, 9022, 9084, 9204, 9267, 9385, 9448,
29288, 29380, 29471, 29561, 29650, 29737, 29824, 29909, 9566, 9629, 9745, 9808, 9924, 9987, 10102, 10165,
29994, 30077, 30159, 30240, 30320, 30399, 30477, 30553, 10279, 10341, 10455, 10517, 10629, 10692, 10803, 10864,
30628, 30702, 30775, 30847, 30918, 30987, 31055, 31122, 10974, 11036, 11145, 11206, 11314, 11375, 11482, 11542,
31188, 31252, 31315, 31377, 31438, 31497, 31555, 31612, 11648, 11708, 11812, 11872, 11975, 12035, 12137, 12195,
31667, 31721, 31774, 31826, 31876, 31925, 31972, 32019, 12296, 12354, 12453, 12511, 12609, 12666, 12762, 12819,
32064, 32107, 32149, 32190, 32230, 32268, 32304, 32340, 12914, 12970, 13063, 13119, 13210, 13265, 13356, 13409,
32374, 32406, 32438, 32467, 32496, 32523, 32548, 32573, 13498, 13551, 13639, 13690, 13776, 13827, 13911, 13962,
32595, 32617, 32637, 32655, 32672, 32688, 32702, 32715, 14044, 14094, 14174, 14223, 14302, 14349, 14427, 14474,
32727, 32737, 32745, 32753, 32758, 32763, 32765, 32767, 14549, 14595, 14669, 14713, 14785, 14828, 14899, 14941,
32767, 32765, 32763, 32758, 32753, 32745, 32737, 32727, 15010, 15051, 15117, 15158, 15223, 15261, 15324, 15363,
32715, 32702, 32688, 32672, 32655, 32637, 32617, 32595, 15423, 15460, 15519, 15555, 15611, 15646, 15700, 15734,
32573, 32548, 32523, 32496, 32467, 32438, 32406, 32374, 15787, 15819, 15870, 15900, 15949, 15978, 16024, 16053,
32340, 32304, 32268, 32230, 32190, 32149, 32107, 32064, 16098, 16125, 16166, 16193, 16232, 16257, 16294, 16317,
32019, 31972, 31925, 31876, 31826, 31774, 31721, 31667, 16353, 16375, 16409, 16429, 16460, 16480, 16508, 16526,
31612, 31555, 31497, 31438, 31377, 31315, 31252, 31188, 16553, 16570, 16594, 16609, 16632, 16645, 16665, 16677,
31122, 31055, 30987, 30918, 30847, 30775, 30702, 30628, 16696, 16707, 16723, 16732, 16745, 16754, 16765, 16771,
30553, 30477, 30399, 30320, 30240, 30159, 30077, 29994, 16780, 16786, 16792, 16796, 16801, 16803, 16805, 16805,
29909, 29824, 29737, 29650, 29561, 29471, 29380, 29288, 16805, 16805, 16803, 16801, 16796, 16792, 16786, 16780,
29196, 29102, 29007, 28911, 28814, 28716, 28617, 28518, 16771, 16765, 16754, 16745, 16732, 16723, 16707, 16696,
28417, 28315, 28213, 28109, 28005, 27900, 27794, 27687, 16677, 16665, 16645, 16632, 16609, 16594, 16570, 16553,
27579, 27470, 27361, 27251, 27139, 27028, 26915, 26802, 16526, 16508, 16480, 16460, 16429, 16409, 16375, 16353,
26688, 26573, 26457, 26341, 26224, 26106, 25988, 25869, 16317, 16294, 16257, 16232, 16193, 16166, 16125, 16098,
25749, 25629, 25508, 25386, 25264, 25142, 25018, 24894, 16053, 16024, 15978, 15949, 15900, 15870, 15819, 15787,
24770, 24645, 24520, 24394, 24267, 24140, 24013, 23885, 15734, 15700, 15646, 15611, 15555, 15519, 15460, 15423,
23757, 23628, 23499, 23369, 23239, 23109, 22978, 22847, 15363, 15324, 15261, 15223, 15158, 15117, 15051, 15010,
22716, 22584, 22452, 22320, 22187, 22054, 21921, 21788, 14941, 14899, 14828, 14785, 14713, 14669, 14595, 14549,
21654, 21520, 21386, 21252, 21118, 20983, 20848, 20713, 14474, 14427, 14349, 14302, 14223, 14174, 14094, 14044,
20578, 20443, 20308, 20172, 20037, 19901, 19765, 19630, 13962, 13911, 13827, 13776, 13690, 13639, 13551, 13498,
19494, 19358, 19223, 19087, 18951, 18815, 18680, 18544, 13409, 13356, 13265, 13210, 13119, 13063, 12970, 12914,
18408, 18273, 18137, 18002, 17867, 17731, 17596, 17461, 12819, 12762, 12666, 12609, 12511, 12453, 12354, 12296,
17327, 17192, 17058, 16923, 16789, 16655, 16522, 16388, 12195, 12137, 12035, 11975, 11872, 11812, 11708, 11648,
16255, 16122, 15989, 15857, 15725, 15593, 15461, 15330, 11542, 11482, 11375, 11314, 11206, 11145, 11036, 10974,
15199, 15068, 14938, 14808, 14678, 14549, 14420, 14291, 10864, 10803, 10692, 10629, 10517, 10455, 10341, 10279,
14163, 14035, 13908, 13781, 13654, 13528, 13402, 13277, 10165, 10102, 9987, 9924, 9808, 9745, 9629, 9566,
13152, 13027, 12903, 12780, 12657, 12534, 12412, 12291, 9448, 9385, 9267, 9204, 9084, 9022, 8902, 8839,
12170, 12049, 11929, 11810, 11691, 11572, 11454, 11337, 8719, 8656, 8536, 8473, 8351, 8288, 8167, 8105,
11220, 11104, 10988, 10873, 10759, 10645, 10531, 10419, 7982, 7920, 7797, 7736, 7612, 7550, 7427, 7365,
10306, 10195, 10084, 9974, 9864, 9755, 9646, 9539, 7241, 7181, 7057, 6996, 6871, 6811, 6686, 6627,
9431, 9325, 9219, 9114, 9009, 8905, 8802, 8699, 6502, 6442, 6316, 6259, 6133, 6074, 5948, 5891,
8597, 8496, 8395, 8295, 8196, 8097, 7999, 7902, 5765, 5708, 5582, 5526, 5400, 5344, 5218, 5163,
7805, 7710, 7614, 7520, 7426, 7333, 7240, 7149, 5037, 4983, 4856, 4803, 4678, 4624, 4499, 4446,
7058, 6967, 6878, 6789, 6700, 6613, 6526, 6440, 4321, 4270, 4145, 4095, 3970, 3920, 3795, 3746,
6355, 6270, 6186, 6103, 6020, 5938, 5857, 5776, 3622, 3575, 3450, 3403, 3280, 3234, 3110, 3066,
5697, 5618, 5539, 5462, 5385, 5308, 5233, 5158, 2942, 2899, 2776, 2734, 2611, 2570, 2448, 2407,
5084, 5010, 4938, 4866, 4794, 4724, 4654, 4585, 2285, 2247, 2125, 2086, 1966, 1930, 1808, 1773,
4516, 4448, 4381, 4314, 4249, 4183, 4119, 4055, 1653, 1619, 1500, 1467, 1349, 1317, 1199, 1168,
3992, 3930, 3868, 3807, 3746, 3687, 3628, 3569, 1050, 1021, 905, 877, 760, 735, 619, 594,
3511, 3454, 3398, 3342, 3287, 3232, 3178, 3125, 480, 456, 342, 320, 208, 187, 75, 55,
3072, 3020, 2969, 2918, 2868, 2819, 2770, 2721, -57, -75, -186, -202, -313, -327, -436, -451,
2674, 2627, 2580, 2534, 2489, 2444, 2400, 2357, -558, -571, -678, -689, -795, -805, -910, -919,
2314, 2271, 2229, 2188, 2147, 2107, 2068, 2029, -1024, -1031, -1135, -1139, -1243, -1247, -1347, -1351,
1990, 1952, 1915, 1878, 1841, 1805, 1770, 1735, -1451, -1452, -1552, -1552, -1650, -1648, -1745, -1742,
1701, 1667, 1634, 1601, 1569, 1537, 1506, 1475, -1839, -1835, -1931, -1924, -2018, -2012, -2104, -2095,
1444, 1414, 1385, 1356, 1327, 1299, 1272, 1245, -2188, -2177, -2269, -2256, -2347, -2333, -2423, -2407,
1218, 1192, 1166, 1140, 1115, 1091, 1066, 1043, -2497, -2481, -2570, -2552, -2637, -2619, -2706, -2683,
1019, 996, 974, 952, 930, 908, 887, 867, -2770, -2746, -2832, -2808, -2892, -2866, -2949, -2922,
846, 827, 807, 788, 769, 750, 732, 714, -3004, -2976, -3059, -3030, -3108, -3076, -3158, -3123,
697, 680, 663, 646, 630, 614, 599, 584, -3202, -3168, -3245, -3210, -3287, -3251, -3326, -3288,
569, 554, 540, 526, 512, 498, 485, 472, -3362, -3323, -3398, -3358, -3432, -3386, -3461, -3417,
459, 447, 435, 423, 411, 400, 389, 378, -3487, -3443, -3513, -3468, -3538, -3492, -3561, -3513,
367, 357, 347, 337, 327, 317, 308, 299, -3581, -3531, -3599, -3550, -3619, -3564, -3630, -3580,
290, 281, 273, 265, 256, 249, 241, 233, -3642, -3590, -3653, -3600, -3663, -3609, -3672, -3616,
226, 219, 212, 205, 198, 192, 186, 179, -3677, -3619, -3680, -3622, -3686, -3626, -3680, -3624,
173, 168, 162, 156, 151, 146, 141, 136, -3680, -3619, -3676, -3615, -3672, -3610, -3668, -3605,
131, 126, 122, 117, 113, 109, 104, 100, -3661, -3596, -3650, -3585, -3641, -3581, -3628, -3563,
97, 93, 89, 86, 82, 79, 76, 73, -3618, -3549, -3604, -3535, -3591, -3523, -3578, -3510,
70, 67, 64, 61, 58, 56, 53, 51, -3564, -3494, -3546, -3475, -3527, -3464, -3515, -3437,
48, 46, 44, 42, 40, 38, 36, 34, -3495, -3420, -3474, -3400, -3455, -3381, -3436, -3364,
32, 31, 29, 27, 26, 24, 23, 22, -3418, -3344, -3395, -3318, -3367, -3296, -3358, -3269,
20, 19, 18, 17, 16, 15, 14, 13, -3327, -3249, -3302, -3225, -3280, -3204, -3260, -3186,
12, 11, 10, 9, 8, 8, 7, 7, -3242, -3166, -3221, -3141, -3193, -3111, -3186, -3102,
6, 5, 5, 5, 4, 4, 3, 3, -3153, -3083, -3135, -3064, -3119, -3048, -3105, -3035,
3, 3, 2, 2, 2, 2, 2, 2, -3094, -3023, -3082, -3005, -3059, -2971, -3043, -2986,
-3020, -2960, -3009, -2945, -2997, -2932, -2987, -2923,
-2979, -2914, -2970, -2901, -2949, -2866, -2901, -2893,
-2917, -2858, -2910, -2851, -2905, -2849, -2905, -2854,
-2911, -2864, -2919, -2874, -2914, -2866, -2830, -2871,
-2957, -2870, -2956, -2898, -2976, -2931, -3004, -2969,
-3037, -3013, -3069, -3058, -3076, -3087, -2904, -2777,
-3261, -3009, -3186, -3057, -3202, -3100, -3233, -3143,
-3270, -3187, -3312, -3235, -3358, -3284, -3409, 0,
} }; } };
void WidebandSpectrum::execute(buffer_c8_t buffer) { void WidebandSpectrum::execute(buffer_c8_t buffer) {
@ -171,18 +179,19 @@ void WidebandSpectrum::execute(buffer_c8_t buffer) {
if( channel_spectrum_request_update == false ) { if( channel_spectrum_request_update == false ) {
channel_spectrum_request_update = true; channel_spectrum_request_update = true;
constexpr int32_t k = 128 * 4 * 8; constexpr int32_t k = 128 * 16;
const auto& window = window_wideband_bin_lpf;
for(size_t i=0; i<channel_spectrum.size(); i++) { for(size_t i=0; i<channel_spectrum.size(); i++) {
int32_t real = 0; int64_t real = 0;
int32_t imag = 0; int64_t imag = 0;
real += buffer.p[i + 0].real() * window_blackmanharris_1024[i + 0]; real += buffer.p[i + 0].real() * window[i + 0];
imag += buffer.p[i + 0].imag() * window_blackmanharris_1024[i + 0]; imag += buffer.p[i + 0].imag() * window[i + 0];
real += buffer.p[i + 256].real() * window_blackmanharris_1024[i + 256]; real += buffer.p[i + 256].real() * window[i + 256];
imag += buffer.p[i + 256].imag() * window_blackmanharris_1024[i + 256]; imag += buffer.p[i + 256].imag() * window[i + 256];
real += buffer.p[i + 512].real() * window_blackmanharris_1024[i + 512]; real += buffer.p[i + 512].real() * window[i + 512];
imag += buffer.p[i + 512].imag() * window_blackmanharris_1024[i + 512]; imag += buffer.p[i + 512].imag() * window[i + 512];
real += buffer.p[i + 768].real() * window_blackmanharris_1024[i + 768]; real += buffer.p[i + 768].real() * window[i + 768];
imag += buffer.p[i + 768].imag() * window_blackmanharris_1024[i + 768]; imag += buffer.p[i + 768].imag() * window[i + 768];
channel_spectrum[i].real(real / k); channel_spectrum[i].real(real / k);
channel_spectrum[i].imag(imag / k); channel_spectrum[i].imag(imag / k);
} }