diff --git a/spafe/utils/preprocessing.py b/spafe/utils/preprocessing.py index eef8d6a..2ee6297 100644 --- a/spafe/utils/preprocessing.py +++ b/spafe/utils/preprocessing.py @@ -9,6 +9,7 @@ from typing import Tuple +import math import numpy as np from scipy.signal import lfilter from dataclasses import dataclass @@ -89,7 +90,15 @@ def stride_trick(a: np.ndarray, stride_length: int, stride_step: int) -> np.ndar https://numpy.org/doc/stable/reference/generated/numpy.lib.stride_tricks.sliding_window_view.html """ a = np.array(a) - nrows = ((a.size - stride_length) // stride_step) + 1 + + # Compute to number of rows and add the missing zeros to complete de last frames before stride tricks + float_nrows = (a.size - stride_length + stride_step) / stride_step + nrows = math.ceil(float_nrows) + + if nrows - float_nrows > 0: + num_zero_to_add = stride_length - a.size + math.floor(float_nrows) * stride_step + a = np.append(a, np.array([0] * num_zero_to_add)) + n = a.strides[0] return np.lib.stride_tricks.as_strided( a, shape=(nrows, stride_length), strides=(stride_step * n, n) @@ -135,11 +144,6 @@ def framing( # make sure to use integers as indices frames = stride_trick(sig, frame_length, frame_step) - if len(frames[-1]) < frame_length: - frames[-1] = np.append( - frames[-1], np.array([0] * (frame_length - len(frames[0]))) - ) - return frames, frame_length