-
Notifications
You must be signed in to change notification settings - Fork 0
/
signals.c
73 lines (65 loc) · 2.16 KB
/
signals.c
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
/*******************************************************************************
*
* This module implements an infrastructure to operate with signals
* based on a vector space representation.
*
* Copyright (c) Dario Sanfilippo 2021.
*
******************************************************************************/
#include "signals.h"
struct Sig {
audio** vec_space;
size_t space_dim;
size_t vec_len;
size_t sr;
};
/* Allocate a space of "dim" vectors of size "len" samples initialised to 0 */
void sig_alloc(Sig* sig, size_t dim, size_t len, size_t srate) {
sig->vec_space = malloc(dim * sizeof(audio*));
for (size_t i = 0; i < dim; i++) {
sig->vec_space[i] = malloc(len * sizeof(audio));
for (size_t j = 0; j < len; j++) {
sig->vec_space[i][j] = 0.0;
}
}
sig->space_dim = dim;
sig->vec_len = len;
sig->sr = srate;
}
/* Free signals memory after use */
void sig_free(Sig* sig) {
for (size_t i = 0; i < sig->space_dim; i++) {
free(sig->vec_space[i]);
}
free(sig->vec_space);
}
/* Set a specific vector to a constant value */
void sig_set_const(Sig* sig, size_t vec_id, audio val) {
assert(vec_id < sig->space_dim);
for (size_t i = 0; i < sig->vec_len; i++) {
sig->vec_space[vec_id][i] = val;
}
}
/* Print a specific sample from a specific vector on screen */
void sig_print_sample(Sig* sig, size_t vec_id, size_t sample_id) {
assert((vec_id < sig->space_dim) & (sample_id < sig->vec_len));
printf("Vec %zu, sample %zu: %.20f\n", vec_id, sample_id,
sig->vec_space[vec_id][sample_id]);
}
/* Print a specific vector on screen */
void sig_print_vec(Sig* sig, size_t vec_id) {
assert(vec_id < sig->space_dim);
for (size_t i = 0; i < sig->vec_len; i++) {
printf("Vec %zu, sample %zu: %.20f\n", vec_id, i,
sig->vec_space[vec_id][i]);
}
}
/* Print an entire space on screen */
void sig_print_space(Sig* sig) {
for (size_t i = 0; i < sig->space_dim; i++) {
for (size_t j = 0; j < sig->vec_len; j++) {
printf("Vec %zu, sample %zu: %.20f\n", i, j,
sig->vec_space[i][j]);
}
}
}