-
Notifications
You must be signed in to change notification settings - Fork 13
/
freqshape.c
148 lines (138 loc) · 3.01 KB
/
freqshape.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#ifndef ARDUINO
// freqshape.c
#include <stdlib.h>
#include <math.h>
#include "sigpro.h"
FUNC(int) sp_freqshape(
float *x0,
float *x1, float *y1, int n1,
float *x2, float *y2, int n2
) {
float a, b, s;
int i, j, k, m;
if (n2 < 2)
return (1); // table too small
for (j = 1; j < n2; j++) {
if (x2[j - 1] > x2[j])
return (2); // table nonmontonic
}
if (x1 != y1)
sp_copy(x1, y1, n1);
sp_rcfft(y1, n1);
j = 0;
m = n1 / 2 + 1;
for (i = 0; i < m; i++) {
if ((x0[i] < x2[0]) || (x0[i] > x2[n2 - 1])) {
return (3); // outside table range
}
while ((j < (n2 - 1)) && (x0[i] >= x2[j + 1])) {
j++;
}
if (x0[i] == x2[j]) {
a = y2[j];
} else if (x0[i] == x2[j + 1]) {
a = y2[j + 1];
} else {
b = (x2[j + 1] - x0[i]) / (x2[j + 1] - x2[j]);
a = b * y2[j] + (1 - b) * y2[j + 1];
}
s = (float) pow(10, -a / 20);
k = i * 2;
y1[k + 0] *= s;
y1[k + 1] *= s;
}
sp_crfft(y1, n1);
return (0);
}
FUNC(int) sp_firdb(
float *b,
int n,
double fs,
float *fr,
float *at,
int nt
) {
double a, s, f, df;
float *y;
int i, j, k, m, n1, nf, err = 0;
static int wt = SP_WT_HAMMING;
nf = sp_nxtpow2(n) * 2;
df = fs / nf;
n1 = nf / 2 + 1;
y = (float *) calloc(nf + 2, sizeof(float));
j = 0;
for (i = 0; i < n1; i++) {
f = i * df;
if ((f < fr[0]) || (f > fr[nt - 1])) {
return (3); // outside table range
}
while ((j < (nt - 1)) && (f >= fr[j + 1])) {
j++;
}
if (f == fr[j]) {
a = at[j];
} else if (f == fr[j + 1]) {
a = at[j + 1];
} else {
s = (fr[j + 1] - f) / (fr[j + 1] - fr[j]);
a = s * at[j] + (1 - s) * at[j + 1];
}
y[i * 2] = (float) pow(10, -a / 20);
}
err = sp_crfft(y, nf);
if (err)
return (err);
m = n / 2;
for (i = 0; i < m; i++) {
j = i + m;
k = i - m + nf;
b[i] = y[k];
b[j] = y[i];
}
err = sp_window(y, n, wt);
for (i = 0; i < n; i++) {
b[i] *= y[i];
}
free(y);
return (err);
}
FUNC(int) sp_frqshp(
float *x, float *y, int n,
int nb, double fs,
float * fr, float *at, int nt,
int wrap
) {
float *f, *b, *z;
int i, m, n1, err = 0;
if (wrap && (n == nb) && (n == sp_nxtpow2(n))) {
n1 = n / 2 + 1;
f = (float *) calloc(n1, sizeof(float));
sp_linspace(f, n1, 0, fs / 2);
err = sp_freqshape(f, x, y, n, fr, at, nt);
free(f);
} else {
b = (float *) calloc(nb, sizeof(float));
z = (float *) calloc(nb, sizeof(float));
err = sp_firdb(b, nb, fs, fr, at, nt);
err = sp_fftfiltz(b, nb, x, y, n, z);
if (!err) {
if (wrap) {
for (i = 0; i < nb; i++) {
y[i] += z[i];
z[i] = y[i];
}
}
m = nb / 2;
for (i = 0; i < (n - m); i++) {
y[i] = y[i + m];
}
for (i = 0; i < m; i++) {
y[i + n - m] = z[i];
}
}
free(b);
free(z);
}
return (err);
}
#endif