inspired by: http://stackoverflow.com/questions/12041519/how-to-get-the-mean-of-a-specific-span-of-time