From c723f59ecc27505566d54d9edde0bdb98a5623b1 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Thu, 30 Jan 2025 08:14:20 -0500 Subject: decibel measuring? --- Core/Src/main.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 53 insertions(+), 12 deletions(-) (limited to 'Core/Src') diff --git a/Core/Src/main.c b/Core/Src/main.c index aa520a3..6b7fad0 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -22,18 +22,23 @@ /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include +#include /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ -typedef struct { +typedef struct { int32_t value : 18; } sample_t; /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ - +#define SAMPLE_COUNT (512) // Sample count per half-transfer +#define MIC_OFFSET_DB ( 0.f) // Linear offset +#define MIC_SENSITIVITY (-26.f) // dBFS value expected at MIC_REF_DB +#define MIC_REF_DB ( 94.f) // dB where sensitivity is specified +#define MIC_BITS (18) /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ @@ -50,10 +55,16 @@ DMA_HandleTypeDef hdma_spi1_tx; UART_HandleTypeDef huart2; /* USER CODE BEGIN PV */ -static /*const*/ uint8_t I2S_Frame_Buffer[8] = { +static const uint8_t I2S_Frame_Buffer[8] = { 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF }; -static uint8_t I2S_Receive_Buffer[4096]; +static uint8_t I2S_Receive_Buffer[SAMPLE_COUNT * sizeof(sample_t)]; + +float ln10; +float MIC_REF_AMPL; + +static float DB_Sum_Squares = 0.f; +static unsigned DB_Count = 0; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ @@ -65,7 +76,7 @@ static void MX_USART2_UART_Init(void); /* USER CODE BEGIN PFP */ static HAL_StatusTypeDef HAL_SPI_TransmitReceive_DMA_Mixed( SPI_HandleTypeDef *hspi, - uint8_t *pTxData, + const uint8_t *pTxData, uint8_t *pRxData, uint16_t TxSize, uint16_t RxSize); @@ -91,7 +102,9 @@ int main(void) { /* USER CODE BEGIN 1 */ - + ln10 = qfp_fln(10.f); + MIC_REF_AMPL = qfp_fmul(qfp_int2float((1 << (MIC_BITS - 1)) - 1), + qfp_fpow(10.f, MIC_SENSITIVITY / 20.f)); /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ @@ -426,7 +439,7 @@ static void MX_GPIO_Init(void) /* USER CODE BEGIN 4 */ HAL_StatusTypeDef HAL_SPI_TransmitReceive_DMA_Mixed( SPI_HandleTypeDef *hspi, - uint8_t *pTxData, + const uint8_t *pTxData, uint8_t *pRxData, uint16_t TxSize, uint16_t RxSize) @@ -507,13 +520,44 @@ void SPI_DMATransmitReceiveCplt(DMA_HandleTypeDef *hdma) (void)hspi; } +static float process(float in_div4) +{ + static float z1 = 0, z2 = 0, z3 = 0, z5 = 0; + + float out1 = qfp_fadd(in_div4, z1); + z1 = qfp_fadd(qfp_fmul(1.062f, out1), z2); + z2 = qfp_fsub(qfp_fmul(-0.14f, out1), in_div4); + + float out2 = qfp_fadd(out1, z3); + z3 = out1; + + float out3 = qfp_fadd(out2, z5); + z5 = qfp_fsub(qfp_fmul(0.985f, out3), out2); + + return out3; +} + void SPI_DMAHalfTransmitReceiveCplt(DMA_HandleTypeDef *hdma) { SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)hdma->Parent; (void)hspi; - sample_t sample = *(sample_t *)I2S_Receive_Buffer; - printf("%d\r\n", sample.value); + sample_t *sample = (sample_t *)I2S_Receive_Buffer; + for (int i = 0; i < SAMPLE_COUNT / 8; i++) { + float f = process(qfp_int2float(sample[i].value / 4)); + DB_Sum_Squares = qfp_fadd(DB_Sum_Squares, qfp_fmul(f, f)); + } + DB_Count += SAMPLE_COUNT / 8; + + if (DB_Count > 48000 / 4) { + float rms = qfp_fsqrt(qfp_fdiv(DB_Sum_Squares, qfp_uint2float(DB_Count))); + float db = qfp_fadd(MIC_OFFSET_DB + MIC_REF_DB, qfp_fmul(20.f, + qfp_flog10(qfp_fdiv(rms, MIC_REF_AMPL)))); + DB_Sum_Squares = 0.f; + DB_Count = 0; + + printf("%d\r\n", qfp_float2int(db)); + } } /* USER CODE END 4 */ @@ -524,7 +568,6 @@ void SPI_DMAHalfTransmitReceiveCplt(DMA_HandleTypeDef *hdma) void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ - /* User can add his own implementation to report the HAL error return state */ __disable_irq(); printf("Unhandled error, halting!\r\n"); while (1) @@ -544,8 +587,6 @@ void Error_Handler(void) void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ - /* User can add his own implementation to report the file name and line number, - ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ printf("Wrong parameters value: file %s on line %d\r\n", file, line); /* USER CODE END 6 */ } -- cgit v1.2.3