comparison model/integrate.c @ 0:5242703e91d3 tip

Initial checkin for AIM92 aimR8.2 (last updated May 1997).
author tomwalters
date Fri, 20 May 2011 15:19:45 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:5242703e91d3
1 /*
2 integrate.c
3 ===========
4
5 */
6
7 #include <math.h>
8
9 #include "stitch.h"
10 #include "source.h"
11 #include "calc.h"
12
13 #include "integrate.h"
14
15
16 struct _integral_state { ScalarType state, upward, downward, igain ; } ;
17
18 static int new_integral_callback( state, buffer, end, input, skip )
19 struct _integral_state *state ;
20 DataType *buffer, *end, *input ;
21 int skip ;
22 {
23 register struct _integral_state *sptr = state ;
24 register DataType *iptr = input ;
25 register DataType *optr = buffer ;
26 register DataType *eptr = end ;
27
28 while( optr < eptr ) {
29
30 /* the more accurate later version */
31
32 *optr = DESCALE( sptr->state ) ;
33
34 sptr->state += sptr->upward * DESCALE( *iptr * sptr->igain - sptr->state ) ;
35
36 iptr += skip ;
37 optr += skip ;
38 }
39
40 return ( sizeof ( DataType ) ) ;
41 }
42
43 static int old_integral_callback( state, buffer, end, input, skip )
44 struct _integral_state *state ;
45 DataType *buffer, *end, *input ;
46 int skip ;
47 {
48 register struct _integral_state *sptr = state ;
49 register DataType *iptr = input ;
50 register DataType *optr = buffer ;
51 register DataType *eptr = end ;
52
53 while( optr < eptr ) {
54
55 /* the "interesting" one */
56
57 *optr = sptr->state ;
58 sptr->state += DESCALE( sptr->upward * ( DESCALE( *iptr * sptr->igain ) - sptr->state ) ) ;
59
60 iptr += skip ;
61 optr += skip ;
62 }
63
64 return ( sizeof ( DataType ) ) ;
65 }
66
67 static void integral_close( state )
68 struct _integral_state *state ;
69 {
70 Delete( state ) ;
71
72 return ;
73 }
74
75 Source LowpassDataTypeSource( source, stages, channels, upward, downward, igain )
76 Source source ;
77 int stages, channels ;
78 double upward, downward, igain ;
79 {
80 struct _integral_state **states ;
81 int stage, chan ;
82
83 for( stage=0 ; stage < abs( stages ) ; stage++ ) {
84
85 states = NewArray( struct _integral_state *, channels, "for states" ) ;
86
87 for( chan=0 ; chan<channels ; chan++ ) {
88
89 states[chan] = New( struct _integral_state * ) ;
90
91 states[chan]->state = 0 ;
92 states[chan]->upward = SCALAR( 1. - exp( -1. / upward ) ) ;
93 states[chan]->downward = SCALAR( 1. - exp( -1. / downward ) ) ;
94 states[chan]->igain = SCALAR( igain ) ;
95
96 }
97
98 if( stages > 0 )
99 source = NewMultiplexedSource( states, new_integral_callback, integral_close, channels, source, "integrate.c integration" ) ;
100 else
101 source = NewMultiplexedSource( states, old_integral_callback, integral_close, channels, source, "integrate.c integration" ) ;
102 }
103
104 return ( source ) ;
105 }
106
107
108
109
110
111 struct _blocksample_state { struct _fillable_source parent ; Source source ; int spacing, channels ; } ;
112
113 static Pointer blocksample_callback( state, bytes, buffer )
114 struct _blocksample_state *state ;
115 ByteCount *bytes ;
116 DataType *buffer ;
117 {
118 register int last = *bytes == 0 ;
119 register StoreType sum ;
120 register DataType *iptr ;
121 register DataType *input = (DataType *) Pull( state->source, *bytes * state->spacing ) ;
122 register DataType *end = (DataType *) ( (Pointer) input + *bytes * state->spacing ) ;
123 int channel ;
124
125 for( channel=0 ; channel<state->channels ; channel++ ) {
126
127 sum=0 ;
128
129 for( iptr=input+channel ; iptr < end ; iptr += state->channels )
130 sum += *iptr ;
131
132 buffer[channel] = sum / state->spacing ;
133 }
134
135 if( !last )
136 return ( (Pointer) buffer ) ;
137 else
138 return ( DeleteFillableSource( state ) ) ;
139 }
140
141 Source BlockSampleSource( source, spacing, channels )
142 Source source ;
143 int spacing, channels ;
144 {
145 DeclareNew( struct _blocksample_state *, state ) ;
146
147 state->source = source ;
148 state->spacing = spacing ;
149 state->channels = channels ;
150
151 source = SetFillableSource( state, blocksample_callback, "integrate.c block sampling" ) ;
152
153 source = NewSegmentingSource( source, channels * sizeof ( DataType ) ) ;
154
155 return ( source ) ;
156 }
157
158
159 struct _downsample_state { struct _pullable_source parent ; Source source ; int spacing, channels ; } ;
160
161 static Pointer downsample_callback( state, bytes )
162 struct _downsample_state *state ;
163 ByteCount *bytes ;
164 {
165 register int last = *bytes == 0 ;
166 Pointer output = Pull( state->source, *bytes * state->spacing ) + *bytes * state->spacing - state->channels * sizeof ( DataType ) ;
167
168 if( !last )
169 return ( output ) ;
170 else
171 return ( DeletePullableSource( state ) ) ;
172 }
173
174 Source DownSampleSource( source, spacing, channels )
175 Source source ;
176 int spacing, channels ;
177 {
178 DeclareNew( struct _downsample_state *, state ) ;
179
180 state->source = source ;
181 state->spacing = spacing ;
182 state->channels = channels ;
183
184 source = SetPullableSource( state, downsample_callback, "integrate.c down sampling" ) ;
185
186 source = NewSegmentingSource( source, channels * sizeof ( DataType ) ) ;
187
188 return ( source ) ;
189 }
190