Преглед изворни кода

Improve rate of temperature change calculation.

Neal Wilson пре 11 година
родитељ
комит
ee05649a1c
1 измењених фајлова са 20 додато и 13 уклоњено
  1. 20
    13
      src/rate.w

+ 20
- 13
src/rate.w Прегледај датотеку

@@ -29,6 +29,7 @@ class RateOfChange : public QObject
29 29
 		int ct;
30 30
 		int st;
31 31
 		QList<Measurement> cache;
32
+		QMap<double,double> smoothCache;
32 33
 };
33 34
 
34 35
 @ The interesting part of this class is in the |newMeasurement()| method. This
@@ -80,23 +81,29 @@ if(cache.size() > 2)
80 81
 	}
81 82
 }
82 83
 
83
-@ To calculate the rate of change, we compare both the time and the
84
-temperature of the first and last measurements in the cache and reduce this to
85
-a change per second value. This is then multiplied by the scale time and sent
86
-to whatever objects require the derived series data. Note that |tdiff| will
87
-generally be very close to the cache time except where this is set to zero, but
88
-it will almost never be exact. Basing the calculation on the data we have
89
-instead of on the data we wish we had should result in better stability in the
90
-derived series.
84
+@ When calculating the rate of change we are doing something that will
85
+fundamentally increase the noise expressed through the derived series.
86
+Delivering a derived series that both lacks excessive volatility and remains
87
+acceptably accurate requires careful tuning.
91 88
 
92 89
 The measurement will carry the fact that it is a relative measurement.
93 90
 
94 91
 @<Calculate rate of change@>=
95
-double mdiff = cache.back().temperature() - cache.front().temperature();
96
-double tdiff = cache.front().time().msecsTo(cache.back().time()) / 1000.0;
97
-double dps = mdiff / tdiff;
98
-double scale = dps * st;
99
-Measurement value(scale, cache.back().time(), cache.back().scale());
92
+QList<double> rates;
93
+for(int i = 1; i < cache.size(); i++)
94
+{
95
+	double mdiff = cache.at(i).temperature() - cache.at(i-1).temperature();
96
+	double tdiff = (double)(cache.at(i-1).time().msecsTo(cache.at(i).time())) / 1000.0;
97
+	rates.append(mdiff/tdiff);
98
+}
99
+double acc = 0.0;
100
+for(int i = 0; i < rates.size(); i++)
101
+{
102
+	acc += rates.at(i);
103
+}
104
+double pavg = acc /= rates.size();
105
+double v2 = pavg * st;
106
+Measurement value(v2, cache.back().time(), cache.back().scale());
100 107
 value.insert("relative", true);
101 108
 emit measurement(value);
102 109
 

Loading…
Откажи
Сачувај