Application of neural network transplantation to STM32F4

Source: Internet
Author: User

is engaged in the subject of intelligent prosthesis, need to use, this to my implementation to play a very good guiding significance, hereby reproduced, and hereby thank the author, https://blog.csdn.net/qingelife/article/details/78429508

Use the color sensor to read the color of the ph test strip and then get the ph value he represents. At first, I wanted to fit a function about RGB and ph, but it always worked poorly. The neural network was used to determine his ph value based on RGB.

The idea is to use MATLAB to train the neural network, then the neural network is exported and transplanted to F4.

x=inputn;

NET=NEWFF (Minmax (x), [5 1],{' Tansig ', ' Purelin '}, ' TRAINLM ');
Net.trainParam.epochs = +;  
Net.trainParam.goal = 1e-6;
[Net,tr]=train (NET,X,OUTPUTN);  % Training Neural network


iw=net.iw{1,1};    % derived neural network
lw=net.lw{2,1};
B1=net.b{1};
B2=NET.B{2};
1 2 3 4 5 6 7 8 9 10 11 12

Calculation function of neural network on F4

void Get_ph ()   //Neural network calculation function
{       
        U8 i=0;
                rgb_data[0]= (float32_t) r/255.0;  The RGB normalization is deposited into the matrix
                rgb_data[1]= (float32_t) g/255.0;
                rgb_data[2]= (float32_t) b/255.0; 
              ARM_MAT_MULT_F32 (&iw,&rgb,&temp);  DSP instruction, matrix multiplication for
                (i=0;i<5;i++)
                    {
                        h_data[i]=2/(1+exp ( -2* (Temp_data[i]+b1[i])))-1;  Tansig function
                    }
                arm_mat_mult_f32 (&lw,&h,&ph);
                Ph_data= (float32_t) ((PH_DATA+B2) *14.00);                 
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14

Use the key variable

float32_t iw_data[15]={37.8146271525703f, 61.6224915049775f, -116.119809374016f,
                                                1.96880581432674f, -0.597865017458179f, -1.00688890479118f, 5.58165621812740f, 1.88117664982108f, -4.44180569880110f, 152.824588    053478f, -132.267718125163f, -22.9259593680010f, 88.8431847421991f,    

551.674736239499f, 404.835691342993f}; float32_t lw_data[5]={-28.7273044233606f,-1.03643090499672f,0.313686607538904f,0.146049290519538f,-

0.0206856023439895f};

float32_t Temp_data[5];

float32_t H_data[5];

float32_t Rgb_data[3];

float32_t Ph_data; float32_t b1[5]={22.6580899725128f,-0.510182522968968f,-3.52855071976284f,-2.82498427573125f,-92.7000542294773f

};

float32_t b2=29.396496369741858f;

Arm_matrix_instance_f32 Iw={5,3,iw_data}; Arm_matrix_inStance_f32 Lw={1,5,lw_data};

Arm_matrix_instance_f32 Temp={5,1,temp_data};

Arm_matrix_instance_f32 H={5,1,h_data};

Arm_matrix_instance_f32 Rgb={3,1,rgb_data}; Arm_matrix_instance_f32 ph={1,1,&ph_data};
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

Note that this opens the F4 FPU and uses the DSP library, so you need to add "arm_math.h" this header file.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.