-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathhilbert.c
79 lines (76 loc) · 2.64 KB
/
hilbert.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
74
75
76
77
78
79
/*
* This file is part of dsp.
*
* Copyright (c) 2020-2025 Michael Barbour <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "hilbert.h"
#include "fir.h"
#include "fir_p.h"
#include "zita_convolver.h"
#include "util.h"
struct effect * hilbert_effect_init(const struct effect_info *ei, const struct stream_info *istream, const char *channel_selector, const char *dir, int argc, const char *const *argv)
{
char *endptr;
struct effect *e;
struct dsp_getopt_state g = DSP_GETOPT_STATE_INITIALIZER;
int conv = 0, opt;
while ((opt = dsp_getopt(&g, argc-1, argv, "pz")) != -1) {
switch (opt) {
case 'p': conv = 1; break;
case 'z': conv = 2; break;
default: goto print_usage;
}
}
if (g.ind != argc-1) {
print_usage:
LOG_FMT(LL_ERROR, "%s: usage: %s", argv[0], ei->usage);
return NULL;
}
const ssize_t taps = strtol(argv[g.ind], &endptr, 10);
CHECK_ENDPTR(argv[g.ind], endptr, "taps", return NULL);
if (taps <= 3) {
LOG_FMT(LL_ERROR, "%s: error: taps must be > 3", argv[0]);
return NULL;
}
if (taps % 2 == 0) {
LOG_FMT(LL_ERROR, "%s: error: taps must be odd", argv[0]);
return NULL;
}
sample_t *h = calloc(taps, sizeof(sample_t));
for (ssize_t i = 0, k = -taps / 2; i < taps; ++i, ++k) {
if (k%2 == 0)
h[i] = 0;
else {
double x = 2.0*M_PI*i/(taps-1);
h[i] = 2.0/(M_PI*k) * (0.42 - 0.5*cos(x) + 0.08*cos(2.0*x));
}
}
if (conv == 1)
e = fir_p_effect_init_with_filter(ei, istream, channel_selector, h, 1, taps, 0);
else if (conv == 2) {
#ifdef HAVE_ZITA_CONVOLVER
e = zita_convolver_effect_init_with_filter(ei, istream, channel_selector, h, 1, taps, 0, 0);
#else
LOG_FMT(LL_ERROR, "%s: warning: zita_convolver not available; using fir_p instead", argv[0]);
e = fir_p_effect_init_with_filter(ei, istream, channel_selector, h, 1, taps, 0);
#endif
}
else e = fir_effect_init_with_filter(ei, istream, channel_selector, h, 1, taps, 0);
free(h);
return e;
}