Skip to content

Commit 2292e1f

Browse files
authored
Update processing.py
1 parent a1955fc commit 2292e1f

1 file changed

Lines changed: 22 additions & 18 deletions

File tree

speechpy/processing.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import decimal
2-
import numpy
2+
import numpy as np
33
import math
44

55
# 1.4 becomes 1 and 1.6 becomes 2. special case: 1.5 becomes 2.
@@ -23,8 +23,8 @@ def Stack_Frames(sig, sampling_frequency, frame_length=0.020, frame_stride=0.020
2323

2424
# Initial necessary values
2525
length_signal = len(sig)
26-
frame_sample_length = int(numpy.round(sampling_frequency * frame_length)) # Defined by the number of samples
27-
frame_stride = int(numpy.round(sampling_frequency * frame_stride))
26+
frame_sample_length = int(np.round(sampling_frequency * frame_length)) # Defined by the number of samples
27+
frame_stride = int(np.round(sampling_frequency * frame_stride))
2828

2929
# Check the feasibility of stacking
3030
if length_signal <= frame_sample_length:
@@ -37,8 +37,8 @@ def Stack_Frames(sig, sampling_frequency, frame_length=0.020, frame_stride=0.020
3737

3838
# Zero padding
3939
len_sig = int((numframes - 1) * frame_stride + frame_sample_length)
40-
additive_zeros = numpy.zeros((length_signal-len_sig,))
41-
signal = numpy.concatenate((sig, additive_zeros))
40+
additive_zeros = np.zeros((length_signal-len_sig,))
41+
signal = np.concatenate((sig, additive_zeros))
4242

4343
else:
4444
# No zero padding! The last frame which does not have enough
@@ -50,15 +50,16 @@ def Stack_Frames(sig, sampling_frequency, frame_length=0.020, frame_stride=0.020
5050
signal = sig[0:len_sig]
5151

5252
# Getting the indices of all frames.
53-
indices = numpy.tile(numpy.arange(0, frame_sample_length), (numframes, 1)) + numpy.tile(
54-
numpy.arange(0, numframes * frame_stride, frame_stride), (frame_sample_length, 1)).T
55-
indices = numpy.array(indices, dtype=numpy.int32)
53+
indices = np.tile(np.arange(0, frame_sample_length), (numframes, 1)) + np.tile(
54+
np.arange(0, numframes * frame_stride, frame_stride), (frame_sample_length, 1)).T
55+
indices = np.array(indices, dtype=np.int32)
5656

5757
# Extracting the frames based on the allocated indices.
5858
frames = signal[indices]
5959

6060
# Apply the windows function
61-
Extracted_Frames = frames * numpy.tile(Filter(frame_sample_length), (numframes, 1))
61+
window = np.tile(Filter(frame_sample_length), (numframes, 1))
62+
Extracted_Frames = frames * window
6263
return Extracted_Frames
6364

6465

@@ -72,8 +73,8 @@ def fft_spectrum(frames, fft_length=512):
7273
:param num_keep_coefficients: The number of coefficients that is kept.
7374
:returns: If frames is an num_frames x sample_per_frame matrix, output will be num_frames x FFT_LENGTH.
7475
"""
75-
SPECTRUM_VECTOR = numpy.fft.rfft(frames, n=fft_length, axis=-1, norm=None)
76-
return numpy.absolute(SPECTRUM_VECTOR)
76+
SPECTRUM_VECTOR = np.fft.rfft(frames, n=fft_length, axis=-1, norm=None)
77+
return np.absolute(SPECTRUM_VECTOR)
7778

7879

7980
def power_spectrum(frames, fft_length=512):
@@ -83,7 +84,7 @@ def power_spectrum(frames, fft_length=512):
8384
:param fft_length: The length of FFT. If fft_length is greater than frame_len, the frames will be zero-padded.
8485
:returns: If frames is an num_frames x sample_per_frame matrix, output will be num_frames x fft_length.
8586
"""
86-
return 1.0 / fft_length * numpy.square(fft_spectrum(frames, fft_length))
87+
return 1.0 / fft_length * np.square(fft_spectrum(frames, fft_length))
8788

8889

8990
def log_power_spectrum(frames, fft_length=512, normalize=True):
@@ -96,9 +97,9 @@ def log_power_spectrum(frames, fft_length=512, normalize=True):
9697
"""
9798
power_spec = power_spectrum(frames, fft_length)
9899
power_spec[power_spec <= 1e-20] = 1e-20
99-
log_power_spec = 10 * numpy.log10(power_spec)
100+
log_power_spec = 10 * np.log10(power_spec)
100101
if normalize:
101-
return log_power_spec - numpy.max(log_power_spec)
102+
return log_power_spec - np.max(log_power_spec)
102103
else:
103104
return log_power_spec
104105

@@ -114,11 +115,11 @@ def Derivative_Feature_Fn(feat,DeltaWindows):
114115
rows, cols = feat.shape
115116

116117
# Difining the vector of differences.
117-
DIF = numpy.zeros(feat.shape, dtype=float)
118+
DIF = np.zeros(feat.shape, dtype=float)
118119
Scale = 0
119120

120121
# Pad only along features in the vector.
121-
FEAT = numpy.lib.pad(feat, ((0, 0), (DeltaWindows, DeltaWindows)), 'edge')
122+
FEAT = np.lib.pad(feat, ((0, 0), (DeltaWindows, DeltaWindows)), 'edge')
122123
for i in range(DeltaWindows):
123124

124125
# Start index
@@ -128,7 +129,7 @@ def Derivative_Feature_Fn(feat,DeltaWindows):
128129
Range = i + 1
129130

130131
dif = Range * FEAT[:,offset+Range:offset+Range+cols] - FEAT[:,offset-Range:offset-Range+cols]
131-
Scale += 2 * numpy.power(Range,2)
132+
Scale += 2 * np.power(Range,2)
132133
DIF += dif
133134

134135
return DIF/Scale
@@ -149,7 +150,7 @@ def Derivative_Feature_Fn(feat,DeltaWindows):
149150
# signal_new = resample(wave, float(f_new) / fs, 'sinc_best')
150151
#
151152
# # Necessary data converting for saving .wav file using scipy.
152-
# signal_new = numpy.asarray(signal_new, dtype=numpy.int16)
153+
# signal_new = np.asarray(signal_new, dtype=np.int16)
153154
#
154155
# # # Uncomment if you want to save the audio file
155156
# # # Save using new format
@@ -159,3 +160,6 @@ def Derivative_Feature_Fn(feat,DeltaWindows):
159160

160161

161162

163+
164+
165+

0 commit comments

Comments
 (0)