# Tool request: Extract duty cycle

I was thinking it would be neat to extract single duty cycles from samples. Say for example:

1. Sample an organ from your old casio keyboard

2. Select sample and click “Extract single duty”

3. A looped short waveform is extracted

At first, my idea was to use some data mining to find repeating patterns, and select the iteration that is closest to the ‘avarage’.

Then again, a more simple and precise approach would probably be to use FFT. Find the avarage amplitude of all harmonics in the full sample, and generate a single duty cycle from that. Then you could even simplify a modulating waveform into a relevant single duty cycle, and simple sounds should probably provide perfect extractions as well.

However, I’m not math genious. Anyone keen on doing, or helping out?

EDIT:

Got a suggestion from Saga_Musix it should be a better idea to just find the fundamental frequency and then mix all these cycles of the sample. This shouldn’t be too difficult to script?

Nice idea.

I can’t help, I don’t have the energy for such stuff at the moment and am no coding guru anyways. But I want to bring you onto a certain track:

You will want to research “autocorrelation” Techniques for the purpose of finding repeating patterns and their base frequencies. The FFT is then useful for further nailing down buisiness. Tuning the FFT to a repeating pattern (oscillations), by adjusting the fft size and/or resampling the wavecycle data, instead of just running at data of unknown pitch with a fixed resolution FFT, will improve the usefulness of the results by lightyears. Each FFT bin will actually be an exact harmonic, not some smeared out representation of data somewhere in between.

Edit addition: also, for averaging or interpolating single cycles, the tuned FFT can prove very useful also. But it needs to be tuned to the correct frequency. You can transform the ffts to frequency power/phase datasets for the harmonics (which will be the fft bins when the fft is tuned), then average/interpolate/process in that domain, transform back, and IFFT, warp around to a zero crossing will be start/end of the cycle, and resample to get the result single cycle loop.

One of my tools might provide a good foundation for this…

As long as you know roughly the cycle length, and the fundamental frequency of a sample doesn’t change over time, ZeroCrossings can do a pretty good job at extracting those cycles:

https://forum.renoise.com/t/new-tool-3-0-zerocrossings/43501

The trick is to head into the sample editor, identify a single cycle and then reduce the selection by a few samples. Next, click “Set from Selection” and hit process.

Reducing the size is necessary since cycles will not always have the exact same length in samples - setting it to a slightly smaller value will give the tool a bit of “wiggle room”.

So no, it’s not fully automated but it can extract from even the most complex, atonal-ish types of sound.

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

afta8,

Interesting. Would you say that your routine for finding the fundamental frequency is reliable? (e g almost always working for tonal sounds, if they’re DC normalized). It should then be a breeze to just mix all the “windows” together. However, I’m guessing the whole process will break once you have a tad of vibrato in the source… This seems complicated to work around.

1. Detect the fundamental (avarage) frequency

2. Stretch all cycles + or - to remove the vibrato. This is probably very complicated and requires detection of vibrato phase, and praying that the vibrato is constant.

3. Mix all windows.

Maybe the vibrato isn’t an issue, but would magically/almost “fix itself” if you just mix all windows?

PS. It’s also a bit tempting to roughly recreate the volume envelope from the source. I’m guessing that can be done with some simple convolution algorithm. Possibly, this could be used to create rather refined chip instruments, that still sounds old school enough.

Further down the road… it could use Renoise instruments (sample fx chains) for vector synthesis, doing things like “simplify this piano sound into 4 crossfading chip samples”. It wouldn’t be terribly difficult to achieve.