Project

General

Profile

Statistics
| Branch: | Revision:

root / src / scave / slidingwinavg.cc @ c87b95b0

History | View | Annotate | Download (2.48 KB)

1
//=========================================================================
2
//  SLIDINGWINAVG.CC - 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
#include <stdlib.h>
19
#include "channel.h"
20
#include "slidingwinavg.h"
21

    
22
USING_NAMESPACE
23

    
24

    
25
SlidingWindowAverageNode::SlidingWindowAverageNode(int windowSize)
26
{
27
    array = NULL;
28
    if (windowSize<1 || windowSize>100000)
29
        throw opp_runtime_error("slidingwinavg: invalid window size %d", windowSize);
30
    winsize = windowSize;
31
    array = new Datum[winsize];
32
    count = pos = 0;
33
    sum = 0;
34
}
35

    
36
SlidingWindowAverageNode::~SlidingWindowAverageNode()
37
{
38
    delete array;
39
}
40

    
41
bool SlidingWindowAverageNode::isReady() const
42
{
43
    return in()->length()>0;
44
}
45

    
46
void SlidingWindowAverageNode::process()
47
{
48
    do {
49
        Datum& arrayElem = array[pos];
50
        if (count<winsize)
51
            count++; // just starting up, filling window [0..pos]
52
        else
53
            sum -= arrayElem.y;
54
        in()->read(&arrayElem,1);
55
        sum += arrayElem.y;
56
        Datum o;
57
        o.x = arrayElem.x;
58
        o.y = sum/count;
59
        out()->write(&o,1);
60
        pos = (pos+1) % winsize;
61
    }
62
    while (in()->length()>0);
63
}
64

    
65
//-----
66

    
67
const char *SlidingWindowAverageNodeType::getDescription() const
68
{
69
    return "Replaces every value with the mean of values in the window:\n"
70
           "yout[k] = SUM(y[i],i=k-winsize+1..k)/winsize";
71
}
72

    
73
void SlidingWindowAverageNodeType::getAttributes(StringMap& attrs) const
74
{
75
    attrs["windowSize"] = "window size";
76
}
77

    
78
void SlidingWindowAverageNodeType::getAttrDefaults(StringMap& attrs) const
79
{
80
    attrs["windowSize"] = "10";
81
}
82

    
83
Node *SlidingWindowAverageNodeType::create(DataflowManager *mgr, StringMap& attrs) const
84
{
85
    checkAttrNames(attrs);
86

    
87
    const char *ws = attrs["windowSize"].c_str();
88
    int windowSize = atoi(ws);
89

    
90
    Node *node = new SlidingWindowAverageNode(windowSize);
91
    node->setNodeType(this);
92
    mgr->addNode(node);
93
    return node;
94
}
95

    
96
void SlidingWindowAverageNodeType::mapVectorAttributes(/*inout*/StringMap &attrs) const
97
{
98
    attrs["type"] = "double";
99
}
100