[STM32] Some basic uses of DSP library functions

For some children's boots who are new to STM32, the DSP library must be a strange thing. In layman's terms, the DSP library is to allow the MCU to use some official library functions such as DSP (digital signal processing chip) functions. It is based on the FPU (floating point operation function) of the MCU. If you don't even know the FPU Let’s start, I personally suggest to check what is FPU and some steps of DSP library transplantation (transplantation is also divided into two types, one is to use lib file, and the other is direct C language function library).
The reference materials recommended here are: Anfulai's STM32-V6 Development Board_Second Edition DSP Digital Signal Processing Tutorial, the download link is here: https://download.csdn.net/download/qq_32006213/87406373 To be honest, as long as you
can Read this tutorial carefully, and don’t need to read my article here on BB, just do some sorting here.

[1] Type: floating-point number, fixed-point number; floating-point number has:
float32_t→float; float64_t→double; fixed-point number will not be introduced, and generally it is rarely used. It should be noted here that if you are using STM32, then you must pay attention to your floating-point calculations often at the beginning because the value you pass in is not a floating-point type or is converted to another type by the compiler. slowed down. For example, you need to use a number: 1.234 during calculation, you can directly write in the formula:

NMSL = 1.234 - 5;
或者
Fun(1.234);

Congratulations, and happy mention of slow computing. Why, because the single-chip microcomputer thinks that the 1.234 you passed in is not a floating-point type but a double type, and the single-chip microcomputer only has an FPU but no fast calculation module for the double type, so you can only bite the bullet and forget it.
Another point is that if you use some functions of the traditional C library in math.h, its input parameters are of double type by default, so even if you ensure that the input is a floating point number, it will also be very slow. I personally think this is one of the disadvantages of traditional libraries compared to DSP libraries. Of course, it is not that you are not allowed to use double types, but when you use floating-point types, DSP libraries will be faster. In order to solve the above problems, it can be changed to:

NMSL = 1.234f - 5.0f;
或者
Fun(1.234f);

So the first thing to pay attention to is to make sure that your variable type is correct, basically use float32_t.
[2] Commonly used functions:
2.1 Trigonometric functions: arm_cos_f32/arm_sin_f32/arm_sin_cos_f32
Use cases:

#define PI 3.141592f
float32_t Angle = 60.0f;
float32_t Angle_Arc = Angle/180*PI;
float32_t Cos = 0.0f;
float32_t Sin = 0.0f;
Cos = arm_cos_f32(Angle_Arc);//注意这个函数要传入的东西是弧度制
Sin = arm_sin_f32(Angle_Arc);//注意这个函数要传入的东西是弧度制
arm_sin_cos_f32(Angle,&Sin,&Cos);//这个函数顶级就顶级在它可以直接传角度,而且角度是可以有小数的,但是使用方式就完全不同了。

2.2 Square root: arm_sqrt_f32

float32_t Input = 9.0f;
float32_t Output = 0.0f;
arm_sqrt_f32(Input,&Output);//这样Output上获得的就是平方根,暂时没试过负数能开出来不,可能有风险哦。

2.3 Maximum value, minimum value, average
value 2.3.1 Maximum value: arm_max_f32 Minimum value: arm_min_f32

float32_t Array[3]={
    
    0f,1.2f,1.3f};
float32_t Min = 0.0f;
uint32_t Min_index = 0;
float32_t Max = 0.0f;
uint32_t Max_index = 0;
arm_max_f32(Array,3,&Max,&Max_index);//数组:长度;获得的最小值;最小值在数组中的位置
arm_min_f32(Array,3,&Min,&Min_index);//最为强大的就是能找到位置啦

