Instrument Neutral Distributed Interface INDI  1.9.5
signals.c
Go to the documentation of this file.
1 /*
2  * libDSPAU - a digital signal processing library for astronomy usage
3  * Copyright (C) 2017 Ilia Platone <info@iliaplatone.com>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "dsp.h"
20 
22 {
23  int k;
24  for(k = 0; k < stream->len; k++) {
25  stream->buf[k] = (rand() % 255) / 255.0;
26  }
27 
28 }
29 
30 void dsp_signals_sinewave(dsp_stream_p stream, double samplefreq, double freq)
31 {
32  freq /= samplefreq;
33  double rad = 0;
34  double x = 0;
35  int k;
36  for(k = 0; k < stream->len; k++) {
37  rad += freq;
38  x = rad;
39  while (x > 1.0)
40  x -= 1.0;
41  x *= M_PI * 2;
42  stream->buf[k] = sin(x);
43  }
44 
45 }
46 
47 void dsp_signals_sawtoothwave(dsp_stream_p stream, double samplefreq, double freq)
48 {
49  freq /= samplefreq;
50  double rad = 0;
51  double x = 0;
52  int k;
53  for(k = 0; k < stream->len; k++) {
54  rad += freq;
55  x = rad;
56  while (x > 1.0)
57  x -= 1.0;
58  stream->buf[k] = (double)(32768+32767*x);
59  }
60 
61 }
62 
63 void dsp_signals_triwave(dsp_stream_p stream, double samplefreq, double freq)
64 {
65  freq /= samplefreq;
66  double rad = 0;
67  double x = 0;
68  int k;
69  for(k = 0; k < stream->len; k++) {
70  rad += freq;
71  x = rad;
72  while (x > 2.0)
73  x -= 2.0;
74  while (x > 1.0)
75  x = 2.0 - x;
76  stream->buf[k] = (double)(32768+32767*x);
77  }
78 
79 }
80 
81 void dsp_modulation_frequency(dsp_stream_p stream, double samplefreq, double freq, double bandwidth)
82 {
83  dsp_stream_p carrier = dsp_stream_new();
84  dsp_signals_sinewave(carrier, samplefreq, freq);
85  double mn = dsp_stats_min(stream->buf, stream->len);
86  double mx = dsp_stats_max(stream->buf, stream->len);
87  double lo = mn * bandwidth * 1.5 / samplefreq;
88  double hi = mx * bandwidth * 0.5 / samplefreq;
89  double *deviation = (double*)malloc(sizeof(double) * stream->len);
90  dsp_buffer_copy(stream->buf, deviation, stream->len);
91  dsp_buffer_deviate(carrier, deviation, hi, lo);
92  memcpy(stream->buf, carrier->buf, stream->len * sizeof(double));
93  dsp_stream_free(carrier);
94 
95 }
96 
97 void dsp_modulation_amplitude(dsp_stream_p stream, double samplefreq, double freq)
98 {
99  dsp_stream_p carrier = dsp_stream_new();
100  dsp_signals_sinewave(carrier, samplefreq, freq);
101  dsp_buffer_sum(stream, carrier->buf, stream->len);
102  dsp_stream_free_buffer(carrier);
103  dsp_stream_free(carrier);
104 
105 }
dsp_signals_triwave
void dsp_signals_triwave(dsp_stream_p stream, double samplefreq, double freq)
Generate a triangular wave.
Definition: signals.c:63
dsp.h
dsp_buffer_sum
void dsp_buffer_sum(dsp_stream_p stream, dsp_t *in, int inlen)
Sum elements of one stream to another's.
Definition: buffer.c:65
dsp_stream_new
DLL_EXPORT dsp_stream_p dsp_stream_new()
Allocate a new DSP stream type.
Definition: stream.c:48
dsp_modulation_frequency
void dsp_modulation_frequency(dsp_stream_p stream, double samplefreq, double freq, double bandwidth)
Generate a frequency modulated wave.
Definition: signals.c:81
dsp_stream_free
DLL_EXPORT void dsp_stream_free(dsp_stream_p stream)
Free the DSP stream passed as argument.
Definition: stream.c:76
dsp_buffer_deviate
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:223
dsp_stream_t
Contains a set of informations and data relative to a buffer and how to use it.
Definition: dsp.h:175
dsp_stats_min
#define dsp_stats_min(buf, len)
Gets the minimum value of the input stream.
Definition: dsp.h:382
dsp_modulation_amplitude
void dsp_modulation_amplitude(dsp_stream_p stream, double samplefreq, double freq)
Generate an amplitude modulated wave.
Definition: signals.c:97
dsp_stream_t::len
int len
The buffers length.
Definition: dsp.h:178
dsp_buffer_copy
#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:791
dsp_signals_whitenoise
void dsp_signals_whitenoise(dsp_stream_p stream)
Generate white noise.
Definition: signals.c:21
dsp_stats_max
#define dsp_stats_max(buf, len)
Gets the maximum value of the input stream.
Definition: dsp.h:398
dsp_stream_free_buffer
DLL_EXPORT void dsp_stream_free_buffer(dsp_stream_p stream)
Free the buffer of the DSP Stream passed as argument.
Definition: stream.c:41
dsp_signals_sawtoothwave
void dsp_signals_sawtoothwave(dsp_stream_p stream, double samplefreq, double freq)
Generate a sawtooth wave.
Definition: signals.c:47
dsp_signals_sinewave
void dsp_signals_sinewave(dsp_stream_p stream, double samplefreq, double freq)
Generate a sinusoidal wave.
Definition: signals.c:30
dsp_stream_t::buf
dsp_t * buf
buffer
Definition: dsp.h:184