Skip to main content
Visitor II
August 24, 2021
Question

How can I increase the 8 kHz audio sampling rate to 16 kHz

  • August 24, 2021
  • 1 reply
  • 2126 views

Hi everyone

Thanks for read my topic

I tried to record the sound, I managed to record the sound at a sample rate of 8 kHz.

this is my code it works great. But I need more sample rate like 11kHz or 16 kHz, I do not know how I can increase my sample rate to this rate.i use STM32F103

I got this code in the source

https://www.hackster.io/christopher-william-sutjiono/read-audio-amplifier-circuit-output-using-stm-32-d9acb5

void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_ADC1_Init(void);
static void MX_TIM3_Init(void);
static void MX_USART1_UART_Init(void);
 
 
/* USER CODE END 0 */
#define SAMP 8000
 
 
int main(void)
{
 
	int k;
	uint8_t buf[40];
	uint16_t dat[SAMP]; // store ADC value 'SAMP'variable is 8000 1 sec for store audio
 
 HAL_Init();
 
 
 SystemClock_Config();
 
 
 MX_GPIO_Init();
 MX_ADC1_Init();
 MX_TIM3_Init();
 MX_USART1_UART_Init();
 
 HAL_TIM_Base_Start(&htim3);
 
 
 
 while (1)
 {
	 for(k=0;k<SAMP;k++)
	 	 {
	 		 while((__HAL_TIM_GET_COUNTER(&htim3))<124);
	 		 HAL_ADC_Start(&hadc1);
	 		 HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);
	 		 dat[k] = HAL_ADC_GetValue(&hadc1); //GET ADC Value
	 	 }
 
	 	 // for loop to print sammples to computer screen with 1 msec pause between samples
 
	 	 for (k=0;k<SAMP;k++)
	 	 {
	 		 sprintf((char*)buf,"%d\r\n", dat[k]);
	 		 HAL_UART_Transmit(&huart1, buf, strlen((char*)buf), HAL_MAX_DELAY);
	 		 HAL_Delay(1);
	 	 }
 
		 HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
		 HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13,GPIO_PIN_SET);
		 HAL_Delay(3000);
		 HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13,GPIO_PIN_RESET);
		 HAL_Delay(3000);
 
 }
 
}
htim3.Instance = TIM3;
 htim3.Init.Prescaler = 16-1;
 htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
 htim3.Init.Period = 125-1;
 htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
 htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
 if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
 {
 Error_Handler();
 }
 sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
 if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
 {
 Error_Handler();
 }
 sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
 if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
 {
 Error_Handler();
 }

    This topic has been closed for replies.

    1 reply

    Super User
    August 25, 2021

    You could try reducing the prescaler from 16-1 to 8-1. May or may not work, depending on optimization settings and other things.

    A better way would be to use a timer to trigger the ADC and store that data into a buffer. Much higher rates and much more accurate timings are possible that way.

    Mford.11Author
    Visitor II
    August 25, 2021

    Hi @TDK 

    thank you for your attention Can you more explain about "trigger the ADC" please ?

    Super User
    August 25, 2021
    The timer TRGO event can be used to trigger an ADC conversion. If the ADC is set up to use DMA, the result can be transferred into memory without the use of the cpu.
    See "Conversion on external trigger" in the reference manual.