-
Notifications
You must be signed in to change notification settings - Fork 7
/
Denoiser.java
281 lines (234 loc) · 10.2 KB
/
Denoiser.java
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import java.util.Arrays;
public class Denoiser implements AudioProcessor {
private static int windowLength;
private static double overlapRatio;
private int fs;
private double noSpeechDuration;
private int noSpeechSegments;
private boolean speechFlag;
private boolean noiseFlag;
private int noiseCounter;
private int noiseLength;
private int noiseThreshold;
private int frameReset;
public Denoiser(int fs) {
windowLength = 256;
overlapRatio = 0.5;
this.fs = fs;
this.noSpeechDuration = 0.4;
this.noSpeechSegments = (int)Math.floor((noSpeechDuration * fs - windowLength) / (overlapRatio * windowLength) + 1);
this.speechFlag = false;
this.noiseFlag = false;
this.noiseLength = 9;
this.noiseThreshold = 3;
this.frameReset = 8;
}
public Denoiser(int fs, double noSpeechDuration) {
windowLength = 256;
overlapRatio = 0.5;
this.fs = fs;
this.noSpeechDuration = noSpeechDuration;
this.noSpeechSegments = (int)Math.floor((noSpeechDuration * fs - windowLength) / (overlapRatio * windowLength) + 1);
this.speechFlag = false;
this.noiseFlag = false;
this.noiseLength = 9;
this.noiseThreshold = 3;
this.frameReset = 8;
}
public Denoiser(int fs, double noSpeechDuration, int noiseLength, int noiseThreshold, int frameReset) {
windowLength = 256;
overlapRatio = 0.5;
this.fs = fs;
this.noSpeechDuration = noSpeechDuration;
this.noSpeechSegments = (int)Math.floor((noSpeechDuration * fs - windowLength) / (overlapRatio * windowLength) + 1);
this.speechFlag = false;
this.noiseFlag = false;
this.noiseLength = noiseLength;
this.noiseThreshold = noiseThreshold;
this.frameReset = frameReset;
}
/**
* Process function for multi-channel inputs
* @param input Multi channel signal
* @return enhanced Multi channel enhanced signal
*/
public double[][] process(double[][] input) {
int channels = input.length;
int signalLength = input[0].length;
double[][] enhanced = new double[channels][signalLength];
for (int i = 0; i < channels; i++) {
enhanced[i] = process(input[i]);
}
return enhanced;
}
/**
* Performs speech denoising on array of doubles based on Speech Enhancement Using a Minimum Mean-Square
* Error Short-Time Spectral Amplitude Estimator by Eprahiam and Malah
* @param input Double array of signal values
* @return enhanced Double array of enhanced signal array
*/
public double[] process(double[] input) {
double[][] sampledSignalWindowed = segmentSignal(input, windowLength, overlapRatio);
int frames = sampledSignalWindowed[0].length;
ComplexNumber[][] sampledSignalWindowedComplex = new ComplexNumber[frames][windowLength];
ComplexNumber[][] signalFFT = new ComplexNumber[frames][windowLength];
double[][] signalFFTMagnitude = new double[frames][windowLength];
double[][] signalFFTPhase = new double[frames][windowLength];
for (int i = 0; i < frames; i++) {
for (int k = 0; k < windowLength; k++) {
sampledSignalWindowedComplex[i][k] = new ComplexNumber(sampledSignalWindowed[k][i]); //convert samples to Complex form for fft and perform transpose
}
}
for (int i = 0; i < frames; i++) {
signalFFT[i] = Utils.fft(sampledSignalWindowedComplex[i]);
}
for (int i = 0; i < frames; i++) {
for (int k = 0; k < windowLength; k++) {
signalFFTMagnitude[i][k] = signalFFT[i][k].mod();
signalFFTPhase[i][k] = signalFFT[i][k].getArg();
}
}
double[][] noise = new double[this.noSpeechSegments][windowLength];
double[][] noiseMag = new double[this.noSpeechSegments][windowLength];
noise = Arrays.copyOfRange(signalFFTMagnitude, 0, this.noSpeechSegments);
for (int i = 0; i < this.noSpeechSegments; i++) {
for (int k = 0; k < windowLength; k++) {
noiseMag[i][k] = Math.pow(noise[i][k], 2);
}
}
double[] noiseMean = Utils.mean(noise, 0);
double[] noiseVar = Utils.mean(noiseMag, 0);
double gamma1p5 = Utils.gamma(1.5);
double[] gain = new double[windowLength];
double[] gamma = new double[windowLength];
double[] gammaUpdate = new double[windowLength];
double[] xi = new double[windowLength];
double[] nu = new double[windowLength];
double alpha = 0.96; //Smoothing factor
Arrays.fill(gain, 1);
Arrays.fill(gamma, 1);
double[][] enhancedSpectrum = new double[frames][windowLength];
for (int i = 0; i < frames; i++) {
if (i < this.noSpeechSegments) {
this.speechFlag = false;
this.noiseCounter = 100;
} else {
vad(signalFFTMagnitude[i], noiseMean);
}
if (this.speechFlag == false) { // Noise estimate update during segements with no speech
for (int k = 0; k < windowLength; k++) {
noiseMean[k] = (this.noiseLength * noiseMean[k] + signalFFTMagnitude[i][k]) / (this.noiseLength + 1);
noiseVar[k] = (this.noiseLength * noiseVar[k] + Math.pow(signalFFTMagnitude[i][k], 2)) / (this.noiseLength + 1);
}
}
for (int k = 0; k < windowLength; k++) {
gammaUpdate[k] = Math.pow(signalFFTMagnitude[i][k], 2) / noiseVar[k];
xi[k] = alpha * Math.pow(gain[k], 2) * gamma[k] + (1 - alpha) * Math.max(gammaUpdate[k] - 1, 0);
gamma[k] = gammaUpdate[k];
nu[k] = gamma[k] * xi[k] / (xi[k] + 1);
gain[k] = (gamma1p5 * Math.sqrt(nu[k])) / gamma[k] * Math.exp(-1 * nu[k] / 2) * ((1 + nu[k]) * Bessel.modBesselFirstZero(nu[k] / 2) + nu[k] * Bessel.modBesselFirstOne(nu[k] / 2));
if (Double.isNaN(gain[k]) || Double.isInfinite(gain[k])) {
gain[k] = xi[k] / (xi[k] + 1);
}
enhancedSpectrum[i][k] = gain[k] * signalFFTMagnitude[i][k];
}
}
ComplexNumber[][] enhancedSpectrumComplex = new ComplexNumber[frames][windowLength];
for (int i = 0; i < frames; i++) {
for (int k = 0; k < windowLength; k++) {
enhancedSpectrumComplex[i][k] = ComplexNumber.exp(new ComplexNumber(0, signalFFTPhase[i][k]));
enhancedSpectrumComplex[i][k] = enhancedSpectrumComplex[i][k].times(enhancedSpectrum[i][k]);
}
}
ComplexNumber[][] enhancedSegments = new ComplexNumber[frames][windowLength];
double[][] enhancedSegmentsReal = new double[windowLength][frames];
for (int i = 0; i < frames; i++) {
enhancedSegments[i] = Utils.ifft(enhancedSpectrumComplex[i]);
}
for (int i = 0; i < frames; i++) {
for (int k = 0; k < windowLength; k++) {
enhancedSegmentsReal[k][i] = enhancedSegments[i][k].getRe(); //convert samples to real from and perform tranpose
}
}
double[] enhanced = overlapAndAdd(enhancedSegmentsReal, overlapRatio);
return enhanced;
}
/**
* Voice activity detector that predicts wheter the current frame contains speech or not
* @param frame Current frame
* @param noise Current noise estimate
* @param noiseCounter Number of previous noise frames
* @param noiseThreshold User set threshold
* @param frameReset Number of frames after which speech flag is reset
*/
private void vad(double[] frame, double[] noise) {
double[] spectralDifference = new double[windowLength];
for (int i = 0; i < windowLength; i++) {
spectralDifference[i] = 20 * (Math.log10(frame[i]) - Math.log10(noise[i]));
if (spectralDifference[i] < 0) {
spectralDifference[i] = 0;
}
}
double diff = Utils.mean(spectralDifference);
if (diff < this.noiseThreshold) {
this.noiseFlag = true;
this.noiseCounter++;
} else {
this.noiseFlag = false;
this.noiseCounter = 0;
}
if (this.noiseCounter > this.frameReset) {
this.speechFlag = false;
} else {
this.speechFlag = true;
}
}
/**
* Windows sampled signal using overlapping Hamming windows
* @param ss The sampled signal
* @param ww The window width
* @param or The overlap ratio
* @return seg The overlapping windowed segments
*/
private double[][] segmentSignal(double[] ss, int ww, double or ) {
int len = ss.length;
double d = 1 - or;
int frames = (int)(Math.floor(len - ww) / ww / d);
int start = 0;
int stop = 0;
double[] window = Utils.hamming(ww);
double[][] seg = new double[ww][frames];
for (int i = 0; i < frames; i++) {
start = (int)(i * ww * or );
stop = start + ww;
for (int k = 0; k < ww; k++) {
seg[k][i] = ss[start + k] * window[k];
}
}
return seg;
}
/**
* Overlap and add segments to calculate reconstructed signal
* @param segments 2D array of overlapping signal segments
* @param or overlap ratio
* @return reconstructedSignal Speech signal post speech denoising
*/
private double[] overlapAndAdd(double[][] segments, double or ) {
int ww = segments.length;
int frames = segments[0].length;
int start = 0;
int stop = 0;
int signalLength = (int)(ww * (1 - or ) * (frames - 1) + ww);
double[] reconstructedSignal = new double[signalLength];
for (int i = 0; i < frames; i++) {
start = (int)(i * ww * or );
stop = start + ww;
for (int k = 0; k < ww; k++) {
reconstructedSignal[start + k] = reconstructedSignal[start + k] + segments[k][i];
}
}
return reconstructedSignal;
}
public static void main(String[] args) {
}
}