Instrument Neutral Distributed Interface INDI 2.2.5
stream.c
Go to the documentation of this file.
1/*
2* DSP API - a digital signal processing library for astronomy usage
3* Copyright © 2017-2022 Ilia Platone
4*
5* This program is free software; you can redistribute it and/or
6* modify it under the terms of the GNU Lesser General Public
7* License as published by the Free Software Foundation; either
8* version 3 of the License, or (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 GNU
13* Lesser General Public License for more details.
14*
15* You should have received a copy of the GNU Lesser General Public License
16* along with this program; if not, write to the Free Software Foundation,
17* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18*/
19
20#include "dsp.h"
21static int dsp_debug = 0;
22static char* dsp_app_name = NULL;
23
24static int DSP_MAX_THREADS = 1;
25
26static unsigned long MAX_THREADS = 1;
27
28static FILE *out = NULL;
29static FILE *err = NULL;
30
31unsigned long int dsp_max_threads(unsigned long value)
32{
33 if(value>0) {
34 MAX_THREADS = value;
35 DSP_MAX_THREADS = value;
36 }
37 return MAX_THREADS;
38}
39
40void dsp_set_stdout(FILE *f)
41{
42 out = f;
43}
44
45void dsp_set_stderr(FILE *f)
46{
47 err = f;
48}
49
50void dsp_print(int x, char* str)
51{
52 if(x == DSP_DEBUG_INFO && out != NULL)
53 fprintf(out, "%s", str);
54 else if(x <= dsp_get_debug_level() && err != NULL)
55 fprintf(err, "%s", str);
56}
57
58void dsp_set_debug_level(int value)
59{
60 dsp_debug = value;
61}
62
63void dsp_set_app_name(char* name)
64{
65 dsp_app_name = name;
66}
67
69{
70 return dsp_debug;
71}
72
74{
75 return dsp_app_name;
76}
77
79{
80 if(stream->buf != NULL) {
81 stream->buf = (dsp_t*)realloc(stream->buf, sizeof(dsp_t) * len);
82 } else {
83 stream->buf = (dsp_t*)malloc(sizeof(dsp_t) * len);
84 }
85 if(stream->dft.buf != NULL) {
86 stream->dft.buf = (double*)realloc(stream->dft.buf, sizeof(double) * len * 2);
87 } else {
88 stream->dft.buf = (double*)malloc(sizeof(double) * len * 2);
89 }
90 if(stream->location != NULL) {
91 stream->location = (dsp_location*)realloc(stream->location, sizeof(dsp_location) * (stream->len));
92 } else {
93 stream->location = (dsp_location*)malloc(sizeof(dsp_location) * (stream->len));
94 }
95 if(stream->magnitude != NULL)
97 if(stream->phase != NULL)
98 dsp_stream_alloc_buffer(stream->phase, len);
99}
100
101void dsp_stream_set_buffer(dsp_stream_p stream, void *buffer, int len)
102{
103 stream->buf = (dsp_t*)buffer;
104 stream->len = len;
105}
106
108{
109 return stream->buf;
110}
111
113{
114 if(stream->buf != NULL)
115 free(stream->buf);
116 if(stream->dft.buf != NULL)
117 free(stream->dft.buf);
118 if(stream->magnitude != NULL)
120 if(stream->phase != NULL)
122}
123
129{
130 dsp_stream_p stream = (dsp_stream_p)malloc(sizeof(dsp_stream) * 1);
131 stream->is_copy = 0;
132 stream->buf = 0;
133 stream->dft.buf = 0;
134 stream->magnitude = NULL;
135 stream->phase = NULL;
136 stream->sizes = (int*)malloc(sizeof(int) * 1);
137 stream->pixel_sizes = (double*)malloc(sizeof(double) * 1);
138 stream->children = (dsp_stream_p*)malloc(sizeof(dsp_stream_p) * 1);
139 stream->ROI = (dsp_region*)malloc(sizeof(dsp_region) * 1);
140 stream->location = (dsp_location*)malloc(sizeof(dsp_location) * 1);
141 stream->target = (double*)malloc(sizeof(double) * 3);
142 stream->stars = (dsp_star*)malloc(sizeof(dsp_star) * 1);
143 stream->triangles = (dsp_triangle*)malloc(sizeof(dsp_triangle) * 1);
144 stream->align_info.offset = (double*)malloc(sizeof(double)*1);
145 stream->align_info.center = (double*)malloc(sizeof(double)*1);
146 stream->align_info.radians = (double*)malloc(sizeof(double)*1);
147 stream->align_info.factor = (double*)malloc(sizeof(double)*1);
148 stream->stars_count = 0;
149 stream->triangles_count = 0;
150 stream->child_count = 0;
151 stream->frame_number = 0;
152 stream->parent = NULL;
153 stream->dims = 0;
154 stream->red = -1;
155 stream->len = 1;
156 stream->wavelength = 0;
157 stream->diameter = 1;
158 stream->focal_ratio = 1;
159 stream->samplerate = 0;
160 return stream;
161}
162
168{
169 if(stream == NULL)
170 return;
171 if(stream->sizes != NULL)
172 free(stream->sizes);
173 if(stream->pixel_sizes != NULL)
174 free(stream->pixel_sizes);
175 if(stream->children != NULL)
176 free(stream->children);
177 if(stream->ROI != NULL)
178 free(stream->ROI);
179 if(stream->location != NULL)
180 free(stream->location);
181 if(stream->target != NULL)
182 free(stream->target);
183 if(stream->stars != NULL)
184 free(stream->stars);
185 if(stream->triangles != NULL)
186 free(stream->triangles);
187 if(stream->magnitude != NULL)
188 dsp_stream_free(stream->magnitude);
189 if(stream->phase != NULL)
190 dsp_stream_free(stream->phase);
191 free(stream);
192 stream = NULL;
193}
194
201{
203 int i;
204 for(i = 0; i < stream->dims; i++)
205 dsp_stream_add_dim(dest, abs(stream->sizes[i]));
206 for(i = 0; i < stream->stars_count; i++)
207 dsp_stream_add_star(dest, stream->stars[i]);
208 for(i = 0; i < stream->triangles_count; i++)
209 dsp_stream_add_triangle(dest, stream->triangles[i]);
210 dest->is_copy = stream->is_copy + 1;
211 dsp_stream_alloc_buffer(dest, dest->len);
212 dest->wavelength = stream->wavelength;
213 dest->samplerate = stream->samplerate;
214 dest->diameter = stream->diameter;
215 dest->focal_ratio = stream->focal_ratio;
216 memcpy(&dest->starttimeutc, &stream->starttimeutc, sizeof(struct timespec));
217 memcpy(&dest->align_info, &stream->align_info, sizeof(dsp_align_info));
218 memcpy(dest->ROI, stream->ROI, sizeof(dsp_region) * stream->dims);
219 memcpy(dest->pixel_sizes, stream->pixel_sizes, sizeof(double) * stream->dims);
220 memcpy(dest->target, stream->target, sizeof(double) * 3);
221 if(dest->location != NULL)
222 memcpy(dest->location, stream->location, sizeof(dsp_location) * stream->len);
223 if(dest->buf != NULL)
224 memcpy(dest->buf, stream->buf, sizeof(dsp_t) * stream->len);
225 if(dest->dft.buf != NULL)
226 memcpy(dest->dft.buf, stream->dft.buf, sizeof(complex_t) * stream->len);
227 return dest;
228}
229
235void dsp_stream_add_dim(dsp_stream_p stream, int size)
236{
237 stream->sizes[stream->dims] = size;
238 stream->len *= size;
239 stream->dims ++;
240 stream->ROI = (dsp_region*)realloc(stream->ROI, sizeof(dsp_region) * (stream->dims + 1));
241 stream->sizes = (int*)realloc(stream->sizes, sizeof(int) * (stream->dims + 1));
242 stream->pixel_sizes = (double*)realloc(stream->pixel_sizes, sizeof(double) * (stream->dims + 1));
243 stream->align_info.dims = stream->dims;
244 stream->align_info.offset = (double*)realloc(stream->align_info.offset, sizeof(double)*stream->dims);
245 stream->align_info.center = (double*)realloc(stream->align_info.center, sizeof(double)*stream->dims);
246 stream->align_info.radians = (double*)realloc(stream->align_info.radians, sizeof(double)*(stream->dims-1));
247 stream->align_info.factor = (double*)realloc(stream->align_info.factor, sizeof(double)*stream->dims);
248 if(stream->magnitude != NULL)
249 dsp_stream_add_dim(stream->magnitude, size);
250 if(stream->phase != NULL)
251 dsp_stream_add_dim(stream->phase, size);
252}
253
260void dsp_stream_set_dim(dsp_stream_p stream, int dim, int size)
261{
262 int d = 0;
263 if(dim < stream->dims) {
264 stream->sizes[dim] = size;
265 stream->len = 1;
266 for(d = 0; d < stream->dims; d++)
267 stream->len *= stream->sizes[d];
268 if(stream->magnitude != NULL)
269 dsp_stream_set_dim(stream->magnitude, dim, size);
270 if(stream->phase != NULL)
271 dsp_stream_set_dim(stream->phase, dim, size);
272 }
273}
274
280void dsp_stream_del_dim(dsp_stream_p stream, int index)
281{
282 int* sizes = (int*)malloc(sizeof(int) * stream->dims);
283 int dims = stream->dims;
284 memcpy(sizes, stream->sizes, sizeof(int) * stream->dims);
285 free(stream->sizes);
286 stream->dims = 0;
287 int i;
288 for(i = 0; i < dims; i++) {
289 if(i != index) {
290 dsp_stream_add_dim(stream, abs(sizes[i]));
291 }
292 }
293 if(stream->magnitude != NULL)
294 dsp_stream_del_dim(stream->magnitude, index);
295 if(stream->phase != NULL)
296 dsp_stream_del_dim(stream->phase, index);
297}
298
305{
306 child->parent = stream;
307 stream->children[stream->child_count] = child;
308 stream->child_count++;
309 stream->children = (dsp_stream_p*)realloc(stream->children, sizeof(dsp_stream_p) * (stream->child_count + 1));
310}
311
317void dsp_stream_del_child(dsp_stream_p stream, int index)
318{
319 dsp_stream_p* children = (dsp_stream_p*)malloc(sizeof(dsp_stream_p) * stream->child_count);
320 int child_count = stream->child_count;
321 memcpy(children, stream->children, sizeof(dsp_stream_p*) * stream->child_count);
322 free(stream->children);
323 stream->child_count = 0;
324 int i;
325 for(i = 0; i < child_count; i++) {
326 if(i != index) {
327 dsp_stream_add_child(stream, children[i]);
328 }
329 }
330}
331
338{
339 int d;
340 stream->stars = (dsp_star*)realloc(stream->stars, sizeof(dsp_star)*(stream->stars_count+1));
341 strcpy(stream->stars[stream->stars_count].name, star.name);
342 stream->stars[stream->stars_count].diameter = star.diameter;
343 stream->stars[stream->stars_count].peak = star.peak;
344 stream->stars[stream->stars_count].flux = star.flux;
345 stream->stars[stream->stars_count].theta = star.theta;
346 stream->stars[stream->stars_count].center.dims = star.center.dims;
347 stream->stars[stream->stars_count].center.location = (double*)malloc(sizeof(double)*star.center.dims);
348 for(d = 0; d < star.center.dims; d++)
349 stream->stars[stream->stars_count].center.location[d] = star.center.location[d];
350 stream->stars_count++;
351}
352
358void dsp_stream_del_star(dsp_stream_p stream, int index)
359{
360 dsp_star* stars = (dsp_star*)malloc(sizeof(dsp_star) * stream->stars_count);
361 int stars_count = stream->stars_count;
362 memcpy(stars, stream->stars, sizeof(dsp_star*) * stream->stars_count);
363 free(stream->stars);
364 stream->stars_count = 0;
365 int i;
366 for(i = 0; i < stars_count; i++) {
367 if(i != index) {
368 dsp_stream_add_star(stream, stars[i]);
369 }
370 }
371}
372
379{
380 int s;
381 int d;
382 int num_baselines = triangle.stars_count*(triangle.stars_count-1)/2;
383 stream->triangles = (dsp_triangle*)realloc(stream->triangles, sizeof(dsp_triangle)*(stream->triangles_count+1));
384 stream->triangles[stream->triangles_count].dims = triangle.dims;
385 stream->triangles[stream->triangles_count].index = triangle.index;
386 stream->triangles[stream->triangles_count].stars_count = triangle.stars_count;
387 stream->triangles[stream->triangles_count].theta = (double*)malloc(sizeof(double)*(stream->dims-1));
388 stream->triangles[stream->triangles_count].ratios = (double*)malloc(sizeof(double)*num_baselines);
389 stream->triangles[stream->triangles_count].sizes = (double*)malloc(sizeof(double)*num_baselines);
390 stream->triangles[stream->triangles_count].stars = (dsp_star*)malloc(sizeof(dsp_star)*triangle.stars_count);
391 for (s = 0; s < triangle.dims; s++) {
392 if(s < stream->dims - 1) {
393 stream->triangles[stream->triangles_count].theta[s] = triangle.theta[s];
394 }
395 }
396 for(s = 0; s < triangle.stars_count; s++) {
397 stream->triangles[stream->triangles_count].stars[s].center.dims = triangle.stars[s].center.dims;
398 stream->triangles[stream->triangles_count].stars[s].diameter = triangle.stars[s].diameter;
399 stream->triangles[stream->triangles_count].stars[s].peak = triangle.stars[s].peak;
400 stream->triangles[stream->triangles_count].stars[s].flux = triangle.stars[s].flux;
401 stream->triangles[stream->triangles_count].stars[s].theta = triangle.stars[s].theta;
402 stream->triangles[stream->triangles_count].stars[s].center.location = (double*)malloc(sizeof(double)*stream->dims);
403 for(d = 0; d < triangle.stars[s].center.dims; d++) {
404 stream->triangles[stream->triangles_count].stars[s].center.location[d] = triangle.stars[s].center.location[d];
405 }
406 }
407 for(s = 0; s < num_baselines; s++) {
408 stream->triangles[stream->triangles_count].sizes[s] = triangle.sizes[s];
409 stream->triangles[stream->triangles_count].ratios[s] = triangle.ratios[s];
410 }
411 stream->triangles_count++;
412}
413
420{
421 int d;
422 free(stream->triangles[index].sizes);
423 free(stream->triangles[index].theta);
424 free(stream->triangles[index].ratios);
425 for(d = 0; d < stream->triangles[index].stars_count; d++) {
426 free(stream->triangles[index].stars[d].center.location);
427 }
428 free(stream->triangles[index].stars);
429 int i;
430 for(i = index; i < stream->triangles_count - 1; i++) {
431 stream->triangles[i] = stream->triangles[i+1];
432 }
433 stream->triangles_count--;
434 if(index < stream->triangles_count) {
435 free(stream->triangles[i].sizes);
436 free(stream->triangles[i].theta);
437 free(stream->triangles[i].ratios);
438 for(d = 0; d < stream->triangles[i].dims; d++) {
439 free(stream->triangles[i].stars[d].center.location);
440 }
441 free(stream->triangles[i].stars);
442 }
443}
444
451int* dsp_stream_get_position(dsp_stream_p stream, int index) {
452 int dim = 0;
453 int y = 0;
454 int m = 1;
455 int* pos = (int*)malloc(sizeof(int) * stream->dims);
456 for (dim = 0; dim < stream->dims; dim++) {
457 y = index / m;
458 y %= stream->sizes[dim];
459 m *= stream->sizes[dim];
460 pos[dim] = y;
461 }
462 return pos;
463}
464
472 int dim = 0;
473 int index = 0;
474 int m = 1;
475 for (dim = 0; dim < stream->dims; dim++) {
476 index += m * pos[dim];
477 m *= stream->sizes[dim];
478 }
479 return index;
480}
481
482static void* dsp_stream_align_th(void* arg)
483{
484 struct {
485 int cur_th;
486 dsp_stream_p stream;
487 } *arguments = arg;
488 dsp_stream_p stream = arguments->stream;
489 dsp_stream_p in = stream->parent;
490 int cur_th = arguments->cur_th;
491 int start = cur_th * stream->len / dsp_max_threads(0);
492 int end = start + stream->len / dsp_max_threads(0);
493 end = Min(stream->len, end);
494 int y;
495 for(y = start; y < end; y++)
496 {
497 int *pos = dsp_stream_get_position(stream, y);
498 int dim;
499 for (dim = 1; dim < stream->dims; dim++) {
500 pos[dim] -= stream->align_info.center[dim];
501 pos[dim-1] -= stream->align_info.center[dim-1];
502 pos[dim] += stream->align_info.offset[dim];
503 pos[dim-1] += stream->align_info.offset[dim-1];
504 double r1 = stream->align_info.radians[dim-1];
505 double x = pos[dim-1];
506 double y = pos[dim];
507 double h = pow(pow(x, 2)+pow(y, 2), 0.5);
508 double r2 = acos(x/h);
509 if(y < 0)
510 r2 = - r2;
511 pos[dim] = sin(r2-r1)*h;
512 pos[dim-1] = cos(r2-r1)*h;
513 pos[dim] /= stream->align_info.factor[dim];
514 pos[dim-1] /= stream->align_info.factor[dim-1];
515 pos[dim] += stream->align_info.center[dim];
516 pos[dim-1] += stream->align_info.center[dim-1];
517 }
518 int x = dsp_stream_set_position(in, pos);
519 free(pos);
520 if(x >= 0 && x < in->len)
521 stream->buf[y] = in->buf[x];
522 }
523 return NULL;
524}
525
527{
528 dsp_stream_p stream = dsp_stream_copy(in);
529 dsp_buffer_set(stream->buf, stream->len, 0);
530 stream->parent = in;
531 size_t y;
532 pthread_t *th = (pthread_t*)malloc(sizeof(pthread_t)*dsp_max_threads(0));
533 struct {
534 int cur_th;
535 dsp_stream_p stream;
536 } thread_arguments[dsp_max_threads(0)];
537 for(y = 0; y < dsp_max_threads(0); y++) {
538 thread_arguments[y].cur_th = y;
539 thread_arguments[y].stream = stream;
540 pthread_create(&th[y], NULL, dsp_stream_align_th, &thread_arguments[y]);
541 }
542 for(y = 0; y < dsp_max_threads(0); y++)
543 pthread_join(th[y], NULL);
544 free(th);
545 dsp_buffer_copy(stream->buf, in->buf, stream->len);
547 dsp_stream_free(stream);
548}
549
555static void* dsp_stream_crop_th(void* arg)
556{
557 struct {
558 int cur_th;
559 dsp_stream_p stream;
560 } *arguments = arg;
561 dsp_stream_p stream = arguments->stream;
562 dsp_stream_p in = stream->parent;
563 int cur_th = arguments->cur_th;
564 int start = cur_th * stream->len / dsp_max_threads(0);
565 int end = start + stream->len / dsp_max_threads(0);
566 end = Min(stream->len, end);
567 int y;
568 for(y = start; y < end; y++)
569 {
570 int *pos = dsp_stream_get_position(stream, y);
571 int dim;
572 int allow = 1;
573 for (dim = 0; dim < stream->dims; dim++) {
574 pos[dim] += in->ROI[dim].start;
575 if(pos[dim] < in->ROI[dim].start || pos[dim] > in->ROI[dim].start + in->ROI[dim].len || pos[dim] < 0 || pos[dim] >= in->sizes[dim])
576 allow &= 0;
577 }
578 if(allow) {
579 int x = dsp_stream_set_position(in, pos);
580 stream->buf[y] = in->buf[x];
581 }
582 else
583 stream->buf[y] = 0;
584 free(pos);
585 }
586 return NULL;
587}
588
590{
591 dsp_stream_p stream = dsp_stream_copy(in);
592 dsp_buffer_set(stream->buf, stream->len, 0);
593 stream->parent = in;
594 size_t y;
595 pthread_t *th = (pthread_t*)malloc(sizeof(pthread_t)*dsp_max_threads(0));
596 struct {
597 int cur_th;
598 dsp_stream_p stream;
599 } thread_arguments[dsp_max_threads(0)];
600 for(y = 0; y < dsp_max_threads(0); y++) {
601 thread_arguments[y].cur_th = y;
602 thread_arguments[y].stream = stream;
603 pthread_create(&th[y], NULL, dsp_stream_crop_th, &thread_arguments[y]);
604 }
605 for(y = 0; y < dsp_max_threads(0); y++)
606 pthread_join(th[y], NULL);
607 free(th);
608 dsp_buffer_copy(stream->buf, in->buf, stream->len);
610 dsp_stream_free(stream);
611}
612
614{
615 dsp_stream_p stream = dsp_stream_copy(in);
616 int* offset = (int*)malloc(sizeof(int)*stream->dims);
617 dsp_buffer_copy(in->align_info.offset, offset, in->dims);
618 int z = dsp_stream_set_position(stream, offset);
619 free(offset);
620 int k = -z;
621 z = Max(0, z);
622 k = Max(0, k);
623 int len = stream->len-z-k;
624 dsp_t *buf = &stream->buf[z];
625 dsp_t *data = &in->buf[k];
626 memset(in->buf, 0, sizeof(dsp_t)*in->len);
627 memcpy(data, buf, sizeof(dsp_t)*len);
629 dsp_stream_free(stream);
630}
631
637static void* dsp_stream_scale_th(void* arg)
638{
639 struct {
640 int cur_th;
641 dsp_stream_p stream;
642 } *arguments = arg;
643 dsp_stream_p stream = arguments->stream;
644 dsp_stream_p in = stream->parent;
645 int cur_th = arguments->cur_th;
646 int start = cur_th * stream->len / dsp_max_threads(0);
647 int end = start + stream->len / dsp_max_threads(0);
648 end = Min(stream->len, end);
649 int y, d;
650 for(y = start; y < end; y++)
651 {
652 int* pos = dsp_stream_get_position(stream, y);
653 double factor = 0.0;
654 for(d = 0; d < stream->dims; d++) {
655 pos[d] -= stream->align_info.center[d];
656 pos[d] /= stream->align_info.factor[d];
657 pos[d] += stream->align_info.center[d];
658 factor += pow(stream->align_info.factor[d], 2);
659 }
660 factor = sqrt(factor);
661 int x = dsp_stream_set_position(in, pos);
662 if(x >= 0 && x < in->len)
663 stream->buf[y] += in->buf[x]/(factor*stream->dims);
664 free(pos);
665 }
666 return NULL;
667}
668
670{
671 dsp_stream_p stream = dsp_stream_copy(in);
672 dsp_buffer_set(stream->buf, stream->len, 0);
673 stream->parent = in;
674 size_t y;
675 pthread_t *th = (pthread_t*)malloc(sizeof(pthread_t)*dsp_max_threads(0));
676 struct {
677 int cur_th;
678 dsp_stream_p stream;
679 } thread_arguments[dsp_max_threads(0)];
680 for(y = 0; y < dsp_max_threads(0); y++) {
681 thread_arguments[y].cur_th = y;
682 thread_arguments[y].stream = stream;
683 pthread_create(&th[y], NULL, dsp_stream_scale_th, &thread_arguments[y]);
684 }
685 for(y = 0; y < dsp_max_threads(0); y++)
686 pthread_join(th[y], NULL);
687 free(th);
688 dsp_buffer_copy(stream->buf, in->buf, stream->len);
690 dsp_stream_free(stream);
691}
692
693static void* dsp_stream_rotate_th(void* arg)
694{
695 struct {
696 int cur_th;
697 dsp_stream_p stream;
698 } *arguments = arg;
699 dsp_stream_p stream = arguments->stream;
700 dsp_stream_p in = stream->parent;
701 int cur_th = arguments->cur_th;
702 int start = cur_th * stream->len / dsp_max_threads(0);
703 int end = start + stream->len / dsp_max_threads(0);
704 end = Min(stream->len, end);
705 int y;
706 for(y = start; y < end; y++)
707 {
708 int *pos = dsp_stream_get_position(stream, y);
709 int dim;
710 for (dim = 1; dim < stream->dims; dim++) {
711 pos[dim] -= stream->align_info.center[dim];
712 pos[dim-1] -= stream->align_info.center[dim-1];
713 double r = stream->align_info.radians[dim-1];
714 double x = pos[dim-1];
715 double y = pos[dim];
716 pos[dim] = x*-sin(r);
717 pos[dim-1] = x*cos(r);
718 pos[dim] += y*cos(r);
719 pos[dim-1] += y*sin(r);
720 pos[dim] += stream->align_info.center[dim];
721 pos[dim-1] += stream->align_info.center[dim-1];
722 }
723 int x = dsp_stream_set_position(in, pos);
724 free(pos);
725 if(x >= 0 && x < in->len)
726 stream->buf[y] = in->buf[x];
727 }
728 return NULL;
729}
730
732{
733 dsp_stream_p stream = dsp_stream_copy(in);
734 dsp_buffer_set(stream->buf, stream->len, 0);
735 stream->parent = in;
736 size_t y;
737 pthread_t *th = (pthread_t*)malloc(sizeof(pthread_t)*dsp_max_threads(0));
738 struct {
739 int cur_th;
740 dsp_stream_p stream;
741 } thread_arguments[dsp_max_threads(0)];
742 for(y = 0; y < dsp_max_threads(0); y++) {
743 thread_arguments[y].cur_th = y;
744 thread_arguments[y].stream = stream;
745 pthread_create(&th[y], NULL, dsp_stream_rotate_th, &thread_arguments[y]);
746 }
747 for(y = 0; y < dsp_max_threads(0); y++)
748 pthread_join(th[y], NULL);
749 free(th);
750 dsp_buffer_copy(stream->buf, in->buf, stream->len);
752 dsp_stream_free(stream);
753}
754
755static double stack_delegate_multiply(double x, double y)
756{
757 return sqrt(x * y);
758}
759
760static double stack_delegate_sum(double x, double y)
761{
762 return (x + y) / 2.0;
763}
764
765static double stack_delegate_subtraction(double x, double y)
766{
767 return fmax(0.0, x - y);
768}
769
770static void* dsp_stream_stack_th(void* arg)
771{
772 struct {
773 int cur_th;
774 dsp_stream_p stream;
775 double(*delegate)(double, double);
776 } *arguments = arg;
777 double(*delegate)(double, double) = arguments->delegate;
778 dsp_stream_p stream = arguments->stream;
779 dsp_stream_p in = stream->parent;
780 int cur_th = arguments->cur_th;
781 int start = cur_th * stream->len / dsp_max_threads(0);
782 int end = start + stream->len / dsp_max_threads(0);
783 end = Min(stream->len, end);
784 int y;
785 for(y = start; y < end; y++)
786 {
787 int *pos = dsp_stream_get_position(stream, y);
788 int x = dsp_stream_set_position(in, pos);
789 free(pos);
790 if(x >= 0 && x < in->len)
791 stream->buf[y] = delegate(stream->buf[y], in->buf[x]);
792 }
793 return NULL;
794}
795
797{
798 dsp_stream_p stream = dsp_stream_copy(in);
799 stream->parent = str;
800 size_t y;
801 pthread_t *th = (pthread_t*)malloc(sizeof(pthread_t)*dsp_max_threads(0));
802 struct {
803 int cur_th;
804 dsp_stream_p stream;
805 double(*delegate)(double, double);
806 } thread_arguments[dsp_max_threads(0)];
807 for(y = 0; y < dsp_max_threads(0); y++) {
808 thread_arguments[y].cur_th = y;
809 thread_arguments[y].stream = stream;
810 thread_arguments[y].delegate = stack_delegate_sum;
811 pthread_create(&th[y], NULL, dsp_stream_stack_th, &thread_arguments[y]);
812 }
813 for(y = 0; y < dsp_max_threads(0); y++)
814 pthread_join(th[y], NULL);
815 free(th);
816 dsp_buffer_copy(stream->buf, in->buf, stream->len);
818 dsp_stream_free(stream);
819}
820
822{
823 dsp_stream_p stream = dsp_stream_copy(in);
824 stream->parent = str;
825 size_t y;
826 pthread_t *th = (pthread_t*)malloc(sizeof(pthread_t)*dsp_max_threads(0));
827 struct {
828 int cur_th;
829 dsp_stream_p stream;
830 double(*delegate)(double, double);
831 } thread_arguments[dsp_max_threads(0)];
832 for(y = 0; y < dsp_max_threads(0); y++) {
833 thread_arguments[y].cur_th = y;
834 thread_arguments[y].stream = stream;
835 thread_arguments[y].delegate = stack_delegate_multiply;
836 pthread_create(&th[y], NULL, dsp_stream_stack_th, &thread_arguments[y]);
837 }
838 for(y = 0; y < dsp_max_threads(0); y++)
839 pthread_join(th[y], NULL);
840 free(th);
841 dsp_buffer_copy(stream->buf, in->buf, stream->len);
843 dsp_stream_free(stream);
844}
845
847{
848 dsp_stream_p stream = dsp_stream_copy(in);
849 stream->parent = str;
850 size_t y;
851 pthread_t *th = (pthread_t*)malloc(sizeof(pthread_t)*dsp_max_threads(0));
852 struct {
853 int cur_th;
854 dsp_stream_p stream;
855 double(*delegate)(double, double);
856 } thread_arguments[dsp_max_threads(0)];
857 for(y = 0; y < dsp_max_threads(0); y++) {
858 thread_arguments[y].cur_th = y;
859 thread_arguments[y].stream = stream;
860 thread_arguments[y].delegate = stack_delegate_subtraction;
861 pthread_create(&th[y], NULL, dsp_stream_stack_th, &thread_arguments[y]);
862 }
863 for(y = 0; y < dsp_max_threads(0); y++)
864 pthread_join(th[y], NULL);
865 free(th);
866 dsp_buffer_copy(stream->buf, in->buf, stream->len);
868 dsp_stream_free(stream);
869}
std::thread th
int dsp_get_debug_level()
get the debug level
Definition stream.c:68
void dsp_set_stderr(FILE *f)
set the error log streeam
Definition stream.c:45
char * dsp_get_app_name()
get the application name
Definition stream.c:73
#define Max(a, b)
if max() is not present you can use this one
Definition dsp.h:187
unsigned long int dsp_max_threads(unsigned long value)
get/set the maximum number of threads allowed
Definition stream.c:31
double dsp_t
Definition dsp.h:76
#define DSP_DEBUG_INFO
Definition dsp.h:129
double complex_t[2]
Definition dsp.h:77
void dsp_set_debug_level(int value)
set the debug level
Definition stream.c:58
#define Min(a, b)
if min() is not present you can use this one
Definition dsp.h:180
void dsp_set_stdout(FILE *f)
set the output log streeam
Definition stream.c:40
void dsp_print(int x, char *str)
log a message to the error or output streams
Definition stream.c:50
void dsp_set_app_name(char *name)
set the application name
Definition stream.c:63
struct dsp_stream_t * dsp_stream_p
struct dsp_stream_t * phase
Fourier transform phase.
Definition dsp.h:429
double samplerate
Sample rate of the buffers.
Definition dsp.h:421
int stars_count
The number of stars of the triangle.
Definition dsp.h:279
double * pixel_sizes
Sensor size.
Definition dsp.h:419
dsp_point center
The center of the star.
Definition dsp.h:248
int frame_number
Frame number (if part of a series)
Definition dsp.h:443
int triangles_count
Triangles of stars or objects quantity.
Definition dsp.h:439
int dims
Dimensions limit.
Definition dsp.h:296
double * factor
Scaling factor.
Definition dsp.h:294
double wavelength
Wavelength observed, used as reference with signal generators or filters.
Definition dsp.h:409
double theta
The deviation of the star.
Definition dsp.h:256
double focal_ratio
Focal ratio.
Definition dsp.h:411
int start
Starting point within the buffer.
Definition dsp.h:335
double * center
Center of rotation coordinates.
Definition dsp.h:290
struct dsp_stream_t * parent
The parent stream.
Definition dsp.h:397
int * sizes
Sizes of each dimension.
Definition dsp.h:389
double * location
Center of the point.
Definition dsp.h:226
dsp_complex dft
Fourier transform.
Definition dsp.h:393
int red
Red pixel (Bayer)
Definition dsp.h:417
double * radians
Rotational offset.
Definition dsp.h:292
double index
The index of the triangle.
Definition dsp.h:267
dsp_star * stars
Stars or objects identified into the buffers - TODO.
Definition dsp.h:433
struct timespec starttimeutc
Time at the beginning of the stream.
Definition dsp.h:407
dsp_star * stars
The stars of the triangle.
Definition dsp.h:277
double * sizes
The sizes of the triangle.
Definition dsp.h:273
double * buf
Linear double array containing complex numbers.
Definition dsp.h:326
double * target
Target coordinates.
Definition dsp.h:405
int stars_count
Stars or objects quantity.
Definition dsp.h:435
struct dsp_stream_t * magnitude
Fourier transform magnitude.
Definition dsp.h:427
double diameter
The diameter of the star.
Definition dsp.h:250
int dims
The dimensions of the triangle.
Definition dsp.h:269
double * theta
The inclination of the triangle.
Definition dsp.h:271
double diameter
Diameter.
Definition dsp.h:413
dsp_align_info align_info
Align/scale/rotation settings.
Definition dsp.h:441
dsp_triangle * triangles
Triangles of stars or objects.
Definition dsp.h:437
int dims
Number of dimensions of the buffers.
Definition dsp.h:387
dsp_location * location
Location coordinates pointer, can be extended to the main buffer size as location companion.
Definition dsp.h:403
struct dsp_stream_t ** children
Children streams.
Definition dsp.h:399
dsp_t * buf
buffer
Definition dsp.h:391
int len
Length of the region.
Definition dsp.h:337
char name[DSP_NAME_SIZE]
The name of the star.
Definition dsp.h:258
int dims
Dimensions limit of the point.
Definition dsp.h:228
int child_count
Children streams count.
Definition dsp.h:401
double peak
The peak of the star.
Definition dsp.h:252
int is_copy
Increments by one on the copied stream.
Definition dsp.h:383
double * ratios
The sizes of the triangle.
Definition dsp.h:275
dsp_region * ROI
Regions of interest for each dimension.
Definition dsp.h:431
int len
The buffers length.
Definition dsp.h:385
double flux
The flux of the star.
Definition dsp.h:254
double * offset
Translation offset.
Definition dsp.h:288
#define dsp_buffer_copy(in, out, len)
Fill the output buffer with the values of the elements of the input stream by casting them to the out...
Definition dsp.h:1054
#define dsp_buffer_set(buf, len, _val)
Fill the buffer with the passed value.
Definition dsp.h:831
void dsp_stream_free(dsp_stream_p stream)
dsp_stream_free
Definition stream.c:167
void dsp_stream_scale(dsp_stream_p in)
Scale a stream.
Definition stream.c:669
void dsp_stream_set_dim(dsp_stream_p stream, int dim, int size)
dsp_stream_set_dim
Definition stream.c:260
void dsp_stream_subtract(dsp_stream_p in, dsp_stream_p str)
Subtract a stream to another.
Definition stream.c:846
void dsp_stream_rotate(dsp_stream_p in)
Rotate a stream around an axis and offset.
Definition stream.c:731
void dsp_stream_del_triangle(dsp_stream_p stream, int index)
dsp_stream_del_triangle
Definition stream.c:419
void dsp_stream_sum(dsp_stream_p in, dsp_stream_p str)
Sum a stream with another.
Definition stream.c:796
int dsp_stream_set_position(dsp_stream_p stream, int *pos)
dsp_stream_set_position
Definition stream.c:471
dsp_stream_p dsp_stream_copy(dsp_stream_p stream)
dsp_stream_copy
Definition stream.c:200
void dsp_stream_add_triangle(dsp_stream_p stream, dsp_triangle triangle)
dsp_stream_add_triangle
Definition stream.c:378
void dsp_stream_add_child(dsp_stream_p stream, dsp_stream_p child)
dsp_stream_add_child
Definition stream.c:304
void dsp_stream_multiply(dsp_stream_p in, dsp_stream_p str)
Multiply a stream with another.
Definition stream.c:821
int * dsp_stream_get_position(dsp_stream_p stream, int index)
dsp_stream_get_position
Definition stream.c:451
void dsp_stream_del_dim(dsp_stream_p stream, int index)
dsp_stream_del_dim
Definition stream.c:280
void dsp_stream_set_buffer(dsp_stream_p stream, void *buffer, int len)
Set the buffer of the stream passed as argument to a specific memory location.
Definition stream.c:101
void dsp_stream_del_child(dsp_stream_p stream, int index)
dsp_stream_del_child
Definition stream.c:317
dsp_stream_p dsp_stream_new()
dsp_stream_new
Definition stream.c:128
void dsp_stream_add_dim(dsp_stream_p stream, int size)
dsp_stream_add_dim
Definition stream.c:235
void dsp_stream_alloc_buffer(dsp_stream_p stream, int len)
Allocate a buffer with length len on the stream passed as argument.
Definition stream.c:78
void dsp_stream_crop(dsp_stream_p in)
Crop the buffers of the stream passed as argument by reading the ROI field.
Definition stream.c:589
void dsp_stream_translate(dsp_stream_p in)
Translate a stream.
Definition stream.c:613
void dsp_stream_free_buffer(dsp_stream_p stream)
Free the buffer of the DSP Stream passed as argument.
Definition stream.c:112
void dsp_stream_del_star(dsp_stream_p stream, int index)
dsp_stream_del_star
Definition stream.c:358
void dsp_stream_add_star(dsp_stream_p stream, dsp_star star)
dsp_stream_add_star
Definition stream.c:337
void dsp_stream_align(dsp_stream_p in)
Perform scale, translate and rotate transformations in-place.
Definition stream.c:526
dsp_t * dsp_stream_get_buffer(dsp_stream_p stream)
Return the buffer of the stream passed as argument.
Definition stream.c:107
Alignment information needed.
Definition dsp.h:286
Delimits a region in a single dimension of a buffer.
Definition dsp.h:333
A star or object contained into a buffer.
Definition dsp.h:246
Contains a set of information and data relative to a buffer and how to use it.
Definition dsp.h:379
A star or object contained into a buffer.
Definition dsp.h:265
The location type.
Definition dsp.h:344