Factor out rxp moving average to a free function

This makes it easier to test.