If you go with finding fundamental frequency then my EasyTune tool might have some useful code, it counts all the zero crossing and guesses at a ‘best’ fundamental frequency for the whole sample. You could then use that to find wave cycles that match or are close…
If you want to pursue the FFT route then this might be useful:https://github.com/h4rm/luafft
Im quite interested in that actually, never quite got my head around FFT at the time… artfwo shared some code on this here:https://forum.renoise.com/t/lua-fft/38211