Does Pytorch have an equivalent implementation to Tensorflow’s tf.signal.frame | TensorFlow Core v2.5.0 ? I’ve been searching everywhere and cannot seem to find anything, even in torchaudio. I know that librosa has an equivalent function, but it requires that the inputs be converted to numpy arrays first
Based on the description I guess that tensor.unfold
would perform the same operation (with an additional F.pad
before if needed).
You are correct, thank you! For completeness, this method appears to produce the same results as tf.signal.frame (albeit not tested extensively)
def frame(signal, frame_length, frame_step, pad_end=False, pad_value=0, axis=-1):
"""
equivalent of tf.signal.frame
"""
signal_length = signal.shape[axis]
if pad_end:
frames_overlap = frame_length - frame_step
rest_samples = np.abs(signal_length - frames_overlap) % np.abs(frame_length - frames_overlap)
pad_size = int(frame_length - rest_samples)
if pad_size != 0:
pad_axis = [0] * signal.ndim
pad_axis[axis] = pad_size
signal = F.pad(signal, pad_axis, "constant", pad_value)
frames=signal.unfold(axis, frame_length, frame_step)
return frames
Reference:
https://superkogito.github.io/blog/SignalFraming.html
1 Like
This one may be correct.
def frame(signal, frame_length, frame_step, pad_end=False, pad_value=0, axis=-1):
"""
equivalent of tf.signal.frame
"""
pad_size = 0
signal_length = signal.shape[axis]
if pad_end:
frames_overlap = frame_length - frame_step
rest_samples = np.abs(signal_length - frames_overlap) % np.abs(frame_step)
if rest_samples != 0:
pad_size = int(frame_length - rest_samples)
pad_axis = [0] * signal.ndim
pad_axis[axis] = pad_size
signal = F.pad(signal, pad_axis, "constant", pad_value)
frames=signal.unfold(axis, frame_length, frame_step)
return frames