forked from BoysTownOrg/chapro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnfc_process.c
186 lines (165 loc) · 4.75 KB
/
nfc_process.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// nfc_process.c - nonlinear-frequency-compression processing functions
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#include "chapro.h"
/***********************************************************/
#ifdef ARM_DSP
static int fft_initialized = 0;
static arm_rfft_fast_instance_f32 fft_instance;
static __inline void
rfft(float *x, int n)
{
if (!fft_initialized) {
arm_rfft_fast_init_f32(&fft_instance, n);
fft_initialized++;
}
arm_rfft_fast_f32(&fft_instance, x, x, 0);
}
static __inline void
rifft(float *x, int n)
{
if (!fft_initialized) {
arm_rfft_fast_init_f32(&fft_instance, n);
fft_initialized++;
}
arm_rfft_fast_f32(&fft_instance, x, x, 1);
}
#else // ARM_DSP
#define rfft(x,n) cha_fft_rc(x,n)
#define rifft(x,n) cha_fft_cr(x,n)
#endif // ARM_DSP
// map frequencies
static __inline void
fmap(float *y, float *x, int nw, int *mm, int nm, float *g1, float *g2)
{
float g, *xx, *yy;
int k, kk, nf, nn;
// apply pre-map gain
if (g1) {
nn = mm[nm - 1];
for (k = 0; k < nn; k++) {
g = g1[k];
x[2 * k ] *= g; // real
x[2 * k + 1] *= g; // imag
}
}
// perform frequency mapping
nn = mm[0] * 2;
nf = nw * 2;
fcopy(y, x, nn);
fzero(y + nn, nf - nn);
for (k = 0; k < (nm - 1); k++) {
yy = y + 2 * k + nn;
for (kk = mm[k]; kk < mm[k + 1]; kk++) {
xx = x + 2 * kk;
yy[0] += xx[0]; // real
yy[1] += xx[1]; // imag
}
}
// apply post-map gain
if (g2) {
nn = mm[0] + nm - 1;
for (k = 0; k < nn; k++) {
g = g2[k];
y[2 * k ] *= g; // real
y[2 * k + 1] *= g; // imag
}
}
}
// short-term FFT analyze
static __inline void
short_term_analyze(float *xx, float *XX, int nw, int ns, float *ww)
{
int i, nf;
nf = nw * 2;
for (i = 0; i < nw; i++) {
XX[i] = xx[i] * ww[i]; // apply window to input
}
fzero(XX + nw, nw);
rfft(XX, nf); // FFT
fcopy(xx, xx + ns, ns); // save last half of input window
}
// short-term FFT synthesize
static __inline void
short_term_synthesize(float *yy, float *YY, int nw, int ns)
{
int i, nf, nn;
nf = nw * 2;
rifft(YY, nf); // IFFT
nn = nf - ns;
fmove(yy, yy + ns, nn); // shift previous output
for (i = 0; i < nn; i++) {
yy[i] += YY[i]; // overlap-add output
}
fcopy(yy + nn, YY + nn, ns); // save response tail
}
// nonlinear-frequency-compression short chunk
static __inline void
nfc_sc(CHA_PTR cp, float *x, float *y, int cs,
float *xx, float *yy, float *XX, float *YY, float *ww,
float *g1, float *g2,
int *mm, int nm, int nw)
{
int icp, ics, nn, ns, ncs;
// process chunk
ncs = CHA_IVAR[_nfc_ncs];
ics = CHA_IVAR[_nfc_ics];
ns = nw / 2;
nn = ics * cs;
fcopy(xx + nn + ns, x, cs);
fcopy(y, yy + nn, cs);
icp = (ics + 1) % ncs;
if (icp == 0) { // perform frequency-map after every shift
short_term_analyze(xx, XX, nw, ns, ww);
fmap(YY, XX, nw, mm, nm, g1, g2); // compress frequency range
short_term_synthesize(yy, YY, nw, ns);
}
// update chunk count
CHA_IVAR[_nfc_ics] = icp;
}
// nonlinear-frequency-compression long chunk
static __inline void
nfc_lc(float *x, float *y, int cs,
float *xx, float *yy, float *XX, float *YY, float *ww,
float *g1, float *g2,
int *mm, int nm, int nw)
{
int k, nn, ns;
// process chunk
ns = nw / 2;
nn = cs / ns;
for (k = 0; k < nn; k++) {
fcopy(xx + nn + ns, x + k * ns, ns);
fcopy(y + k * ns, yy + nn, ns);
// perform frequency-map after every shift
short_term_analyze(xx, XX, nw, ns, ww);
fmap(YY, XX, nw, mm, nm, g1, g2); // compress frequency range
short_term_synthesize(yy, YY, nw, ns);
}
}
/***********************************************************/
// nonlinear-frequency-compression analysis
FUNC(void)
cha_nfc_process(CHA_PTR cp, float *x, float *y, int cs)
{
float *ww, *xx, *yy, *XX, *YY, *g1, *g2;
int nw, nm, *mm;
// copy parameters and pointers from cha_data
nw = CHA_IVAR[_nfc_nw];
nm = CHA_IVAR[_nfc_nm];
mm = (int *) cp[_nfc_mm];
ww = (float *) cp[_nfc_ww];
xx = (float *) cp[_nfc_xx];
yy = (float *) cp[_nfc_yy];
XX = (float *) cp[_nfc_XX];
YY = (float *) cp[_nfc_YY];
g1 = (float *) cp[_nfc_g1];
g2 = (float *) cp[_nfc_g2];
if (cs <= (nw / 2)) { // short chunk ??
nfc_sc(cp, x, y, cs, xx, yy, XX, YY, ww, g1, g2, mm, nm, nw);
} else { // long chunk (not yet implemented)
nfc_lc(x, y, cs, xx, yy, XX, YY, ww, g1, g2, mm, nm, nw);
}
}