Alhtough I didn’t get anywhere perfect in the end, there’s some useful discussion/thinking in this older thread:
https://discourse.flucoma.org/t/weighted-pitch-analysis/
(some of the patches may not work anymore since the syntax has changed a few times since then)