From e219c005090b66de8a3293f0e587ac8e840ccc1d Mon Sep 17 00:00:00 2001 From: Shriram Shastry Date: Tue, 28 May 2024 10:45:47 +0530 Subject: [PATCH] Audio: Optimize and fix TDFB direction calculation This check-in refines the TDFB direction calculation, addressing both performance and correctness: Fixes: - Correct infinite loop in `max_mic_distance` by fixing loop conditions. - Fix off-by-one error in `line_array_mode_check` ensuring all checks for co-linearity among microphone locations are performed. Optimizations: - Enhance `theoretical_time_differences` efficiency by substituting division with multiplication using a precomputed reciprocal. These modification collectively enhance the algorithm's robustness and computational efficiency. Signed-off-by: Shriram Shastry --- src/audio/tdfb/tdfb_direction.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/audio/tdfb/tdfb_direction.c b/src/audio/tdfb/tdfb_direction.c index 49ad5fdfe7ee..5a5513e5daf6 100644 --- a/src/audio/tdfb/tdfb_direction.c +++ b/src/audio/tdfb/tdfb_direction.c @@ -125,10 +125,8 @@ static int16_t max_mic_distance(struct tdfb_comp_data *cd) /* Max lag based on largest array dimension. Microphone coordinates are Q4.12 meters */ for (i = 0; i < cd->config->num_mic_locations; i++) { - for (j = 0; i < cd->config->num_mic_locations; i++) { - if (j == i) - continue; - + /* Start from i+1 halves the amount of iteration & to eliminate redundant checks */ + for (j = i + 1; j < cd->config->num_mic_locations; j++) { dx = cd->mic_locations[i].x - cd->mic_locations[j].x; dy = cd->mic_locations[i].y - cd->mic_locations[j].y; dz = cd->mic_locations[i].z - cd->mic_locations[j].z; @@ -159,7 +157,8 @@ static bool line_array_mode_check(struct tdfb_comp_data *cd) * Form vector AB(a,b,c) from x(i+1) - x(i), y(i+1) - y(i), z(i+1) - z(i) * Form vector AC(d,e,f) from x(i+2) - x(i), y(i+2) - y(1), z(i+2) - z(i) */ - for (i = 0; i < num_mic_locations - 3; i++) { + /* Calculates cross product only once */ + for (i = 0; i < num_mic_locations - 2; i++) { a = cd->mic_locations[i + 1].x - cd->mic_locations[i].x; b = cd->mic_locations[i + 1].y - cd->mic_locations[i].y; c = cd->mic_locations[i + 1].z - cd->mic_locations[i].z; @@ -282,9 +281,10 @@ static void level_update(struct tdfb_comp_data *cd, int frames, int ch_count, in /* Calculate mean square level */ for (n = 0; n < frames; n++) { s = *p; + tmp += ((int32_t)s * s); p += ch_count; + /* handle circular buffer */ tdfb_cinc_s16(&p, cd->direction.d_end, cd->direction.d_size); - tmp += ((int64_t)s * s); } /* Calculate mean square power */