portapack-mayhem/firmware/baseband/proc_wideband_spectrum.cpp

208 lines
9.7 KiB
C++

/*
* Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
*
* This file is part of PortaPack.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#include "proc_wideband_spectrum.hpp"
#include "event_m4.hpp"
#include "i2s.hpp"
using namespace lpc43xx;
#include <cstdint>
#include <cstddef>
#include <array>
/*
Employ window-presum technique with a window constructed to act like a bin filter.
See: http://www.embedded.com/design/real-time-and-performance/4007611/DSP-Tricks-Building-a-practical-spectrum-analyzer
import scipy.signal
d = scipy.signal.remez(1024, (0.0, 32000, 48000, 10000000), (1, 0), Hz=20000000)
for n in range(0, len(d), 8): print(' '.join(['%5d,' % int(round(v * 32768 * 128)) for v in d[n:n+8]]))
*/
static constexpr std::array<int16_t, 1024> window_wideband_bin_lpf { {
0, -3409, -3284, -3358, -3235, -3312, -3187, -3270,
-3143, -3233, -3100, -3202, -3057, -3186, -3009, -3261,
-2777, -2904, -3087, -3076, -3058, -3069, -3013, -3037,
-2969, -3004, -2931, -2976, -2898, -2956, -2870, -2957,
-2871, -2830, -2866, -2914, -2874, -2919, -2864, -2911,
-2854, -2905, -2849, -2905, -2851, -2910, -2858, -2917,
-2893, -2901, -2866, -2949, -2901, -2970, -2914, -2979,
-2923, -2987, -2932, -2997, -2945, -3009, -2960, -3020,
-2986, -3043, -2971, -3059, -3005, -3082, -3023, -3094,
-3035, -3105, -3048, -3119, -3064, -3135, -3083, -3153,
-3102, -3186, -3111, -3193, -3141, -3221, -3166, -3242,
-3186, -3260, -3204, -3280, -3225, -3302, -3249, -3327,
-3269, -3358, -3296, -3367, -3318, -3395, -3344, -3418,
-3364, -3436, -3381, -3455, -3400, -3474, -3420, -3495,
-3437, -3515, -3464, -3527, -3475, -3546, -3494, -3564,
-3510, -3578, -3523, -3591, -3535, -3604, -3549, -3618,
-3563, -3628, -3581, -3641, -3585, -3650, -3596, -3661,
-3605, -3668, -3610, -3672, -3615, -3676, -3619, -3680,
-3624, -3680, -3626, -3686, -3622, -3680, -3619, -3677,
-3616, -3672, -3609, -3663, -3600, -3653, -3590, -3642,
-3580, -3630, -3564, -3619, -3550, -3599, -3531, -3581,
-3513, -3561, -3492, -3538, -3468, -3513, -3443, -3487,
-3417, -3461, -3386, -3432, -3358, -3398, -3323, -3362,
-3288, -3326, -3251, -3287, -3210, -3245, -3168, -3202,
-3123, -3158, -3076, -3108, -3030, -3059, -2976, -3004,
-2922, -2949, -2866, -2892, -2808, -2832, -2746, -2770,
-2683, -2706, -2619, -2637, -2552, -2570, -2481, -2497,
-2407, -2423, -2333, -2347, -2256, -2269, -2177, -2188,
-2095, -2104, -2012, -2018, -1924, -1931, -1835, -1839,
-1742, -1745, -1648, -1650, -1552, -1552, -1452, -1451,
-1351, -1347, -1247, -1243, -1139, -1135, -1031, -1024,
-919, -910, -805, -795, -689, -678, -571, -558,
-451, -436, -327, -313, -202, -186, -75, -57,
55, 75, 187, 208, 320, 342, 456, 480,
594, 619, 735, 760, 877, 905, 1021, 1050,
1168, 1199, 1317, 1349, 1467, 1500, 1619, 1653,
1773, 1808, 1930, 1966, 2086, 2125, 2247, 2285,
2407, 2448, 2570, 2611, 2734, 2776, 2899, 2942,
3066, 3110, 3234, 3280, 3403, 3450, 3575, 3622,
3746, 3795, 3920, 3970, 4095, 4145, 4270, 4321,
4446, 4499, 4624, 4678, 4803, 4856, 4983, 5037,
5163, 5218, 5344, 5400, 5526, 5582, 5708, 5765,
5891, 5948, 6074, 6133, 6259, 6316, 6442, 6502,
6627, 6686, 6811, 6871, 6996, 7057, 7181, 7241,
7365, 7427, 7550, 7612, 7736, 7797, 7920, 7982,
8105, 8167, 8288, 8351, 8473, 8536, 8656, 8719,
8839, 8902, 9022, 9084, 9204, 9267, 9385, 9448,
9566, 9629, 9745, 9808, 9924, 9987, 10102, 10165,
10279, 10341, 10455, 10517, 10629, 10692, 10803, 10864,
10974, 11036, 11145, 11206, 11314, 11375, 11482, 11542,
11648, 11708, 11812, 11872, 11975, 12035, 12137, 12195,
12296, 12354, 12453, 12511, 12609, 12666, 12762, 12819,
12914, 12970, 13063, 13119, 13210, 13265, 13356, 13409,
13498, 13551, 13639, 13690, 13776, 13827, 13911, 13962,
14044, 14094, 14174, 14223, 14302, 14349, 14427, 14474,
14549, 14595, 14669, 14713, 14785, 14828, 14899, 14941,
15010, 15051, 15117, 15158, 15223, 15261, 15324, 15363,
15423, 15460, 15519, 15555, 15611, 15646, 15700, 15734,
15787, 15819, 15870, 15900, 15949, 15978, 16024, 16053,
16098, 16125, 16166, 16193, 16232, 16257, 16294, 16317,
16353, 16375, 16409, 16429, 16460, 16480, 16508, 16526,
16553, 16570, 16594, 16609, 16632, 16645, 16665, 16677,
16696, 16707, 16723, 16732, 16745, 16754, 16765, 16771,
16780, 16786, 16792, 16796, 16801, 16803, 16805, 16805,
16805, 16805, 16803, 16801, 16796, 16792, 16786, 16780,
16771, 16765, 16754, 16745, 16732, 16723, 16707, 16696,
16677, 16665, 16645, 16632, 16609, 16594, 16570, 16553,
16526, 16508, 16480, 16460, 16429, 16409, 16375, 16353,
16317, 16294, 16257, 16232, 16193, 16166, 16125, 16098,
16053, 16024, 15978, 15949, 15900, 15870, 15819, 15787,
15734, 15700, 15646, 15611, 15555, 15519, 15460, 15423,
15363, 15324, 15261, 15223, 15158, 15117, 15051, 15010,
14941, 14899, 14828, 14785, 14713, 14669, 14595, 14549,
14474, 14427, 14349, 14302, 14223, 14174, 14094, 14044,
13962, 13911, 13827, 13776, 13690, 13639, 13551, 13498,
13409, 13356, 13265, 13210, 13119, 13063, 12970, 12914,
12819, 12762, 12666, 12609, 12511, 12453, 12354, 12296,
12195, 12137, 12035, 11975, 11872, 11812, 11708, 11648,
11542, 11482, 11375, 11314, 11206, 11145, 11036, 10974,
10864, 10803, 10692, 10629, 10517, 10455, 10341, 10279,
10165, 10102, 9987, 9924, 9808, 9745, 9629, 9566,
9448, 9385, 9267, 9204, 9084, 9022, 8902, 8839,
8719, 8656, 8536, 8473, 8351, 8288, 8167, 8105,
7982, 7920, 7797, 7736, 7612, 7550, 7427, 7365,
7241, 7181, 7057, 6996, 6871, 6811, 6686, 6627,
6502, 6442, 6316, 6259, 6133, 6074, 5948, 5891,
5765, 5708, 5582, 5526, 5400, 5344, 5218, 5163,
5037, 4983, 4856, 4803, 4678, 4624, 4499, 4446,
4321, 4270, 4145, 4095, 3970, 3920, 3795, 3746,
3622, 3575, 3450, 3403, 3280, 3234, 3110, 3066,
2942, 2899, 2776, 2734, 2611, 2570, 2448, 2407,
2285, 2247, 2125, 2086, 1966, 1930, 1808, 1773,
1653, 1619, 1500, 1467, 1349, 1317, 1199, 1168,
1050, 1021, 905, 877, 760, 735, 619, 594,
480, 456, 342, 320, 208, 187, 75, 55,
-57, -75, -186, -202, -313, -327, -436, -451,
-558, -571, -678, -689, -795, -805, -910, -919,
-1024, -1031, -1135, -1139, -1243, -1247, -1347, -1351,
-1451, -1452, -1552, -1552, -1650, -1648, -1745, -1742,
-1839, -1835, -1931, -1924, -2018, -2012, -2104, -2095,
-2188, -2177, -2269, -2256, -2347, -2333, -2423, -2407,
-2497, -2481, -2570, -2552, -2637, -2619, -2706, -2683,
-2770, -2746, -2832, -2808, -2892, -2866, -2949, -2922,
-3004, -2976, -3059, -3030, -3108, -3076, -3158, -3123,
-3202, -3168, -3245, -3210, -3287, -3251, -3326, -3288,
-3362, -3323, -3398, -3358, -3432, -3386, -3461, -3417,
-3487, -3443, -3513, -3468, -3538, -3492, -3561, -3513,
-3581, -3531, -3599, -3550, -3619, -3564, -3630, -3580,
-3642, -3590, -3653, -3600, -3663, -3609, -3672, -3616,
-3677, -3619, -3680, -3622, -3686, -3626, -3680, -3624,
-3680, -3619, -3676, -3615, -3672, -3610, -3668, -3605,
-3661, -3596, -3650, -3585, -3641, -3581, -3628, -3563,
-3618, -3549, -3604, -3535, -3591, -3523, -3578, -3510,
-3564, -3494, -3546, -3475, -3527, -3464, -3515, -3437,
-3495, -3420, -3474, -3400, -3455, -3381, -3436, -3364,
-3418, -3344, -3395, -3318, -3367, -3296, -3358, -3269,
-3327, -3249, -3302, -3225, -3280, -3204, -3260, -3186,
-3242, -3166, -3221, -3141, -3193, -3111, -3186, -3102,
-3153, -3083, -3135, -3064, -3119, -3048, -3105, -3035,
-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) {
sample_count += buffer.count;
if( sample_count > 400000 ) {
sample_count -= 400000;
if( channel_spectrum_request_update == false ) {
channel_spectrum_request_update = true;
constexpr int32_t k = 128 * 16;
const auto& window = window_wideband_bin_lpf;
for(size_t i=0; i<channel_spectrum.size(); i++) {
int64_t real = 0;
int64_t imag = 0;
real += buffer.p[i + 0].real() * window[i + 0];
imag += buffer.p[i + 0].imag() * window[i + 0];
real += buffer.p[i + 256].real() * window[i + 256];
imag += buffer.p[i + 256].imag() * window[i + 256];
real += buffer.p[i + 512].real() * window[i + 512];
imag += buffer.p[i + 512].imag() * window[i + 512];
real += buffer.p[i + 768].real() * window[i + 768];
imag += buffer.p[i + 768].imag() * window[i + 768];
channel_spectrum[i].real(real / k);
channel_spectrum[i].imag(imag / k);
}
channel_spectrum_sampling_rate = buffer.sampling_rate;
channel_filter_pass_frequency = 0;
channel_filter_stop_frequency = 0;
events_flag(EVT_MASK_SPECTRUM);
}
}
i2s::i2s0::tx_mute();
}