feat: Audio Comparison - Signal Framing Completed

parent 15fe6897
......@@ -51,99 +51,61 @@ np.allclose(y_filt, np.concatenate([y_filt_1, y_filt_2]))
# Framing and windowing of voice signals
# In[21]:
# In[6]:
def framing(sig, fs=16000, win_len=0.025, win_hop=0.01):
"""
transform a signal into a series of overlapping frames.
Args:
sig (array) : a mono audio signal (Nx1) from which to compute features.
fs (int) : the sampling frequency of the signal we are working with.
Default is 16000.
win_len (float) : window length in sec.
Default is 0.025.
win_hop (float) : step between successive windows in sec.
Default is 0.01.
Returns:
array of frames.
frame length.
"""
#
# # pre-emphasis
# if pre_emph:
# sig = pre_emphasis(sig=sig, pre_emph_coeff=0.97)
#
# # -> framing
# frames, frame_length = framing(sig=sig,
# fs=fs,
# win_len=win_len,
# win_hop=win_hop)
#
# # -> windowing
# windows = windowing(frames=frames,
# frame_len=frame_length,
# win_type=win_type)
#
# In[25]:
# compute frame length and frame step (convert from seconds to samples)
import numpy as np
frame_length = win_len * fs
frame_step = win_hop * fs
signal_length = len(sig)
frames_overlap = frame_length - frame_step
def framing(sig, fs=16000, win_len=0.025, win_hop=0.01):
"""
transform a signal into a series of overlapping frames.
Args:
sig (array) : a mono audio signal (Nx1) from which to compute features.
fs (int) : the sampling frequency of the signal we are working with.
Default is 16000.
win_len (float) : window length in sec.
Default is 0.025.
win_hop (float) : step between successive windows in sec.
Default is 0.01.
Returns:
array of frames.
frame length.
"""
# compute frame length and frame step (convert from seconds to samples)
frame_length = win_len * fs
frame_step = win_hop * fs
signal_length = len(sig)
frames_overlap = frame_length - frame_step
# Make sure that we have at least 1 frame+
num_frames = np.abs(signal_length - frames_overlap) // np.abs(frame_length - frames_overlap)
rest_samples = np.abs(signal_length - frames_overlap) % np.abs(frame_length - frames_overlap)
# Pad Signal to make sure that all frames have equal number of samples
# without truncating any samples from the original signal
if rest_samples != 0:
pad_signal_length = int(frame_step - rest_samples)
z = np.zeros((pad_signal_length))
pad_signal = np.append(sig, z)
num_frames += 1
else:
pad_signal = sig
# make sure to use integers as indices
frame_length = int(frame_length)
frame_step = int(frame_step)
num_frames = int(num_frames)
# compute indices
idx1 = np.tile(np.arange(0, frame_length), (num_frames, 1))
idx2 = np.tile(np.arange(0, num_frames * frame_step, frame_step),
(frame_length, 1)).T
indices = idx1 + idx2
frames = pad_signal[indices.astype(np.int32, copy=False)]
return frames
# In[ ]:
# In[ ]:
num_frames = np.abs(signal_length - frames_overlap) // np.abs(frame_length - frames_overlap)
rest_samples = np.abs(signal_length - frames_overlap) % np.abs(frame_length - frames_overlap)
# Pad Signal to make sure that all frames have equal number of samples
# without truncating any samples from the original signal
if rest_samples != 0:
pad_signal_length = int(frame_step - rest_samples)
z = np.zeros((pad_signal_length))
pad_signal = np.append(sig, z)
num_frames += 1
else:
pad_signal = sig
# make sure to use integers as indices
frame_length = int(frame_length)
frame_step = int(frame_step)
num_frames = int(num_frames)
# compute indices
idx1 = np.tile(np.arange(0, frame_length), (num_frames, 1))
idx2 = np.tile(np.arange(0, num_frames * frame_step, frame_step),
(frame_length, 1)).T
indices = idx1 + idx2
frames = pad_signal[indices.astype(np.int32, copy=False)]
return frames
# In[ ]:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment