Certainly, this can be done.
The only thing to keep in mind is that zero and negative values have to be treated separately.
Do you want to compress sample by sample or with any time constraints (look ahead, attack, decay).

There are several functions to convert between linear and logarithmic. The simplest are ‘linear-to-db’ and ‘db-to-linear’.
And there are ‘s-log’ and s-exp’ witch work with base ‘e’.
The latter is more suitable since it can work with any base.
Should the base or the exponent be changed by the amplitude?
For example: if you take the root, then the equation would be
y(T)=exp(exponent * log(x(T)))
The amplitude would be the base and the exponent fixed.
For an exponent of 0.5 (=square root) this would give:
1 → 1 (ratio 1:1)
0.5 → 0.7071 (comp 1:1.414)
0.25 → 0.5 (comp 1:2)
0.01 → 0.1 (comp 1:10)

This would naturally approach a square wave, since low values are more and more amplified with decreasing (fixed) exponent.
The same can be done with altering exponent, but I have to ponder about this case.
Sorry, you know, Ican’t see the picture, therefore I’m a little bit lost.

The poster produced an animation where what looks like a normal voice performance wave is subsequently boosted in the quiet areas and crushed in the loud ones, both plus and minus – in short, a voice track well on its way to being a series of square waves.

I produced an almost identical wave shape using the widely reviled Effect > Leveler on its most severe, extreme setting.

So this is already written. We await the response.

What gave me the idea was I that I was faced with trying to correct recording of a keyboard where the wrong type of swell-pedal was used : an exponential one instead of a linear one. So I thought exponential compression could correct the overly loud sections. Using usual compression tools solved the problem , but I was still interested to experiment with exponential compression.

Just the simplest case without look-ahead, attack or decay is what I was after, however I think Koz has cracked it : applying leveller on the heaviest setting repeatedly (about 6 times) produces a very close match for the exponential compression …

It does sound somewhat different from square-wave : the rounded corners mean it doesn’t have as many harmonics,
it’s like an early computer-game.

What the waveform dB view is really doing mathematically is something like:

Take sample value in [-1, 1]
Take its absolute value, now in [0,1]
Convert to dB (a certain linear scaling of the natural logarithm), now in [-infinity, 0]
Clip to the interval [-60, 0]
Linearly scale -60 to 0 height in the graph and 0 to 1
Negate if the original sample is negated

I suppose one could write the Nyquist equivalent with the right uses of mult, s-abs, s-log, s-max… except how do you do the last step? And perhaps better to clip before logarithm avoiding really taking log 0.

So some arbitrary clipping is part of this exercise.