feat: Audio Comparison - Signal Framing Completed

parent 15fe6897
...@@ -51,11 +51,13 @@ np.allclose(y_filt, np.concatenate([y_filt_1, y_filt_2])) ...@@ -51,11 +51,13 @@ np.allclose(y_filt, np.concatenate([y_filt_1, y_filt_2]))
# Framing and windowing of voice signals # Framing and windowing of voice signals
# In[21]: # In[6]:
import numpy as np
def framing(sig, fs=16000, win_len=0.025, win_hop=0.01): def framing(sig, fs=16000, win_len=0.025, win_hop=0.01):
""" """
transform a signal into a series of overlapping frames. transform a signal into a series of overlapping frames.
...@@ -72,78 +74,38 @@ np.allclose(y_filt, np.concatenate([y_filt_1, y_filt_2])) ...@@ -72,78 +74,38 @@ np.allclose(y_filt, np.concatenate([y_filt_1, y_filt_2]))
array of frames. array of frames.
frame length. frame length.
""" """
# compute frame length and frame step (convert from seconds to samples)
frame_length = win_len * fs
# frame_step = win_hop * fs
# # pre-emphasis signal_length = len(sig)
# if pre_emph: frames_overlap = frame_length - frame_step
# 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)
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+ # Make sure that we have at least 1 frame+
num_frames = np.abs(signal_length - frames_overlap) // np.abs(frame_length - frames_overlap)
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)
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 # Pad Signal to make sure that all frames have equal number of samples
# without truncating any samples from the original signal # without truncating any samples from the original signal
if rest_samples != 0:
if rest_samples != 0:
pad_signal_length = int(frame_step - rest_samples) pad_signal_length = int(frame_step - rest_samples)
z = np.zeros((pad_signal_length)) z = np.zeros((pad_signal_length))
pad_signal = np.append(sig, z) pad_signal = np.append(sig, z)
num_frames += 1 num_frames += 1
else: else:
pad_signal = sig pad_signal = sig
# make sure to use integers as indices # make sure to use integers as indices
frame_length = int(frame_length)
frame_length = int(frame_length) frame_step = int(frame_step)
frame_step = int(frame_step) num_frames = int(num_frames)
num_frames = int(num_frames)
# compute indices # compute indices
idx1 = np.tile(np.arange(0, frame_length), (num_frames, 1))
idx1 = np.tile(np.arange(0, frame_length), (num_frames, 1)) idx2 = np.tile(np.arange(0, num_frames * frame_step, frame_step),
idx2 = np.tile(np.arange(0, num_frames * frame_step, frame_step),
(frame_length, 1)).T (frame_length, 1)).T
indices = idx1 + idx2 indices = idx1 + idx2
frames = pad_signal[indices.astype(np.int32, copy=False)] frames = pad_signal[indices.astype(np.int32, copy=False)]
return frames return frames
# In[ ]:
# In[ ]:
# In[ ]: # 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