File: align.c

package info (click to toggle)
openvlbi 3.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,524 kB
  • sloc: ansic: 21,182; cpp: 4,119; sh: 141; makefile: 5
file content (338 lines) | stat: -rw-r--r-- 13,392 bytes parent folder | download | duplicates (2)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*  libDSP - a digital signal processing library
*   Copyright © 2017-2023  Ilia Platone
*
*   This program is free software; you can redistribute it and/or modify
*   it under the terms of the GNU General Public License as published by
*   the Free Software Foundation; either version 2 of the License, or
*   (at your option) any later version.
*
*   This program is distributed in the hope that it will be useful,
*   but WITHOUT ANY WARRANTY; without even the implied warranty of
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*   GNU General Public License for more details.
*
*   You should have received a copy of the GNU General Public License along
*   with this program; if not, write to the Free Software Foundation, Inc.,
*   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "dsp.h"
#include <time.h>

typedef struct {
    double delta;
    double *diff;
} delta_diff;

static int dsp_qsort_delta_diff_desc (const void *arg1, const void *arg2)
{
    delta_diff* a1 = (delta_diff*)arg1;
    delta_diff* a2 = (delta_diff*)arg2;
    return ((*a1).delta < (*a2).delta ? 1 : -1);
}

static int dsp_qsort_delta_diff_asc (const void *arg1, const void *arg2)
{
    delta_diff* a1 = (delta_diff*)arg1;
    delta_diff* a2 = (delta_diff*)arg2;
    return ((*a1).delta > (*a2).delta ? 1 : -1);
}

static int dsp_qsort_triangle_delta_desc(const void *arg1, const void *arg2)
{
    dsp_triangle* a = (dsp_triangle*)arg1;
    dsp_triangle* b = (dsp_triangle*)arg2;
    int ret = 0;
    int s = 0;
    for(s = 0; s < (*a).stars_count; s++)
        ret |= ((*a).sizes[s] < (*b).sizes[s]);
    return (ret ? 1 : -1);
}

static int dsp_qsort_triangle_delta_asc(const void *arg1, const void *arg2)
{
    dsp_triangle* a = (dsp_triangle*)arg1;
    dsp_triangle* b = (dsp_triangle*)arg2;
    int ret = 0;
    int s = 0;
    for(s = 0; s < (*a).stars_count; s++)
        ret |= ((*a).sizes[s] > (*b).sizes[s]);
    return (ret ? 1 : -1);
}

static int dsp_qsort_triangle_diameter_desc(const void *arg1, const void *arg2)
{
    dsp_triangle* a = (dsp_triangle*)arg1;
    dsp_triangle* b = (dsp_triangle*)arg2;
    int ret = 0;
    int s = 0;
    for(s = 0; s < (*a).stars_count; s++) {
        ret |= ((*a).stars[s].diameter < (*b).stars[s].diameter);
        ret |= ((*a).stars[s].peak < (*b).stars[s].peak);
        ret |= ((*a).stars[s].flux < (*b).stars[s].flux);
    }
    return (ret ? 1 : -1);
}

static int dsp_qsort_triangle_diameter_asc(const void *arg1, const void *arg2)
{
    dsp_triangle* a = (dsp_triangle*)arg1;
    dsp_triangle* b = (dsp_triangle*)arg2;
    int ret = 0;
    int s = 0;
    for(s = 0; s < (*a).stars_count; s++) {
        ret |= ((*a).stars[s].diameter > (*b).stars[s].diameter);
        ret |= ((*a).stars[s].peak > (*b).stars[s].peak);
        ret |= ((*a).stars[s].flux > (*b).stars[s].flux);
    }
    return (ret ? 1 : -1);
}

static int dsp_qsort_star_diameter_desc(const void *arg1, const void *arg2)
{
    dsp_star* a = (dsp_star*)arg1;
    dsp_star* b = (dsp_star*)arg2;
    return ((*a).diameter < (*b).diameter ? 1 : -1);
}

