ΣSigmaStudio Ask AI Русский Log in

asymmetricsoftclipper and advancedclip formulas are simply wrong?

SigmaDSP & SigmaStudio MaxAudioDSP Jun 4, 2023 00:00 5 replies Original on forum ↗
Subscribe
MaxAudioDSP Question
Jun 4, 2023 00:00

KJBob Hello, some time has passed since fooling around with Sigma DSPs. I'm now busy with ML stuff. I'm trying to port some of the clipping algorithms that are documented here into a pytorch library. Below an implementation of the advancedclip algo in python using numpy library

import numpy as np
import matplotlib.pyplot as plot

def advanced_clip(samples, threshold):
out = []
for In in samples:
theta = np.abs(In) - threshold / 1 - threshold
if np.abs(In) >= threshold:
out_ = (In * threshold + (1 - threshold) * np.tanh(theta))
out.append(out_)
else:
out.append(In)
return np.array(out)

start_time = 0
end_time = 0.004 # To show 4 periods
sample_rate = 48000
time = np.arange(start_time, end_time, 1/sample_rate)
theta = 0
frequency = 1000
amplitude = 1
sinewave = amplitude * np.sin(2 * np.pi * frequency * time + theta)

clip_out = advanced_clip(sinewave, 0.5)

plot.plot(time, sinewave, color='g', label='sin')
plot.title('Clipper output with sine input')
plot.xlabel('Time')
plot.ylabel('Amplitude')
plot.plot(time, clip_out, color='r', label='clip')
plot.legend()
plot.grid(True, which='both')
plot.savefig('test_clipper.png')

as you can see I'm obtaining pretty much odd results. I'm not asking to debug my Python code. I've tried several parenthesis combinations, for example (threshold / (1 - threshold)) obtaining even more odd results. I'm reporting

here the original formula as outlined in online doc https://wiki.analog.com/resources/tools-software/sigmastudio/toolbox/nonlinearprocessors/advancedclip. My question is simple: someone has ever tested the formula that is provided in the previous link? See an excerpt below

The input is compared with the threshold value, if input is less than threshold the input is pass trough to output. If input is greater than or equal to threshold then (In * threshold + (1 - threshold) * tanh(thetha)).

Where thetha = abs(in)- threshold / 1 - threshold

advancedclip #tanh #python

software

test_clipper.png-640x480.png
test_clipper.png-640x480.png
KJBob Reply
Jun 5, 2023 00:00

Hello,

I made a few spreadsheets to play with the wiki formula. The result below represents the original formula after fixing its negative excursion. The corresponding file is Clip-1.xlsx.

Just as you have found, we see a jump each time the input crosses the threshold. The formula in the wiki is simply wrong as you said. I made two modified spreadsheets (Clip-2 and Clip-3) in an attempt to get a smooth clip. Clip-2 goes into clipping very smoothly, but not very strongly and worse, it doesn't respond well to threshold adjustments. Clip-3 does respond to threshold adjustments but isn't as smooth. Hopefully, a SE who knows the algorithm can chime in on this topic.

Best regards,

Bob

Advanced-Clip.zip

Clip1-wfm.png
Clip1-wfm.png
KJBob Reply
Jun 5, 2023 00:00

Hi MaxAudioDSP,

This version, Clip-4, goes smoothly into clipping (that is, both the output function and its first derivative appear jump-free). The two graphs below show that clipping begins almost imperceptibly with the input signal just above threshold, then progressively gets stronger for higher input levels. At 24 dB above threshold, it appears like a hard clip -- except that even here, we can see a curved transition to/from the flat portions of the waveform. Does this match the performance of the Advanced Clipper block itself? I can't compare it because although I have a -1787 eval board, I can't figure out how to get audio through it (its User Guide didn't make sense to me).

If this is what's desired, the wiki's description for the advanced clip might read:

If abs(input) <= threshold, output = input

Else,

If input > 0, output = threshold*(1+tanh(theta))

Else,

output = - threshold*(1+tanh(theta))

theta = (abs(input)-threshold)/threshold

Thanks to you, now I understand why these chips include a complete set of "scientific calculator" functions.

Best regards,

Bob

Clip4.zip

Clip-4_0dB.jpg
Clip-4_0dB.jpg
Clip-4_24dB.png
Clip-4_24dB.png
Clip4.zip36.6 KB
MaxAudioDSP Reply
Jun 7, 2023 00:00

KJBob you're my hero here just for you to know! This is my pytorch WIP implementation github.com/.../networks.py

KJBob Reply
Jun 7, 2023 00:00

Hi MaxAudioDSP,

Your code appears quite impressive. I understand that Python is a powerful scripting language, yet I have yet to learn it. There were vacuum tubes in the lab when I went to school.

Best regards,

Bob

MaxAudioDSP Reply
Jun 7, 2023 00:00

KJBob hironically, the whole scope for the above script it to use ML to obtain a "clone" of the sound of a real tube amplifier. So you see, you pretty much fit! If you remember my Aida DSP prj years ago, it eventually turned into something like this https://aida-x.cc. No spam, I sincerely like to share stuff with you since you're one of the very best there, thanks a lot again!