NanoEdge AI Library for n-class classification (nCC)

1. What is NanoEdge AI Library for n-class classification?

NanoEdge™ AI Library is an Artificial Intelligence (AI) static library for embedded C software running on Arm® Cortex® microcontrollers.

When embedded on microcontrollers, it gives them the ability to easily "classify" sensor patterns, by themselves, without the need for the user to have additional skills in Mathematics, Machine Learning, or data science.

The NanoEdge AI static library for n-class classification is the code that contains an AI model (for example, as a bundle of signal treatment, Machine Learning model, and optimally tuned hyperparameters) designed to determine which class a given sensor pattern belongs to. All classes are defined by the user in NanoEdge AI Studio (NanoEdgeAIStudio), also called the Studio, and are used during the training process of the AI model.

2. Install / Getting started

The main functions available via the library are:

init() run first before the classification process
classification() run a classification iteration (inference)

2.1. Static library

  • In NanoEdge AI Studio, after obtaining a library, click Compile (on the "Deployment" screen, which follows the "Benchmark" and "Emulator" screens)
  • Open the .zip file obtained
  • Select and copy the static library libneai.a
  • Link this static library to your project code

2.2. NanoEdge AI Library functions

Most NanoEdge AI function return the status of the library in the following enum, neai_state:

enum neai_state {
    NEAI_OK = 0,
    NEAI_ERROR = 1,
    NEAI_NOT_INITIALIZED = 2,
    NEAI_INVALID_PARAM = 3,
    NEAI_NOT_SUPPORTED = 4,
    NEAI_LEARNING_DONE = 5,
    NEAI_LEARNING_IN_PROGRESS = 6
};

Here are the possible statuses:

NEAI_OK: library working as expected
NEAI_ERROR: internal error with the library
NEAI_NOT_INITIALIZED: learn or detect functions were called without running the init function first; initialize your library.
NEAI_INVALID_PARAM: neai function was called with one or more incorrect or missing parameters.
NEAI_NOT_SUPPORTED: board not supported
NEAI_LEARNING_DONE: minimum number of learning iterations reached
NEAI_LEARNING_IN_PROGRESS: fail-safe to prevent insufficient number of learning iterations; run more iterations.
Info white.png Information

Although they are present in all NanoEdge AI header files, the NEAI_LEARNING_DONE and NEAI_LEARNING_IN_PROGRESS statuses are only relevant for anomaly detection libraries.

2.2.1. Initialization

enum neai_state neai_classification_init(void);

Initialization must be called at the beginning.

  • Input:
None
  • Output:
the neai_state enum (NEAI_OK == 0, in case of success).

2.2.2. Classification

enum neai_state neai_classification(float *in, float *probabilities, int *id_class);

This function returns the class identifier.

  • Input:
float *in, the length of the buffer is NEAI_INPUT_SIGNAL_LENGTH * NEAI_INPUT_AXIS_NUMBER.
float *probabilities, the length of the buffer is NEAI_NUMBER_OF_CLASSES .
int *id_class, the variable that contains the class ID returned by the function, as a result of inference.
  • Output:
The class identifier, to which the input has been attributed with the highest probability.
The neai_state enum (NEAI_OK == 0, in case of success).
Info white.png Information
  • The int *id_class variable must be defined prior to calling the detection function, and pointed to using &id_class when passed as an argument (see code example below).
  • The class identifier returned by the function is a number between 0 and NEAI_NUMBER_OF_CLASSES - 1.
  • The user can easily get the name of the class associated with the identifier returned by the function using the getter function neai_get_class_name(id_class), defined in the header file NanoEdgeAI.h. The name of classes is defined by the user in the Studio.
  • probabilities[] contains the probabilities of all classes.
  • The sum of the all class probabilities is equal to 1.
Warning white.png Warning

NanoEdge AI Library uses float data types instead of int. If you are using intdata types, convert (cast) them into float.

2.3. Example "Hello World!"

Header files:

NanoEdgeAI.h (provided in the .zip file that you download by clicking Compile in NanoEdge AI Studio (on the "Deploy" screen after obtaining a library))

Example of NanoEdge AI Library header file:

This snippet is provided AS IS, and by taking it, you agree to be bound to the license terms that can be found here for the component: Application.


/* =============
Copyright (c) 2026, STMicroelectronics

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions
  and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this list of
  conditions and the following disclaimer in the documentation and/or other materials provided
  with the distribution.

* Neither the name of the copyright holders nor the names of its contributors may be used to
  endorse or promote products derived from this software without specific prior written
  permission.

*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER /
 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 POSSIBILITY OF SUCH DAMAGE.*
*/


#ifndef NANOEDGEAI_H
#define NANOEDGEAI_H

#include <stdint.h>

/* NEAI ID */
#define NEAI_ID "None"

/* Input signal configuration */
#define NEAI_INPUT_SIGNAL_LENGTH 512
#define NEAI_INPUT_AXIS_NUMBER 3


/* Classification configuration */
#define NEAI_NUMBER_OF_CLASSES 4

/* NEAI State Enum */
enum neai_state {
    NEAI_OK = 0,
    NEAI_ERROR = 1,
    NEAI_NOT_INITIALIZED = 2,
    NEAI_INVALID_PARAM = 3,
    NEAI_NOT_SUPPORTED = 4,
    NEAI_LEARNING_DONE = 5,
    NEAI_LEARNING_IN_PROGRESS = 6
};


