39 lines
1.1 KiB
Python
39 lines
1.1 KiB
Python
import numpy as np
|
|
from converted_code.smartstd import smartstd
|
|
|
|
def smartMovingStd(x, T, period=None):
|
|
"""
|
|
Python implementation of the MATLAB smartMovingStd function.
|
|
Calculates the moving standard deviation over a rolling window,
|
|
ignoring NaN and Inf values.
|
|
|
|
Parameters:
|
|
x (ndarray): Input array
|
|
T (int): Window size
|
|
period (int, optional): If provided, data is sampled every 'period'
|
|
|
|
Returns:
|
|
ndarray: Moving standard deviation with NaNs at the beginning
|
|
"""
|
|
if isinstance(x, list):
|
|
x = np.array(x)
|
|
|
|
# Get dimensions
|
|
if len(x.shape) == 1:
|
|
x = x.reshape(-1, 1)
|
|
|
|
rows, cols = x.shape
|
|
|
|
# Initialize output array with NaNs
|
|
sd = np.full_like(x, np.nan)
|
|
|
|
if period is None:
|
|
# Regular moving standard deviation
|
|
for t in range(T, rows + 1):
|
|
sd[t-1, :] = smartstd(x[t-T:t, :])
|
|
else:
|
|
# Moving standard deviation with period
|
|
for t in range(T*period, rows + 1):
|
|
sd[t-1, :] = smartstd(x[t-T*period:t, :])
|
|
|
|
return sd |