File: convolution.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 (60 lines) | stat: -rw-r--r-- 2,444 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
/*  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"

void dsp_convolution_convolution(dsp_stream_p stream, dsp_stream_p matrix) {
    int x, y, d;
    dsp_t mn = dsp_stats_min(stream->buf, stream->len);
    dsp_t mx = dsp_stats_max(stream->buf, stream->len);
    int* d_pos = (int*)malloc(sizeof(int)*stream->dims);
    for(y = 0; y < matrix->len; y++) {
        int* pos = dsp_stream_get_position(matrix, y);
        for(d = 0; d < stream->dims; d++) {
            d_pos[d] = stream->sizes[d]/2+pos[d]-matrix->sizes[d]/2;
        }
        x = dsp_stream_set_position(stream, d_pos);
        free(pos);
        if(x >= 0 && x < stream->magnitude->len)
            stream->magnitude->buf[x] *= sqrt(matrix->magnitude->buf[y]);
    }
    free(d_pos);
    dsp_fourier_idft(stream);
    dsp_buffer_stretch(stream->buf, stream->len, mn, mx);
}

void dsp_convolution_correlation(dsp_stream_p stream, dsp_stream_p matrix) {
    int x, y, d;
    dsp_t mn = dsp_stats_min(stream->buf, stream->len);
    dsp_t mx = dsp_stats_max(stream->buf, stream->len);
    int* d_pos = (int*)malloc(sizeof(int)*stream->dims);
    dsp_buffer_shift(matrix->magnitude);
    for(y = 0; y < matrix->len; y++) {
        int* pos = dsp_stream_get_position(matrix, y);
        for(d = 0; d < stream->dims; d++) {
            d_pos[d] = stream->sizes[d]/2+pos[d]-matrix->sizes[d]/2;
        }
        x = dsp_stream_set_position(stream, d_pos);
        free(pos);
        stream->magnitude->buf[x] *= sqrt(matrix->magnitude->buf[y]);
    }
    dsp_buffer_shift(matrix->magnitude);
    free(d_pos);
    dsp_fourier_idft(stream);
    dsp_buffer_stretch(stream->buf, stream->len, mn, mx);
}