An update:
After countless hours of looking for the cause of the inconsistent encoder reading, the reason has been found and eliminated. The reason was a slipping pulley!
Thanks to everyone for helping!
Seems you were right Dave for suspecting a mechanical issue.