Mercurial Hosting > traffic-intelligence
diff trafficintelligence/utils.py @ 1200:4356065ed3ca
updated simple moving average filter and cleaned tests
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Thu, 08 Dec 2022 10:24:22 -0500 |
parents | d71a4d174b1a |
children | 69b531c7a061 |
line wrap: on
line diff
--- a/trafficintelligence/utils.py Fri Nov 18 15:55:09 2022 -0500 +++ b/trafficintelligence/utils.py Thu Dec 08 10:24:22 2022 -0500 @@ -431,15 +431,13 @@ smoothed[point] = max(set(window_values), key=window_values.count) return smoothed -def filterMovingWindow(inputSignal, halfWidth): +def filterMovingWindow(inputSignal, halfWidth, mode = 'valid'): '''Returns an array obtained after the smoothing of the input by a moving average - The first and last points are copied from the original.''' - width = float(halfWidth*2+1) + 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') - result = convolve(win/width,array(inputSignal),'same') - result[:halfWidth] = inputSignal[:halfWidth] - result[-halfWidth:] = inputSignal[-halfWidth:] - return result + return convolve(win/width, array(inputSignal), mode) def linearRegression(x, y, deg = 1, plotData = False): '''returns the least square estimation of the linear regression of y = ax+b