#ifdef __cplusplus
extern "C" {
#endif

/**
 * @brief  Must be called at the beginning to initialize the classification model by loading the
 *         pretrained model.
 * @return NEAI_OK on success, error code otherwise.
 */
enum neai_state neai_classification_init(void);

/**
 * @brief  Perform classification on a new input sample by returning the probability for each
 *         class and the predicted class ID.
 * @param  in             [in]   Pointer to the input signal array
 *                               (size NEAI_INPUT_SIGNAL_LENGTH * NEAI_INPUT_AXIS_NUMBER).
 * @param  probabilities  [out]  Pointer to the output probabilities array
 *                               (size NEAI_NUMBER_OF_CLASSES).
 * @param  id_class       [out]  Pointer to the predicted class ID
 *                               (integer in range [0, NEAI_NUMBER_OF_CLASSES - 1]).
 * @return NEAI_OK on success.
 *         Error code otherwise.
 */
enum neai_state neai_classification(float *in, float *probabilities, int *id_class);

/* ===== Common getter functions ===== */
/**
 * @brief  Get the NEAI identifier.
 * @return Pointer to a string containing the NEAI ID.
 */
char* neai_get_id(void);

/**
 * @brief  Get the input signal size (number of samples per axis).
 * @return Input signal size.
 */
int neai_get_input_signal_size(void);

/**
 * @brief  Get the number of input axes/channels.
 * @return Number of input axes.
 */
int neai_get_axis_number(void);


/* ===== Specific classification getter functions ===== */
/**
 * @brief  Get the number of classes in the classification model.
 * @return Number of classes.
 */
int neai_get_number_of_classes(void);

/**
 * @brief  Get the class name for a given class ID.
 * @param  id_class [in]  Class ID
 *                        (integer in range [0, NEAI_NUMBER_OF_CLASSES - 1]).
 * @return Pointer to the class name string, or NULL if the ID is invalid.
 */
const char* neai_get_class_name(int id_class);


#ifdef __cplusplus
}
#endif

#endif /* NANOEDGEAI_H */


/* =============
Declarations to add to your main program to use the NanoEdge AI library.
You may copy-paste them directly or rename variables as needed.
WARNING: Respect the structures, types, and buffer sizes; only variable names may be changed.

enum neai_state state;   // Captures return states from NEAI functions
int id_class;   // Predicted class ID returned by classification function
float input_signal[NEAI_INPUT_SIGNAL_LENGTH * NEAI_INPUT_AXIS_NUMBER];   // Input signal buffer
float probabilities[NEAI_NUMBER_OF_CLASSES];   // Output probabilities buffer returned by classification function
============= */
Warning DB.png Important
  • The input buffer collected from your sensor must be declared in the main program as float input_signal[NEAI_INPUT_SIGNAL_LENGTH * NEAI_INPUT_AXIS_NUMBER];. A declaration template is provided in the comment block at the end of NanoEdgeAI.h.


Main program: main.c

This program must be completed by the user (depending on the applications or the desired features).

This snippet is provided AS IS, and by taking it, you agree to be bound to the license terms that can be found here for the component: Application.


/**
  **************************************************************************
  * Demo: NanoEdge AI process to include in main program body
  *
  * @note  This program must be completed and customized by the user
  **************************************************************************
  */

/* Includes --------------------------------------------------------------------*/
#include "NanoEdgeAI.h"
/* Private define --------------------------------------------------------------*/
/* Private variables defined by user -------------------------------------------*/
float input_signal[NEAI_INPUT_SIGNAL_LENGTH * NEAI_INPUT_AXIS_NUMBER]; // Buffer of input values
float probabilities[NEAI_NUMBER_OF_CLASSES]; // Buffer of class probabilities
/* Private function prototypes defined by user ---------------------------------*/
/*
 * @brief Collect data process
 *
 * This function is defined by user, depends on applications and sensors
 *
 * @param input_signal: [in, out] buffer of sample values
 * @retval None
 * @note   If NEAI_INPUT_AXIS_NUMBER = 3 (cf NanoEdgeAI.h), the buffer must be
 *         ordered as follow:
 *         [x0 y0 z0 x1 y1 z1 ... xn yn zn], where xi, yi and zi
 *         are the values for x, y and z axes, n is equal to
 *         NEAI_INPUT_SIGNAL_LENGTH (cf NanoEdgeAI.h)
 */
void fill_buffer(float *input_signal)
{
    /* USER BEGIN */
    /* USER END */
}
/* -----------------------------------------------------------------------------*/
int main(void)
{
    /* Initialization ------------------------------------------------------------*/
    enum neai_state error_code = neai_classification_init();
    if (error_code != NEAI_OK) {
        /* Check the returned error code (cf NanoEdgeAI.h). */
    }

    /* Classification ------------------------------------------------------------*/
    int id_class = 0;
    while (1) {
        fill_buffer(input_signal);
        neai_classification(input_signal, probabilities, &id_class);
        /* USER BEGIN */
        /*
        * e.g.: Trigger functions depending on id_class
        * (print output class probabilities using probabilities[id_class],
        * print the name of the associated class using the getter function defined
        * in NanoEdgeAI.h, neai_get_class_name(id_class),
        * blink LED, ring alarm, etc.).
        */
        /* USER END */
    }
}

3. Resources

Documentation
All NanoEdge AI Studio documentation is available here.

Tutorials
Step-by-step tutorials to use NanoEdge AI Studio to build a smart device from A to Z.