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 Monitor and Tuning 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 Simulation to tune parameters and watch live data when the application code is running on the STM32 Discovery board.

Available versions of this example:

****************************************************************************
The Class 'Simulink.DeferredMCOSClass' has been added to package 'Simulink' multiple times.
 It is possible that not all definitions are the same.  The first definition will be used.
****************************************************************************

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.

Open the Modeling tab and press CTRL+E to open Configuration Parameters dialog box. Go to Code Generation > Interface and select Code replacement library > ARM Cortex-M (CMSIS) 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. In the Block Parameters: Mic In dialog box, set audio frame to 20ms.

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

4. Ensure synchronization of all rates in the model.

a. Open the Modeling tab and press CTRL+E to open Configuration Parameters dialog box.

b. Go to Hardware Implementation > Operating system/scheduler and select Base rate trigger > "Mic In" block DMA interrupt..

5. To ensure "Mic In" block DMA interrupt as the Base rate trigger, the Mic In block has to be scheduled at the base rate. Open the Debug tab, select Information Overlays, and select Sample Time > Colors and make sure that the Mic In block shows in Red which indicates that it is scheduled at the base rate.

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. In the Block Parameters: Audio In dialog box,set audio frame to 20ms.

3. Double click on the Audio Out block to set the Audio out Sampling frequency. The 'Audio Out' block sample rate has to match the 'Audio In' block 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. Open the Debug tab, select Information Overlays, and select Sample Time > Colors and make sure that the Audio In block shows in Red which indicates that it is scheduled at the base rate.

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 Hardware and Model for Monitoring and Tuning

In this task, you will perform Monitor and Tune action for the model. 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. Monitor and Tune enables this capability.

The Monitor and Tune action 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. For a successful communication between the hardware and the model, set Logging buffer size (in bytes) to a value large enough to accomodate logged signals. For example, in this model the buffer size is specified as 20000. Any value less than 20000 might result in a blank Spectra Analyzer block output window.

Refer to Task 4 of the Code Verification and Validation with PIL and Monitoring and Tuning example to configure the model and set the proper connections between the STM32 Discovery board and the host machine.

To efficiently Monitor and Tune, you need to configure the settings to reduce memory required on the target.

1. On the Hardware tab, click Control Panel

2. Click Signal & Triggering on the Connection and triggering dialog box.

3. Select the signal from Signal selection dialog box and set the Duration to '1'.

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

External mode verbose mode is enabled by default in this example, which enables the display of debug messages for each Monitor and Tune 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. Performing Monitor and Tune on the audio algorithm showed how to tune parameters and watch live data running on the target.