Instrument Neutral Distributed Interface INDI  1.9.5
buffer.c
Go to the documentation of this file.
1 /*
2  * libDSPAU - a digital signal processing library for astronomy usage
3  * Copyright (C) 2017 Ilia Platone <info@iliaplatone.com>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "dsp.h"
20 
22 {
23  if(stream->dims == 0)
24  return;
25  dsp_t* tmp = (dsp_t*)malloc(sizeof(dsp_t) * stream->len);
26  int x, d;
27  for(x = 0; x < stream->len/2; x++) {
28  int* pos = dsp_stream_get_position(stream, x);
29  for(d = 0; d < stream->dims; d++) {
30  if(pos[d]<stream->sizes[d] / 2) {
31  pos[d] += stream->sizes[d] / 2;
32  } else {
33  pos[d] -= stream->sizes[d] / 2;
34  }
35  }
36  tmp[x] = stream->buf[dsp_stream_set_position(stream, pos)];
37  tmp[dsp_stream_set_position(stream, pos)] = stream->buf[x];
38  free(pos);
39  }
40  memcpy(stream->buf, tmp, stream->len * sizeof(dsp_t));
41  free(tmp);
42 }
43 
45 {
46  int k;
47 
48  dsp_t mean = dsp_stats_mean(stream->buf, stream->len);
49  for(k = 0; k < stream->len; k++)
50  stream->buf[k] = stream->buf[k] - mean;
51 
52 }
53 
54 void dsp_buffer_sub(dsp_stream_p stream, dsp_t* in, int inlen)
55 {
56  int len = Min(stream->len, inlen);
57 
58  int k;
59  for(k = 0; k < len; k++) {
60  stream->buf[k] = stream->buf[k] - in[k];
61  }
62 
63 }
64 
65 void dsp_buffer_sum(dsp_stream_p stream, dsp_t* in, int inlen)
66 {
67  int len = Min(stream->len, inlen);
68 
69  int k;
70  for(k = 0; k < len; k++) {
71  stream->buf[k] += in[k];
72  }
73 
74 }
75 
76 void dsp_buffer_div(dsp_stream_p stream, dsp_t* in, int inlen)
77 {
78  int len = Min(stream->len, inlen);
79 
80  int k;
81  for(k = 0; k < len; k++) {
82  stream->buf[k] = stream->buf[k] / in[k];
83  }
84 
85 }
86 
87 void dsp_buffer_mul(dsp_stream_p stream, dsp_t* in, int inlen)
88 {
89  int len = Min(stream->len, inlen);
90 
91  int k;
92  for(k = 0; k < len; k++) {
93  stream->buf[k] = stream->buf[k] * in[k];
94  }
95 
96 }
97 
98 void dsp_buffer_pow(dsp_stream_p stream, dsp_t* in, int inlen)
99 {
100  int len = Min(stream->len, inlen);
101 
102  int k;
103  for(k = 0; k < len; k++) {
104  stream->buf[k] = pow(stream->buf[k], in[k]);
105  }
106 
107 }
108 
109 void dsp_buffer_log(dsp_stream_p stream, dsp_t* in, int inlen)
110 {
111  int len = Min(stream->len, inlen);
112 
113  int k;
114  for(k = 0; k < len; k++) {
115  stream->buf[k] = Log(stream->buf[k], in[k]);
116  }
117 
118 }
119 
120 void dsp_buffer_1sub(dsp_stream_p stream, double val)
121 {
122  int k;
123 
124  for(k = 0; k < stream->len; k++) {
125  stream->buf[k] = val - stream->buf[k];
126  }
127 
128 }
129 
130 void dsp_buffer_sub1(dsp_stream_p stream, double val)
131 {
132  int k;
133 
134  for(k = 0; k < stream->len; k++) {
135  stream->buf[k] = stream->buf[k] - val;
136  }
137 
138 }
139 
140 void dsp_buffer_sum1(dsp_stream_p stream, double val)
141 {
142  int k;
143 
144  for(k = 0; k < stream->len; k++) {
145  stream->buf[k] += val;
146  }
147 
148 }
149 
150 void dsp_buffer_1div(dsp_stream_p stream, double val)
151 {
152  int k;
153 
154  for(k = 0; k < stream->len; k++) {
155  stream->buf[k] = (dsp_t)(val / (dsp_t)stream->buf[k]);
156  }
157 
158 }
159 
160 void dsp_buffer_div1(dsp_stream_p stream, double val)
161 {
162  int k;
163 
164  for(k = 0; k < stream->len; k++) {
165  stream->buf[k] = (dsp_t)((dsp_t)stream->buf[k] / val);
166  }
167 
168 }
169 
170 void dsp_buffer_mul1(dsp_stream_p stream, double val)
171 {
172  int k;
173 
174  for(k = 0; k < stream->len; k++) {
175  stream->buf[k] = (dsp_t)((dsp_t)stream->buf[k] * val);
176  }
177 
178 }
179 
180 void dsp_buffer_pow1(dsp_stream_p stream, double val)
181 {
182  int k;
183 
184  for(k = 0; k < stream->len; k++) {
185  stream->buf[k] = (dsp_t)pow((dsp_t)stream->buf[k], val);
186  }
187 
188 }
189 
190 void dsp_buffer_log1(dsp_stream_p stream, double val)
191 {
192  int k;
193 
194  for(k = 0; k < stream->len; k++) {
195  stream->buf[k] = (dsp_t)Log((dsp_t)stream->buf[k], val);
196  }
197 
198 }
199 
200 static int compare( const void* a, const void* b)
201 {
202  dsp_t int_a = * ( (dsp_t*) a );
203  dsp_t int_b = * ( (dsp_t*) b );
204 
205  if ( int_a == int_b ) return 0;
206  else if ( int_a < int_b ) return -1;
207  else return 1;
208 }
209 
210 void dsp_buffer_median(dsp_stream_p stream, int size, int median)
211 {
212  int k;
213  int mid = (size / 2) + (size % 2);
214  dsp_t* sorted = (dsp_t*)malloc(size * sizeof(dsp_t));
215  for(k = mid; k < stream->len; k++) {
216  memcpy (sorted, stream->buf + (k - mid), size * sizeof(dsp_t));
217  qsort(sorted, size, sizeof(dsp_t), compare);
218  stream->buf[k] = sorted[median];
219  }
220 
221 }
222 
223 void dsp_buffer_deviate(dsp_stream_p stream, dsp_t* deviation, dsp_t mindeviation, dsp_t maxdeviation)
224 {
225  dsp_stream_p tmp = dsp_stream_copy(stream);
226  int k;
227  for(k = 1; k < stream->len; k++) {
228  stream->buf[(int)Max(0, Min(stream->len, ((deviation[k] - mindeviation) * (maxdeviation - mindeviation) + mindeviation) + k))] = tmp->buf[k];
229  }
230  dsp_stream_free(tmp);
231 }
dsp_buffer_mul
void dsp_buffer_mul(dsp_stream_p stream, dsp_t *in, int inlen)
Multiply elements of one stream to another's.
Definition: buffer.c:87
dsp_buffer_shift
void dsp_buffer_shift(dsp_stream_p stream)
Shift a stream on each dimension.
Definition: buffer.c:21
dsp.h
dsp_buffer_log1
void dsp_buffer_log1(dsp_stream_p stream, double val)
Logarithm elements of the input stream using the given base.
Definition: buffer.c:190
dsp_buffer_sum
void dsp_buffer_sum(dsp_stream_p stream, dsp_t *in, int inlen)
Sum elements of one stream to another's.
Definition: buffer.c:65
Log
#define Log(a, b)
Logarithm of a with arbitrary base b.
Definition: dsp.h:79
dsp_buffer_sub1
void dsp_buffer_sub1(dsp_stream_p stream, double val)
Subtract a value from elements of the input stream.
Definition: buffer.c:130
dsp_stream_free
DLL_EXPORT void dsp_stream_free(dsp_stream_p stream)
Free the DSP stream passed as argument.
Definition: stream.c:76
Max
#define Max(a, b)
if max() is not present you can use this one
Definition: dsp.h:72
dsp_buffer_pow1
void dsp_buffer_pow1(dsp_stream_p stream, double val)
Expose elements of the input stream to the given power.
Definition: buffer.c:180
dsp_buffer_deviate
void dsp_buffer_deviate(dsp_stream_p stream, dsp_t *deviation, dsp_t mindeviation, dsp_t maxdeviation)
Deviate forward the first input stream using the second stream as indexing reference.
Definition: buffer.c:223
dsp_stream_t
Contains a set of informations and data relative to a buffer and how to use it.
Definition: dsp.h:175
dsp_buffer_log
void dsp_buffer_log(dsp_stream_p stream, dsp_t *in, int inlen)
Logarithm elements of one stream using another's as base.
Definition: buffer.c:109
dsp_stream_copy
DLL_EXPORT dsp_stream_p dsp_stream_copy(dsp_stream_p stream)
Create a copy of the DSP stream passed as argument.
Definition: stream.c:89
dsp_buffer_1sub
void dsp_buffer_1sub(dsp_stream_p stream, double val)
Subtract each element of the input stream a value.
Definition: buffer.c:120
Min
#define Min(a, b)
if min() is not present you can use this one
Definition: dsp.h:65
dsp_buffer_median
void dsp_buffer_median(dsp_stream_p stream, int size, int median)
Median elements of the inut stream.
Definition: buffer.c:210
dsp_buffer_1div
void dsp_buffer_1div(dsp_stream_p stream, double val)
Divide a value to each element of the input stream.
Definition: buffer.c:150
dsp_stream_set_position
DLL_EXPORT int dsp_stream_set_position(dsp_stream_p stream, int *pos)
Obtain the position the DSP stream by parsing multidimensional indexes.
Definition: stream.c:202
dsp_stream_t::len
int len
The buffers length.
Definition: dsp.h:178
dsp_buffer_pow
void dsp_buffer_pow(dsp_stream_p stream, dsp_t *in, int inlen)
Expose elements of one stream to another's.
Definition: buffer.c:98
dsp_stats_mean
#define dsp_stats_mean(buf, len)
A mean calculator.
Definition: dsp.h:459
dsp_buffer_sum1
void dsp_buffer_sum1(dsp_stream_p stream, double val)
Sum elements of the input stream to a value.
Definition: buffer.c:140
dsp_stream_t::sizes
int * sizes
Sizes of each dimension.
Definition: dsp.h:182
dsp_buffer_div1
void dsp_buffer_div1(dsp_stream_p stream, double val)
Divide elements of the input stream to a value.
Definition: buffer.c:160
dsp_stream_get_position
DLL_EXPORT int * dsp_stream_get_position(dsp_stream_p stream, int index)
Return the multidimensional positional indexes of a DSP stream by specify a linear index.
Definition: stream.c:188
dsp_stream_t::dims
int dims
Number of dimensions of the buffers.
Definition: dsp.h:180
dsp_buffer_div
void dsp_buffer_div(dsp_stream_p stream, dsp_t *in, int inlen)
Divide elements of one stream to another's.
Definition: buffer.c:76
dsp_t
#define dsp_t
Definition: dsp.h:57
dsp_buffer_sub
void dsp_buffer_sub(dsp_stream_p stream, dsp_t *in, int inlen)
Subtract elements of one stream from another's.
Definition: buffer.c:54
dsp_buffer_mul1
void dsp_buffer_mul1(dsp_stream_p stream, double val)
Multiply elements of the input stream to a value.
Definition: buffer.c:170
dsp_buffer_removemean
void dsp_buffer_removemean(dsp_stream_p stream)
Subtract mean from stream.
Definition: buffer.c:44
dsp_stream_t::buf
dsp_t * buf
buffer
Definition: dsp.h:184