An4841 Digital Signal Processing For Stm32 Microcontrollers Using Cmsis Stmicroelectronics
An4841 Digital Signal Processing For Stm32 Microcontrollers Using Cmsis Stmicroelectronics
Application note
Digital signal processing for STM32 microcontrollers using CMSIS
Introduction
This application note describes the development of digital filters for analog signals, and the
transformations between time and frequency domains. The examples discussed in this
document include a low-pass and a high-pass FIR filter, as well as Fourier fast transforms
with floating and fixed point at different frequencies.
The associated firmware (X-CUBE-DSPDEMO), applicable to STM32F429xx and
STM32F746xx MCUs, can be adapted to any STM32 microcontroller.
Digital Signal Processing (DSP) is the mathematical manipulation and processing of
signals. Signals to be processed come in various physical formats that include audio, video
or any analog signal that carries information, such as the output signal of a microphone.
Both Cortex®-M4-based STM32F4 Series and Cortex®-M7-based STM32F7 Series provide
instructions for signal processing, and support advanced SIMD (Single Instruction Multi
Data) and Single cycle MAC (Multiply and Accumulate) instructions.
The use of STM32 MCUs in a real-time DSP application not only reduces cost, but also
reduces the overall power consumption.
The following documents are considered as references:
• PM0214, “STM32F3 and STM32F4 Series Cortex®-M4 programming manual”, available
on www.st.com
• PM0253, “STM32F7 Series Cortex®-M7 programming manual”, available on www.st.com
• CMSIS - Cortex® Microcontroller Software Interface Standard, available on
www.arm.com
• Arm® compiler toolchain Compiler reference, available on http://infocenter.arm.com
• “Developing Optimized Signal Processing Software on the Cortex®-M4 Processor”,
technical paper by Shyam Sadasivan, available on www.techonline.com.
Contents
3 Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.1 Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.2 Transforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
5 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
List of tables
List of figures
ELWV
6LJQ
([SRQHQWELWV 0DQWLVVDELWV
ELW
069
ELWV
6LJQ
([SRQHQWELWV 0DQWLVVDELWV
ELW
069
ELWV
069
Available fixed-point data sizes in Cortex®-Mx cores are 8-, 16- and 32-bit.
The most common format used for DSP operations are Q7, Q15 and Q31, with only
fractional bits to represent numbers between -1.0 and + 1.0.
The representation of a Q15 number is:
bs –1 –2 – 14 – 15
Value = ( – 1 ) × ( b 14 × 2 + b 13 × 2 + …+ b 1 × 2 + b0 × 2 )
where bs is the sign bit (the 15th bit), and bn is the digit for bit n.
The range of numbers supported in a Q15 number is comprised between -1.0 and 1.0,
corresponding to the smallest and largest integers that can be represented, respectively
-32768 and 32767.
For example, the number 0.25 will be encoded in Q15 as 0x2000(8192).
When performing operations on fixed-point the equation is as follows:
c = a <operand> b
where a, b and c are all fixed-point numbers, and <operand> refers to addition, subtraction,
multiplication, or division. This equation remains true for floating-point numbers as well.
Note: Care must be taken when doing operations on fixed-point numbers.
For example, if c = a x b with a and b in Q31 format, this will lead to a wrong result since the
compiler will treat it as an integer operation, consequently it will generate “muls a, b” and will
keep only the least significant 32 bits of the result.
The Cortex®-Mx cores feature several instructions that result in efficient implementation of
DSP algorithms.
The SSAT (Signed SATurate) instruction is used to scale and saturate a signed value to any
bit position, with optional shift before saturating.
Performs two 16-bit integer arithmetic additions in parallel, saturating the results to the
__qadd16
16-bit signed integer range -215 ≤ x ≤ 215 - 1.
__uhadd16 Performs two unsigned 16-bit integer additions, halving the results.
__shadd18 Performs four signed 8-bit integer additions, halving the results.
Performs two 16-bit signed multiplications, takes the difference of the products,
__smlsd subtracting the high half-word product from the low half-word product, and adds the
difference to a 32-bit accumulate operand.
3 Algorithms
3.1 Filters
The most common digital filters are:
• FIR (Finite Impulse Response): used, among others, in motor control and audio
equalization
• IIR (Infinite Impulse Response): used in smoothing data
The IIR filter can be used to implement filters such as Butterworth, Chebyshev, and Bessel.
3.2 Transforms
A transform is a function that converts data from a domain into another.
The FFT (Fast Fourier Transform) is a typical example: it is an efficient algorithm used to
convert a discrete time-domain signal into an equivalent frequency-domain signal based on
the Discrete Fourier Transform (DFT).
This demonstration also shows how easy it is to migrate an application from an STM32F4
microcontroller to one of the STM32F7 Series.
A graphical user interface is designed using STemWin, to simplify access to different
features of the demonstration.
FFT_Length depends on the user choice, it can be 1024, 256 or 64. The user can find FFT
initialization and processing for other formats in the fft_processing.c source file.
Results on STM32F429I-DISCO
To run one of the FFT examples select FFT, then connect PA5 to PA0.
Signal shape and spectrum are displayed on the LCD.
By varying the slider position the user can see the new input signal shape and the FFT
spectrum of the input signal updated in real time, as illustrated in Figure 6.
Figure 6. Running FFT 1024 points with input data in Float-32 on STM32F429I-DISCO
Results on STM32F746-DISCO
In this case it is possible to take advantage of the existing connection between PA4 and
DCMI_HSYNC. No other connections are needed since PA4 is configured as an output for
DAC1 and an input for ADC1.
Signal shape and spectrum are displayed on the LCD.
By varying the slider position the user can see the new input signal shape and the FFT
spectrum of the input signal updated in real time, as illustrated in Figure 7.
Figure 7. Running FFT 1024 points with input data in Float-32 on STM32F746-DISCO
The code below shows the initialization and the processing function for the floating-point
FIR filter.
The user can find FIR initialization and processing for other formats in the fir_processing.c
source file.
Input data to the FIR filter is the sum of the 1 kHz and 15 kHz sine waves (see Figure 9),
generated by MATLAB® in floating point format using the following script:
The magnitude spectrum of the input signal (Figure 10) shows that there are two
frequencies, 1 kHz and 15 kHz.
As the noise is positioned around 15 kHz, the cutoff point must be set at a lower frequency,
namely at 6 kHz.
Type Low-pass
Order 28
Sampling frequency 48 kHz
Cut-off frequency 6 kHz
The low-pass filter is designed with MATLAB®, using the commands shown below
The Filter Visualization Tool (FVT) is a practical tool allowing the user to verify the details
and the parameters of the built filter.
In Figure 11 are reported (left to right, top to bottom):
• magnitude response
• filter gain (in dB) vs. frequency (in Hz)
• impulse response
• step response
AN4841
AN4841 Rev 2
Results on STM32F429I-DISCO
This example considers two scenarios:
1. a FIR low-pass filter that includes Float-32, Q31 and Q15 data format
2. a FIR high-pass filter that includes only Q15 data format.
The oscilloscope screen captures for three different configurations are reported in
Figure 14. Left to right are shown
1. a low-pass FIR filter when the input data is floating point
2. a low-pass FIR filter with Q15 input data
3. a high-pass FIR filter with Q15 input data
Results on STM32F746-DISCO
The same example has been run on the STM32F746, the waveforms are visible in
Figure 15. Left to right are shown:
1. a low-pass FIR filter when the input data is floating point.
2. a low-pass FIR filter with Q15 input data.
3. a high-pass FIR filter with Q15 input data.
Table 5 summarizes the results, achieved using MDK-Arm™ (5.14.0.0) toolchain supporting
C Compiler V5.05 with Level 3 (-O3) for time optimization.
5 Revision history
STMicroelectronics NV and its subsidiaries (“ST”) reserve the right to make changes, corrections, enhancements, modifications, and
improvements to ST products and/or to this document at any time without notice. Purchasers should obtain the latest relevant information on
ST products before placing orders. ST products are sold pursuant to ST’s terms and conditions of sale in place at the time of order
acknowledgement.
Purchasers are solely responsible for the choice, selection, and use of ST products and ST assumes no liability for application assistance or
the design of Purchasers’ products.
Resale of ST products with provisions different from the information set forth herein shall void any warranty granted by ST for such product.
ST and the ST logo are trademarks of ST. All other product or service names are the property of their respective owners.
Information in this document supersedes and replaces information previously supplied in any prior versions of this document.