Instrument Neutral Distributed Interface INDI  2.0.2
filters.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"
21 
23 {
24  dsp_t* in = stream->buf;
25  dsp_t *out = (dsp_t*)malloc(sizeof(dsp_t) * stream->len);
26  int len = stream->len;
27  dsp_t mean = dsp_stats_mean(stream->buf, stream->len);
28  int val = 0;
29  int i;
30  for(i = 0; i < len; i++) {
31  val = in [i] - mean;
32  out [i] = (abs (val) + mean);
33  }
34  memcpy(stream->buf, out, stream->len * sizeof(dsp_t));
35  free(out);
36 }
37 
38 void dsp_filter_lowpass(dsp_stream_p stream, double Frequency)
39 {
40  int d, x;
41  double radius = 0.0;
42  for(d = 0; d < stream->dims; d++) {
43  radius += pow(stream->sizes[d]/2.0, 2);
44  }
45  radius = sqrt(radius);
46  dsp_fourier_dft(stream, 1);
47  for(x = 0; x < stream->len; x++) {
48  int* pos = dsp_stream_get_position(stream, x);
49  double dist = 0.0;
50  for(d = 0; d < stream->dims; d++) {
51  dist += pow(stream->sizes[d]/2.0-pos[d], 2);
52  }
53  free(pos);
54  dist = sqrt(dist);
55  dist *= M_PI/radius;
56  if(dist>Frequency)
57  stream->magnitude->buf[x] = 0.0;
58  }
59  dsp_fourier_idft(stream);
60 }
61 
62 void dsp_filter_highpass(dsp_stream_p stream, double Frequency)
63 {
64  int d, x;
65  double radius = 0.0;
66  for(d = 0; d < stream->dims; d++) {
67  radius += pow(stream->sizes[d]/2.0, 2);
68  }
69  radius = sqrt(radius);
70  dsp_fourier_dft(stream, 1);
71  for(x = 0; x < stream->len; x++) {
72  int* pos = dsp_stream_get_position(stream, x);
73  double dist = 0.0;
74  for(d = 0; d < stream->dims; d++) {
75  dist += pow(stream->sizes[d]/2.0-pos[d], 2);
76  }
77  free(pos);
78  dist = sqrt(dist);
79  dist *= M_PI/radius;
80  if(dist<Frequency)
81  stream->magnitude->buf[x] = 0.0;
82  }
83  dsp_fourier_idft(stream);
84 }
85 
86 void dsp_filter_bandreject(dsp_stream_p stream, double LowFrequency, double HighFrequency)
87 {
88  int d, x;
89  double radius = 0.0;
90  for(d = 0; d < stream->dims; d++) {
91  radius += pow(stream->sizes[d]/2.0, 2);
92  }
93  radius = sqrt(radius);
94  dsp_fourier_dft(stream, 1);
95  for(x = 0; x < stream->len; x++) {
96  int* pos = dsp_stream_get_position(stream, x);
97  double dist = 0.0;
98  for(d = 0; d < stream->dims; d++) {
99  dist += pow(stream->sizes[d]/2.0-pos[d], 2);
100  }
101  free(pos);
102  dist = sqrt(dist);
103  dist *= M_PI/radius;
104  if(dist<HighFrequency&&dist>LowFrequency)
105  stream->magnitude->buf[x] = 0.0;
106  }
107  dsp_fourier_idft(stream);
108 }
109 
110 void dsp_filter_bandpass(dsp_stream_p stream, double LowFrequency, double HighFrequency)
111 {
112  int d, x;
113  double radius = 0.0;
114  for(d = 0; d < stream->dims; d++) {
115  radius += pow(stream->sizes[d]/2.0, 2);
116  }
117  radius = sqrt(radius);
118  dsp_fourier_dft(stream, 1);
119  for(x = 0; x < stream->len; x++) {
120  int* pos = dsp_stream_get_position(stream, x);
121  double dist = 0.0;
122  for(d = 0; d < stream->dims; d++) {
123  dist += pow(stream->sizes[d]/2.0-pos[d], 2);
124  }
125  free(pos);
126  dist = sqrt(dist);
127  dist *= M_PI/radius;
128  if(dist>HighFrequency||dist<LowFrequency)
129  stream->magnitude->buf[x] = 0.0;
130  }
131  dsp_fourier_idft(stream);
132 }
double dsp_t
Definition: dsp.h:69
int * sizes
Sizes of each dimension.
Definition: dsp.h:373
struct dsp_stream_t * magnitude
Fourier transform magnitude.
Definition: dsp.h:411
int dims
Number of dimensions of the buffers.
Definition: dsp.h:371
dsp_t * buf
buffer
Definition: dsp.h:375
int len
The buffers length.
Definition: dsp.h:369
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:420
void dsp_filter_lowpass(dsp_stream_p stream, double Frequency)
A low pass filter.
Definition: filters.c:38
void dsp_filter_bandreject(dsp_stream_p stream, double LowFrequency, double HighFrequency)
A band reject filter.
Definition: filters.c:86
void dsp_filter_bandpass(dsp_stream_p stream, double LowFrequency, double HighFrequency)
A band pass filter.
Definition: filters.c:110
void dsp_filter_highpass(dsp_stream_p stream, double Frequency)
A high pass filter.
Definition: filters.c:62
void dsp_filter_squarelaw(dsp_stream_p stream)
A square law filter.
Definition: filters.c:22
DLL_EXPORT void dsp_fourier_idft(dsp_stream_p stream)
Perform an inverse discrete Fourier Transform of a dsp_stream.
Definition: fft.c:172
DLL_EXPORT void dsp_fourier_dft(dsp_stream_p stream, int exp)
Perform a discrete Fourier Transform of a dsp_stream.
Definition: fft.c:135
#define dsp_stats_mean(buf, len)
A mean calculator.
Definition: dsp.h:649
Contains a set of informations and data relative to a buffer and how to use it.
Definition: dsp.h:363