cancel
Showing results for 
Search instead for 
Did you mean: 

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

Mford.11
Associate II

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();
  }

5 REPLIES 5
TDK
Guru

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.

If you feel a post has answered your question, please click "Accept as Solution".

Hi @TDK 

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

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.
If you feel a post has answered your question, please click "Accept as Solution".

Excuse me , is there example ? i did read about "Conversion on external trigger"

I don't have example code for you. I am sure there are examples online.
If you feel a post has answered your question, please click "Accept as Solution".