static int dsp_qsort_star_diameter_asc(const void *arg1, const void *arg2)
{
    dsp_star* a = (dsp_star*)arg1;
    dsp_star* b = (dsp_star*)arg2;
    return ((*a).diameter > (*b).diameter ? 1 : -1);
}

static int dsp_qsort_double_desc (const void *arg1, const void *arg2)
{
    double* a = (double*)arg1;
    double* b = (double*)arg2;
    return ((*a) < (*b) ? 1 : -1);
}

static int dsp_qsort_double_asc (const void *arg1, const void *arg2)
{
    double* a = (double*)arg1;
    double* b = (double*)arg2;
    return ((*a) > (*b) ? 1 : -1);
}

static double calc_match_score(dsp_triangle t1, dsp_triangle t2, dsp_align_info align_info)
{
    int x = 0;
    int d = 0;
    int div = 0;
    int stars_count = fmin(t1.stars_count, t2.stars_count);
    int num_baselines = stars_count*(stars_count-1)/2;
    double err = 0.0;
    err += fabs((t2.index-t1.index)/t1.index);
    div++;
    for(d = 0; d < align_info.dims; d++) {
        for(x = 0; x < stars_count; x++) {
            err += fabs(t2.stars[x].diameter/align_info.factor[d]-t1.stars[x].diameter)/t1.stars[x].diameter;
            div++;
            err += fabs(t2.stars[x].flux/align_info.factor[d]-t1.stars[x].flux)/t1.stars[x].flux;
            div++;
            err += fabs(t2.stars[x].peak/align_info.factor[d]-t1.stars[x].peak)/t1.stars[x].peak;
            div++;
        }
        for(x = 0; x < num_baselines; x++) {
            err += fabs(t2.sizes[x]/align_info.factor[d]-t1.sizes[x])/t1.sizes[x];
            div++;
        }
    }
    return err / div;
}

dsp_align_info *dsp_align_fill_info(dsp_triangle t1, dsp_triangle t2)
{
    dsp_align_info *align_info = (dsp_align_info*)malloc(sizeof(dsp_align_info));
    int dims = t1.dims;
    int d, x;
    int num_baselines = t1.stars_count*(t1.stars_count-1)/2;
    align_info->dims = dims;
    align_info->center = (double*)malloc(sizeof(double)*(dims));
    align_info->offset = (double*)malloc(sizeof(double)*(dims));
    align_info->radians = (double*)malloc(sizeof(double)*(dims-1));
    align_info->factor = (double*)malloc(sizeof(double)*(dims));
    for(d = 0; d < dims; d++) {
        align_info->factor[d] = 0;
        align_info->offset[d] = 0;
        align_info->center[d] = 0;
        if(d < dims - 1)
            align_info->radians[d] = 0;
    }
    for(d = 0; d < dims; d++) {
        align_info->center[d] = t2.stars[0].center.location[d];
        align_info->offset[d] = t2.stars[0].center.location[d] - t1.stars[0].center.location[d];
        if(d < dims - 1) {
            align_info->radians[d] = t1.theta[d] - t2.theta[d];
            if(align_info->radians[d] >= M_PI*2.0)
                align_info->radians[d] -= M_PI*2.0;
            if(align_info->radians[d] < 0.0)
                align_info->radians[d] += M_PI*2.0;
        }
        for(x = 0; x < num_baselines; x++) {
            align_info->factor[d] += t2.sizes[x] / t1.sizes[x];
        }
        align_info->factor[d] /= num_baselines;
    }
    align_info->score = calc_match_score(t1, t2, *align_info);
    return align_info;
}

void dsp_align_free_triangle(dsp_triangle *t)
{
    int d;
    free(t->sizes);
    free(t->theta);
    free(t->ratios);
    for(d = 0; d < t->dims; d++) {
        free(t->stars[d].center.location);
    }
    free(t->stars);
    free(t);
}