In fact, the second parameter may not use the overall length, and the first parameter may not pass in the address of the first element of the array. If you want to request a local maximum/minimum, you can pass in the n address of the data you want to start calculating by manipulating the first parameter, for example: (
float32_t *)(Array +n) → means starting from the nth , As for why it is written like this, it is because there are many different types. It is a safer habit to write this way, and more people can query (uint32_t *)Array+n by themselves. Why is there a problem? [Hint: Memcpy function also needs attention]. Then fill in 5 for the length. So now it is to find the maximum/minimum value among the five numbers starting from nth in the array.
2.3.2 mean arm_mean_f32

float32_t Array[3]={
    
    0f,1.2f,1.3f};
float32_t Mean = 0.0f;
arm_mean_f32(Array,3,&Mean);

2.4 Standard deviation, root mean square, and variance
These three have great statistical significance and may be used when processing a series of data. As for what each number means, please inquire by yourself.

float32_t Array[3]={
    
    0f,1.2f,1.3f};
float32_t Std = 0.0f;
float32_t Rms = 0.0f;
float32_t Var = 0.0f;
arm_std_f32(Array,3,&Std);//标准差
arm_rms_f32(Array,3,&Rms);//均方根
arm_var_f32(Array,3,&Var);//方差

2.5 Data copying and filling

float32_t Array_A[3]={
    
    0f,1.2f,1.3f};
float32_t Array_B[3];
arm_copy_f32(Array_A,Array_B,3);//拷贝
arm_fill_f32(5.0f,Array_B,3);//将5.0f填充至Array_B

2.6 Basic operations: summation, difference, multiplication, dot multiplication, absolute value, inverse number, and proportionality The above are
basically performed on multiple data in two arrays, if there are not many variables /Number, the direct formula is not bad.

float32_t Array_A[3]={
    
    0f,1.2f,1.3f};
float32_t Array_B[3] ={
    
    3.5f,-5.0f,2.5f};
float32_t Result_Array[3];
float32_t Dot_result;
arm_add_f32(Array_A,Array_B,Result_Array,3);//求和:Result_Array[n] = Array_A[n]+Array_B[n]
arm_sub_f32(Array_A,Array_B,Result_Array,3);//求差:Result_Array[n] = Array_A[n]-Array_B[n],这个要记得是前面减去后面
arm_abs_f32(Array_B,Result_Array,3);//求绝对值Result_Array[n] =|Array_B[n]|
arm_mult_f32(Array_A,Array_B,Result_Array,3);//求乘法Result_Array[n] = Array_A[n]*Array_B[n]
arm_dot_prod_f32(Array_A,Array_B,3,&Dot_result);//点乘得到的是一个值,详情看线性代数。Dot_result = Array_A[0]*Array_B[0]+···Array_A[2]*Array_B[2]
arm_negate_f32(Array_A,Result_Array,3);//求相反数:Result_Array[n] = -Array_A[n]
arm_scale_f32(Array_A,1.5f,Result_Array,3);//求比例乘积后:Result_Array[n] = Array_A[n]*1.5f;

2.7 Matrix operations and complex number operations
are basically not used, and it is a little bit difficult to use a single-chip computer to calculate matrices. The general steps are complex calculations run on Matlab, and the algorithm is simplified into a single formula and then implemented on the arm, so I won't continue to explain it here. If you must use the matrix, here are three suggestions:
1. When using the matrix, you must use it strictly according to the matrix definition in Arm_math. If you want to perform array operations, you will not need the DSP library. Regarding the definition and initialization of the matrix, be sure to read it in detail: the definition of the structure arm_matrix_instance_f32.
2. The matrix operation of the single chip computer may not be able to calculate the result and keeps recursive. Don't ask me how I know... Including you calculate some matrix operations on Matlab. If it is not established by itself, it is meaningless and will not converge. .
3. The inverse matrix calculation method of the DSP library has limitations. Some of them can be calculated on matlab, and the DSP library cannot be solved. So if you encounter problems, it is recommended to simulate first to make sure that there are no problems with the matrix and values.

Guess you like

Origin blog.csdn.net/qq_32006213/article/details/128819536