Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions spafe/utils/preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from typing import Tuple

import math
import numpy as np
from scipy.signal import lfilter
from dataclasses import dataclass
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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


Expand Down