Branch data Line data Source code
1 : : /*
2 : : * history.cpp - history class implementation
3 : : *
4 : : * Copyright (C) 2006, 2007 Stefan Jahn <stefan@lkcc.org>
5 : : *
6 : : * This is free software; you can redistribute it and/or modify
7 : : * it under the terms of the GNU General Public License as published by
8 : : * the Free Software Foundation; either version 2, or (at your option)
9 : : * any later version.
10 : : *
11 : : * This software is distributed in the hope that it will be useful,
12 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : : * GNU General Public License for more details.
15 : : *
16 : : * You should have received a copy of the GNU General Public License
17 : : * along with this package; see the file COPYING. If not, write to
18 : : * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19 : : * Boston, MA 02110-1301, USA.
20 : : *
21 : : * $Id$
22 : : *
23 : : */
24 : :
25 : : #if HAVE_CONFIG_H
26 : : # include <config.h>
27 : : #endif
28 : :
29 : : #include <stdio.h>
30 : : #include <stdlib.h>
31 : : #include <string.h>
32 : : #include <cmath>
33 : :
34 : : #include "precision.h"
35 : : #include "tvector.h"
36 : : #include "poly.h"
37 : : #include "spline.h"
38 : : #include "history.h"
39 : :
40 : : namespace qucs {
41 : :
42 : :
43 : : /* This function drops those values in the history which are newer
44 : : than the specified time. */
45 : 0 : void history::truncate (const nr_double_t tcut)
46 : : {
47 : : int i;
48 : 0 : int ts = this->t->size ();
49 : :
50 [ # # ]: 0 : for (i = this->leftidx (); i < ts; i++)
51 : : {
52 [ # # ]: 0 : if ( (*this->t)[i] > tcut)
53 : : {
54 : 0 : break;
55 : : }
56 : : }
57 : 0 : this->truncate (ts - i);
58 : 0 : }
59 : :
60 : :
61 : : /* This function drops those values in the history which are older
62 : : than the specified age of the history instance. */
63 : 192469 : void history::drop (void) {
64 : 192469 : nr_double_t f = this->first ();
65 : 192469 : nr_double_t l = this->last ();
66 [ - + ][ # # ]: 192469 : if (age > 0.0 && l - f > age) {
67 : : unsigned int r;
68 : 0 : unsigned int i = this->leftidx ();
69 [ # # ]: 0 : for (r = 0; i < this->t->size (); r++, i++)
70 [ # # ]: 0 : if (l - (*this->t)[i] < age)
71 : 0 : break;
72 : : // keep 2 values being older than specified age
73 : 0 : r += this->unused () - 2;
74 [ # # ]: 0 : if (r > 127)
75 : : /* erase the first r value */
76 [ # # ][ # # ]: 0 : this->values->erase(this->values->begin(),this->values->begin()+r);
77 : : }
78 : 192469 : }
79 : :
80 : : /* Interpolates a value using 2 left side and 2 right side values if
81 : : possible. */
82 : 0 : nr_double_t history::interpol (nr_double_t tval, int idx, bool left) {
83 [ # # ][ # # ]: 0 : static spline spl (SPLINE_BC_NATURAL);
[ # # ][ # # ]
84 [ # # ][ # # ]: 0 : static tvector<nr_double_t> x (4);
[ # # ][ # # ]
85 [ # # ][ # # ]: 0 : static tvector<nr_double_t> y (4);
[ # # ][ # # ]
86 : :
87 [ # # ]: 0 : unsigned int n = left ? idx + 1: idx;
88 [ # # ][ # # ]: 0 : if (n > 1 && n + 2 < this->values->size ()) {
[ # # ]
89 : 0 : int i, k, l = this->leftidx ();
90 [ # # ]: 0 : for (k = 0, i = n - 2; k < 4; i++, k++) {
91 : 0 : x (k) = (*this->t)[i + l];
92 : 0 : y (k) = (*this->values)[i];
93 : : }
94 [ # # ][ # # ]: 0 : spl.vectors (y, x);
95 : 0 : spl.construct ();
96 : 0 : return spl.evaluate (tval).f0;
97 : : }
98 : 0 : return (*this->values)[idx];
99 : : }
100 : :
101 : : /* The function returns the value nearest to the given time value. If
102 : : the otional parameter is true then additionally cubic spline
103 : : interpolation is used. */
104 : 0 : nr_double_t history::nearest (nr_double_t tval, bool interpolate) {
105 [ # # ]: 0 : if (t == NULL)
106 : 0 : return 0.0;
107 : :
108 : 0 : int l = this->leftidx ();
109 : 0 : int r = t->size () - 1;
110 : 0 : int i = -1;
111 : 0 : nr_double_t diff = std::numeric_limits<nr_double_t>::max();
112 : 0 : sign = true;
113 [ # # ]: 0 : i = seek (tval, l, r, diff, i);
114 : 0 : i = i - l;
115 [ # # ]: 0 : if (interpolate)
116 [ # # ]: 0 : return interpol (tval, i, sign);
117 : 0 : return (*this->values)[i];
118 : : }
119 : :
120 : : /* The function is utilized in order to find the nearest value to a
121 : : given time value. Since the time vector is ordered a recursive
122 : : lookup algorithm can be used. The function returns the current
123 : : index into the time vector as well as the error in time. */
124 : 0 : int history::seek (nr_double_t tval, int l, int r, nr_double_t& diff,
125 : : int idx) {
126 : 0 : int i = (l + r) / 2;
127 [ # # ]: 0 : if (l == r)
128 : 0 : return i;
129 : 0 : nr_double_t v = (*this->t)[i];
130 : 0 : nr_double_t d = v - tval;
131 [ # # ]: 0 : if (fabs (d) < diff) {
132 : : // better approximation
133 : 0 : diff = fabs (d);
134 : 0 : sign = d < 0.0 ? true : false;
135 : 0 : idx = i;
136 : : }
137 [ # # ]: 0 : else if (i == l) {
138 : : // no better
139 : 0 : return idx;
140 : : }
141 [ # # ]: 0 : if (d < 0.0) {
142 : : // look later
143 : 0 : return seek (tval, i, r, diff, idx);
144 : : }
145 [ # # ]: 0 : else if (d > 0.0) {
146 : : // look earlier
147 : 0 : return seek (tval, l, i, diff, idx);
148 : : }
149 : 0 : return idx;
150 : : }
151 : :
152 : : } // namespace qucs
|