Statistics
| Branch: | Revision:

root / src / scave / windowavg.h @ e1750c09

History | View | Annotate | Download (3.28 KB)

1
//=========================================================================
2
//  WINDOWAVG.H - part of
3
//                  OMNeT++/OMNEST
4
//           Discrete System Simulation in C++
5
//
6
//  Author: Andras Varga
7
//
8
//=========================================================================
9

    
10
/*--------------------------------------------------------------*
11
  Copyright (C) 1992-2008 Andras Varga
12
  Copyright (C) 2006-2008 OpenSim Ltd.
13

14
  This file is distributed WITHOUT ANY WARRANTY. See the file
15
  `license' for details on this and other legal matters.
16
*--------------------------------------------------------------*/
17

    
18
#ifndef _WINDOWAVG_H_
19
#define _WINDOWAVG_H_
20

    
21
#include "commonnodes.h"
22

    
23
NAMESPACE_BEGIN
24

    
25

    
26
/**
27
 * Processing node which performs window average. Replaces every batch
28
 * of values with their means.
29
 */
30
class SCAVE_API WindowAverageNode : public FilterNode
31
{
32
    protected:
33
        int winsize;
34
        Datum *array;
35

    
36
    public:
37
        WindowAverageNode(int windowSize);
38
        virtual ~WindowAverageNode();
39
        virtual bool isReady() const;
40
        virtual void process();
41
};
42

    
43
class SCAVE_API WindowAverageNodeType : public FilterNodeType
44
{
45
    public:
46
        virtual const char *getName() const {return "winavg";}
47
        virtual const char *getDescription() const;
48
        virtual void getAttributes(StringMap& attrs) const;
49
        virtual void getAttrDefaults(StringMap& attrs) const;
50
        virtual Node *create(DataflowManager *mgr, StringMap& attrs) const;
51
        virtual void mapVectorAttributes(/*inout*/StringMap &attrs, /*out*/StringVector &warnings) const;
52
};
53

    
54
/**
55
 *
56
 */
57
class SCAVE_API TimeWindowAverageNode : public FilterNode
58
{
59
    protected:
60
        simultime_t winsize;
61
        simultime_t win_end;
62
        double sum;
63
        long count;
64
        void collect(const Datum &d) { sum += d.y; count++; }
65
        void outputWindowAverage();
66
        bool inCurrentWindow(const Datum &d) const { return (!d.xp.isNil() && d.xp < win_end) || d.x < win_end.dbl(); }
67
        void moveWindow(const Datum &d);
68

    
69
    public:
70
        TimeWindowAverageNode(simultime_t windowSize);
71
        virtual ~TimeWindowAverageNode();
72
        virtual bool isFinished() const;
73
        virtual bool isReady() const;
74
        virtual void process();
75
};
76

    
77
inline void TimeWindowAverageNode::outputWindowAverage()
78
{
79
    if (count > 0)
80
    {
81
        Datum d;
82
        d.x = win_end.dbl();
83
        d.xp = win_end;
84
        d.y = sum / count;
85
        out()->write(&d, 1);
86
        sum = 0.0;
87
        count = 0;
88
    }
89
}
90

    
91
inline void TimeWindowAverageNode::moveWindow(const Datum &d)
92
{
93
    BigDecimal x = d.xp.isNil() ? BigDecimal(d.x) : d.xp;
94
    if (x < win_end + winsize)
95
        win_end += winsize;
96
    else
97
    {
98
        BigDecimal k = floor(x / winsize);
99
        win_end = (k.dbl() * winsize) + winsize;
100
    }
101
}
102

    
103
class SCAVE_API TimeWindowAverageNodeType : public FilterNodeType
104
{
105
    public:
106
        virtual const char *getName() const {return "timewinavg";}
107
        virtual const char *getDescription() const;
108
        virtual void getAttributes(StringMap& attrs) const;
109
        virtual void getAttrDefaults(StringMap& attrs) const;
110
        virtual Node *create(DataflowManager *mgr, StringMap& attrs) const;
111
        virtual void mapVectorAttributes(/*inout*/StringMap &attrs, /*out*/StringVector &warnings) const;
112
};
113

    
114

    
115
NAMESPACE_END
116

    
117

    
118
#endif
119

    
120