dsp_triangle *dsp_align_calc_triangle(dsp_star* stars, int num_stars)
{
    int x;
    int y;
    int d;
    dsp_triangle *triangle = (dsp_triangle*)malloc(sizeof(dsp_triangle));
    triangle->dims = stars[0].center.dims;
    triangle->stars_count = num_stars;
    int num_baselines = triangle->stars_count*(triangle->stars_count-1)/2;
    delta_diff *deltadiff = (delta_diff*)malloc(sizeof(delta_diff)*num_baselines);
    triangle->sizes = (double*)malloc(sizeof(double)*num_baselines);
    triangle->ratios = (double*)malloc(sizeof(double)*num_baselines);
    triangle->stars = (dsp_star*)malloc(sizeof(dsp_star)*triangle->stars_count);
    triangle->theta = (double*)malloc(sizeof(double)*(triangle->dims-1));
    triangle->index = 0.0;
    qsort(stars, triangle->stars_count, sizeof(dsp_star), dsp_qsort_star_diameter_desc);
    int idx = 0;
    for(x = 0; x < triangle->stars_count; x++) {
        for(y = x+1; y < triangle->stars_count; y++) {
            deltadiff[idx].diff = (double*)malloc(sizeof(double)*triangle->dims);
            for(d = 0; d < triangle->dims; d++) {
                deltadiff[idx].diff[d] = stars[x].center.location[d]-stars[y].center.location[d];
                deltadiff[idx].delta += pow(deltadiff[idx].diff[d], 2);
            }
            deltadiff[idx].delta = sqrt(deltadiff[idx].delta);
            idx++;
        }
    }
    qsort(deltadiff, num_baselines, sizeof(delta_diff), dsp_qsort_delta_diff_desc);
    for(d = 0; d < triangle->dims-1; d++) {
        triangle->theta[d] = acos(deltadiff[0].diff[d] / deltadiff[0].delta);
        if(deltadiff[0].diff[d+1] < 0)
            triangle->theta[d] = M_PI*2.0-triangle->theta[d];
        for(x = 1; x < num_baselines; x++) {
            double index = acos(deltadiff[x].diff[d] / deltadiff[x].delta);
            if(deltadiff[x].diff[d+1] < 0)
                index = M_PI*2.0-index;
            index -= triangle->theta[d];
            triangle->index += index;
        }
    }
    triangle->index /= (num_baselines+triangle->dims-2)*M_PI*2;
    for(d = 0; d < triangle->stars_count; d++) {
        triangle->stars[d].diameter = stars[d].diameter;
        triangle->stars[d].peak = stars[d].peak;
        triangle->stars[d].flux = stars[d].flux;
        triangle->stars[d].theta = stars[d].theta;
        triangle->stars[d].center.dims = stars[d].center.dims;
        triangle->stars[d].center.location = (double*)malloc(sizeof(double)*stars[d].center.dims);
        for(x = 0; x < triangle->stars[d].center.dims; x++) {
            triangle->stars[d].center.location[x] = stars[d].center.location[x];
        }
    }
    for(d = 0; d < num_baselines; d++) {
        triangle->sizes[d] = deltadiff[d].delta;
        triangle->ratios[d] = deltadiff[d].delta / deltadiff[0].delta;
        free(deltadiff[d].diff);
    }
    free(deltadiff);
    return triangle;
}

