aboutsummaryrefslogtreecommitdiffstats
path: root/filter.c
blob: 74ae3fdfda8ec16c6a933a2b506a131c7b34a038 (plain)
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
/* $Id: filter.c,v 1.2 2000/03/06 06:04:06 reinelt Exp $
 *
 *  smooth and damp functions
 *
 * Copyright 1999, 2000 by Michael Reinelt (reinelt@eunet.at)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *
 * $Log: filter.c,v $
 * Revision 1.2  2000/03/06 06:04:06  reinelt
 *
 * minor cleanups
 *
 *
 */

/* 
 *
 * exported fuctions:
 *
 * smooth (name, period, value)
 *   returns an average value over a given period
 *   uses global variable "tick"
 *
 * damp (name, value)
 *   damps a value with exp(-t/tau) 
 *   uses global variable "tau"
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <sys/time.h>

#include "filter.h"

extern int tick;
extern int tau;

#define SLOTS 64
#define SECONDS(x) (x.tv_sec+x.tv_usec/1000000.0)

typedef struct {
  char *name;
  int slots;
  struct timeval *time;
  double *value;
} FILTER;


double smooth(char *name, int period, double value)
{
  static FILTER *Filter=NULL;
  static int nFilter=0;
  struct timeval now;
  double t, v;
  int i, j;
  
  gettimeofday (&now, NULL);
  
  for (i=0; i<nFilter; i++) {
    if (strcmp(name, Filter[i].name)==0) 
      break;
  }
  
  if (i==nFilter) {
    int slots=(period+tick-1)/tick;
    if (slots<2) 
      slots=2;
    else if (slots>SLOTS)
      slots=SLOTS;

    nFilter++;
    Filter=realloc(Filter, nFilter*sizeof(FILTER));
    Filter[i].name=strdup(name);
    Filter[i].slots=slots;
    Filter[i].time=malloc(slots*sizeof(Filter[i].time[0]));
    Filter[i].value=malloc(slots*sizeof(Filter[i].value[0]));
    for (j=0; j<slots; j++) {
      Filter[i].time[j]=now;
      Filter[i].value[j]=value;
    }
  }
  
  for (j=Filter[i].slots-1; j>0; j--) {
    Filter[i].time[j]=Filter[i].time[j-1];
    Filter[i].value[j]=Filter[i].value[j-1];
  }
  Filter[i].time[0]=now;
  Filter[i].value[0]=value;
  
  t = SECONDS(Filter[i].time[0]) - SECONDS(Filter[i].time[Filter[i].slots-1]);
  v = Filter[i].value[0]-Filter[i].value[Filter[i].slots-1];

  if (t==0.0 || v<0.0)
    return 0;
  else
    return v/t;
}


double damp(char *name, double value)
{
  static FILTER *Filter=NULL;
  static int nFilter=0;
  struct timeval now;
  double max;
  int i, j;
  
  if (tau==0.0)
    return value;
  
  gettimeofday (&now, NULL);
  
  for (i=0; i<nFilter; i++) {
    if (strcmp(name, Filter[i].name)==0) 
      break;
  }
  
  if (i==nFilter) {
    int slots=log(100)*tau/tick;
    if (slots<1) 
      slots=1;
    else if (slots>SLOTS)
      slots=SLOTS;

    nFilter++;
    Filter=realloc(Filter, nFilter*sizeof(FILTER));
    Filter[i].name=strdup(name);
    Filter[i].slots=slots;
    Filter[i].time=malloc(slots*sizeof(Filter[i].time));
    Filter[i].value=malloc(slots*sizeof(Filter[i].value));
    for (j=0; j<slots; j++) {
      Filter[i].time[j]=now;
      Filter[i].value[j]=0;
    }
  }
  
  max=value;
  for (j=Filter[i].slots-1; j>0; j--) {
    double t = SECONDS(Filter[i].time[j]) - SECONDS(Filter[i].time[j-1]);
    Filter[i].time[j]=Filter[i].time[j-1];
    Filter[i].value[j]=Filter[i].value[j-1]*exp(-t/tau);
    if (Filter[i].value[j]>max) max=Filter[i].value[j];
  }

  Filter[i].time[0]=now;
  Filter[i].value[0]=value;
  
  return max;
}