NanoEdge AI Library for extrapolation (E)

1. What is NanoEdge AI Library for extrapolation?

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 extrapolate a numerical value from sensor patterns, by themselves, without the need for the user to have additional skills in Mathematics, Machine Learning, or data science.

2. Install / Getting started

The main functions available via the library are:

neai_extrapolation_init() run first before the extrapolation process
neai_extrapolation() run an extrapolation iteration to predict a value

2.1. Static library

In NanoEdge AI Studio, after obtaining a library, click Compile on the "Deployment" screen.

The .zip file obtained contains:

  • libneai.a: the static precompiled NanoEdge AI library
  • NanoEdgeAI.h: the NanoEdge AI header file, with the function declarations and the library constants
  • emulators/: the NanoEdge AI Emulator (Python script nanoedgeai_studio_emulator.py and compiled libraries libneai.dll for Windows® and libneai.so for Linux®), see the NanoEdge AI Emulator page
  • metadata.json: library metadata (target, algorithm type, estimated RAM and flash usage, performance)
  • artifacts/: JSON files describing the model parameters and the preprocessing configuration and parameters
  • docs/: the documentation of the library in PDF format
  • for_llms.txt: an example prompt to give to an LLM, together with metadata.json and the JSON files of artifacts/, to get a complete Markdown description of the library (preprocessing, ML configuration and parameters)

To use the library, add libneai.a and NanoEdgeAI.h to your project, then link the static library to your project code. For example in STM32CubeIDE, go to Project -> Properties -> C/C++ Build -> Settings -> MCU GCC Linker -> Libraries. Add neai in the Libraries section and the libneai.a path in the Library search path section. Click Apply and Close.

2.2. NanoEdge AI Library functions

2.2.1. Initialization

enum neai_state neai_extrapolation_init(void);

Initialization must be called at the beginning.

2.2.2. Extrapolation

enum neai_state neai_extrapolation(float *in, float *prediction);

This function returns the predicted target value.

  • Input:
float *in, input buffer, the length of the buffer is NEAI_INPUT_SIGNAL_LENGTH * NEAI_INPUT_AXIS_NUMBER
float *prediction: pointer to the predicted value
  • Output:
predicted value
the neai_state enum

Most NanoEdge AI functions 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: extrapolation function was called without running the init function first
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
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.3. Example "Hello World!"

Header file: NanoEdgeAI.h

NanoEdgeAI.h is provided in the .zip file that you download by clicking Compile in NanoEdge AI Studio. It declares the following elements:

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.


#ifndef NANOEDGEAI_H
#define NANOEDGEAI_H

#include <stdint.h>
#include <stdbool.h>

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

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

/* 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

/* ===== Extrapolation API ===== */
enum neai_state neai_extrapolation_init(void);
enum neai_state neai_extrapolation(float *in, float *prediction);

/* ===== Common getter functions ===== */
char* neai_get_id(void);
int neai_get_input_signal_size(void);
int neai_get_axis_number(void);


#ifdef __cplusplus
}
#endif

#endif /* NANOEDGEAI_H */

Main program: main.c

This program must be completed by the user (depending for instance on the application 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.


/* Includes ------------------------------------------------------------------*/
#include "NanoEdgeAI.h"

float input_signal[NEAI_INPUT_SIGNAL_LENGTH * NEAI_INPUT_AXIS_NUMBER];

/*
 * @brief Collect data process
 *
 * This function is defined by user, depends on applications and sensors
 *
 * @param input_signal: [in, out] buffer of sample values
 * @note   If NEAI_INPUT_AXIS_NUMBER = 3 (cf NanoEdgeAI.h), the buffer must be
 *         ordered as follows:
 *         [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_extrapolation_init();

    if (error_code != NEAI_OK) {
        /* Check the returned error code (cf NanoEdgeAI.h). */
    }

    /* Extrapolation -------------------------------------------------------------*/
    float prediction = 0.0f;
    while (1) {
        fill_buffer(input_signal);
        neai_extrapolation(input_signal, &prediction);
        /* USER BEGIN */
        /*
         * e.g.: Trigger functions depending on predicted value, prediction
         * (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.