int dsp_align_get_offset(dsp_stream_p stream1, dsp_stream_p stream2, double tolerance, double target_score, int num_stars)
{
    dsp_align_info *align_info;
    double decimals = pow(10, tolerance);
    double div = 0.0;
    int d, t1, t2, x, y;
    double phi = 0.0;
    double ratio = decimals*1600.0/div;
    dsp_star *stars = (dsp_star*)malloc(sizeof(dsp_star)*num_stars);
    for(d = 0; d < stream1->dims; d++) {
        div += pow(stream2->sizes[d], 2);
    }
    div = pow(div, 0.5);
    pwarn("decimals: %lf\n", decimals);
    target_score = (1.0-target_score / 100.0);
    stream2->align_info.dims = stream2->dims;
    stream2->align_info.triangles_count = 2;
    stream2->align_info.score = 1.0;
    stream2->align_info.decimals = decimals;
    pgarb("creating triangles for reference frame...\n");
    while(stream1->triangles_count > 0)
        dsp_stream_del_triangle(stream1, stream1->triangles_count-1);
    for(x = 0; x < stream1->stars_count * (stream1->stars_count-num_stars+1) / 2; x++) {
        for(y = 0; y < num_stars; y++) {
            stars[y] = stream1->stars[(x + y * (x / stream1->stars_count + 1)) % stream1->stars_count];
        }
        dsp_triangle *t = dsp_align_calc_triangle(stars, num_stars);
        dsp_stream_add_triangle(stream1, *t);
        dsp_align_free_triangle(t);
    }
    pgarb("creating triangles for current frame...\n");
    while(stream2->triangles_count > 0)
        dsp_stream_del_triangle(stream2, stream2->triangles_count-1);
    for(x = 0; x < stream2->stars_count * (stream2->stars_count-num_stars+1) / 2; x++) {
        for(y = 0; y < num_stars; y++) {
            stars[y] = stream2->stars[(x + y * (x / stream2->stars_count + 1)) % stream2->stars_count];
        }
        dsp_triangle *t = dsp_align_calc_triangle(stars, num_stars);
        dsp_stream_add_triangle(stream2, *t);
        dsp_align_free_triangle(t);
    }
    free(stars);
    stream2->align_info.center = (double*)malloc(sizeof(double)*stream2->align_info.dims);
    stream2->align_info.factor = (double*)malloc(sizeof(double)*stream2->align_info.dims);
    stream2->align_info.offset = (double*)malloc(sizeof(double)*stream2->align_info.dims);
    stream2->align_info.radians = (double*)malloc(sizeof(double)*(stream2->align_info.dims-1));
    for(t1 = 0; t1 < stream1->triangles_count; t1++) {
        for(t2 = 0; t2 < stream2->triangles_count; t2++) {
            align_info = dsp_align_fill_info(stream1->triangles[t1], stream2->triangles[t2]);
            if(align_info->score < stream2->align_info.score) {
                memcpy(stream2->align_info.center, align_info->center, sizeof(double)*stream2->align_info.dims);
                memcpy(stream2->align_info.factor, align_info->factor, sizeof(double)*stream2->align_info.dims);
                memcpy(stream2->align_info.offset, align_info->offset, sizeof(double)*stream2->align_info.dims);
                memcpy(stream2->align_info.radians, align_info->radians, sizeof(double)*(stream2->align_info.dims-1));
                memcpy(stream2->align_info.triangles, align_info->triangles, sizeof(dsp_triangle)*2);
                stream2->align_info.score = align_info->score;
                stream2->align_info.decimals = decimals;
            }
            free(align_info->center);
            free(align_info->factor);
            free(align_info->offset);
            free(align_info->radians);
            free(align_info);
        }
    }
    double radians = stream2->align_info.radians[0];
    for(d = 0; d < stream1->dims; d++) {
        phi += pow(stream2->align_info.offset[d], 2);
    }
    phi = pow(phi, 0.5);
    stream2->align_info.err = 0xf;
    if(floor(stream2->align_info.score * decimals)  < floor(target_score * decimals))
        stream2->align_info.err &= ~DSP_ALIGN_NO_MATCH;
    if(fabs(phi * ratio * decimals) < 1.0)
        stream2->align_info.err &= ~DSP_ALIGN_TRANSLATED;
    if(floor(fabs(stream2->align_info.factor[0] - 1.0) * decimals) < 1)
        stream2->align_info.err &= ~DSP_ALIGN_SCALED;
    if(floor(fabs(radians) * decimals) < 1)
        stream2->align_info.err &= ~DSP_ALIGN_ROTATED;
    return stream2->align_info.err;
}