-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
681 lines (512 loc) · 25.6 KB
/
functions.py
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 1 16:00:19 2021
@author: Panagiotis Antivasis
"""
from time import time, strftime, gmtime
from structs import Deterministic, Frame
from numpy import arange, zeros, blackman, hamming, \
argwhere, insert, flipud, asarray, append, multiply, \
real, imag, pi, divide, log10, angle, diff, unwrap, sin, cos, \
std, concatenate, tile, dot, ndarray, transpose, conjugate, ones
from numpy.linalg import inv
from scipy.interpolate import interp1d
from scipy.signal import lfilter
from scipy.io.wavfile import read
from misc import arrayByIndex, mytranspose, end, transpose1dArray, normalize, \
isContainer, isEmpty, ellipFilter, \
medfilt, min_interp_size
from copy import deepcopy
from tqdm import tqdm
from warnings import filterwarnings
from SWIPE import swipep
def eaQHMAnalysisAndSynthesis(speechFile: str, gender: str or tuple = 'other', step: int = 15,
maxAdpt: int = 10, pitchPeriods: int = 3, analysisWindow: int = 32, fullWaveform: bool = True,
fc: int = 0, partials: int = 0, printPrompts: bool = True, loadingScreen: bool = True):
'''
Performs adaptive Quasi-Harmonic Analysis of Speech
using the extended adaptive Quasi-Harmonic Model and decomposes
speech into AM-FM components according to that model,
while iteratively refining it.
Parameters
----------
speechFile : str
The location of the mono .wav file to be analysed.
gender : str or tuple, optional
The gender of the speaker, defining the pitch limit of SWIPEP.
If str, 'male' limit is [70, 180], 'female' is [70, 180] and 'child' can also be used for [300, 600]. Any other string is [70, 500].
If tuple, gender[0] is the minimum and gender[1] the maximum pitch limit.
The default is 'other'.
step : int, optional
The step size of the processing in samples. The default is 15.
maxAdpt : int, optional
The maximum number of adaptations allowed. The default is 10.
pitchPeriods : int, optional
The number of analysis window size, in pitch periods. The default is 3.
analysisWindow : int, optional
The steps of the pitch analysis window, where the analysis starts. The default is 32.
fullWaveform : bool, optional
Determines if a full waveform length analysis will be performed. The default is True.
fc : int, optional
Applies a high pass filtering at the specified Hz before the analysis starts. If <= 0, no filter is applied. The default is 0.
partials : int, optional
The number of partials to be used. If <= 0, it is determined by the pitch estimations. The default is 0.
printPrompts : bool, optional
Determines if prompts of this process will be printed. The default is True.
loadingScreen : bool, optional
Determines if a tqdm loading screen will be displayed in the console. The default is True.
Returns
-------
s_recon : (length) array_like
The refined signal.
SRER : list
An array containing all the adaptation numbers of the signal.
DetComponents : (No_ti) array_like
The Deterministic components of the signal. An array containing elements of Deterministic-class type.
endTime : float
The total time the process takes in seconds.
'''
filterwarnings("ignore")
startTime = time()
fs, s = read(speechFile)
s = transpose1dArray(s/normalize)
length = len(s)
if fc > 0:
s = transpose(ellipFilter(transpose(s), fs, fc))
s2 = deepcopy(s)
if isinstance(gender, tuple):
f0min = gender[0]
f0max = gender[1]
elif gender == 'male':
f0min = 70
f0max = 180
elif gender == 'female':
f0min = 160
f0max = 300
elif gender == 'child':
f0min = 300
f0max = 600
else:
f0min = 70
f0max = 500
f0s = swipep(transpose(s2)[0], fs, speechFile, [f0min, f0max])
f0s = getLinear(f0s, arange(0, len(s2)-1, round(fs*5/1000))/fs)
Fmax = int(fs/2-200)
if partials > 0:
Kmax = partials
else:
Kmax = int(round(Fmax/min(f0s[:,1])) + 10)
analysisWindowSamples = analysisWindow*step
frames, frame_step = voicedUnvoicedFrames(s, fs, gender)
if not fullWaveform:
sp_i = []
ss = zeros((len(s), 1))
for f in frames:
if f.isSpeech and f.isVoiced:
sp_i.append(f.ti)
else:
if not isEmpty(sp_i):
sp_v = arange(sp_i[0]-frame_step, end(sp_i)+frame_step+1)
ss[sp_v] = s[sp_v]
sp_i.clear()
deterministic_part = ss
else:
for i, f in enumerate(frames):
if f.ti > analysisWindowSamples/2 and f.isSpeech and (not f.isVoiced) and f.ti < length - analysisWindowSamples/2:
frames[i].isVoiced = True
if f.ti > analysisWindowSamples/2 and (not f.isSpeech) and (not f.isVoiced) and f.ti < length - analysisWindowSamples/2:
frames[i].isSpeech = True
frames[i].isVoiced = True
deterministic_part = s
ti = arange(1,length,step)
No_ti = len(ti)
DetComponents = [Deterministic() for _ in range(No_ti)]
SRER = []
framei = (ti/frame_step)
framei_int = framei.astype(int)
window_lengths = zeros(No_ti, int)
fm_current = zeros((length, Kmax), float)
am_current = zeros((length, Kmax), float)
std_det = std(deterministic_part)
for a in range(maxAdpt+1):
adptStartTime = time()
if printPrompts:
print('---- Adaptation No. {} ----\n'.format(a))
a0_recon = zeros(length, float)
am_recon = zeros((length, Kmax), float)
fm_recon = zeros((length, Kmax), float)
ph_recon = zeros((length, Kmax), float)
if loadingScreen:
analysisloop = tqdm(total=No_ti, position=0, leave=True)
for i, tith in enumerate(ti):
if loadingScreen:
analysisloop.set_description("Analysis".format(i))
if tith > analysisWindowSamples and tith < length-analysisWindowSamples:
if frames[framei_int[i]-1].isVoiced and frames[framei_int[i]].isVoiced:
if a == 0:
framei_dec = framei[i] - framei_int[i]
f0 = (1-framei_dec)*f0s[framei_int[i]-1][1] + framei_dec*f0s[framei_int[i]][1]
K = int(min(Kmax, int(Fmax/f0)))
f0range = arange(-K,K+1)*f0
window_lengths[i] = max(120, round((pitchPeriods/2)*(fs/f0)))
window_range = arange(-window_lengths[i]-1,window_lengths[i])
window = blackman(2*window_lengths[i]+1)
amplitudes, slopes = iqhmLS_complexamps(s[window_range + tith], f0range, window, fs)
fmismatch = zeros(len(f0range), float)
else:
window_range = arange(-window_lengths[i]-1,window_lengths[i])
window = hamming(2*window_lengths[i]+1)
fm_current_nonzeros = argwhere(fm_current[tith-1])
if len(fm_current_nonzeros) == 0:
fm_current_nonzeros = 0
K = 1
fm_current[tith-1, fm_current_nonzeros] = 140
am_current[tith-1, fm_current_nonzeros] = 10e-4
fm = fm_current[tith + window_range, fm_current_nonzeros]
am = am_current[tith + window_range, fm_current_nonzeros]
fm_len = len(fm)
fm_zeros = argwhere(fm == 0)
fm_nonzeros = argwhere(fm)
if len(fm_zeros) != 0:
fm_zeros_index = fm_zeros[0][0]
fm_nonzeros_index = fm_nonzeros[0][0]
if fm_zeros_index == 0:
fm[fm_zeros_index]= fm[fm_nonzeros_index]
am[fm_zeros_index] = am[fm_nonzeros_index]
fm_nonzeros = insert(fm_nonzeros, 0, fm_zeros_index)
fm_zeros_index = end(fm_zeros)
fm_nonzeros_index = end(fm_nonzeros)
if fm_zeros_index == fm_len-1:
fm[fm_zeros_index] = fm[fm_nonzeros_index]
am[fm_zeros_index] = am[fm_nonzeros_index]
fm_nonzeros = append(fm_nonzeros, fm_zeros_index)
x_new = arange(0, fm_len)
if len(transpose(fm_nonzeros)) == 1:
fm_nonzeros = transpose(fm_nonzeros)[0]
fm = interp1d(fm_nonzeros, fm[fm_nonzeros])(x_new)
am = interp1d(fm_nonzeros, am[fm_nonzeros])(x_new)
else:
fm = transpose(fm_current[tith + window_range, fm_current_nonzeros])
am = transpose(am_current[tith + window_range, fm_current_nonzeros])
Kend = len(fm_current_nonzeros)
K = end(fm_current_nonzeros)+1
for k in range(Kend):
fm_len = len(fm[:, k])
fm_zeros = argwhere(fm[:, k] == 0)
fm_nonzeros = argwhere(fm[:, k])
if len(fm_zeros) != 0:
fm_zeros_index = fm_zeros[0][0]
fm_nonzeros_index = fm_nonzeros[0][0]
if fm_zeros_index == 0:
fm[fm_zeros_index][k]= fm[fm_nonzeros_index][k]
am[fm_zeros_index][k] = am[fm_nonzeros_index][k]
fm_nonzeros = insert(fm_nonzeros, 0, fm_zeros_index)
fm_zeros_index = end(fm_zeros)
fm_nonzeros_index = end(fm_nonzeros)
if fm_zeros_index == fm_len-1:
fm[fm_zeros_index][k] = fm[fm_nonzeros_index][k]
am[fm_zeros_index][k] = am[fm_nonzeros_index][k]
fm_nonzeros = append(fm_nonzeros, fm_zeros_index)
x_new = arange(0, fm_len)
if len(transpose(fm_nonzeros)) == 1:
fm_nonzeros = transpose(fm_nonzeros)[0]
fm[:, k] = interp1d(fm_nonzeros, fm[fm_nonzeros, k])(x_new)
am[:, k] = interp1d(fm_nonzeros, am[fm_nonzeros, k])(x_new)
if isinstance(fm_current_nonzeros, ndarray):
fm_current_nonzeros = concatenate((transpose(-flipud(fm_current_nonzeros+1))[0], [0], transpose(fm_current_nonzeros)[0]+1)) + K
tmp_zeros = zeros((fm_len, 1), float)
fm = concatenate((-flipud(fm), tmp_zeros, fm), axis=1)
am = concatenate((flipud(am), tmp_zeros, am), axis=1)
else:
fm_current_nonzeros = asarray([K-1, K, K+1])
tmp_zeros = zeros((fm_len, 1), float)
fm = concatenate((-flipud(transpose1dArray(fm)), tmp_zeros, transpose1dArray(fm)), axis=1)
am = concatenate((flipud(transpose1dArray(am)), tmp_zeros, transpose1dArray(am)), axis=1)
amplitudes_tmp, slopes_tmp = eaqhmLS_complexamps(s[window_range + tith], am, fm, window, fs)
fmismatch_tmp = fs/(2*pi)*divide(multiply(real(amplitudes_tmp), imag(slopes_tmp)) - multiply(imag(amplitudes_tmp), real(slopes_tmp)), abs(amplitudes_tmp)**2)
amplitudes = arrayByIndex(fm_current_nonzeros, transpose(amplitudes_tmp)[0])
slopes = arrayByIndex(fm_current_nonzeros, transpose(slopes_tmp)[0])
fmismatch = arrayByIndex(fm_current_nonzeros, transpose(fmismatch_tmp)[0])
a0_recon[tith-1] = real(amplitudes[K])
amplitudes = mytranspose(amplitudes[K+1:2*K+1])
slopes = mytranspose(slopes[K+1:2*K+1])
fmismatch = mytranspose(fmismatch[K+1:2*K+1])
amplitude_log_max = 20*log10(max(abs(amplitudes)))-150
h = f0/(a+1)
for k in range(K):
amplitude_log = 20*log10(abs(amplitudes[k]))
if amplitude_log > amplitude_log_max and abs(fmismatch[k]) < h:
am_recon[tith-1][k] = abs(amplitudes[k])
ph_recon[tith-1][k] = angle(amplitudes[k])
if a == 0:
fm_recon[tith-1][k] = (k+1)*f0
elif f0 > f0min:
fm_recon[tith-1][k] = fm_current[tith-1][k] + fmismatch[k]
else:
fm_recon[tith-1][k] = fm_current[tith-1][k]
DetComponents[i] = Deterministic(ti=tith-1, isSpeech=True, isVoiced=True)
else:
DetComponents[i] = Deterministic(ti=tith-1, isSpeech=True, isVoiced=False)
else:
DetComponents[i] = Deterministic(ti=tith-1, isSpeech=False, isVoiced=False)
if loadingScreen:
analysisloop.update(1)
if loadingScreen:
analysisloop.close()
fm_current = zeros((length, Kmax), float)
am_current = zeros((length, Kmax), float)
a0_recon = interp1d(ti-1, a0_recon[ti-1], kind=3, fill_value="extrapolate")(arange(0, length))
if loadingScreen:
interploop = tqdm(total=Kmax, position=0, leave=True)
for k in range(Kmax):
if loadingScreen:
interploop.set_description("Interpolation".format(k))
am_recon_nonzeros = argwhere(am_recon[:, k])
diff_am_recon_nonzeros = diff(concatenate(([0], transpose(am_recon_nonzeros)[0], [length-1])))
diff_indices = (diff_am_recon_nonzeros <= step).astype(int)
diff_indices_diff = diff(diff_indices)
diff_ones = argwhere(diff_indices_diff == 1)
diff_minus_ones = argwhere(diff_indices_diff == -1)
for i, st_tith in enumerate(diff_ones):
am_indices = transpose(am_recon_nonzeros[st_tith[0]: diff_minus_ones[i][0]+1])[0]
am_range = arange(am_recon_nonzeros[st_tith[0]], am_recon_nonzeros[diff_minus_ones[i][0]]+1)
am_recon[am_range, k] = interp1d(am_indices, am_recon[am_indices, k])(am_range)
if len(am_indices) >= min_interp_size:
fm_recon[am_range, k] = interp1d(am_indices, fm_recon[am_indices, k], kind=3)(am_range)
else:
am_indices_tmp = concatenate((arange(0, (min_interp_size-len(am_indices))*step, step), am_indices))
fm_recon[am_range, k] = interp1d(am_indices_tmp, fm_recon[am_indices_tmp, k], kind=3)(am_range)
ph_recon[am_range, k]= phase_integr_interpolation(2*pi/fs*fm_recon[:, k], ph_recon[:, k], am_indices)
fm_current[am_range, k] = concatenate(([fm_recon[am_range[0]][k]], fs/(2*pi)*diff(unwrap(ph_recon[am_range, k]))))
if loadingScreen:
interploop.update(1)
if loadingScreen:
interploop.close()
am_current = am_recon
s_recon_tmp = a0_recon + 2*multiply(am_recon, cos(ph_recon)).sum(axis=1)
s_recon_tmpT = transpose1dArray(s_recon_tmp)
SRER.append(20*log10(std_det/std(deterministic_part-s_recon_tmpT)))
if printPrompts:
print('\nSRER: {} dB in Adaptation No: {}'.format(SRER[a], a))
print('Adaptation Time: {}\n'.format(strftime("%H:%M:%S", gmtime(time() - adptStartTime))))
if a != 0:
if SRER[a] <= SRER[a-1]:
break
s_recon = deepcopy(s_recon_tmp)
a0_fin = a0_recon
am_fin = am_recon
fm_fin = fm_recon
pm_fin = ph_recon
for i, d in enumerate(DetComponents):
if d.isVoiced:
ti = d.ti
am_nonzeros = argwhere(am_fin[ti])
DetComponents[i].a0 = a0_fin[ti]
DetComponents[i].amplitudes = arrayByIndex(am_nonzeros, am_fin[ti, am_nonzeros])
DetComponents[i].frange = arrayByIndex(am_nonzeros, fm_fin[ti, am_nonzeros])
DetComponents[i].pk = arrayByIndex(am_nonzeros, pm_fin[ti, am_nonzeros])
endTime = time() - startTime
if printPrompts:
print('Signal adapted to {} dB SRER'.format(round(max(SRER), 6)))
print('Total Time: {}\n\n'.format(strftime("%H:%M:%S", gmtime(endTime))))
return s_recon, SRER, DetComponents, endTime
def iqhmLS_complexamps(s, f0range, window, fs: int):
'''
Computes iteratively the parameters of first order complex polynomial
model using Least Squares.
Parameters
----------
s : array_like
The part of the signal to be computed.
f0range : array_like
The estimated frequencies.
window : array_like
The window of the signal to be computed.
fs : int
The sampling frequency.
Returns
-------
amplitudes : array_like
Amplitude of harmonics.
slopes : array_like
Slope of harmonics.
'''
windowT = transpose1dArray(window)
midlen = (len(s)-1)/2
K = len(f0range)
window_range = arange(-midlen,midlen+1)
window_rangeT = transpose1dArray(window_range)
t = (window_rangeT*2*pi*f0range)/fs
E = cos(t) + 1j* sin(t)
E = concatenate((E, tile(window_rangeT, (1, K))*E), axis=1)
Ewindow = multiply(tile(windowT, (1, 2*K)), E)
EwindowT = conjugate(transpose(Ewindow))
R = dot(EwindowT, Ewindow)
#assert(cond(R) < 10**(10)),'CAUTION!!! Bad condition of matrix.'
windowSignal = multiply(windowT, s)
arr = dot(EwindowT, windowSignal)
ampsl = dot(inv(R), arr)
amplitudes = ampsl[0:K]
slopes = ampsl[K:2*K+1]
return amplitudes, slopes
def eaqhmLS_complexamps(s, am, fm, window, fs):
'''
Computes the parameters of first order complex polynomial
model using Least Squares, a AM and a FM model for the frequency
Parameters
----------
s : array_like
The part of the signal to be computed.
am : array_like
The estimated instantaneous amplitudes.
fm : array_like
The estimated instantaneous frequencies.
window : array_like
The window of the signal to be computed.
fs : int
The sampling frequency.
Returns
-------
amplitudes : array_like
Amplitude of harmonics.
slopes : array_like
Slope of harmonics.
'''
windowT = transpose1dArray(window)
length = len(fm)
K = len(fm[0])
midlen = int((length-1)/2)
window_range = arange(-midlen,midlen+1)
window_rangeT = transpose1dArray(window_range)
f_an = zeros((K, length), float)
for k in range(K):
f_an[k] = lfilter([1], [1, -1], fm[:, k])
f_an[k] -= f_an[k][midlen]
t = (2*pi*f_an)/fs
tT = transpose(t)
E1 = cos(tT) + 1j* sin(tT)
eps = 10e-5
E2 = multiply(divide(eps+am, tile(am[midlen], (2*midlen+1, 1))+eps), E1)
E = concatenate((E2, tile(window_rangeT, (1, K))*E2), axis=1)
Ewindow = multiply(E, tile(windowT, (1, 2*K)))
EwindowT = conjugate(transpose(Ewindow))
R = dot(EwindowT, Ewindow)
#assert(cond(R) < 10**(15)),'CAUTION!!! Bad condition of matrix.'
windowSignal = multiply(windowT, s)
arr = dot(EwindowT, windowSignal)
ampsl = dot(inv(R), arr)
amplitudes = ampsl[0:K]
slopes = ampsl[K:2*K+1]
return amplitudes, slopes
def phase_integr_interpolation(fm_recon, ph_recon, indices):
'''
Computes phase interpolation using integration of instantaneous frequency.
Parameters
----------
fm_recon : array_like
The instantaneous frequencies.
ph_recon : array_like
The instantaneous phases.
indices : array_like
The indices to be interpolated.
Returns
-------
pm_final : array_like
A simplified array-like object.
'''
length = len(fm_recon)
pm_final = zeros(length, float)
for i in range(len(indices)-1):
pm_inst = lfilter([1], [1, -1], fm_recon[indices[i]:indices[i+1]+1])
pm_inst += tile(ph_recon[indices[i]]-pm_inst[0], len(pm_inst))
M = round((end(pm_inst) - ph_recon[indices[i+1]])/(2*pi))
er = pi*(end(pm_inst)-ph_recon[indices[i+1]]-2*pi*M)/(2*(indices[i+1]-indices[i]))
t = arange(0, indices[i+1]-indices[i]+1)
ft = sin(pi*t/(indices[i+1]-indices[i]))
ftT = transpose(ft)
pm_inst -= lfilter([1], [1, -1], ftT*er)
pm_final[indices[i]:indices[i+1]+1] = pm_inst
pm_final = pm_final[indices[0]:end(indices)+1]
return pm_final
def voicedUnvoicedFrames(s, fs: int, gender: str):
'''
Estimation of speech/nonspeech and voiced/unvoiced frames.
Parameters
----------
s : array_like
The signal to be estimated.
fs : int
The sampling frequency.
gender : str
The gender of the speaker.
Returns
-------
frames : array_like
An array of structures containing the time instants, if they are voiced and if they are speech.
frame_step : int
The step of the frames.
'''
s = ellipFilter(transpose(s)[0], fs, 30)
length = len(s)
s_speechNonspeech_thres = -60
v_voicedUnvoiced_thres = 10
s_smoothedSpeech_thres = -50
if gender == "male":
s_smooth = ellipFilter(s, fs, 1000, 'lowpass')
else:
s_smooth = ellipFilter(s, fs, 1500, 'lowpass')
windowLen = int(round(0.03*fs))
if windowLen % 2 == 0:
windowLen += 1
step = int(round(0.005*fs))
midlen = (windowLen-1)/2
window_range = arange(-midlen-1, midlen, dtype=int)
ti = arange(1,length,step)
No_ti = len(ti)
isSpeech = zeros(No_ti, dtype=bool)
isVoiced = zeros(No_ti, dtype=bool)
for i, tith in enumerate(ti):
if tith > midlen and tith < length-midlen:
spEn = 20*log10(std(s[tith + window_range]))
spEn_smooth = 20*log10(std(s_smooth[tith + window_range]))
isSpeech[i] = (spEn > s_speechNonspeech_thres).any()
if isSpeech[i]:
isVoiced[i] = (spEn-spEn_smooth < v_voicedUnvoiced_thres).any() and (spEn_smooth > s_smoothedSpeech_thres).any()
isSpeech = medfilt(isSpeech, 5)
isVoiced = medfilt(isVoiced, 5)
frames = []
for i, tith in enumerate(ti):
frames.append(Frame(tith, isSpeech[i], isVoiced[i]))
return frames, frames[1].ti-frames[0].ti
def getLinear(v, t):
'''
Linearly interpolates a time-data array
'''
if isContainer(t):
value = ones((len(t),len(v[0])))
for ind in range(len(t)):
value[ind][0] = t[ind]
value[ind][1:] = getLinear(v, t[ind])
elif isinstance(t, float):
times = v[:, 0]
previ = end(argwhere(times<=t))
if isEmpty(previ):
value = v[0, 1:]
previ = 0
nexti = 0
g = 1
elif previ == len(v):
value = v[len(v)-1, 1:]
nexti = previ
g = 0
elif times[previ] == t:
value = v[previ, 1:]
nexti = previ
g = 0
else:
nexti=previ+1
g = (t-times[previ])/(times[nexti]-times[previ])
if g<0 or g>1:
raise ValueError('linearity factor unbound, g not in [0, 1]')
value = v[previ, 1:]*(1-g) + v[nexti, 1:]*g;
return value