Sine wave predictor

This document provides a guide to using Consentium's TinyML library for a continuous sine function inference example. This example uses TensorFlow Lite Micro with an Edge board compatible with ESP32/Raspberry Pi Pico W.

Overview

This example demonstrates how to perform continuous inference on a sine function using a TensorFlow Lite model. The model predicts the sine value for a given input ( x ), and the results are compared with the actual sine value calculated using the sin function.

Prerequisites

  • Hardware: ESP32 or Raspberry Pi Pico W compatible Edge board.

  • Software: Consentium's TinyML library, EdgeNeuron library, TensorFlow Lite Micro.

Code Explanation

Includes and Constants

#include <EdgeNeuron.h>
#include "model.h"

// Tensor arena for TensorFlow Lite to store tensors
constexpr int kTensorArenaSize = 2000;
alignas(16) uint8_t tensor_arena[kTensorArenaSize];
  • EdgeNeuron.h: Include the EdgeNeuron library.

  • model.h: Include the header file for the TensorFlow Lite model.

  • tensor_arena: Defines the memory arena for TensorFlow Lite to manage tensor allocations.

Global Variables

float x = 0.0;
float step = 0.1; // Step size for x increments
  • x: The input value for the sine function.

  • step: The increment value for x in each loop iteration.

Setup Function

void setup() {
  Serial.begin(9600);
  while (!Serial);

  Serial.println("Continuous sine function inference example.");
  Serial.println("Initializing TensorFlow Lite Micro Interpreter...");

  // Initialize the model
  if (!initializeModel(model, tensor_arena, kTensorArenaSize)) {
    Serial.println("Model initialization failed!");
    while (true);  // Halt execution on initialization failure
  }

  Serial.println("Model initialization done.");
  Serial.println("Running continuous inference on sine function.");
}
  • Initializes the serial communication.

  • Prints initialization messages.

  • Calls initializeModel() to load and set up the TensorFlow Lite model.

Loop Function

void loop() {
  // Ensure x stays within the [0, 2π] range (reset after 2π)
  if (x > 6.28) {
    x = 0.0;
  }

  // Set input value in the model's input tensor
  setModelInput(x, 0);

  // Run the inference
  if (!runModelInference()) {
    Serial.println("Inference Failed!");
    return;
  }

  // Get the predicted output
  float y_predicted = getModelOutput(0);

  // Get the actual sine of x
  float y_actual = sin(x);

  // Print both the predicted and actual sine values
  Serial.print("Input x: ");
  Serial.print(x, 2);
  Serial.print(" | Predicted sin(x): ");
  Serial.print(y_predicted, 2);
  Serial.print(" | Actual sin(x): ");
  Serial.println(y_actual, 2);

  // Increment x by the defined step size for the next loop iteration
  x += step;

  // Add a small delay to make the output readable in the Serial Monitor
  delay(500);  // Adjust delay as needed for your use case
}
  • Resets x to 0 if it exceeds (2\pi).

  • Sets the input value x in the model.

  • Runs the inference and retrieves the output.

  • Calculates and prints both the predicted and actual sine values.

  • Increments x and includes a delay to make the output readable.

License

This code is licensed under the MIT license. All text in the license header must be included in any redistribution.

Last updated

Was this helpful?