comparison 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
comparison
equal deleted inserted replaced
1199:6a6a4d5958f7 1200:4356065ed3ca
429 upper_bound_check = min(len(cat_list)-1,point+halfWidth+1) 429 upper_bound_check = min(len(cat_list)-1,point+halfWidth+1)
430 window_values = cat_list[lower_bound_check:upper_bound_check] 430 window_values = cat_list[lower_bound_check:upper_bound_check]
431 smoothed[point] = max(set(window_values), key=window_values.count) 431 smoothed[point] = max(set(window_values), key=window_values.count)
432 return smoothed 432 return smoothed
433 433
434 def filterMovingWindow(inputSignal, halfWidth): 434 def filterMovingWindow(inputSignal, halfWidth, mode = 'valid'):
435 '''Returns an array obtained after the smoothing of the input by a moving average 435 '''Returns an array obtained after the smoothing of the input by a moving average
436 The first and last points are copied from the original.''' 436 The size of the output depends on the mode: 'full', 'same', 'valid'
437 width = float(halfWidth*2+1) 437 See https://numpy.org/doc/stable/reference/generated/numpy.convolve.html.'''
438 width = min(len(inputSignal), int(halfWidth*2+1))
438 win = ones(width,'d') 439 win = ones(width,'d')
439 result = convolve(win/width,array(inputSignal),'same') 440 return convolve(win/width, array(inputSignal), mode)
440 result[:halfWidth] = inputSignal[:halfWidth]
441 result[-halfWidth:] = inputSignal[-halfWidth:]
442 return result
443 441
444 def linearRegression(x, y, deg = 1, plotData = False): 442 def linearRegression(x, y, deg = 1, plotData = False):
445 '''returns the least square estimation of the linear regression of y = ax+b 443 '''returns the least square estimation of the linear regression of y = ax+b
446 as well as the plot''' 444 as well as the plot'''
447 coef = polyfit(x, y, deg) 445 coef = polyfit(x, y, deg)