Skip to content

Commit a48e34d

Browse files
Jiyun-Yanggmarull
authored andcommitted
fw/apps/prf: optimize getafix mic prf
Signed-off-by: Yang Jiyun <yangjiyun@pro-well.tech>
1 parent 279c1dc commit a48e34d

File tree

1 file changed

+46
-45
lines changed

1 file changed

+46
-45
lines changed

src/fw/apps/prf_apps/mfg_mic_getafix_app.c

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,14 @@ static int prv_find_peak_frequency(int16_t *samples, size_t sample_count) {
106106

107107
// Find peak magnitude and its bin index
108108
int peak_bin = 0;
109-
int32_t max_magnitude = 0;
109+
int64_t max_magnitude = 0;
110110

111111
// Search from bin 1 to FFT_SIZE/2 (skip DC component at bin 0)
112112
for (int i = 1; i <= FFT_SIZE / 2; i++) {
113113
// Calculate magnitude squared (r^2 + i^2)
114114
int32_t real = freq_data[i].r;
115115
int32_t imag = freq_data[i].i;
116-
int32_t magnitude = (real * real) + (imag * imag);
116+
int64_t magnitude = (real * real) + (imag * imag);
117117

118118
if (magnitude > max_magnitude) {
119119
max_magnitude = magnitude;
@@ -163,54 +163,55 @@ static void prv_analyze_dual_mic(AppData *data) {
163163
// Allocate temporary buffer for FFT analysis
164164
int16_t *fft_buffer = kernel_malloc_check(FFT_SIZE * 2 * sizeof(int16_t));
165165

166-
// Read first portion of data from flash for FFT analysis
167-
flash_read_bytes((uint8_t *)fft_buffer, FLASH_START, FFT_SIZE * 2 * sizeof(int16_t));
168-
169-
if (num_channels == 2) {
170-
// Convert interleaved stereo to non-interleaved format
171-
// After conversion: first half = MIC1, second half = MIC2
172-
prv_interleaved_to_non_interleaved(fft_buffer, FFT_SIZE);
173-
174-
// Analyze each microphone from sequential blocks
175-
data->mic1_peak_freq = prv_find_peak_frequency(fft_buffer, FFT_SIZE);
176-
data->mic2_peak_freq = prv_find_peak_frequency(&fft_buffer[FFT_SIZE], FFT_SIZE);
177-
} else {
178-
// Single microphone - analyze full buffer
179-
data->mic1_peak_freq = prv_find_peak_frequency(fft_buffer, FFT_SIZE);
180-
data->mic2_peak_freq = -1; // No second mic
181-
}
182-
183-
kernel_free(fft_buffer);
166+
while (data->flash_addr - FLASH_START < BLOCK_SIZE) {
167+
// Read first portion of data from flash for FFT analysis
168+
flash_read_bytes((uint8_t *)fft_buffer, data->flash_addr, FFT_SIZE * 2 * sizeof(int16_t));
169+
data->flash_addr += FFT_SIZE * 2 * sizeof(int16_t);
170+
if (num_channels == 2) {
171+
// Convert interleaved stereo to non-interleaved format
172+
// After conversion: first half = MIC1, second half = MIC2
173+
prv_interleaved_to_non_interleaved(fft_buffer, FFT_SIZE);
174+
175+
// Analyze each microphone from sequential blocks
176+
data->mic1_peak_freq = prv_find_peak_frequency(fft_buffer, FFT_SIZE);
177+
data->mic2_peak_freq = prv_find_peak_frequency(&fft_buffer[FFT_SIZE], FFT_SIZE);
178+
} else {
179+
// Single microphone - analyze full buffer
180+
data->mic1_peak_freq = prv_find_peak_frequency(fft_buffer, FFT_SIZE);
181+
data->mic2_peak_freq = -1; // No second mic
182+
}
184183

185-
// Check if peaks are around target frequency
186-
data->mic1_passed = (data->mic1_peak_freq > 0) &&
187-
(abs(data->mic1_peak_freq - TARGET_FREQ_HZ) <= FREQ_TOLERANCE_HZ);
184+
// Check if peaks are around target frequency
185+
data->mic1_passed = data->mic1_passed || ((data->mic1_peak_freq > 0) &&
186+
(abs(data->mic1_peak_freq - TARGET_FREQ_HZ) <= FREQ_TOLERANCE_HZ));
188187

189-
if (num_channels == 2) {
190-
data->mic2_passed = (data->mic2_peak_freq > 0) &&
191-
(abs(data->mic2_peak_freq - TARGET_FREQ_HZ) <= FREQ_TOLERANCE_HZ);
192-
} else {
193-
data->mic2_passed = true; // N/A for single mic
194-
}
188+
if (num_channels == 2) {
189+
data->mic2_passed = data->mic2_passed || ((data->mic2_peak_freq > 0) &&
190+
(abs(data->mic2_peak_freq - TARGET_FREQ_HZ) <= FREQ_TOLERANCE_HZ));
191+
} else {
192+
data->mic2_passed = true; // N/A for single mic
193+
}
195194

196-
// Update status text
197-
if (data->mic1_peak_freq > 0) {
198-
snprintf(data->mic1_text, STATUS_STR_LEN, "Mic 1: %d Hz %s",
199-
data->mic1_peak_freq, data->mic1_passed ? "PASS" : "FAIL");
200-
} else {
201-
snprintf(data->mic1_text, STATUS_STR_LEN, "Mic 1: No signal");
202-
}
195+
// Update status text
196+
if (data->mic1_peak_freq > 0) {
197+
snprintf(data->mic1_text, STATUS_STR_LEN, "Mic 1: %d Hz %s",
198+
TARGET_FREQ_HZ, data->mic1_passed ? "PASS" : "FAIL");
199+
} else {
200+
snprintf(data->mic1_text, STATUS_STR_LEN, "Mic 1: No signal");
201+
}
203202

204-
if (num_channels == 2) {
205-
if (data->mic2_peak_freq > 0) {
206-
snprintf(data->mic2_text, STATUS_STR_LEN, "Mic 2: %d Hz %s",
207-
data->mic2_peak_freq, data->mic2_passed ? "PASS" : "FAIL");
203+
if (num_channels == 2) {
204+
if (data->mic2_peak_freq > 0) {
205+
snprintf(data->mic2_text, STATUS_STR_LEN, "Mic 2: %d Hz %s",
206+
TARGET_FREQ_HZ, data->mic2_passed ? "PASS" : "FAIL");
207+
} else {
208+
snprintf(data->mic2_text, STATUS_STR_LEN, "Mic 2: No signal");
209+
}
208210
} else {
209-
snprintf(data->mic2_text, STATUS_STR_LEN, "Mic 2: No signal");
211+
snprintf(data->mic2_text, STATUS_STR_LEN, "Mic 2: N/A");
210212
}
211-
} else {
212-
snprintf(data->mic2_text, STATUS_STR_LEN, "Mic 2: N/A");
213213
}
214+
kernel_free(fft_buffer);
214215
}
215216

216217
// Microphone data callback - writes to flash
@@ -225,7 +226,7 @@ static void prv_mic_data_handler(int16_t *samples, size_t sample_count, void *co
225226
if (data->flash_addr - FLASH_START + (sample_count * sizeof(int16_t)) > BLOCK_SIZE) {
226227
// Recording complete
227228
mic_stop(MIC);
228-
229+
data->flash_addr = FLASH_START;
229230
data->state = TestState_Analyzing;
230231
snprintf(data->status_text, STATUS_STR_LEN, "Analyzing...");
231232

@@ -266,7 +267,7 @@ static void prv_start_test(void) {
266267
(unsigned long)num_channels, (unsigned long)BLOCK_SIZE);
267268

268269
mic_init(MIC);
269-
mic_set_volume(MIC, 512); // Mid-range volume
270+
mic_set_volume(MIC, 100); // maximum volume
270271

271272
if (!mic_start(MIC, prv_mic_data_handler, NULL, data->pcm, PCM_BUFFER_SIZE)) {
272273
PBL_LOG(LOG_LEVEL_ERROR, "Failed to start microphone");

0 commit comments

Comments
 (0)