-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMSE3D.h
175 lines (120 loc) · 4.01 KB
/
MSE3D.h
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#pragma once
#include "ISignalOp.h"
extern bool FileSave;
extern unsigned int SaveDirIndex;
using namespace System;
namespace SignalProcessing
{
ref class MSE3D : public Operation {
private:
unsigned int Order;
String ^name;
double threshold;
public:
MSE3D() {
this->Name = "MSE3D";
}
property String ^Name {
String ^get() {
return name;
}
void set(String ^val) {
name = val;
}
}
property double Threshold {
double get() {
return threshold;
}
void set(double val) {
threshold = val;
}
}
virtual void SetName(String ^p_name)
{
this->Name = p_name;
}
virtual String ^GetName()
{
return this->Name;
}
virtual array<array<float>^> ^ PreprocessActionTemplate(array<array<float> ^> ^p_saved_action_definition, array<int> ^p_sdi)
{
return p_saved_action_definition;
}
virtual virtual void SetFileSaveEnabled(boolean val, double threshold)
{
Threshold = threshold;
FileSave = true;
}
virtual array<ViewSerialEvent> ^Apply(array<ViewSerialEvent> ^p_action)
{
return nullptr;
}
virtual array<double>^ Apply(array<array<float> ^> ^p_saved_action_definition, array<array<float> ^> ^p_last_actions, array<int> ^p_data_offsets) override
{
array<double> ^distances = gcnew array<double>(p_data_offsets->Length);
array<double> ^ mse = gcnew array<double>(p_data_offsets->Length);
double action_original_length = 0;
unsigned int sensor_data_index_of_saved = 0;
unsigned int sensor_data_index_of_last = p_data_offsets[0];
const unsigned int const saved_action_length = p_saved_action_definition->Length;
const unsigned int const last_action_length = p_last_actions->Length;
double sum_of_squares = 0.0f;
for (int sdi = 0; sdi < p_data_offsets->Length; sdi++) {
sum_of_squares = 0;
action_original_length = 0;
sensor_data_index_of_last = p_data_offsets[sdi];
for (int i = 0; i < saved_action_length; i++) {
sum_of_squares = sum_of_squares + Math::Pow((p_last_actions[last_action_length - i - 1][sensor_data_index_of_last] - p_saved_action_definition[saved_action_length - i - 1][sdi]), 2);
action_original_length = action_original_length + Math::Pow(p_saved_action_definition[saved_action_length - i - 1][sensor_data_index_of_saved], 2);
}
mse[sdi] = sum_of_squares;
// distances[sdi] = Math::Sqrt(mse[sdi] / action_original_length);
distances[sdi] = Math::Sqrt(mse[sdi]);
}
if (distances[0] < threshold) {
FileSave = true;
}
if (FileSave) {
String ^dir = gcnew String(Environment::CurrentDirectory + "\\SensorData\\Signals\\"+ SaveDirIndex.ToString() + this->Name);
if (!IO::Directory::Exists(dir)) {
IO::Directory::CreateDirectory(dir);
}
/****************************/
String ^path = gcnew String(dir + "\\saved_action.txt");
System::IO::TextWriter ^pTextWriter;
if (!IO::File::Exists(path)) {
pTextWriter = gcnew System::IO::StreamWriter(path, true);
pTextWriter->Flush();
}
for (int j = 0; j < saved_action_length; j++) {
pTextWriter->Write(p_saved_action_definition[saved_action_length - j - 1][sensor_data_index_of_saved] + "\t");
}
pTextWriter->Flush();
pTextWriter->Close();
/****************************/
path = gcnew String(dir + "\\last_action.txt");
if (!IO::File::Exists(path)) {
pTextWriter = gcnew System::IO::StreamWriter(path, true);
pTextWriter->Flush();
}
for (int j = 0; j < saved_action_length; j++) {
pTextWriter->Write(p_last_actions[last_action_length - j - 1][sensor_data_index_of_last] + "\t");
}
pTextWriter->Flush();
pTextWriter->Close();
/************DISTANCE****************/
path = gcnew String(dir + "\\euclidian_distance.txt");
if (!IO::File::Exists(path)) {
pTextWriter = gcnew System::IO::StreamWriter(path, true);
pTextWriter->Flush();
}
pTextWriter->Write(distances[0] + "\t");
pTextWriter->Flush();
pTextWriter->Close();
}
return distances;
}
};
}