GithubHelp home page GithubHelp logo

dil2743 / naivebayesclassification Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 7.46 MB

Implementation of Naive Bayes Classifier on Cortex M0+ microcontroller.

C 96.25% Assembly 0.24% Makefile 2.59% C++ 0.86% Python 0.06%

naivebayesclassification's Introduction

NaiveBayesClassification

Objective

On Cortex M0+ microcontroller perform a Naive Bayes classification using CMSIS-DSP, every 10ms. Any data can be taken for this.

The firmware should be written to exhibit minimal power consumption without performance loss.

  • The project is developed on CubeIDE Version: 1.5.1 can be found here

Microcontroller selection

STMicroelectronics has a good line of low power controllers, as per above requirement the MCU should be on Cortex M0+ and must consume list power with desired performance.

Stm32 CubeIDE provides a nice interface for selecting the MCU, as can be seen from below image that with the help of available filters such as Core, Series, Line and Package it become quite easy to narrow down the list.

Stm32L071CBTx is selected from available options after analysing various available options on the basis of available RAM, Flash, power consumption and DMIPS.

Stm32L071CBTx

The Python script can be found here which can be used to train NaiveBayes classifier and to generate required parameters.

Writing Firmware

Low Power Mode

  • RTC is configured at 10ms for generating interrupt which will be used as source for waking the controller from Stop mode.



	if (HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 0x20, RTC_WAKEUPCLOCK_RTCCLK_DIV16)

			!= HAL_OK) {

		Error_Handler();

	}

  • Also sleep on exit is enabled so that the controller wakes up, executes the sub-routine and automataclly went to Stop mode as soon as the sub-routine exits.

HAL_PWR_EnableSleepOnExit();

  • Stop mode is used as low power mode and the analysis about the same can be found below

Power Consumption

The clock is configured at 32 Mhz as the subroutine execution takes about 26000 DWT cycle which is approx 0.1ms time on core and it can be seen from above analysis that the Run mode is only consuming 7.5 mAmps of current while stop mode is consuming approx 750nAmps leading to average consumption of approx. 73 micro-AMps.

Flow Design

  •  	The controller is configured in Stop mode with SleepOnExit feature
    
  •  	As soon as it receives a INT it will wake up and
    
  •  	execute the sub-routine, once the sub-routine is completed it will go again into Stop mode.
    
  • Wakeup Sub-routine


/*

 * @info : Callback function for RTC - this routine will be executed every time the controller wakes from low power mode

 *

 */



void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc) {

	SystemClock_Config();

	HAL_ResumeTick();

	my_test_task();

}

  • Test Function

/*

 * @info : test function - this function will execute at a every 10ms and will use the @func my_pridictor(x,y) to

 * 			Predict the output based on the given input

 * 			Here the test a pseudo test. A more random test can be generated using ADC for generating Random numbers

 * @input : None

 * @output : None

 */

void my_test_task(void) {

	uint32_t output_cluster_index = my_pridictor(

			input_test_array[index_counter],

			input_test_array[index_counter + 1]);

	if (index_counter > 9) {

		index_counter = 0;

	} else {

		index_counter += 2;

	}

}

  • Classifier Sub-routine

/*

 * @info Naive_Bayes Predictor : This function will predict the class/index with which the given inputs should belong

 * @input : float x : first feature of the object

 * @input : float y : second feature of the object

 * @output : returns the index with which the object belongs based on its features and trained model data

 */

uint32_t my_pridictor(float x, float y) {

	/* Array of input data */



	float32_t in[2];



	/* Result of the classifier */

	float32_t result[NB_OF_CLASSES];

	float32_t maxProba;

	uint32_t index;



	S.vectorDimension = VECTOR_DIMENSION;

	S.numberOfClasses = NB_OF_CLASSES;

	S.theta = theta;

	S.sigma = sigma;

	S.classPriors = classPriors;

	S.epsilon = 4.328939296523643e-09f;



	in[0] = x;

	in[1] = y;



	arm_gaussian_naive_bayes_predict_f32(&S, in, result);



	arm_max_f32(result, NB_OF_CLASSES, &maxProba, &index);



	return index;

}

naivebayesclassification's People

Contributors

dil2743 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.