2024-11-06 10:03 AM
Hello everyone,
I'm working on a project where I need to transmit audio data from two microphones (left and right channels) over UART from an STM32 microcontroller. The audio data is initially sampled at high resolution (32-bit), but I’m converting it to an 8-bit format to reduce the data size before transmission.
The audiowave seems alright for quiet sounds, but when it comes to louder things the amplitude on the output seems to clip, or to cut when it reaches 127 and it continues on -128
my settings are:
DFSDM: 76,8
DIVIDER for the output clock: 32 so i get 2,4MHz
FOSR: 25 so i get 98kHz on each micropfone, so as i think for 2 microphones = 48kHz according to excel included in DFSDM tutorial.
Right Bit shift: 4 for both mics
the code:
/* USER CODE BEGIN PV */
#define AUDIO_REC 1000
int32_t LewaRecBuf[AUDIO_REC];
int32_t PrawaRecBuf[AUDIO_REC];
int8_t PlayBuf[AUDIO_REC];
uint32_t DmaRecHalfLeftBuffCplt = 0;
uint32_t DmaRecHalfRightBuffCplt = 0;
uint32_t DmaRecLeftBuffCplt = 0;
uint32_t DmaRecRightBuffCplt = 0;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_DFSDM1_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
uint16_t i = 0;
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_USART1_UART_Init();
MX_DFSDM1_Init();
/* USER CODE BEGIN 2 */
HAL_DFSDM_FilterRegularStart_DMA(&hdfsdm1_filter0, LewaRecBuf, AUDIO_REC);
HAL_DFSDM_FilterRegularStart_DMA(&hdfsdm1_filter1, PrawaRecBuf, AUDIO_REC);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1) {
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
if (DmaRecHalfLeftBuffCplt == 1 && DmaRecHalfRightBuffCplt == 1) {
// Przetwarzanie pierwszej połowy bufora dla obu mikrofonów
for (i = 0; i < AUDIO_REC / 2; i++) {
PlayBuf[i * 2] = (int8_t)(LewaRecBuf[i] >> 8); // Lewy kanał
PlayBuf[i * 2 + 1] = (int8_t)(PrawaRecBuf[i] >> 8); // Prawy kanał
}
DmaRecHalfLeftBuffCplt = 0; // Wyczyszczenie flagi lewego kanału
DmaRecHalfRightBuffCplt = 0; // Wyczyszczenie flagi prawego kanału
HAL_UART_Transmit(&huart1, (uint8_t*) PlayBuf,
((AUDIO_REC / 2) * 2), 1000);
}
if (DmaRecLeftBuffCplt == 1 && DmaRecRightBuffCplt == 1) {
// Przetwarzanie drugiej połowy bufora dla obu mikrofonów
for (i = AUDIO_REC / 2; i < AUDIO_REC; i++) {
PlayBuf[i * 2] = (int8_t)(LewaRecBuf[i] >> 8); // Lewy kanał
PlayBuf[i * 2 + 1] = (int8_t)(PrawaRecBuf[i] >> 8); // Prawy kanał
}
DmaRecLeftBuffCplt = 0;
DmaRecRightBuffCplt = 0;
HAL_UART_Transmit(&huart1, (uint8_t*) &PlayBuf[AUDIO_REC / 2],
((AUDIO_REC / 2) * 2), 1000);
}
}
/* USER CODE END 3 */
}
/* USER CODE BEGIN 4 */
void HAL_DFSDM_FilterRegConvHalfCpltCallback(
DFSDM_Filter_HandleTypeDef *hdfsdm_filter) {
if (hdfsdm_filter == &hdfsdm1_filter0) {
DmaRecHalfLeftBuffCplt = 1;
} else if (hdfsdm_filter == &hdfsdm1_filter1) {
DmaRecHalfRightBuffCplt = 1;
}
}
void HAL_DFSDM_FilterRegConvCpltCallback(
DFSDM_Filter_HandleTypeDef *hdfsdm_filter) {
if (hdfsdm_filter == &hdfsdm1_filter0) {
DmaRecLeftBuffCplt = 1;
} else if (hdfsdm_filter == &hdfsdm1_filter1) {
DmaRecRightBuffCplt = 1;
}
}
Also the problem is (as the serialplot shows) i only recive 24k of samples for second.
Can you please help me to get the amplitude to better shape which is not as shredded as it is.
Filip:)
2024-11-06 01:59 PM
How does shifting a 32-bit sample 8 places right get you a signed 8-bit sample? You get 24-bit, but not sign extended
What's the working range of the 32-bit samples? What form, and unsigned value? What's the 'zero' point.