Skip to content

Commit

Permalink
simplified dominant search logic, add mag multiplier
Browse files Browse the repository at this point in the history
  • Loading branch information
AsiiaPine committed Oct 8, 2024
1 parent 01b56f5 commit 376985b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
19 changes: 8 additions & 11 deletions Src/common/FFT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void FFT::update(float *input) {
sizeof(real_t) * overlap_start * 3);
_fft_buffer_index[axis] = overlap_start * 3;
}
find_dominant();
_find_dominant();
}

void FFT::find_peaks(uint8_t axis) {
Expand All @@ -61,12 +61,9 @@ void FFT::find_peaks(uint8_t axis) {
float bin_mag_sum = 0;
// calculate magnitudes for each fft bin
for (uint16_t fft_index = 0; fft_index < size/2; fft_index ++) {
real_t real_imag[2] = {0, 0};
fft::get_real_imag_by_index(_fft_output_buffer.data(), real_imag, size, fft_index);
float real_f, imag_f;
fft::convert_real_t_to_float(&real_imag[0], &real_f, 1);
fft::convert_real_t_to_float(&real_imag[1], &imag_f, 1);
const float fft_magnitude = sqrtf(real_f * real_f + imag_f * imag_f);
auto real = fft::get_imag_by_index(fft_output_buffer_float, fft_index);
auto imag = fft::get_imag_by_index(fft_output_buffer_float, fft_index);
const float fft_magnitude = sqrtf(real * real + imag * imag);
_peak_magnitudes_all[fft_index] = fft_magnitude;
bin_mag_sum += fft_magnitude;
}
Expand All @@ -88,7 +85,7 @@ void FFT::find_peaks(uint8_t axis) {
}
}

void FFT::find_dominant() {
void FFT::_find_dominant() {
if (!is_updated()) {
return;
}
Expand Down Expand Up @@ -126,7 +123,7 @@ void FFT::_identify_peaks_bins(float peak_magnitude[MAX_NUM_PEAKS],

if (largest_peak_index > 0) {
raw_peak_index[i] = largest_peak_index;
peak_magnitude[i] = _peak_magnitudes_all[largest_peak_index];
peak_magnitude[i] = largest_peak;
// remove peak + sides (included in frequency estimate later)
_peak_magnitudes_all[largest_peak_index - 1] = 0;
_peak_magnitudes_all[largest_peak_index] = 0;
Expand All @@ -145,7 +142,7 @@ uint16_t FFT::_estimate_peaks(float* peak_magnitude,
continue;
}
float adjusted_bin = 0.5f *
estimate_peak_freq(fft, 2 * raw_peak_index[peak_new]);
_estimate_peak_freq(fft, 2 * raw_peak_index[peak_new]);
if (adjusted_bin > size || adjusted_bin < 0) {
continue;
}
Expand Down Expand Up @@ -196,7 +193,7 @@ static constexpr float tau(float x) {
return addend_1 - multiplier_2 * addend_2;
}

float FFT::estimate_peak_freq(float fft[], int peak_index) {
float FFT::_estimate_peak_freq(float fft[], int peak_index) {
if (peak_index < 2 || peak_index >= size) {
return -1;
}
Expand Down
4 changes: 2 additions & 2 deletions Src/common/FFT.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ class FFT {
uint8_t n_axes;
float _sample_rate_hz;

float estimate_peak_freq(float fft[], int peak_index);
float _estimate_peak_freq(float fft[], int peak_index);
void find_peaks(uint8_t axis);
void find_dominant();
void _find_dominant();
// void identify_bin_peaks(uint8_t axis);
void _identify_peaks_bins(float peak_magnitude[MAX_NUM_PEAKS],
uint16_t raw_peak_index[MAX_NUM_PEAKS]);
Expand Down
4 changes: 2 additions & 2 deletions Src/modules/imu/imu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void ImuModule::update_accel_fft() {
}
fft_accel.update(accel.data());
pub.msg.accelerometer_integral[0] = fft_accel.dominant_frequency;
pub.msg.accelerometer_integral[1] = fft_accel.dominant_mag;
pub.msg.accelerometer_integral[1] = fft_accel.dominant_mag * 1000;
pub.msg.accelerometer_integral[2] = fft_accel.dominant_snr;
}

Expand All @@ -112,6 +112,6 @@ void ImuModule::update_gyro_fft() {
}
fft_gyro.update(gyro.data());
pub.msg.rate_gyro_integral[0] = fft_gyro.dominant_frequency;
pub.msg.rate_gyro_integral[1] = fft_gyro.dominant_mag;
pub.msg.rate_gyro_integral[1] = fft_gyro.dominant_mag * 1000;
pub.msg.rate_gyro_integral[2] = fft_gyro.dominant_snr;
}
1 change: 1 addition & 0 deletions Src/platform/ubuntu/rfft.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ namespace fft {
inline T get_imag_by_index(T* in, uint16_t index) {
return 0;
}

/*
The function written based on fftw3 library.
@param plan: The plan for the r2c transform.
Expand Down

0 comments on commit 376985b

Please sign in to comment.