diff --git a/Src/common/FFT.cpp b/Src/common/FFT.cpp index 042a0a2..daae553 100644 --- a/Src/common/FFT.cpp +++ b/Src/common/FFT.cpp @@ -119,7 +119,11 @@ void FFT::find_peaks(uint8_t axis) { // snr is in dB float snr; if (bin_mag_sum - peak_magnitude[peak_new] < 1.0e-19f) { - snr = 0; + if (bin_mag_sum > 0) { + snr = MIN_SNR; + } else { + snr = 0.0f; + } } else { snr = 10.f * log10f((size - 1) * peak_magnitude[peak_new] / (bin_mag_sum - peak_magnitude[peak_new])); @@ -135,9 +139,6 @@ void FFT::find_peaks(uint8_t axis) { for (int peak_prev = 0; peak_prev < MAX_NUM_PEAKS; peak_prev++) { bool peak_close = (fabsf(freq_adjusted - peak_frequencies_prev[peak_prev]) < (_resolution_hz * 0.25f)); - // if (!peak_close && peak_frequencies_prev[peak_prev] > 0) { - // continue; - // } _fft_updated[axis] = true; // keep @@ -203,7 +204,7 @@ float FFT::estimate_peak_freq(float fft[], int peak_index) { float ap = (real[k + 1] * real[k] + imag[k + 1] * imag[k]) / divider; // dp = -ap / (1 – ap) - if (1.0f - ap < 1.0e-19f) { + if (std::fabs(1.0f - ap) < 1.0e-19f) { return -1; } float dp = -ap / (1.f - ap); @@ -212,7 +213,7 @@ float FFT::estimate_peak_freq(float fft[], int peak_index) { float am = (real[k - 1] * real[k] + imag[k - 1] * imag[k]) / divider; // dm = am / (1 – am) - if (1.0f - am < 1.0e-19f) { + if (std::fabs(1.f - am) < 1.0e-19f) { return -1; } float dm = am / (1.f - am); diff --git a/Src/modules/imu/imu.cpp b/Src/modules/imu/imu.cpp index ef1f37d..029383b 100644 --- a/Src/modules/imu/imu.cpp +++ b/Src/modules/imu/imu.cpp @@ -82,12 +82,6 @@ void ImuModule::spin_once() { pub.publish(); } } - if (HAL_GetTick() % 1000 == 0) { - char buffer[90]; - snprintf(buffer, sizeof(buffer), "%d %d %d\n", int(1000 * fft_accel.peak_snr[0][0]), - int(1000 * fft_accel.peak_snr[1][0]), int(1000 * fft_accel.peak_snr[2][0])); - logger.log_info(buffer); - } } void ImuModule::get_vibration(std::array data) { diff --git a/Tests/common/test_fft.cpp b/Tests/common/test_fft.cpp index fce67d0..4b7feae 100644 --- a/Tests/common/test_fft.cpp +++ b/Tests/common/test_fft.cpp @@ -11,7 +11,6 @@ #include "FFT.hpp" -static constexpr auto ABS_ERR = 0.1f; unsigned int seed = 1; template @@ -63,8 +62,8 @@ struct InitParamMultiSignalWithRes { InitParamOneSignalWithRes OneSignalTestParams[7] = { // 0 - {{InitFFTParamType{ .sample_rate_hz = 100, .n_axes = 1, .window_size = 24}, - InitOneSignParamType{ .sample_rate_hz = 100, .freq_hz = 3, .amplitude = 1}}, + {{InitFFTParamType{ .sample_rate_hz = 100, .n_axes = 1, .window_size = 50}, + InitOneSignParamType{ .sample_rate_hz = 100, .freq_hz = 3, .amplitude = 10}}, true}, // 1 {{InitFFTParamType{ .sample_rate_hz = 1000, .n_axes = 1, .window_size = 512}, @@ -79,14 +78,14 @@ InitParamOneSignalWithRes OneSignalTestParams[7] = { InitOneSignParamType{ .sample_rate_hz = 512, .freq_hz = 100, .amplitude = 10}}, true}, // 4 - {{InitFFTParamType{ .sample_rate_hz = 1024, .n_axes = 3, .window_size = 400}, - InitOneSignParamType{ .sample_rate_hz = 1024, .freq_hz = 100, .amplitude = 1}}, + {{InitFFTParamType{ .sample_rate_hz = 1024, .n_axes = 3, .window_size = 512}, + InitOneSignParamType{ .sample_rate_hz = 1024, .freq_hz = 100, .amplitude = 10}}, true}, // 5 {{InitFFTParamType{ .sample_rate_hz = 256, .n_axes = 3, .window_size = 256}, InitOneSignParamType{ .sample_rate_hz = 256, .freq_hz = 100, .amplitude = 1}}, true}, - // 7 + // 6 {{InitFFTParamType{ .sample_rate_hz = 300, .n_axes = 1, .window_size = 200}, InitOneSignParamType{ .sample_rate_hz = 1000, .freq_hz = 100, .amplitude = 1}}, false}, @@ -249,6 +248,7 @@ class TestFFTOneSignalParametrized : public TestFFTBase init_parameters = parameters.parameters; @@ -278,32 +306,18 @@ class TestFFTOneSignalParametrized : public TestFFTBase