-
Notifications
You must be signed in to change notification settings - Fork 1
/
processWindSensorData.js
172 lines (113 loc) · 4.31 KB
/
processWindSensorData.js
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
// process wind speed / guest
on({id: "0_userdata.0.IoT.WindSensor.WindSpeedArray", change: "any"}, function (obj) {
var value = obj.state.val;
var oldValue = obj.oldState.val;
setState('0_userdata.0.IoT.Weather.WindSpeed', calc_PDF(value)); //in km/h
setState('0_userdata.0.IoT.Weather.WindGuest', calc_MAX(value)); //in km/h
});
// process wind direction
on({id: "0_userdata.0.IoT.WindSensor.WindDirectionArray", change: "any"}, function (obj) {
var value = obj.state.val;
var oldValue = obj.oldState.val;
var WindDirectionNumeric = calc_PDF(value);
setState('0_userdata.0.IoT.Weather.WindDirectionNumeric', WindDirectionNumeric);
setState('0_userdata.0.IoT.Weather.WindDirection', get_WindDirectionName(WindDirectionNumeric));
});
// ################################### get wind direction name
function get_WindDirectionName(WindDirectionNumeric) {
switch (WindDirectionNumeric) {
case 22: return "Nord-Nordost";
case 45: return "Nordost";
case 67: return "Ost-Nordost";
case 90: return "Ost";
case 112: return "Ost-Südost";
case 135: return "Südost";
case 157: return "Süd-Südost";
case 180: return "Süd";
case 202: return "Süd-Südwest";
case 225: return "Südwest";
case 247: return "West-Südwest";
case 270: return "West";
case 292: return "West-Nordwest";
case 315: return "Nordwest";
case 337: return "Nord-Nordwest";
case 360: return "Nord";
}
return "n/a";
}
// ################################### get maximum
function calc_MAX(inputString) {
var i = 0;
// split string into an array and parse into an numeric array
var StrArray = inputString.split(",");
let NumArray = new Array();
var length = StrArray.length;
for (i = 0; i < length; i++ ) {
NumArray[i] = parseFloat(StrArray[i]);
};
return (Math.max.apply(Math, NumArray));
}
// ################################### calc mean value (normal distribution)
function calc_PDF(inputString) {
var i = 0;
// split string into an array and parse into an numeric array
var StrArray = inputString.split(",");
let NumArray = new Array();
var length = StrArray.length;
for (i = 0; i < length; i++ ) {
NumArray[i] = parseFloat(StrArray[i]);
};
// calculate the arithmetic mean
var sum = 0;
for (i = 0; i < length; i++ ) {
sum += NumArray[i];
};
var arMean = sum / length;
// calculate the variant and standard deviation
var x = 0;
for (i = 0; i < length; i++ ) {
x += Math.pow(NumArray[i] - arMean, 2);
};
var varinat = 1 / (length - 1) * x;
var stdDev = Math.sqrt(varinat);
// calculate the normal distribution (Probability Density Function)
let PDFArray = new Array();
let PDFparam = new Array();
PDFparam[0] = arMean;
PDFparam[1] = stdDev;
PDFArray = pdf("norm" ,NumArray, PDFparam);
// get the median and the predicted value
var median = Math.max.apply(Math, PDFArray);
const medianNumber = (element) => element == median;
var index = PDFArray.findIndex(medianNumber);
//for debugging
/*
var max = Math.max.apply(Math, NumArray);
var min = Math.min.apply(Math, NumArray);
console.log("sum = " + sum + ", arMean = " + arMean + ", varinat = " + varinat + ", stdDev = " + stdDev + ", median = " + median + ", index = " + index);
*/
return(NumArray[index]);
}
// ################################### PDF algorithem (University of Utah)
function pdf(type,xpdft,paramt) {
var ypdf = new Array;
if (xpdft.constructor != Array) {
var xpdf = new Array;
xpdf[0] = xpdft;
}
else {xpdf = xpdft;}
if (paramt.constructor != Array) {
var param = new Array;
param[0] = paramt;
}
else {param = paramt}
if (type=='norm') { //normal, gaussian distribution
if (param == null) {param = new Array(0,1);}
var c1 = Math.sqrt(1 / (2 * Math.PI)) / param[1];
var c2 = 1 / (2 * param[1] * param[1]);
for ( var ip = 0; ip < xpdf.length; ip++ ){
ypdf[ip] = c1 * Math.exp(-(xpdf[ip] - param[0]) * (xpdf[ip] - param[0]) * c2);
}
}
return ypdf;
}