Mercurial > hg > nsaunier > traffic-intelligence
diff trafficintelligence/utils.py @ 1250:77fbd0e2ba7d
dltrack works with moving average filtering
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Thu, 15 Feb 2024 22:04:35 -0500 |
| parents | 69b531c7a061 |
| children | ad60e5adf084 |
line wrap: on
line diff
--- a/trafficintelligence/utils.py Thu Feb 15 14:09:52 2024 -0500 +++ b/trafficintelligence/utils.py Thu Feb 15 22:04:35 2024 -0500 @@ -11,7 +11,7 @@ from scipy.stats import rv_continuous, kruskal, shapiro, lognorm, norm, t, chi2_contingency from scipy.spatial import distance from scipy.sparse import dok_matrix -from numpy import zeros, array, exp, sum as npsum, int64 as npint, arange, cumsum, mean, median, percentile, isnan, ones, convolve, dtype, isnan, NaN, ma, isinf, savez, load as npload, log, polyfit +from numpy import zeros, array, exp, sum as npsum, int64 as npint, arange, cumsum, mean, median, percentile, isnan, ones, convolve, dtype, isnan, NaN, ma, isinf, savez, load as npload, log, polyfit, float64 from numpy.random import random_sample, permutation as nppermutation from pandas import DataFrame, concat, crosstab import matplotlib.pyplot as plt @@ -431,13 +431,18 @@ smoothed[point] = max(set(window_values), key=window_values.count) return smoothed -def filterMovingWindow(inputSignal, halfWidth, mode = 'valid'): +def filterMovingWindow(inputSignal, halfWidth): '''Returns an array obtained after the smoothing of the 1-D input by a moving average The size of the output depends on the mode: 'full', 'same', 'valid' See https://numpy.org/doc/stable/reference/generated/numpy.convolve.html.''' - width = min(len(inputSignal), int(halfWidth*2+1)) - win = ones(width,'d') - return convolve(win/width, array(inputSignal), mode) + halfWidth = min(floor((len(inputSignal)-1)/2.), halfWidth) + win = ones(2*halfWidth+1)/(2*halfWidth+1) + filtered = array(inputSignal, dtype=float64) + filtered[halfWidth:-halfWidth] = convolve(inputSignal, win, 'valid') # .ravel() + for i in range(halfWidth-1): + filtered[i] = sum(inputSignal[:2*i+1])/(2*i+1) + filtered[-1-i] = sum(inputSignal[-1-2*i:])/(2*i+1) + return filtered def linearRegression(x, y, deg = 1, plotData = False): '''returns the least square estimation of the linear regression of y = ax+b
