Made the pitch detection quite ruthlessly discard values before averaging, still not optimal, determining the best subset of the data is an NP hard problem, not satisfied with current method, might be easier (and simpler) to just take the median as it is robust to outliers and the accuracy of the current approach really depends on the threshold value.