Control Sampling Rate of a gen~ Object

ADwellingOx's icon

Hi,

I'm wondering if it's possible to control the sample rate of processes inside a gen~ object? I'd basically like to be able to down/up sample within a gen~ object without affecting max's overall sampling rate.

Any help would be great!

Alex

Peter McCulloch's icon

Not directly wthin gen~, but you can wrap it inside a poly~ and up/downsample there. If you're just doing it for efficiency, be sure to make sure that you're not using the high-quality interpolation filters. (see the poly~ help file for more info)

ADwellingOx's icon

Ah ok, thanks for the response!
I tried doing that with poly~ but it only allows you to choose sample rates that are proportional to the main sampling rate which is pretty limiting. Basically i'm trying to recreate the feedback oscillator FBSineN from SuperCollider and in that you can choose the rate (any frequency!) that it iterates through the formula.

Any other workarounds? It'd definitely be useful for other stuff as well.
I'd like to try and keep it all in Max and not use the sc3~ object or send OSC to supercollider if possible.

Peter McCulloch's icon

Ah, okay, this is downsampling, then? Upsample by 2, then use sah and a phasor to downsample.

ADwellingOx's icon

Oh yeah, thanks for the help! I also didn't notice the 'downsamp~' and 'degrade~' objects for some reason.. they also do the trick.

Marc Carlton's icon

does anyone have any example patches of sah and phasor to upsample?

Dario's icon

I think you can only use sah and phasor to downsample

Peter McCulloch's icon

Generally speaking you don’t want sah~ if you are upsampling, because that will alter the frequency content of your signal. You can upsample and resample in gen~ but the math and control logic is definitely not for the timid. (You can find tutorials on how the process works online)

As a thumbnail sketch: you stuff zeros between samples (so to go up by a factor of two, you put a zero between every incoming sample and the next), then use a low pass filter (preferably linear phase or near linear phase) on that signal (with the zeros!) to get rid of all the mirror image copies of the spectrum above 20kHz (or your desired Nyquist frequency) that stuffing the zeros creates. Your signal is now upsampled.

This works with any integer multiple of the sampling rate. However, let’s say what you actually want is 1.9391 the original sample rate. First, you’d want to make sure that you used a lower cutoff frequency in the previous step. From there you would need to interpolate down to your target rate. The ideal interpolation function is the sinc function, but this happens to also be infinite so you would use some approximation of it.

You would then do your operations at the new sample rate then resample in a similar fashion to get back down to your original sample rate.