Documentation

Parametric Audio Equalizer for STM32 Discovery Boards

This example shows how to model a three band parametric equalizer algorithm and run it on the ARM® Cortex M based STMicroelectronics® STM32 Discovery boards.

Required MathWorks products:

  • MATLAB

  • Signal Processing Toolbox

  • DSP System Toolbox

  • Simulink

  • MATLAB Coder

  • Simulink Coder

  • Embedded Coder

  • Embedded Coder Support Package for STMicroelectronics Discovery Boards

  • DSP System Toolbox Support Package for ARM Cortex-M Processors

Required Hardware

  • STMicroelectronics STM32F4-Discovery or STM32F746G-Discovery board

  • Headphones

  • If using a STM32F4-Discovery, a USB TTL-232 adapter for External mode simulation

Introduction

This example is an extension of the DSP Parametric Equalizer example from the DSP System Toolbox that you can use to simulate the behavior of the audio algorithm on your host machine. Also refer to the Parametric Audio Equalizer for ARM Cortex-M Processor example that explains how to take advantage of the ARM Cortex-M Code Replacement Library (CRL) for DSP functions.

In this example, you will learn how to:

  • Set ARM Cortex-M CMSIS Code Replacement Library (CRL)

  • Use STM32F4-Discovery or STM32F746G-Discovery audio peripherals and schedule your algorithm based on audio DMA interrupts.

  • Configure the Model for External Mode Simulation to tune parameters and watch live data when the application code is running on the STM32 Discovery board.

Available versions of this example:

Prerequisites

We recommend completing:

NOTE: Install DSP System Toolbox Support Package for ARM Cortex-M Processors to view examples and help for ARM Cortex-M DST Code Replacement Library.

Set ARM Cortex-M CMSIS Code Replacement Library (CRL)

The DSP System Toolbox Support Package for ARM Cortex-M Processors provides an optimized Code Replacement Library for signal processing and math operations.

You can refer to the Supported CMSIS Functions for ARM Cortex-M Processors and the CMSIS Conditions for DSP Blocks to Support ARM Cortex-M help pages in the DSP System Toolbox Support Package for ARM Cortex-M Processors documentation for more information.

Follow the steps below to set the ARM Cortex-M Code Replacement Library for the Parametric Audio Equalizer for STM32 Discovery model:

Use STM32F4-Discovery audio peripherals and schedule your algorithm based on audio DMA interrupts.

In the Parametric Audio Equalizer for STM32F4-Discovery example model, the audio equalization is performed on audio data coming from the microphone available on the STM32F4-Discovery board. The model uses a 'Mic In' block from the STM32F4-Discovery library to get data from the microphone. This block outputs a frame of 16-bit audio samples.

The 'Mic In' block is set with a sample frequency of 44100Hz. The frame size is set to 44.1*20 to get a 20 ms audio frame containing 882 16-bit audio samples

Follow these steps to configure the Parametric Audio Equalizer for STM32F4-Discovery model:

1. Double click on the Mic In block to set the microphone Sample rate.

2. The parametric audio equalizer used in this examples requires a 20ms audio frame. The steps in the snapshot below show how to set the 'Mic in' block accordingly.

3. The 'Audio Out' block sample rate has to match the 'Mic In' block sample rate. Double click on the Audio Out block to set the Audio out Sample rate.

4. In this example, the "Mic In" block DMA interrupt is selected as the Base rate trigger for the model. This will ensure the synchronization of all rates in the model. The snapshot below shows how to access the Base rate trigger parameter available in the Configuration Parameters of the model under Hardware Implementation > Scheduler options.

5. To use the "Mic In" block DMA interrupt as the Base rate trigger, the Mic In block has to be scheduled at the base rate. On the model toolbar, click on Display > Sample Time > Colors and make sure that the Mic In block shows in Red which indicates that it is scheduled at the base rate. The snapshot below explains this operation.

Note: You can also choose the "Audio Out" block DMA interrupt as the Base rate trigger in step 4. In this case, the "Audio Out" block should be scheduled at base rate. This will prevent the use of a faster rate in the model.

Use STM32F746G-Discovery audio peripherals

In the Parametric Audio Equalizer for STM32F746G-Discovery example model, the audio equalization is performed on a single channel audio data coming from the dual microphones available on the STM32F746G-Discovery board. The model uses a 'Audio In' block from the STM32F746G-Discovery library to get data from the microphone. This block outputs a frame of 2 * 16-bit audio samples. Hence a submatrix block is used in the model to extract a single channel and at the output stage the same channel is copied to the other channel.

The 'Audio In' block is set with a sample frequency of 44100Hz. The frame size is set to 44.1*20 to get a 20 ms audio frame containing 882, 2 channel 16-bit audio samples

Follow these steps to configure the Parametric Audio Equalizer for STM32F746G-Discovery model:

1. Double click on the Audio In block to set the microphone Sampling frequency.

2. The parametric audio equalizer used in this examples requires a 20ms audio frame. The steps in the snapshot below show how to set the 'Audio in' block accordingly.

3. The 'Audio Out' block sample rate has to match the 'Audio In' block sampling frequency. Double click on the Audio Out block to set the Audio out Sampling frequency.

4. In this example, the "Audio In" block DMA interrupt is automatically selected as the Base rate trigger for the model. This will ensure the synchronization of all rates in the model.

5. On the model toolbar, click on Display > Sample Time > Colors and make sure that the Audio In block shows in Red which indicates that it is scheduled at the base rate. The snapshot below explains this operation.

Note: Using the Audio Input or Audio Output blocks in the model will prevent the use of a faster rate in the model.

Configure the Model for External Mode Simulation

In this task, you will run the model in External mode. When you are prototyping and developing algorithms, it is useful to monitor signals and tune parameters while the model is running in real-time on the hardware. Simulink External mode enables this capability.

The External mode simulation uses a serial communication interface with the STM32 Discovery board. For the STM32F4-Discovery board, you must use a serial adapter like the USB TTL-232 to exchange data between the host machine and the board. On the STM32F746G-Discovery board, you will use the serial over USB provided by the same USB connection needed for programming the board. So there is no need for an extra serial connection on the STM32F746G-Discovery board.

Open the Parametric Audio Equalizer model corresponding to your target hardware. By default, the model is using serial COM port 28 on the host machine. Refer to Task 4 of the Code Verification and Validation with PIL and External Mode example to configure the model and set the proper connections between the STM32 Discovery board and the host machine.

To efficiently run in External mode, you need to configure the External mode settings to reduce memory required on the target. Follow the steps shown in the snapshot below.

To start the External mode simulation, switch the model simulation mode to External and click on the Play button as explained in Task 4 of the Code Verification and Validation with PIL and External Mode example.

External mode verbose mode is enabled by default in this example, which enables the display of debug messages for each External mode operation. Once the simulation starts, double click on the Parametric Audio Equalizer block to launch the User Interface to tune the filter coefficients from the GUI. You can change center frequencies, bandwidths and gains for the three equalizer bands from the GUI.

New coefficients generated by the GUI will be sent to the target. The verbose mode message "Got EXT_SETPARAM_RESPONSE from target with status OK" on the MATLAB command window will indicate a successful update of filter coefficients. You can watch the response of the audio equalizer algorithm running on the target using the Spectrum Analyzer. External mode allows data to be updated from the target to the Spectrum Analyzer GUI.

Summary

This example showed how to generate code taking advantage of Code Replacement Library (CRL) for ARM Cortex-M CMSIS DSP functions, using the audio blocks available for the STM32 Discovery board. Running External mode on the audio algorithm showed how to tune parameters and watch live data running on the target.

Was this topic helpful?