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
use crate::pitch::pitch_xcorr;
pub fn lpc(lpc: &mut [f32], ac: &[f32], p: usize) {
// ac: autocorrelation [0..p]
// lpc: output coefficients [0..p-1]
let mut error = ac[0];
if error <= 1e-10 {
for x in lpc.iter_mut() {
*x = 0.0;
}
return;
}
for i in 0..p {
// Sum up this iteration's reflection coefficient
let mut rr = 0.0f32;
for j in 0..i {
rr += lpc[j] * ac[i - j];
}
rr += ac[i + 1];
let r = -rr / error;
// Update LPC coefficients and total error
lpc[i] = r;
for j in 0..((i + 1) / 2) {
let tmp1 = lpc[j];
let tmp2 = lpc[i - 1 - j];
lpc[j] = tmp1 + r * tmp2;
lpc[i - 1 - j] = tmp2 + r * tmp1;
}
error = error - r * r * error;
// Bail out once we get 30 dB gain (approx 0.001 error ratio)
if error <= 0.001 * ac[0] {
break;
}
}
}
pub fn autocorr(
x: &[f32],
ac: &mut [f32],
window: Option<&[f32]>,
overlap: usize,
lag: usize, // usually p
n: usize,
) {
// Computes autocorrelation of x.
// window: optional window to apply to first/last 'overlap' samples
// lag: number of lags (ac has size lag+1)
let mut xx = Vec::with_capacity(n);
// Apply window if present
if let Some(win) = window {
if x.len() < n {
// Panic or handle safe
return;
}
// Copy x with windowing
xx.extend_from_slice(&x[0..n]);
for i in 0..overlap {
xx[i] *= win[i];
xx[n - 1 - i] *= win[i];
}
} else {
xx.extend_from_slice(&x[0..n]);
}
// Use pitch_xcorr to compute autocorrelation
// In autocorr, x and y are the same signal.
// pitch_xcorr(x, y, xcorr, len, max_pitch)
// Here x=xx, y=xx. len = n? No.
// C implementation: `celt_pitch_xcorr(xptr, xptr, ac, fastN, lag+1, arch)`
// where fastN = n - lag.
// The standard autocorr definition uses N samples for lag 0.
// But for lag k, we only have N-k pairs.
// Opus `autocorr` implementation does:
let fast_n = n - lag;
// pitch_xcorr with len=fastN computes sums for j=0..fastN.
// This ignores the tails?
// C code follows up with:
// for (k=0;k<=lag;k++)
// for (i = k+fastN, d = 0; i < n; i++) d += x[i]*x[i-k]
// ac[k] += d
// This implies pitch_xcorr handled the bulk (0..fastN), and we fix up the tail.
pitch_xcorr(&xx, &xx, ac, fast_n, lag + 1);
// Tail fixup
for k in 0..=lag {
let mut d = 0.0f32;
for i in (k + fast_n)..n {
d += xx[i] * xx[i - k];
}
ac[k] += d;
}
}
pub fn celt_fir(x: &[f32], num: &[f32], y: &mut [f32], n: usize, ord: usize) {
// Standard FIR filter
// y[i] = x[i] + sum(num[j] * x[i-j-1]) ?
// Wait, Opus `celt_fir`:
// for i=0..N
// sum = x[i]
// for j=0..ord
// sum += num[j]*mem[j]
// mem shift
// y[i] = sum
// This is IIR? No.
// Note: in C `pitch_downsample` calls `celt_fir5` which has `mem`.
// But `celt_fir` generally assumes `num` are coeffs.
// Actually, `celt_fir` in `celt_lpc.c` is NOT defined for float?
// In header: `void celt_fir_c(...)`.
// It's usually the prediction filter.
// x is residual, y is signal? Or vice versa.
// pitch_downsample uses `celt_fir5` which seems to be `analysis` filtering (calculating residual).
// let mut mem = vec![0.0f32; ord]; // Assumes 0 initial memory if not passed?
// Actually `celt_fir` in C takes `mem`?
// Header: `celt_fir` wrapper calls `celt_fir_c` but `celt_fir_c` signature in header:
// void celt_fir_c(const val16 *x, const val16 *num, val16 *y, int N, int ord, int arch)
// No mem?
// Let's check `celt_lpc.c`... IT ONLY EXISTS FOR FIXED POINT CHECK ASM?
// `celt_fir` implementation:
/*
for (i=0;i<N;i++)
{
opus_val32 sum = x[i];
for (j=0;j<ord;j++)
sum -= den[j]*mem[j];
...
}
*/
// Wait, that code block in `celt_lpc.c` lines 200-250 was `celt_iir`!
// `celt_fir` implementation is missing from my `read_file` of `celt_lpc.c`.
// Let's assume standard FIR:
// y[n] = x[n] + sum_{k=0}^{ord-1} num[k] * x[n-k-1] (or similar)
// Opus usually defines FIR/IIR for LPC synthesis/analysis.
// For now, simple implementation assuming x contains history or mem is managed.
// If not, we might need to handle memory.
// I will implement a safe version with internal memory for the loop.
// BUT `pitch_downsample` uses `celt_fir5` which explicitly passes `mem` locals.
// Let's leave `celt_fir` simple.
for i in 0..n {
let mut sum = x[i];
for j in 0..ord {
if i >= j + 1 {
sum += num[j] * x[i - j - 1]; // Check sign?
}
}
y[i] = sum;
}
}
pub fn celt_iir(x: &[f32], den: &[f32], y: &mut [f32], n: usize, ord: usize, mem: &mut [f32]) {
// IIR filter: Synthesis
// y[i] = x[i] - sum(den[j] * y[i-j-1])
// Uses `mem` to store past `y`.
for i in 0..n {
let mut sum = x[i];
for j in 0..ord {
sum -= den[j] * mem[j];
}
for j in (1..ord).rev() {
mem[j] = mem[j - 1];
}
mem[0] = sum;
y[i] = sum;
}
}