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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
-- |
-- Module: FRP.Netwire.Analyze
-- Copyright: (c) 2013 Ertugrul Soeylemez
-- License: BSD3
-- Maintainer: Ertugrul Soeylemez <es@ertes.de>
module FRP.Netwire.Analyze
( -- * Linear graphs
lAvg,
lGraph,
lGraphN,
-- * Staircase graphs
sAvg,
sGraph,
sGraphN,
-- * Peaks
highPeak,
highPeakBy,
lowPeak,
lowPeakBy,
-- * Debug
avgFps,
framerate
)
where
import Control.Wire
import qualified Data.Foldable as F
import qualified Data.Sequence as Seq
import qualified FRP.Netwire.Utils.Timeline as Tl
import Prelude hiding ((.), id)
-- | Average framerate over the last given number of samples. One
-- important thing to note is that the value of this wire will generally
-- disagree with 'sAvg' composed with 'framerate'. This is expected,
-- because this wire simply calculates the arithmetic mean, whereas
-- 'sAvg' will actually integrate the framerate graph.
--
-- Note: This wire is for debugging purposes only, because it exposes
-- discrete time. Do not taint your application with discrete time.
--
-- * Complexity: O(n) time and space wrt number of samples.
avgFps ::
(RealFloat b, HasTime t s)
=> Int -- ^ Number of samples.
-> Wire s e m a b
avgFps int | int < 1 = error "avgFps: Non-positive number of samples"
avgFps int = loop Seq.empty
where
intf = fromIntegral int
afps = (/ intf) . F.foldl' (+) 0
loop ss' =
mkSF $ \ds _ ->
let fps = recip . realToFrac . dtime $ ds
ss = Seq.take int (fps Seq.<| ss')
in if isInfinite fps
then (afps ss', loop ss')
else ss `seq` (afps ss, loop ss)
-- | Current framerate.
--
-- Note: This wire is for debugging purposes only, because it exposes
-- discrete time. Do not taint your application with discrete time.
--
-- * Inhibits: when the clock stopped ticking.
framerate ::
(Eq b, Fractional b, HasTime t s, Monoid e)
=> Wire s e m a b
framerate =
mkPure $ \ds _ ->
let dt = realToFrac (dtime ds)
in (if dt == 0 then Left mempty else Right (recip dt), framerate)
-- | High peak.
--
-- * Depends: now.
highPeak :: (Ord a) => Wire s e m a a
highPeak = highPeakBy compare
-- | High peak with respect to the given comparison function.
--
-- * Depends: now.
highPeakBy :: (a -> a -> Ordering) -> Wire s e m a a
highPeakBy = peakBy GT
-- | Calculate the average of the signal over the given interval (from
-- now). This is done by calculating the integral of the corresponding
-- linearly interpolated graph and dividing it by the interval length.
-- See 'Tl.linAvg' for details.
--
-- Linear interpolation can be slow. If you don't need it, you can use
-- the staircase variant 'sAvg'.
--
-- Example: @lAvg 2@
--
-- * Complexity: O(s) space, O(s) time wrt number of samples in the
-- interval.
--
-- * Depends: now.
lAvg ::
(Fractional a, Fractional t, HasTime t s)
=> t -- ^ Interval size.
-> Wire s e m a a
lAvg int =
mkSF $ \ds x ->
let t = dtime ds in
(x, loop t (Tl.singleton t x))
where
loop t' tl' =
mkSF $ \ds x ->
let t = t' + dtime ds
t0 = t - int
tl = Tl.linCutL t0 (Tl.insert t x tl')
a = Tl.linAvg t0 t tl
in (a, loop t tl)
-- | Produce a linearly interpolated graph for the given points in time,
-- where the magnitudes of the points are distances from /now/.
--
-- Linear interpolation can be slow. If you don't need it, you can use
-- the faster staircase variant 'sGraph'.
--
-- Example: @lGraph [0, 1, 2]@ will output the interpolated inputs at
-- /now/, one second before now and two seconds before now.
--
-- * Complexity: O(s) space, O(n * log s) time, where s = number of
-- samples in the interval, n = number of requested data points.
--
-- * Depends: now.
lGraph ::
(Fractional a, Fractional t, HasTime t s)
=> [t] -- ^ Data points to produce.
-> Wire s e m a [a]
lGraph qts =
mkSF $ \ds x ->
let t = dtime ds in
(x <$ qts, loop t (Tl.singleton t x))
where
earliest = maximum (map abs qts)
loop t' tl' =
mkSF $ \ds x ->
let t = t' + dtime ds
tl = Tl.linCutL (t - earliest) (Tl.insert t x tl')
ps = map (\qt -> Tl.linLookup (t - abs qt) tl) qts
in (ps, loop t tl)
-- | Graph the given interval from now with the given number of evenly
-- distributed points in time. Convenience interface to 'lGraph'.
--
-- Linear interpolation can be slow. If you don't need it, you can use
-- the faster staircase variant 'sGraphN'.
--
-- * Complexity: O(s) space, O(n * log s) time, where s = number of
-- samples in the interval, n = number of requested data points.
--
-- * Depends: now.
lGraphN ::
(Fractional a, Fractional t, HasTime t s)
=> t -- ^ Interval to graph from now.
-> Int -- ^ Number of data points to produce.
-> Wire s e m a [a]
lGraphN int n
| int <= 0 = error "lGraphN: Non-positive interval"
| n <= 0 = error "lGraphN: Non-positive number of data points"
lGraphN int n =
let n1 = n - 1
f qt = realToFrac int * fromIntegral qt / fromIntegral n1
in lGraph (map f [0..n1])
-- | Low peak.
--
-- * Depends: now.
lowPeak :: (Ord a) => Wire s e m a a
lowPeak = lowPeakBy compare
-- | Low peak with respect to the given comparison function.
--
-- * Depends: now.
lowPeakBy :: (a -> a -> Ordering) -> Wire s e m a a
lowPeakBy = peakBy LT
-- | Given peak with respect to the given comparison function.
peakBy ::
(Eq o)
=> o -- ^ This ordering means the first argument is larger.
-> (a -> a -> o) -- ^ Compare two elements.
-> Wire s e m a a
peakBy o comp = mkSFN $ \x -> (x, loop x)
where
loop x' =
mkSFN $ \x ->
id &&& loop $
if comp x x' == o then x else x'
-- | Calculate the average of the signal over the given interval (from
-- now). This is done by calculating the integral of the corresponding
-- staircase graph and dividing it by the interval length. See
-- 'Tl.scAvg' for details.
--
-- See also 'lAvg'.
--
-- Example: @sAvg 2@
--
-- * Complexity: O(s) space, O(s) time wrt number of samples in the
-- interval.
--
-- * Depends: now.
sAvg ::
(Fractional a, Fractional t, HasTime t s)
=> t -- ^ Interval size.
-> Wire s e m a a
sAvg int =
mkSF $ \ds x ->
let t = dtime ds in
(x, loop t (Tl.singleton t x))
where
loop t' tl' =
mkSF $ \ds x ->
let t = t' + dtime ds
t0 = t - int
tl = Tl.scCutL t0 (Tl.insert t x tl')
a = Tl.scAvg t0 t tl
in (a, loop t tl)
-- | Produce a staircase graph for the given points in time, where the
-- magnitudes of the points are distances from /now/.
--
-- See also 'lGraph'.
--
-- Example: @sGraph [0, 1, 2]@ will output the inputs at /now/, one
-- second before now and two seconds before now.
--
-- * Complexity: O(s) space, O(n * log s) time, where s = number of
-- samples in the interval, n = number of requested data points.
--
-- * Depends: now.
sGraph ::
(Fractional t, HasTime t s)
=> [t] -- ^ Data points to produce.
-> Wire s e m a [a]
sGraph qts =
mkSF $ \ds x ->
let t = dtime ds in
(x <$ qts, loop t (Tl.singleton t x))
where
earliest = maximum (map abs qts)
loop t' tl' =
mkSF $ \ds x ->
let t = t' + dtime ds
tl = Tl.scCutL (t - earliest) (Tl.insert t x tl')
ps = map (\qt -> Tl.scLookup (t - abs qt) tl) qts
in (ps, loop t tl)
-- | Graph the given interval from now with the given number of evenly
-- distributed points in time. Convenience interface to 'sGraph'.
--
-- See also 'lGraphN'.
--
-- * Complexity: O(s) space, O(n * log s) time, where s = number of
-- samples in the interval, n = number of requested data points.
--
-- * Depends: now.
sGraphN ::
(Fractional t, HasTime t s)
=> t -- ^ Interval to graph from now.
-> Int -- ^ Number of data points to produce.
-> Wire s e m a [a]
sGraphN int n
| int <= 0 = error "sGraphN: Non-positive interval"
| n <= 0 = error "sGraphN: Non-positive number of data points"
sGraphN int n =
let n1 = n - 1
f qt = realToFrac int * fromIntegral qt / fromIntegral n1
in sGraph (map f [0..n1])
|