Instrument Neutral Distributed Interface INDI  2.0.2
signals.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  int k;
25  for(k = 0; k < stream->len; k++) {
26  stream->buf[k] = (rand() % 255) / 255.0;
27  }
28 
29 }
30 
31 void dsp_signals_sinewave(dsp_stream_p stream, double samplefreq, double freq)
32 {
33  freq /= samplefreq;
34  double rad = 0;
35  double x = 0;
36  int k;
37  for(k = 0; k < stream->len; k++) {
38  rad += freq;
39  x = rad;
40  while (x > 1.0)
41  x -= 1.0;
42  x *= M_PI * 2;
43  stream->buf[k] = sin(x);
44  }
45 
46 }
47 
48 void dsp_signals_sawtoothwave(dsp_stream_p stream, double samplefreq, double freq)
49 {
50  freq /= samplefreq;
51  double rad = 0;
52  double x = 0;
53  int k;
54  for(k = 0; k < stream->len; k++) {
55  rad += freq;
56  x = rad;
57  while (x > 1.0)
58  x -= 1.0;
59  stream->buf[k] = (dsp_t)(32768+32767*x);
60  }
61 
62 }
63 
64 void dsp_signals_triwave(dsp_stream_p stream, double samplefreq, double freq)
65 {
66  freq /= samplefreq;
67  double rad = 0;
68  double x = 0;
69  int k;
70  for(k = 0; k < stream->len; k++) {
71  rad += freq;
72  x = rad;
73  while (x > 2.0)
74  x -= 2.0;
75  while (x > 1.0)
76  x = 2.0 - x;
77  stream->buf[k] = (dsp_t)(32768+32767*x);
78  }
79 
80 }
81 
82 void dsp_modulation_frequency(dsp_stream_p stream, double samplefreq, double freq, double bandwidth)
83 {
84  dsp_stream_p carrier = dsp_stream_new();
85  dsp_signals_sinewave(carrier, samplefreq, freq);
86  double mn = dsp_stats_min(stream->buf, stream->len);
87  double mx = dsp_stats_max(stream->buf, stream->len);
88  double lo = mn * bandwidth * 1.5 / samplefreq;
89  double hi = mx * bandwidth * 0.5 / samplefreq;
90  dsp_t *deviation = (dsp_t*)malloc(sizeof(dsp_t) * stream->len);
91  dsp_buffer_copy(stream->buf, deviation, stream->len);
92  dsp_buffer_deviate(carrier, deviation, hi, lo);
93  memcpy(stream->buf, carrier->buf, stream->len * sizeof(dsp_t));
94  dsp_stream_free(carrier);
95 
96 }
97 
98 void dsp_modulation_amplitude(dsp_stream_p stream, double samplefreq, double freq)
99 {
100  dsp_stream_p carrier = dsp_stream_new();
101  dsp_signals_sinewave(carrier, samplefreq, freq);
102  dsp_buffer_sum(stream, carrier->buf, stream->len);
103  dsp_stream_free_buffer(carrier);
104  dsp_stream_free(carrier);
105 
106 }
double dsp_t
Definition: dsp.h:69
dsp_t * buf
buffer
Definition: dsp.h:375
int len
The buffers length.
Definition: dsp.h:369
#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:1038
void dsp_buffer_sum(dsp_stream_p stream, dsp_t *in, int inlen)
Sum elements of one stream to another's.
Definition: buffer.c:68
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:391
DLL_EXPORT void dsp_stream_free(dsp_stream_p stream)
Free the DSP stream passed as argument.
Definition: stream.c:163
DLL_EXPORT dsp_stream_p dsp_stream_new(void)
Allocate a new DSP stream type.
Definition: stream.c:124
DLL_EXPORT 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_signals_sawtoothwave(dsp_stream_p stream, double samplefreq, double freq)
Generate a sawtooth wave.
Definition: signals.c:48
void dsp_signals_sinewave(dsp_stream_p stream, double samplefreq, double freq)
Generate a sinusoidal wave.
Definition: signals.c:31
void dsp_signals_triwave(dsp_stream_p stream, double samplefreq, double freq)
Generate a triangular wave.
Definition: signals.c:64
void dsp_modulation_amplitude(dsp_stream_p stream, double samplefreq, double freq)
Generate an amplitude modulated wave.
Definition: signals.c:98
void dsp_modulation_frequency(dsp_stream_p stream, double samplefreq, double freq, double bandwidth)
Generate a frequency modulated wave.
Definition: signals.c:82
void dsp_signals_whitenoise(dsp_stream_p stream)
Generate white noise.
Definition: signals.c:22
#define dsp_stats_min(buf, len)
Gets the minimum value of the input stream.
Definition: dsp.h:562
#define dsp_stats_max(buf, len)
Gets the maximum value of the input stream.
Definition: dsp.h:580
Contains a set of informations and data relative to a buffer and how to use it.
Definition: dsp.h:363