Overview
========
This application detects anomalies in accelerometer sensor readings 
using autoencoder, a neural network that learns in unsupervides manner.
The application is based on NXP's agm01_tensorflow_lite_adt application
for the i.MX RT1062 EVK. NXP provides an excellent user guide that describes
the application:

https://www.nxp.com/docs/en/user-guide/IMXRTADUG.pdf

Hardware
========
This application can be run on Embedded Artists iMX RT1176 Developer's Kit
together with the Mikroe 6DOF IMU 3 Click module.

https://www.mikroe.com/6dof-imu-3-click

Changes
======= 
Compared to the original application there are a number of changes.

1. Only 3 accelerometer readings are collected where the original application
   collected 3 accelerometer and 3 gyroscope readings. 
2. By default the model is trained on 10000 readings instead of only 1000
   readings. The reason is to make it easier to introduce some noise
   while collecting data.  

Collect data
=============
The first step is to collect "good" sensor data. Set the DATA_COLLECT symbol
definition to true in adt.cpp, build and run the application. By default,
10000 samples are collected. This number can be changed in the SAMPLE_NUM symbol
definition. Data is printed to an external console. The recommendation is to 
use TeraTerm and to save the output to a log file (File -> Log).
The board is put on a stable surface. While data is collected you can introduce
some (valid) noise by gently tapping with your fingers on the table beside the
board. 

Train the autoencoder model
===========================

  Install Python libraries
  ------------------------

  Create new Anaconda environment (optional)

  >> conda create -n adt pip
  >> activate adt

  Install Python libraries from requirements.txt placed in the adt_train folder

  >> pip install -r requirements.txt

  Run the adt_train.py script
  ---------------------------

  >> python adt_train.py

  The script creates an autoencoder model and trains it with data loaded from
  input_data.txt file. The trained model is converted into a tflite model and dumped
  as a C header file. The trained Keras model, converted tflite model and
  dumped C header file are stored as keras_model.h5, adt_model.tflite and 
  adt_model.h files.
  The script also creates a parameters.h file with parameters of the model.
  
Run the application
===================

Copy adt_model.h and parameters.h files into the project. Set the DATA_COLLECT 
symbol definition value to false and run the application.  

When the application doesn't detect any anomalies the output will look like:

(%29 us) 67.14
(%31 us) 60.6
(%30 us) 23.72
(%33 us) 40.61
(%32 us) 30.94
(%24 us) 42.95
(%30 us) 28.71
(%33 us) 41.47
(%32 us) 18.25
(%33 us) 46.34
(%33 us) 22.43
(%33 us) 46.93
(%32 us) 11.88
(%32 us) 48.3
(%33 us) 33.67
(%34 us) 44.61
(%20 us) 8.11
(%29 us) 34.41


When anomalies are detected (e.g. when you tap on the table):


(%29 us) 237.24
(%29 us) 275.91
(%34 us) 740.24 anomaly detected!!!

(%31 us) 632.11 anomaly detected!!!

(%30 us) 494.67 anomaly detected!!!

(%31 us) 422.59 anomaly detected!!!

(%30 us) 63.16
(%35 us) 895.33 anomaly detected!!!

(%33 us) 7408.64 anomaly detected!!!
  
