-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLPC.java
More file actions
226 lines (210 loc) · 6.91 KB
/
LPC.java
File metadata and controls
226 lines (210 loc) · 6.91 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
* Copyright (C) 2010 Preston Lacey http://javaflacencoder.sourceforge.net/
* All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package javaFlacEncoder;
/**
* This class is used to calculate LPC Coefficients for a FLAC stream.
*
* @author Preston Lacey
*/
public class LPC {
/** The error calculated by the LPC algorithm */
protected double rawError;
/** The coefficients as calculated by the LPC algorithm */
protected double[] rawCoefficients;
private double[] tempCoefficients;
/** The order of this LPC calculation */
protected int order;
static int[] tempSum = null;
/**
* Constructor creates an LPC object of the given order.
* @param order Order for this LPC calculation.
*/
public LPC(int order) {
this.order = order;
rawError = 0;
rawCoefficients = new double[order+1];
tempCoefficients = new double[order+1];
}
/**
* Get this LPC object's order
* @return order used for this LPC calculation.
*/
public int getOrder () { return order; }
/**
* Get the error for this LPC calculation
* @return lpc error
*/
public double getError() { return rawError; }
/**
* Get the calculated LPC Coefficients as an array.
* @return lpc coefficients in an array.
*/
public double[] getCoefficients() { return rawCoefficients; }
/**
* Calculate an LPC using the given Auto-correlation data. Static method
* used since this is slightly faster than a more strictly object-oriented
* approach.
*
* @param lpc LPC to calculate
* @param R Autocorrelation data to use
*/
public static void calculate(LPC lpc, long[] R) {
int coeffCount = lpc.order;
//calculate first iteration directly
double[] A = lpc.rawCoefficients;
for(int i = 0; i < coeffCount+1; i++) A[i] = 0.0;
A[0] = 1;
double E = R[0];
//calculate remaining iterations
if(R[0] == 0) {
for(int i = 0; i < coeffCount+1; i++)
A[i] = 0.0;
}
else {
double[] ATemp = lpc.tempCoefficients;
for(int i = 0; i < coeffCount+1; i++) ATemp[i] = 0.0;
for(int k = 0; k < coeffCount; k++) {
double lambda = 0.0;
double temp = 0;
for(int j = 0; j <= k; j++) {
temp += A[j]*R[k+1-j];
}
lambda = -temp/E;
for(int i = 0; i <= k+1; i++) {
ATemp[i] = A[i]+lambda*A[k+1-i];
}
System.arraycopy(ATemp, 0, A, 0, coeffCount+1);
E = (1-lambda*lambda)*E;
}
}
lpc.rawError = E;
}
/**
* Calculate an LPC using a prior order LPC's values to save calculations.
*
* @param lpc LPC to calculate
* @param R Auto-correlation data to use.
* @param priorLPC Prior order LPC to use(may be any order lower than our
* target LPC)
*
*/
public static void calculateFromPrior(LPC lpc, long[] R, LPC priorLPC) {
int coeffCount = lpc.order;
//calculate first iteration directly
double[] A = lpc.rawCoefficients;
for(int i = 0; i < coeffCount+1; i++) A[i] = 0.0;
A[0] = 1;
double E = R[0];
int startIter = 0;
if(priorLPC != null && priorLPC.order < lpc.order) {
startIter = priorLPC.order;
E = priorLPC.rawError;
System.arraycopy(priorLPC.rawCoefficients, 0, A, 0, startIter+1);
}
//calculate remaining iterations
if(R[0] == 0) {
for(int i = 0; i < coeffCount+1; i++)
A[i] = 0.0;
}
else {
double[] ATemp = lpc.tempCoefficients;
for(int i = 0; i < coeffCount+1; i++) ATemp[i] = 0.0;
for(int k = startIter; k < coeffCount; k++) {
double lambda = 0.0;
double temp = 0.0;
for(int j = 0; j <= k; j++) {
temp -= A[j]*R[k-j+1];
}
lambda = temp/E;
for(int i = 0; i <= k+1; i++) {
ATemp[i] = A[i]+lambda*A[k+1-i];
}
System.arraycopy(ATemp, 0, A, 0, coeffCount+1);
E = (1-lambda*lambda)*E;
}
}
lpc.rawError = E;
}
/**
* Create auto-correlation coefficients(up to a maxOrder of 32).
* @param R Array to put results in.
* @param samples Samples to calculate the auto-correlation for.
* @param count number of samples to use
* @param start index of samples array to start at
* @param increment number of indices to increment between valid samples(for
* interleaved arrays)
* @param maxOrder maximum order to calculate.
*/
public static void createAutoCorrelation(long[] R, int []samples, int count,
int start, int increment, int maxOrder) {
if(increment == 1 && start == 0) {
for(int i = 0; i <= maxOrder; i++) {
R[i] = 0;
long temp = 0;
for(int j = 0; j < count-i; j++) {
temp += (long)samples[j]*(long)samples[j+i];
}
R[i] += temp;
}
}
else {
for(int i = 0; i <= maxOrder; i++) {
R[i] = 0;
int baseIndex = increment*i;
long temp = 0;
int innerLimit = (count-i)*increment;
for(int j = start; j < innerLimit; j+=increment) {
temp += (long)samples[j]*(long)samples[j+baseIndex];
}
R[i] += temp;
}
}
}
/**
* Apply a window function to sample data
* @param samples Samples to apply window to. Values in this array are left
* unaltered.
* @param count number of samples to use
* @param start index of samples array to start at
* @param increment number of indices to increment between valid samples(for
* interleaved arrays)
* @param windowedSamples array containing windowed values. Return values
* are packed(increment of one).
*
*/
public static void window(int[] samples, int count, int start, int increment,
int[] windowedSamples) {
int[] values = windowedSamples;
int loopCount = 0;
float halfway = count/2.0f;
float hth = halfway*halfway;
float windowCount = -halfway;
int limit = count*increment+start;
for(int i = start; i < limit; i+=increment) {
//float innerCount = (windowCount < 0) ? -windowCount:windowCount;
float innerCountSquared = windowCount*windowCount;
windowCount++;
//double val = 1.0-(double)(innerCount/halfway);
float val = 1.0f-( innerCountSquared/hth );
double temp = ((double)samples[i])*val;
temp = (temp >0) ? temp+0.5:temp-0.5;
values[loopCount++] = (int)temp;
}
}
}