-
Notifications
You must be signed in to change notification settings - Fork 0
/
conv.cc
executable file
·211 lines (192 loc) · 7.98 KB
/
conv.cc
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* conv.cc
*
* Created on: Feb 4, 2009
* Author: root
*/
#include "simlab.h"
#include <stdio.h>
#ifdef JAVA
#include <jni.h>
#else
#ifdef WIN
#define JNIEXPORT __declspec(dllexport)
#else
#define JNIEXPORT extern "C"
#endif
#endif
JNIEXPORT int zero();
extern simlab data;
extern c_const<int> iconst;
extern c_const<float> fconst;
template <typename T,typename K> T* t_deconvolve( T* buffer, int length, K* c_buffer, int c_length, T* ret ) {
for( int k = length-1; k >= c_length; k-- ) {
int i = k-c_length;
T val = (buffer[k]-ret[i])/c_buffer[c_length-1];
int start = i-c_length-1 > 0 ? i-c_length-1 : 0;
for( int t = start; t < i; t++ ) {
ret[t] += val*c_buffer[t-start];
}
ret[i] = val;
}
}
template <typename T,typename K> T* t_convolve( T* buffer, int length, K* c_buffer, int c_length, T* ret ) {
for( int k = 0; k < length; k++ ) {
for( int t = k; t < k+c_length; t++ ) {
ret[t] += buffer[k]*c_buffer[t-k];
}
}
}
template <typename T,typename K> void t_conv( T* buffer, int length, int chunk, K* c_buffer, int c_length, int c_chunk ) {
int retchunk = (chunk+c_chunk-1);
int retlen = retchunk*(length/chunk);
T* ret = new T[ retlen ];
data.buffer = (long)ret;
data.length = retlen;
zero();
//int r = 0;
for( int i = 0; i < length; i+=chunk ) {
int w = i/chunk;
for( int k = i; k < i+chunk; k++ ) {
for( int t = k; t < k+c_chunk; t++ ) {
ret[w*retchunk+(t-i)] += (T)(buffer[k]*c_buffer[(w*c_chunk+(t-k))%c_length]);
}
}
}
}
template <typename T,typename K> void t_deconv( T* buffer, int length, int chunk, K* c_buffer, int c_length, int c_chunk ) {
int retchunk = chunk-c_chunk+1;
int retlen = retchunk*(length/chunk);
T* ret = new T[ retlen ];
data.buffer = (long)ret;
data.length = retlen;
zero();
int r = 0;
for( int i = 0; i < length; i+=chunk ) {
int w = i/chunk;
for( int k = i; k < i+retchunk; k++ ) {
ret[r] = (T)(buffer[k]/c_buffer[(w*c_chunk)%c_length]);
for( int t = k+1; t < k+c_chunk; t++ ) {
buffer[t] -= (T)(ret[r]*c_buffer[(w*c_chunk+(t-k))%c_length]);
}
r++;
}
}
}
template <typename T,typename K,typename V> class c_conv {
public:
c_conv( T tbuf, int tlen, K kbuf, int klen, int rlen ) : t(tbuf), tl(tlen), k(kbuf), kl(klen), rl(rlen) {
rchunk = tl+kl-1;
}
virtual V & operator[]( int i ) {
r = 0;
int w = i/rchunk;
int v = i%rchunk;
int wt = w*tl;
int wk = w*kl;
int start = v-(kl-1) < 0 ? 0: v-(kl-1);
int stop = v >= tl ? tl : v+1;
for( int u = start; u < stop; u++ ) {
r += t[wt + u]*k[wk + kl + start - u - 1];
}
return r;
}
T t;
int tl;
K k;
int kl;
int rl;
V r;
int rchunk;
};
template <typename T> void t_conver( T buffer, int chunk, int c_chunk, int rlen ) {
if( data.type < 0 ) {
if( data.type == -66 ) {
data.buffer = (long)new c_conv<c_simlab<double&>&,T,double>( *(c_simlab<double&>*)data.buffer, chunk, buffer, c_chunk, rlen );
data.length = rlen;
}
} else {
if( data.type == 66 ) {
data.buffer = (long)new c_conv<double*,T,double>( (double*)data.buffer, chunk, buffer, c_chunk, rlen );
data.type = -66;
data.length = rlen;
}
}
}
JNIEXPORT int convolver( simlab convee, simlab chnk, simlab c_chnk ) {
int ret = 3;
if( c_chnk.buffer == 0 ) {
c_chnk.buffer = convee.length;
ret = 2;
}
if( chnk.buffer == 0 ) {
chnk.buffer = data.length;
ret = 1;
}
int chunk = chnk.buffer;
int c_chunk = c_chnk.buffer;
int retchunk = chunk+c_chunk-1;
int retlen = retchunk*(data.length/chunk);
if( convee.type < 0 ) {
if( convee.type == -66 ) t_conver<c_simlab<double&>&>( *(c_simlab<double&>*)convee.buffer, chunk, c_chunk, retlen );
} else {
if( convee.type == 66 ) t_conver<double*>( (double*)convee.buffer, chunk, c_chunk, retlen );
}
/*if( data.type == 66 ) {
if( convee.type == 66 ) t_conver( (double*)data.buffer, data.length, chunk, (double*)convee.buffer, convee.length, c_chunk );
else if( convee.type == 32 ) t_conv( (double*)data.buffer, data.length, chunk, (int*)convee.buffer, convee.length, c_chunk );
else if( convee.type == 8 ) t_conv( (double*)data.buffer, data.length, chunk, (unsigned char*)convee.buffer, convee.length, c_chunk );
} else if( data.type == 32 ) {
if( convee.type == 66 ) t_conv( (int*)data.buffer, data.length, chunk, (double*)convee.buffer, convee.length, c_chunk );
else if( convee.type == 32 ) t_conv( (int*)data.buffer, data.length, chunk, (int*)convee.buffer, convee.length, c_chunk );
else if( convee.type == 8 ) t_conv( (int*)data.buffer, data.length, chunk, (unsigned char*)convee.buffer, convee.length, c_chunk );
} else if( data.type == 8 ) {
if( convee.type == 66 ) t_conv( (unsigned char*)data.buffer, data.length, chunk, (double*)convee.buffer, convee.length, c_chunk );
else if( convee.type == 32 ) t_conv( (unsigned char*)data.buffer, data.length, chunk, (int*)convee.buffer, convee.length, c_chunk );
else if( convee.type == 8 ) t_conv( (unsigned char*)data.buffer, data.length, chunk, (unsigned char*)convee.buffer, convee.length, c_chunk );
}*/
return ret;
}
JNIEXPORT int conv( simlab convee, simlab chnk, simlab c_chnk ) {
if( chnk.buffer == 0 ) chnk.buffer = data.length;
if( c_chnk.buffer == 0 ) c_chnk.buffer = convee.length;
int chunk = chnk.buffer;
int c_chunk = c_chnk.buffer;
if( data.type == 66 ) {
if( convee.type == 66 ) t_conv( (double*)data.buffer, data.length, chunk, (double*)convee.buffer, convee.length, c_chunk );
//(chunk+c_chunk-1)*(data.length/chunk) )
else if( convee.type == 32 ) t_conv( (double*)data.buffer, data.length, chunk, (int*)convee.buffer, convee.length, c_chunk );
else if( convee.type == 8 ) t_conv( (double*)data.buffer, data.length, chunk, (unsigned char*)convee.buffer, convee.length, c_chunk );
} else if( data.type == 32 ) {
if( convee.type == 66 ) t_conv( (int*)data.buffer, data.length, chunk, (double*)convee.buffer, convee.length, c_chunk );
else if( convee.type == 32 ) t_conv( (int*)data.buffer, data.length, chunk, (int*)convee.buffer, convee.length, c_chunk );
else if( convee.type == 8 ) t_conv( (int*)data.buffer, data.length, chunk, (unsigned char*)convee.buffer, convee.length, c_chunk );
} else if( data.type == 8 ) {
if( convee.type == 66 ) t_conv( (unsigned char*)data.buffer, data.length, chunk, (double*)convee.buffer, convee.length, c_chunk );
else if( convee.type == 32 ) t_conv( (unsigned char*)data.buffer, data.length, chunk, (int*)convee.buffer, convee.length, c_chunk );
else if( convee.type == 8 ) t_conv( (unsigned char*)data.buffer, data.length, chunk, (unsigned char*)convee.buffer, convee.length, c_chunk );
}
return 3;
}
JNIEXPORT int deconv( simlab convee, simlab chnk, simlab c_chnk ) {
if( chnk.buffer == 0 ) chnk.buffer = data.length;
if( c_chnk.buffer == 0 ) c_chnk.buffer = convee.length;
int chunk = chnk.buffer;
int c_chunk = c_chnk.buffer;
if( data.type == 66 ) {
if( convee.type == 66 ) {
t_deconv( (double*)data.buffer, data.length, chunk, (double*)convee.buffer, convee.length, c_chunk );
}
else if( convee.type == 32 ) t_deconv( (double*)data.buffer, data.length, chunk, (int*)convee.buffer, convee.length, c_chunk );
else if( convee.type == 8 ) t_deconv( (double*)data.buffer, data.length, chunk, (unsigned char*)convee.buffer, convee.length, c_chunk );
} else if( data.type == 32 ) {
if( convee.type == 66 ) t_deconv( (int*)data.buffer, data.length, chunk, (double*)convee.buffer, convee.length, c_chunk );
else if( convee.type == 32 ) t_deconv( (int*)data.buffer, data.length, chunk, (int*)convee.buffer, convee.length, c_chunk );
else if( convee.type == 8 ) t_deconv( (int*)data.buffer, data.length, chunk, (unsigned char*)convee.buffer, convee.length, c_chunk );
} else if( data.type == 8 ) {
if( convee.type == 66 ) t_deconv( (unsigned char*)data.buffer, data.length, chunk, (double*)convee.buffer, convee.length, c_chunk );
else if( convee.type == 32 ) t_deconv( (unsigned char*)data.buffer, data.length, chunk, (int*)convee.buffer, convee.length, c_chunk );
else if( convee.type == 8 ) t_deconv( (unsigned char*)data.buffer, data.length, chunk, (unsigned char*)convee.buffer, convee.length, c_chunk );
}
return 3;
}