audio icon indicating copy to clipboard operation
audio copied to clipboard

Resampling at arbitrary time steps

Open pfeatherstone opened this issue 1 year ago • 5 comments

🚀 The feature

Currently, torchaudio.functional.resample can only resample at regular time points and the period is determined by orig_freq and new_freq.

Is it possible to resample at arbitrary time steps? So rather than specifying a resampling ratio, we specify a array of time steps.

Motivation, pitch

I would like to be able to model jitter in an ADC which can be modelled by a slightly varying sample rate. If you integrate a sample rate curve (which isn't constant), you get irregular time steps. A function such as the one suggested above would allow me to resample using these time steps and model a jittery ADC.

Alternatives

I've rolled out my own function but it's not super efficient. Some experts might do a better job.

Additional context

No response

pfeatherstone avatar Jan 12 '24 09:01 pfeatherstone

Does your jitter effect change the sample rate periodically? If so, I think it's possible to reuse part of the resampling algorithm we have. resample finds the lcm of orig_freq and new_freq to pre-compute the since filters and apply them periodically. You can apply the same logic to get the needed since filters between the orig_freq and your modulation frequency (let's name it mod_freq).

If you want something that's more general, like np.interp, unfortunately, we don't have a similar function so far.

yoyolicoris avatar Jan 13 '24 03:01 yoyolicoris

Does your jitter effect change the sample rate periodically? If so, I think it's possible to reuse part of the resampling algorithm we have. resample finds the lcm of orig_freq and new_freq to pre-compute the since filters and apply them periodically. You can apply the same logic to get the needed since filters between the orig_freq and your modulation frequency (let's name it mod_freq).

If you want something that's more general, like np.interp, unfortunately, we don't have a similar function so far.

The jitter is gaussian random and so the sample rate is slightly different at every sample. You can model jitter (I think) as a sample rate that's undergoing a very small gaussian random walk between a minimum and upper bound. You can then calculate the time points by integrating the sample rate over time. Then I need to resample using those time points. So yes, something like np.interp but using a user defined filter, such as kaiser - sinc filter would be appropriate. Currently i'm using a polyphase filter. This works but I think we could do better.

pfeatherstone avatar Jan 13 '24 07:01 pfeatherstone

@pfeatherstone You can use https://github.com/aliutkus/torchinterp1d for that purpose. In fact it's exactly what I used it for, implementing differentiable time warping.

faroit avatar Jan 16 '24 18:01 faroit

Nice. Except you can't set the filter...

pfeatherstone avatar Jan 16 '24 18:01 pfeatherstone

I'll post the code I have tomorrow. To be honest, it would be great for someone to review

pfeatherstone avatar Jan 16 '24 18:01 pfeatherstone