Very nice. Same approach as mine but much simpler. I will rewrite my code.
The thing to be careful about is that, most often, when generating an “artificial” signal, we generate full cycles. And any of the approaches above work well. But when using the output of an instrument, the signal can begin or end at any point of the cycle. In this case using the fft might result in a bias. Of course, if the number of pulses is very large, this is minimized.
On the other hand, if the number of pulses (or cycles in general) is small counting them directly as your approach does is the best approach and interpolation can also help a lot.
Paulo