Actually it's not quite as simple as that... If I take the average of two samples of an exponentially changing voltage it will not correspond to the voltage halfway in time between the two samples. It would if the voltage was changing in a linear fashion, but not if it is changing non-linearly, which of course it is. Seems like a chunk of calculus would be involved. I'll have to think of an approximation instead. Maybe count the time between the rising and falling edge and use that figure to index a lookup table to supply a fudge factor to the reading.
True, but for your one part in 256, and a good enough filter, I'd expect the error will not be large.
If the triangle wave is 500mV ~ 1V, it will be 'close enough' to triangle, for the interpolate to work.
Addit: for fun, I checked that in Spice. The two-peaks average checks confirms it is well below 8 bits LSB
5V of PWM 100Hz 50% into 47k 1uF filter is 265mV p-p ripple
Error from 50% is 333uV or ~14 bits
Error from 20% is 4.4mV or ~10 bits
Error from 5% is 1.3mV or ~12 bits
An alternative approach, for fun, using more hardware and less software, is to use a good prescaler to lock the timer to ~ 100% of the Period, using a DLL.
Then, the capture difference is already in %/256.
eg if we take 8MHz and 120Hz a prescaler that divides by 261 then by 256, results in 119.4Hz.
Small MCUs tends to not have granular prescalers, so a second timer and pin-link may be needed.
This approach works if the frequency does not change too rapidly. as the tracking lock is not fast.