I want to create a function identical to matlabs movmean function, whereby a sliding window moves through each datapoint in a list/array, and creates a new datapoint based on the average of its neighbors (centered on the current datapoint). I have created an illustration to demonstrate what I want the function to do here. Basically: the function creates the average of the points in the window - and shrinks near the endpoints to only include existing elements (i.e., the "shrink" option in matlabs function). In the illustration, the thicker middle bar shows the current index; and it takes the average of the current and last index of the window size is even.
I have tried to get this to work all day, but I don't seem to have the mental capacity to figure it out. I am happy to attach some code of what I have tried if you want.
EDIT: I want to clarify that, for example, np.convolve, which has been suggested as a method for moving mean, wont work in this case, since this method creates a moving average by taking the average of the current index value + the next two index values, if your window is size 3. In the function I want, I want to create the average of the index value before, the current index value and the upcoming index value, if the window is size 3.
I should also mention that I cannot look at the source code of the movmean in matlab - try yourself 'edit movmean' in matlab. You can just see the comment text, and not the source code.