-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassify.h
More file actions
141 lines (125 loc) · 3.79 KB
/
classify.h
File metadata and controls
141 lines (125 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#ifndef _CLASSIFY_H_
#define _CLASSIFY_H_
#define BUFFER_SIZE 60
#include <stdint.h>
#include "asr.h"
/**
* Approximates sigmoid with the given value
*/
static void sigmoid(int32_t* var);
/**
* Classifications this MLP is capable of
*/
typedef enum
{
PEAK = 0,
TROU = 1,
NOT_EX = 2
} ExType;
/**
* Weights for the connections between the MLP neural network.
*
* This represent float which have been multiplied by 2^12 and then
* rounded to the nearest int. As such, when multiplying two of these
* together, remember to shift down by 12 afterwards.
*/
const int32_t W[131] =
{
/* Input Layer: */
/* Base: */
/* Node A: */ -43403,
/* Node B: */ 30268,
/* Scales: */
/* | Ax | Bx | Ay | By | Az | Bz */
/* 01 */ 464, 23676, 499, 8908, -10668, 11246,
/* 02 */ 1482, 1298, 5179, -565, -9677, 16349,
/* 03 */ 5083, -8960, 510, -1656, -6787, 3242,
/* 04 */ 5925, -8889, -3035, -4796, -5080, -10290,
/* 05 */ -7241, 5360, -2785, -1244, 1751, -3528,
/* 06 */ -14774, 9158, -5371, 797, 10074, -313,
/* 07 */ -3100, 11701, -2773, -5160, 813, 3781,
/* 08 */ -4060, 13962, 5938, -10342, 2956, -3331,
/* 09 */ -6115, 20771, 9524, -5828, 1338, -2575,
/* 10 */ -8448, 30821, 3333, 8536, 4551, -9402,
/* 11 */ -10063, 24610, -3425, 13005, 5505, -18427,
/* 12 */ -11923, 21109, -7792, 5357, 6195, -21152,
/* 13 */ -8048, 15546, -6207, 7038, 9076, -20100,
/* 14 */ -3547, 14209, -6307, 13832, 6578, -17065,
/* 15 */ -137, 14819, -9717, 20252, 9355, -14432,
/* 16 */ 2341, 5250, -8438, 23385, 5112, -2703,
/* 17 */ 6296, 3696, -8911, 11275, -658, -14108,
/* 18 */ 3277, -1036, -13273, -6731, -6120, -16144,
/* 19 */ 1310, -8152, -13371, -7781, -8820, 463,
/* 20 */ -936, -20966, -13463, 1216, -6256, -14997,
/* Output Layer: */
/* Base | Node A | Node B */
/* EX_PEAK: */ -21807, -27118, 34204,
/* EX_TROU: */ -20642, 37124, -22091,
/* NOT_EX: */ 22397, -39003, -34865
};
/**
* Classifies the data in the given buffer using the hardcoded neural network
*
* @param int8_t rear The index of the last element in the buffer
* @param int16_t[] buffer A circular buffer
* @return ExType The classified form of movement
*/
static inline ExType classify(int8_t rear, int16_t buffer[])
{
int32_t* offset=(int32_t*)W;
int32_t nodeA = *offset++;
int32_t nodeB = *offset++;
int8_t j = rear;
do
{
j = (j + 1) % BUFFER_SIZE;
nodeA += asr(*offset++ * buffer[j], 12);
nodeB += asr(*offset++ * buffer[j], 12);
} while (j != rear);
sigmoid(&nodeA);
sigmoid(&nodeB);
int32_t v[3];
for (int32_t* i = v; i < v + 3; i++)
{
*i = *offset++;
*i += asr(*offset++ * nodeA, 12);
*i += asr(*offset++ * nodeB, 12);
sigmoid(i);
}
// Detects peak or trough
if (v[0] > v[2] && v[0] > v[1])
{
return PEAK;
}
else if (v[1] > v[2])
{
return TROU;
}
else //Not exercise
{
return NOT_EX;
}
}
/**
* Approximates Sigmoid for the given value
*
* @param int32_t* var Pointer to an int value
*/
static void sigmoid(int32_t* var)
{
if (*var < -5 << 12)
{
*var = 0;
}
else if (*var > 5 << 12)
{
*var = 1 << 12;
}
else
{
//*var *= 410;
//*var = asr(*var, 12);
*var = ((410 * *var) >> 12) + 2048;
}
}
#endif