]> code.bitgloo.com Git - clyne/u0-decibels.git/commitdiff
fix reporting to match time elapsed
authorClyne Sullivan <clyne@bitgloo.com>
Thu, 30 Jan 2025 15:22:56 +0000 (10:22 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Thu, 30 Jan 2025 15:22:56 +0000 (10:22 -0500)
Core/Src/main.c

index 6b7fad023db822aa908a7c01d032a9607dcc6559..56a07234ba6a1ce19e5487ea562c4809cfaceed4 100644 (file)
@@ -34,11 +34,12 @@ typedef struct  {
 \r
 /* Private define ------------------------------------------------------------*/\r
 /* USER CODE BEGIN PD */\r
-#define SAMPLE_COUNT    (512)   // Sample count per half-transfer\r
-#define MIC_OFFSET_DB   (  0.f) // Linear offset\r
-#define MIC_SENSITIVITY (-26.f) // dBFS value expected at MIC_REF_DB\r
-#define MIC_REF_DB      ( 94.f) // dB where sensitivity is specified\r
-#define MIC_BITS        (18)\r
+#define SAMPLE_COUNT        (256)   // Sample count per half-transfer\r
+#define SAMPLES_PER_REPORT  (48000) // Report every second given our 48 kHz rate\r
+#define MIC_OFFSET_DB       (  0.f) // Linear offset\r
+#define MIC_SENSITIVITY     (-26.f) // dBFS value expected at MIC_REF_DB\r
+#define MIC_REF_DB          ( 94.f) // dB where sensitivity is specified\r
+#define MIC_BITS            (18)\r
 /* USER CODE END PD */\r
 \r
 /* Private macro -------------------------------------------------------------*/\r
@@ -58,7 +59,7 @@ UART_HandleTypeDef huart2;
 static const uint8_t I2S_Frame_Buffer[8] = {\r
   0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF\r
 };\r
-static uint8_t I2S_Receive_Buffer[SAMPLE_COUNT * sizeof(sample_t)];\r
+static uint8_t I2S_Receive_Buffer[SAMPLE_COUNT * 2 * sizeof(sample_t)];\r
 \r
 float ln10;\r
 float MIC_REF_AMPL;\r
@@ -547,16 +548,16 @@ void SPI_DMAHalfTransmitReceiveCplt(DMA_HandleTypeDef *hdma)
     float f = process(qfp_int2float(sample[i].value / 4));\r
     DB_Sum_Squares = qfp_fadd(DB_Sum_Squares, qfp_fmul(f, f));\r
   }\r
-  DB_Count += SAMPLE_COUNT / 8;\r
+  DB_Count += SAMPLE_COUNT * 2; // Pretend we sampled the entire I2S buffer\r
 \r
-  if (DB_Count > 48000 / 4) {\r
+  if (DB_Count > SAMPLES_PER_REPORT) {\r
     float rms = qfp_fsqrt(qfp_fdiv(DB_Sum_Squares, qfp_uint2float(DB_Count)));\r
     float db = qfp_fadd(MIC_OFFSET_DB + MIC_REF_DB, qfp_fmul(20.f,\r
       qfp_flog10(qfp_fdiv(rms, MIC_REF_AMPL))));\r
     DB_Sum_Squares = 0.f;\r
     DB_Count = 0;\r
 \r
-    printf("%d\r\n", qfp_float2int(db));\r
+    printf("%d dB\r\n", qfp_float2int(db));\r
   }\r
 }\r
 /* USER CODE END 4 */\r