[music-dsp] family of soft clipping functions ... crossed with ... Modular Synthesis Language Moselle Alpha Release Today.
As an illustration of my newly-released software I dug through the recent archive for something that would be easy and fun to implement in Moselle, and came across this post from forum stalwart Robert Bistow-Johnson. The following is a Moselle program (or patch) that implements the first half-dozen functions in Robert's series of soft-clippers. MIDI general controller 1 selects which clipping function to use, while general controller 2 selects the waveform to input. (The oscillator is spec'd as an LFO as the LFO doesn't scale its amplitude with pitch, which saved me a bit of typing.) Each of the functions is repeated in the Oscilloscope module so they can all be seen simultaneously, even though only one is heard at a time. See a screenshot of the Oscilloscope with waveforms and harmonic spectrum at: http://moselle.invisionzone.com/uploads/gallery/album_1/gallery_1_1_12533.gif The self-contained Moselle IDE is available for no-cost download at: http://moselle.invisionzone.com/index.php?/files/file/2-moselle-alpha-release The following is literally the Moselle patch that produced the screenshot (and woke up my wife while I was jamming with it). [LFO] Waveform = Select( General2, Sawtooth, Triangle, Sine) Frequency = Voice:Frequency [Voice] Mono = IF( LFO -1, -1, LFO 1, 1, Select( General1, LFO, (LFO - 1 * LFO^3/3 )*3/2, (LFO - 2 * LFO^3/3 + 1 * LFO^5/5 )*15/8, (LFO - 3 * LFO^3/3 + 3 * LFO^5/5 - 1 * LFO^7/7 )*35/16, (LFO - 4 * LFO^3/3 + 6 * LFO^5/5 - 4 * LFO^7/7 + 1 * LFO^9/9 )*315/128, (LFO - 5 * LFO^3/3 + 10 * LFO^5/5 - 10 * LFO^7/7 + 5 * LFO^9/9 - LFO^11/11 )*3465/1280 ) [Scope] Probe1 = LFO Probe2 = (LFO - 1 * LFO^3/3 )*3/2 Probe3 = (LFO - 2 * LFO^3/3 + 1 * LFO^5/5 )*15/8 Probe4 = (LFO - 3 * LFO^3/3 + 3 * LFO^5/5 - 1 * LFO^7/7 )*35/16 Probe5 = (LFO - 4 * LFO^3/3 + 6 * LFO^5/5 - 4 * LFO^7/7 + 1 * LFO^9/9 )*315/128 Probe6 = (LFO - 5 * LFO^3/3 + 10 * LFO^5/5 - 10 * LFO^7/7 + 5 * LFO^9/9 - LFO^11/11 )*3465/1280 Probe7 = Voice:Mono Start = LFO:SyncOut Stop= LFO:SyncOut FreqHint= LFO:Frequency at the last AES in NYC, i was talking with some other folks (that likely hang out here, too) about this family of soft clipping curves made outa polynomials (so you have some idea of how high in frequency any generated images will appear). these are odd-order, odd-symmetry polynomials that are monotonic from -1 x +1 and have as many continuous derivatives as possible at +/- 1 where these curves might be spliced to constant-valued rails. the whole idea is to integrate the even polynomial (1 - x^2)^N x g(x) = integral{ (1 - v^2)^N dv} 0 you figger this out using binomial expansion and integrating each power term. normalize g(x) with whatever g(1) is so that the curve is g(x)/g(1) and splice that to two constant functions for the rails { -1x = -1 { f(x) = { g(x)/g(1) -1 = x = +1 { { +1 +1 = x you can hard limit (at +/- 1) before passing through this soft clipper and it still works fine. but it has some gain in the linear region which is g(0). if you want to add some even harmonic distortion to this, add a little bit of (1 - x^2)^M to f(x) for |x| 1 and it's still smooth everywhere, but there is a little DC added (which has to be the case for even-symmetry distortion). M does not have to be the same as N and i wouldn't expect it to be. you can think of f(x) as a smooth approximation the sign or signum function sgn(x) = lim f(a*x) a - +inf or sgn(x) = lim f(x) N - +inf which motivates using this as a smooth approximation of the sgn(x) function as an alternative to (2/pi)*arctan(a*x) or tanh(a*x). from the sgn(x) function, you can create smooth versions of the unit step function and use that for splicing. as many derivatives are continuous in the splice as possible. and it satisfies conditions of symmetry and complementarity that are useful in our line of work. u(x) = 1/2 * (1 + sgn(x)) =approx 1/2*(1 + f(x)) you can run a raised cosine (Hann) through this and get a more flattened Hann. in some old stuff i wrote, i dubbed this window: w(x) = 1/2 + (9/16)*cos(pi*x) - (1/16)*cos(3*pi*x) as the Flattened Hann Window but long ago Carla Scaletti called it the Bristow-Johnson window in some Kyma manual. i don't think it deserves that label (i've seen that function in some wavelet/filterbank lit since for half-band filters). you get that window by running a simple Hann through the biased waveshaper: 1/2 * ( 1 + f(2x-1) ) with N=1. you will get an even more pronounced effect (of smoothly flattening the Hann) with higher N. below
Re: [music-dsp] family of soft clipping functions.
On 03/11/2013, robert bristow-johnson r...@audioimagination.com wrote: the point is that if you upsample, then soft-clip, then LPF, and finally downsample back to the original sample rate, you need only prevent the aliases from getting back into your *original* baseband. it doesn't matter that *some* of the images have folded over and become non-harmonic aliases, just as long as they do not survive into the final output. i don't know how better to explain it, without a drawing. Oh, hang on, I think I get it. So an nth-order polynomial expands the spectrum produced by n times. Let's say a 3rd-order poly turns a bandwidth of 22kHz into one of 66kHz. What you're saying is that you only need to upsample a 44.1kHz signal by 2, do the shaping, and run a low pass at the original 22kHz - because whilst everything above 44.1kHz will fold back, it'll only fold back down to frequencies above 22.3kHz and leave your original 0-22kHz bandwidth alone, right? (But, say, a 4th-order poly would require upsampling by more than 2, otherwise it'd produce harmonics up to 88kHz, which would fold back into your desired bandwidth.) Have I understood properly? -- dupswapdrop -- the music-dsp mailing list and website: subscription info, FAQ, source code archive, list archive, book reviews, dsp links http://music.columbia.edu/cmc/music-dsp http://music.columbia.edu/mailman/listinfo/music-dsp
Re: [music-dsp] family of soft clipping functions.
I think I've been caught out on the html email thing as well, I wonder how many posts have gone completely missing that I've sent? Here is one I sent 5 days ago, sorry if this is a double up, I checked the archives but couldn't find anything: Hi Robert, Thanks very much for the post! I plotted the shapes and noticed that as the order increased the maximum output value decreased. I find for audio use it's nice to have the maximum output remain constant, for example at +-1 like a tanh. This can easily be added to your equations by scaling the x^2 term by the appropriate amount, lets call it a, so the final formula is: f[x, a, n] = Integrate[(1 - a v^2)^n, {v, 0, x}] So then the input bounds will change from -1, 1 to some other value say -x1 to x1, and then there are two variables a and x1 that can be solved by the following two equations: f'[x1, a, n] == 0 and f[x1, a, n] == 1 The first few solutions to these equations are: 3rd order with x1 = 3/2, f3(x) = x - (4 x^3)/27 5th order with x1 = 15/8, f5(x) = x - (128 x^3)/675 + (4096 x^5)/253125 7th order with x1 = 35/16, f7(x) = x - (256 x^3)/1225 + (196608 x^5)/7503125 - (16777216 x^7)/12867859375 9th order with x1 = 315/128, f9(x) = x - (65536 x^3)/297675 + (536870912 x^5)/16409334375 - (17592186044416 x^7)/6838508054109375 + (72057594037927936 x^9)/872422665003003515625 By comparing the results for lots of values of n I noticed that these are the same solutions that you get if you have f[x] = x + a(3) x^3 + ... + a(2n+1) x^(2n+1) and solve for the a coeffs and x1 using the system of equations f[x1] == 1, f'[x1]==0, ..., f'n[x1]==0. Enjoy! Andy -- cytomic - sound music software -- dupswapdrop -- the music-dsp mailing list and website: subscription info, FAQ, source code archive, list archive, book reviews, dsp links http://music.columbia.edu/cmc/music-dsp http://music.columbia.edu/mailman/listinfo/music-dsp
Re: [music-dsp] family of soft clipping functions.
On 11/2/13 6:58 PM, Wen Xue wrote: But, soft-clipping is not going to change periodicity, is it? it should not. that's why we don't want non-harmonic components (aliases) to survive the soft-clipping process. the point is that if you upsample, then soft-clip, then LPF, and finally downsample back to the original sample rate, you need only prevent the aliases from getting back into your *original* baseband. it doesn't matter that *some* of the images have folded over and become non-harmonic aliases, just as long as they do not survive into the final output. i don't know how better to explain it, without a drawing. bestest, r b-j So if you soft-clip a sine wave, be it polynomial or not, the outcome is periodical at the same period, so contains only perfect harmonics. It cannot behave in the folded alias way one usually suspect. On 02/11/2013 06:36, robert bristow-johnson wrote: just to be clear. the general rule is that an Nth-order polynomial can generate images at frequencies up to the Nth multiple of the frequency of the original baseband image. it is sufficient to oversample by a factor of (N+1)/2 to prevent any of these generated images from potentially folding back into the baseband. e.g. 3rd-order softclipping requires upsampling by a factor of 2. another e.g. 7th-order softclipping requires upsampling by a factor of 4 to avoid any folded aliases from contaminating the original baseband. -- dupswapdrop -- the music-dsp mailing list and website: subscription info, FAQ, source code archive, list archive, book reviews, dsp links http://music.columbia.edu/cmc/music-dsp http://music.columbia.edu/mailman/listinfo/music-dsp
Re: [music-dsp] Family of soft clipping functions.
If you are just looking for a minimally noticable softclip, with analog sound, just two three-order distortions in series is optimal. With a crossfade on 1 and 2, and a threshold on knee-depth, it really gives a sound many wants, and is very optimal, in terms of computing power required aswell. This I have already done in my softclip plugin. Peace Be With You. -- Ove Karlsen, www.ovekarlsen.com -- dupswapdrop -- the music-dsp mailing list and website: subscription info, FAQ, source code archive, list archive, book reviews, dsp links http://music.columbia.edu/cmc/music-dsp http://music.columbia.edu/mailman/listinfo/music-dsp
Re: [music-dsp] family of soft clipping functions.
But, soft-clipping is not going to change periodicity, is it? So if you soft-clip a sine wave, be it polynomial or not, the outcome is periodical at the same period, so contains only perfect harmonics. It cannot behave in the folded alias way one usually suspect. Xue On 02/11/2013 06:36, robert bristow-johnson wrote: just to be clear. the general rule is that an Nth-order polynomial can generate images at frequencies up to the Nth multiple of the frequency of the original baseband image. it is sufficient to oversample by a factor of (N+1)/2 to prevent any of these generated images from potentially folding back into the baseband. e.g. 3rd-order softclipping requires upsampling by a factor of 2. another e.g. 7th-order softclipping requires upsampling by a factor of 4 to avoid any folded aliases from contaminating the original baseband. -- dupswapdrop -- the music-dsp mailing list and website: subscription info, FAQ, source code archive, list archive, book reviews, dsp links http://music.columbia.edu/cmc/music-dsp http://music.columbia.edu/mailman/listinfo/music-dsp
Re: [music-dsp] family of soft clipping functions.
On 10/30/13 4:24 PM, Theo Verelst wrote: For whoever follows this, I thought and worked a bit about the clipping idea, and especially I was thinking about the harmonic behavior of the clipping function of N degree, so i tried using Maxima, and found there are distinct harmonics added to a sine wave passing through it. just to be clear. the general rule is that an Nth-order polynomial can generate images at frequencies up to the Nth multiple of the frequency of the original baseband image. it is sufficient to oversample by a factor of (N+1)/2 to prevent any of these generated images from potentially folding back into the baseband. e.g. 3rd-order softclipping requires upsampling by a factor of 2. another e.g. 7th-order softclipping requires upsampling by a factor of 4 to avoid any folded aliases from contaminating the original baseband. -- r b-j r...@audioimagination.com Imagination is more important than knowledge. -- dupswapdrop -- the music-dsp mailing list and website: subscription info, FAQ, source code archive, list archive, book reviews, dsp links http://music.columbia.edu/cmc/music-dsp http://music.columbia.edu/mailman/listinfo/music-dsp
Re: [music-dsp] family of soft clipping functions.
On 2013-11-01, robert bristow-johnson wrote: just to be clear. the general rule is that an Nth-order polynomial can generate images at frequencies up to the Nth multiple of the frequency of the original baseband image. Quite so. So in addition, if you want to really keep it clean of aliasing artifacts, you typically want to oversample your nonlinear system as many times over as is the highest polynomial order you're using. Then there are at at least two troublesome things one the way. First, how overdriven transformers, coils, capacitors and tubes/transistors work. All of them tend to saturate, and when they do, the effect tends to be of ridiculously high order. We're not talking aabout second order, here, but something like 200 before we get to the proper curve even in the steady state, memoryless curve. And secondly, in the right proportion, purposeful aliasing can actually sound pretty good and just right. I used it to good effect once in my youth, at least. :) -- Sampo Syreeni, aka decoy - de...@iki.fi, http://decoy.iki.fi/front +358-40-3255353, 025E D175 ABE5 027C 9494 EEB0 E090 8BA9 0509 85C2 -- dupswapdrop -- the music-dsp mailing list and website: subscription info, FAQ, source code archive, list archive, book reviews, dsp links http://music.columbia.edu/cmc/music-dsp http://music.columbia.edu/mailman/listinfo/music-dsp
Re: [music-dsp] family of soft clipping functions.
Hi For whoever follows this, I thought and worked a bit about the clipping idea, and especially I was thinking about the harmonic behavior of the clipping function of N degree, so i tried using Maxima, and found there are distinct harmonics added to a sine wave passing through it. Maybe this follows from the definition, but anyhow maxima can simplify the sine terms in the clipped signal and make them explicit components, for instance for N=6: http://www.theover.org/Dsp/Softclip/softclip3.png Might be interesting as a frequency limited/controlled envelope as well, such that the combined spectrum of the signal and the envelope shape through the VCA multiplication has a (added) spectrum which remains Nyquist correct. T.V. -- dupswapdrop -- the music-dsp mailing list and website: subscription info, FAQ, source code archive, list archive, book reviews, dsp links http://music.columbia.edu/cmc/music-dsp http://music.columbia.edu/mailman/listinfo/music-dsp
Re: [music-dsp] family of soft clipping functions.
robert bristow-johnson wrote: at the last AES in NYC, i was talking with some other folks (that likely hang out here, too) about this family of soft clipping curves made outa polynomials... Th script you supplied worked fine with the Open Source (and free) Octave as well, and gives this graphical result: http://www.theover.org/Dsp/Softclip/softclip1.png T.V. -- dupswapdrop -- the music-dsp mailing list and website: subscription info, FAQ, source code archive, list archive, book reviews, dsp links http://music.columbia.edu/cmc/music-dsp http://music.columbia.edu/mailman/listinfo/music-dsp
Re: [music-dsp] family of soft clipping functions.
This reminds me of experimenting with polynomials as an amplitude enveloping function for a soft synthesiser. There was something rather alluring about the idea of a one-line-of-code amplitude envelope - unfortunately it made creating the envelopes pretty tiresome, and when I thought about the performance I realised it was probably slower to do that maths than the few conditionals you would use for a standard piecewise envelope. So basically a rubbish idea unless you have some strange need to have your amplitude envelope be a single line of code :I -Original Message- From: music-dsp-boun...@music.columbia.edu [mailto:music-dsp-boun...@music.columbia.edu] On Behalf Of robert bristow-johnson Sent: 29 October 2013 01:56 To: music-dsp@music.columbia.edu Subject: [music-dsp] family of soft clipping functions. at the last AES in NYC, i was talking with some other folks (that likely hang out here, too) about this family of soft clipping curves made outa polynomials (so you have some idea of how high in frequency any generated images will appear). these are odd-order, odd-symmetry polynomials that are monotonic from -1 x +1 and have as many continuous derivatives as possible at +/- 1 where these curves might be spliced to constant-valued rails. the whole idea is to integrate the even polynomial (1 - x^2)^N x g(x) = integral{ (1 - v^2)^N dv} 0 you figger this out using binomial expansion and integrating each power term. normalize g(x) with whatever g(1) is so that the curve is g(x)/g(1) and splice that to two constant functions for the rails { -1x = -1 { f(x) = { g(x)/g(1) -1 = x = +1 { { +1 +1 = x you can hard limit (at +/- 1) before passing through this soft clipper and it still works fine. but it has some gain in the linear region which is g(0). if you want to add some even harmonic distortion to this, add a little bit of (1 - x^2)^M to f(x) for |x| 1 and it's still smooth everywhere, but there is a little DC added (which has to be the case for even-symmetry distortion). M does not have to be the same as N and i wouldn't expect it to be. you can think of f(x) as a smooth approximation the sign or signum function sgn(x) = lim f(a*x) a - +inf or sgn(x) = lim f(x) N - +inf which motivates using this as a smooth approximation of the sgn(x) function as an alternative to (2/pi)*arctan(a*x) or tanh(a*x). from the sgn(x) function, you can create smooth versions of the unit step function and use that for splicing. as many derivatives are continuous in the splice as possible. and it satisfies conditions of symmetry and complementarity that are useful in our line of work. u(x) = 1/2 * (1 + sgn(x)) =approx 1/2*(1 + f(x)) you can run a raised cosine (Hann) through this and get a more flattened Hann. in some old stuff i wrote, i dubbed this window: w(x) = 1/2 + (9/16)*cos(pi*x) - (1/16)*cos(3*pi*x) as the Flattened Hann Window but long ago Carla Scaletti called it the Bristow-Johnson window in some Kyma manual. i don't think it deserves that label (i've seen that function in some wavelet/filterbank lit since for half-band filters). you get that window by running a simple Hann through the biased waveshaper: 1/2 * ( 1 + f(2x-1) ) with N=1. you will get an even more pronounced effect (of smoothly flattening the Hann) with higher N. below is a matlab file that demonstrates this as a soft clipping function. BTW, Olli N, this can be integrated with that splicing theory thing we were talking about approximately a year ago. it would define the odd-symmetry component. we should do an AES paper about this. i think now there is nearly enough meat to make a decent paper. before i didn't think so. FILE: softclip.m line_color = ['g' 'c' 'b' 'm' 'r']; figure; hold on; x = linspace(-2, 2, 2^18 + 1); x = max(x, -1); % hard clip for |x| 1 x = min(x, +1); for N = 0:10 n = linspace(0, N, N+1); a = (-1).^n ./ (factorial(N-n) .* factorial(n) .* (2*n + 1)); a = a/sum(a); y = x .* polyval(fliplr(a), x.^2); % stupid MATLAB puts the coefs in the wrong order plot(x, y, line_color(mod(N,length(line_color))+1) ); end hold off; have fun with it, if you're so inclined. L8r, -- r b-j r...@audioimagination.com Imagination is more important than knowledge. -- dupswapdrop -- the music-dsp mailing list and website: subscription info, FAQ, source code archive, list archive, book reviews, dsp links http://music.columbia.edu/cmc/music-dsp http
Re: [music-dsp] family of soft clipping functions.
robert bristow-johnson wrote: x g(x) = integral{ (1 - v^2)^N dv} 0 you figger this out using binomial expansion and integrating each power term. Maybe a nice worksheet of (wx)Maxima, the FOS algebraic manipulation programs: http://www.theover.org/Dsp/Softclip/softclip2.png I thought G1() would be close to the second order Taylor approximation of the cos(x), but that isn't accurate... Regards, Theo V. -- dupswapdrop -- the music-dsp mailing list and website: subscription info, FAQ, source code archive, list archive, book reviews, dsp links http://music.columbia.edu/cmc/music-dsp http://music.columbia.edu/mailman/listinfo/music-dsp