Modifications made to the Matlab script to include averaging