From 77daf1a6f31e0618490ceb194047114cd8496e0d Mon Sep 17 00:00:00 2001 From: Shriram Shastry Date: Sun, 23 Jun 2024 19:39:07 +0530 Subject: [PATCH 1/3] Audio: Bugfix: Optimize inner loop in max_mic_distance calculation Start inner loop from i+1 to halve iterations and eliminate redundant checks. Signed-off-by: Shriram Shastry --- src/audio/tdfb/tdfb_direction.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/audio/tdfb/tdfb_direction.c b/src/audio/tdfb/tdfb_direction.c index 49ad5fdfe7ee..f20c5410f0d9 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; From 8adadca4830a2bd62b8547d049d8c1f08550171d Mon Sep 17 00:00:00 2001 From: Shriram Shastry Date: Sun, 23 Jun 2024 19:43:47 +0530 Subject: [PATCH 2/3] Audio: Bugfix: Correct loop boundary in line_array_mode_check Adjust loop boundary to ensure correct number of elements are processed. Signed-off-by: Shriram Shastry --- src/audio/tdfb/tdfb_direction.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/audio/tdfb/tdfb_direction.c b/src/audio/tdfb/tdfb_direction.c index f20c5410f0d9..8bb87a973406 100644 --- a/src/audio/tdfb/tdfb_direction.c +++ b/src/audio/tdfb/tdfb_direction.c @@ -157,7 +157,7 @@ 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++) { + 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; From ee2bdabb7024dd377ba4992911a69a728fe94aed Mon Sep 17 00:00:00 2001 From: Shriram Shastry Date: Sun, 23 Jun 2024 19:46:50 +0530 Subject: [PATCH 3/3] Audio: Bugfix: Optimize level_update function and improve comments Use int32_t instead of int64_t to improve performance in level_update. Improve comments in max_mic_distance function to better explain the distance calculation between all possible microphone pairs. Correct the typo in line_array_mode_check function. Signed-off-by: Shriram Shastry Address reviewer comments: Improve comments in max_mic_distance and correct typo in line_array_mode_check --- src/audio/tdfb/tdfb_direction.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/audio/tdfb/tdfb_direction.c b/src/audio/tdfb/tdfb_direction.c index 8bb87a973406..84f288e26497 100644 --- a/src/audio/tdfb/tdfb_direction.c +++ b/src/audio/tdfb/tdfb_direction.c @@ -125,7 +125,9 @@ 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++) { - /* Start from i+1 halves the amount of iteration & to eliminate redundant checks */ + /* Calculate distances between all possible microphone pairs to + * find the maximum distance + */ 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; @@ -155,7 +157,7 @@ static bool line_array_mode_check(struct tdfb_comp_data *cd) /* Cross product of vectors AB and AC is (0, 0, 0) if they are co-linear. * 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) + * Form vector AC(d,e,f) from x(i+2) - x(i), y(i+2) - y(i), z(i+2) - z(i) */ for (i = 0; i < num_mic_locations - 2; i++) { a = cd->mic_locations[i + 1].x - cd->mic_locations[i].x; @@ -280,9 +282,9 @@ 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; tdfb_cinc_s16(&p, cd->direction.d_end, cd->direction.d_size); - tmp += ((int64_t)s * s); } /* Calculate mean square power */