(Att) 4647
(Att) 4647
User manual
STM32F10x
DSP library
Introduction
This user manual describes the STM32F10x DSP (digital signal processing) library, which is
a suite of common digital signal processing functions:
● PID controller
● Fast Fourier transform
● FIR and IIR filters
The library contains C and assembly functions. The assembly code is ported on ARM®,
GCC and IAR Systems™ assemblers.
Contents
2 PID controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.1 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.2 DSP library functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.2.1 DoPID function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.2.2 DoFullPID function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.2.3 PID_stm32 function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
8 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
9 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
List of tables
List of figures
The STM32F10x DSPLib is a suite of common functions for signal processing. It includes
the following functions:
2 PID controller
2.1 Description
The proportional-integral-derivative or PID controller is commonly used in the industry. It is a
feedback loop controller that manages the process command with a view to reducing the
error between the desired set point and the measured process variable.
The following block diagram shows the parallel structure of a PID controller. This is the
structure implemented in this DSP library.
ai15447
Example
/* Fill the coefficients table */
Coeff[0] = Kp;/*proportional coefficient*/
Example
/* Fill the coefficients table */
Coeff[0] = Kp;/*proportional coefficient*/
Coeff[1] = Ki;/*integral coefficient*/
Coeff[2] = Kd;/*derivative coefficient*/
The PID_stm32 function is used in the same way as the DoPID function. The error must be
computed, then the PID_stm32 function is called to improve the PID control process and to
return the appropriate command according to the coefficients table.
3.1 Description
The discrete Fourier transform (DFT) converts N complex values from the time domain to
the frequency domain.
The fast Fourier transform (FFT) is an optimized algorithm designed to compute the DFT
efficiently.
The STM32F10x DSP library provides a complex radix-4, with decimation-in-time, linear-
order FFT.
Let x[N] be the time signal samples. To use the FFT functions of the DSP library, the
following conditions must be satisfied:
● N is a power of 4
● All the signal samples must be 32-bit data containing the 16-bit real part followed by the
16-bit imaginary part (in the little Endian order: imaginary_real).
Example
#define N 64 /*Number of points*/
uint32_t x[N],y[N]; /* input and output arrays */
uint16_t real[N], imag[N]; /* real and imaginary arrays */
/* Fill the input array */
for(i=0; i<N; i++)
x[i] = (((uint16_t)(real[i])) | ((uint32_t)(imag[i]<<16)));
cr4_fft_64_stm32(y, x, N); /*computes the FFT of the x[N] samples*/
1. Comment all the FFT coefficients in the FFT function assembly file.
/* TableFFT_V7
;N=16
DC16 0x4000,0x0000, 0x4000,0x0000, 0x4000,0x0000
DC16 0xdd5d,0x3b21, 0x22a3,0x187e, 0x0000,0x2d41
DC16 0xa57e,0x2d41, 0x0000,0x2d41, 0xc000,0x4000
DC16 0xdd5d,0xe782, 0xdd5d,0x3b21, 0xa57e,0x2d41
; N=64
DC16 0x4000,0x0000, 0x4000,0x0000, 0x4000,0x0000
DC16 0x2aaa,0x1294, 0x396b,0x0646, 0x3249,0x0c7c
...
*/
2. Then, in the main, include the table_fft.h file, which is a part of the DSP library.
3. Finally, go to the FFT function assembly code, and inverse the comment in the following
lines:
ADRL R0, TableFFT_V7 /* Coeff in Flash */
//LDR.W R0, =TableFFT /* Coeff in RAM */
4.1 Description
The finite impulse filter (FIR) is a digital filter that is linearly dependent on a fixed finite
number of input samples.
The FIR filter can be defined by the number of coefficients to be processed (also called
taps), which gives the number of MAC (multiply-accumulate) operations to be done.
c0 c1 c2 cN-1
yn
+ + + +
ai15448
The FIR filter of the DSP library is a direct-form real FIR filter that uses an array of M 16-bit
coefficients to filter N 16-bit samples.
Let us put:
● a, the output vector of length N
● c, the coefficients vector of length M
● x, the input vector
So, x must have a length of M + N – 1.
The filter coefficients and their number must be filled into a structure of the COEFS type.
The coefficients structure is defined as follows:
typedef struct {
short *h;
unsigned int nh;
}COEFS;
Example
#define M 32 /*number of coefficients*/
#define N 32 /*number of output samples*/
5.1 Description
The infinite impulse response (IIR) filter is a digital filter that depends linearly on a finite
number of input samples and a finite number of previous filter outputs.
The IIR filter is represented by Equation 1 below.
Equation 1
M N
yn = bi x n – i – ai y n – i
i=0 i=1
Equation 1 is known as an auto-regressive moving average form (ARMA). The first sum of
Equation 1 represents the moving average part, which is similar to an FIR block, and the
second sum represents the auto-regressive part, which is the feedback from previous
outputs.
This IIR filter structure is called direct form I.
Figure 3 shows the structure of the direct form I of a second-order IIR filter. Direct form I
uses four delays to realize a second-order IIR filter.
To reduce the number of delays, the canonical form may be used. Figure 4 represents the
block diagram of the canonical form of a second-order IIR filter. The number of delays is
reduced from 4 to only 2.
xn yn
b0 + +
Z-1 Z-1
b1 + + -a1
Z-1 Z-1
b2 + + -a2
ai15449
xn wn yn
+ b0 +
Z-1
+ -a1 b1 +
Z-1
-a2 b2
wn-2
ai15450
The DSP library implements the two forms (direct form I and the canonical form) of the IIR
filter:
● Direct form I is used to design an IIR filter of order 4 (ARMA IIR)
● The canonical form is used to design an IIR filter of order 8, by using 4 second-order
IIR filter sections (biquads) arranged in series (biquad IIR filter).
Example
#define NY 32/*number of outputs, must be a multiple of 4 and >= 8*/
/* Coefficients for the ARMA IIR filter */
short h2[5] = { 0x09c2, 0x270a, 0x3a8f, 0x270a, 0x09c2 };
short h1[5] = { 0x7fff, 0xd24a, 0x72ca, 0xcf4e, 0x1ad4 };
/* Input and output vectors */
short x[NY+4],y[NY+4];
Example
#define NY 32/*number of outputs*/
/* Coefficients for the biquad IIR filter: 4 sections, with 5
coefficients in each section */
int16_t Coeff[20] = {...};
/* Input and output vectors */
short x[NY],y[NY];
/* Fill the input vector x */
...
/* Improve the filtering of NY samples */
iir_biquad_stm32(y, x, Coeff, NY);
This section provides the STM32F10x DSP library benchmark results, which are computed
using the IAR EWARM 5.20 toolchain, with high-speed optimization.
DoPID 52
DoFullPID 58
PID_stm32 72
cr4_fft_64_stm32 718(1)
cr4_fft_256_stm32 1486(1)
cr4_fft_1024_stm32 4560(1)
fir_16by16_stm32 162
iiarma_stm32 156
iir_biquad_stm32 294
1. FFT code size was computed with FFT coefficients table stored in Flash memory. If the FFT coefficients
are stored in SRAM, the code size of the three FFT functions is equal to 480 bytes.
Analysis of the PID timing shows that assembly code is not as fast as C code. The compiler
is more efficient in accessing variables than manual optimization (offset computation and
data placement in literal pool).
The STM32F10x DSP demo is an example that illustrates how to use the 64-point FFT
function of the STM32F10x DSP library. It consists of an LCD that displays the FFT
transformation.
The demo runs on the STM3210B-EVAL board, and shows the 64-point FFT transformation
of two signals:
1. a sine wave
2. a dual sine wave
The two types of wave are displayed at variable frequencies and for each frequency the FFT
is computed and the result is plotted as shown in Figure 5:
● The frequency (f1) of the sine wave is increased from 40 Hz to 4 kHz by steps of 30 Hz.
● The dual sine wave is the sum of a constant-frequency (f2) sine wave and the above
described variable-frequency (f1) sine wave.
Figure 5. FFT of a sine wave with frequency f1 and of a dual sine wave with
frequencies f1 and f2, both sampled at a frequency Fs
Sine wave
–Fs –f1 f1 Fs
2 2
ai15649b
8 Conclusion
This user manual describes the STM32F10x DSP library, which contains:
● a PID controller
● complex 16-bit radix-4 FFT optimized functions for 64, 256 and 1024 points
● a 16-bit FIR filter
● a 16-bit direct-form I IIR filter
● a 16-bit canonical-form IIR filter designed by biquads
9 Revision history
Information in this document is provided solely in connection with ST products. STMicroelectronics NV and its subsidiaries (“ST”) reserve the
right to make changes, corrections, modifications or improvements, to this document, and the products and services described herein at any
time, without notice.
All ST products are sold pursuant to ST’s terms and conditions of sale.
Purchasers are solely responsible for the choice, selection and use of the ST products and services described herein, and ST assumes no
liability whatsoever relating to the choice, selection or use of the ST products and services described herein.
No license, express or implied, by estoppel or otherwise, to any intellectual property rights is granted under this document. If any part of this
document refers to any third party products or services it shall not be deemed a license grant by ST for the use of such third party products
or services, or any intellectual property contained therein or considered as a warranty covering the use in any manner whatsoever of such
third party products or services or any intellectual property contained therein.
UNLESS OTHERWISE SET FORTH IN ST’S TERMS AND CONDITIONS OF SALE ST DISCLAIMS ANY EXPRESS OR IMPLIED
WARRANTY WITH RESPECT TO THE USE AND/OR SALE OF ST PRODUCTS INCLUDING WITHOUT LIMITATION IMPLIED
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE (AND THEIR EQUIVALENTS UNDER THE LAWS
OF ANY JURISDICTION), OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT.
UNLESS EXPRESSLY APPROVED IN WRITING BY AN AUTHORIZED ST REPRESENTATIVE, ST PRODUCTS ARE NOT
RECOMMENDED, AUTHORIZED OR WARRANTED FOR USE IN MILITARY, AIR CRAFT, SPACE, LIFE SAVING, OR LIFE SUSTAINING
APPLICATIONS, NOR IN PRODUCTS OR SYSTEMS WHERE FAILURE OR MALFUNCTION MAY RESULT IN PERSONAL INJURY,
DEATH, OR SEVERE PROPERTY OR ENVIRONMENTAL DAMAGE. ST PRODUCTS WHICH ARE NOT SPECIFIED AS "AUTOMOTIVE
GRADE" MAY ONLY BE USED IN AUTOMOTIVE APPLICATIONS AT USER’S OWN RISK.
Resale of ST products with provisions different from the statements and/or technical features set forth in this document shall immediately void
any warranty granted by ST for the ST product or service described herein and shall not create or extend in any manner whatsoever, any
liability of ST.
Information in this document supersedes and replaces all information previously supplied.
The ST logo is a registered trademark of STMicroelectronics. All other names are the property of their respective owners.