Clip Fix..and Fix..and Fix..

I had the occasion to use Clip Fix the way it was intended [gasp] – on an otherwise properly recorded show with gentle clipping three or four times down a three minute performance.

It did some pretty impressive repairs, but on a whim I applied it again and got half-again better correction. And then again. My impression is I would have eventually gotten little or no correction just based on the law of diminishing returns.

Is it really doing that, or am I suffering from caffeine deprivation again.


I think it is.

As I understand it, from a quick look at the code, it reconstructs the peak based on the slope of the waveform either side of the clipped region.
If you repeat the effect on a section that has already been fixed, (which is what you did?) it is now working from a new peak, which is no longer flat, so the slope on either side is less steep and the effect is less pronounced … and so ad infinitum.


Can you tell what the control(s) actually do?


I think it goes like this:

Find the amplitude of the clipped peak.
Go to the points on either side where the amplitude is at the threshold level (Default = 95% of the peak)
Find the slopes of the waveform at these points.
Using these slopes, draw a cubic spline curve (don’t ask me to explain!) joining these two points, to replace the clipped peak.

If you change the threshold level, you are moving the points on the waveform at which you measure the slope. Decreasing the threshold moves the points downward on the waveform. Depending on what the waveform looks like, this will probably give you a different slope, and so change the amplitude of the curve that is drawn between them.

I don’t know what effect changing the threshold has on the sound, (I haven’t tried it), but a lower threshold means you are replacing more of the original wave when the curve is drawn.



Looking at this again, I realise that I shouldn’t try to analyse code at 2am, especially when it is in a language I am only barely familiar with (my experience to date being Fortran, Basic and C).

To correct my earlier post; the Threshold setting is the level (compared to the highest level found in the selection) at which the sample is considered to be clipped. Anything above this level will be Fixed. The rest I think I got right …

Your question prompted a fit of curiosity so I tried an experiment which went as follows:

Generate a 440 Hz tone in Audacity.
Amplify it to +3dB to clip it
Export it as a WAV and re-import it (otherwise Audacity remembers it was originally a sine wave)
Amplify it to -10dB and run ClipFix on it
Amplify it to 0dB

The resulting “Fixed” wave is close to a pure sine wave, but has some nasty-sounding harmonics. To see what these are, open a second track, generate a 440 Hz tone at 0dB in it, invert it and export the two tracks as a WAV.

The resulting WAV is a measure of the inaccuracy in the reconstruction of the sine wave. It has an amplitude of about -45dB (with a few spikes which can be got rid of) and, if you amplify it and look at the spectrum you can see harmonics all the way up to 20kHz and beyond. All very nasty-sounding.

If you now repeat ClipFix on the already fixed wave and do the comparison with the pure sine wave again, the result has a much lower level of high frequency harmonics, and doesn’t sound quite so bad.

Repeating the whole process a third time shows only a slight difference compared to the second time, all of which just confirms what you heard in the first place.

The question then becomes; If ClipFix introduces all these high harmonics (even if they are only at -45dB or less) would a carefully chosen low-pass filter after using ClipFix give a better result than repeating ClipFix?

Now I think I’ve done enough thinking for one night!


<<<The question then becomes; If ClipFix introduces all these high harmonics (even if they are only at -45dB or less) would a carefully chosen low-pass filter after using ClipFix give a better result than repeating ClipFix?>>>

No. Because that would affect the whole show, not just the portion that clip-fix fixed.

“How come the show sounds muddy now that I applied clip-fix?”

The question forcing itself past my lips is why doesn’t clipfix automatically apply itself three times? I see no down side to that and I’m guessin’ you can’t either. For I = “3”…


Yes it does, but nowhere near the level of the “nasty-sounding harmonics” that are created when you cliped the sine wave. Did you look at the spectrum of the clipped sine wave before running ClipFix?

Running ClipFix a second time on a clipped sine wave actually increases the distortion (though the change is barely audible).

The first time you run ClipFix it looks at the maximum amplitude, and as you said, anything that goes beyond 95% (default setting) of that threshold is considered “clipped”. The waveform is then “reconstructed” based on the slope of the waveform as it passes beyond that 95% threshold and the slope as it drops back to within the 95%. The “cubic spline curve” is a method of drawing a smooth curve from one point to the next. You’ve used Bezier curves in graphics programs? The cubic spline curve is rather like that.

Now you run Clipfix a second time. This time the 95% mark is not touching the original audio at all, it is “reconstructing” the top bit of the curve that was drawn the first time. Considering that Clipfix has no idea what the original waveform was like, the reconstructed curve is just a guess at what it may have been like. It is a rather “blind” guess as it takes no account of the harmonic content of the waveform either side of the clipped region. No matter how many times you run ClipFix, it still has no idea about the original waveform and is simply modifying the reconstructed region. The resultant waveform from multiple passes may be more like, or less like the original waveform, depending on what the waveform “should” have been (had it not been clipped).

The curve chosen by Benjamin Schwartz (the author) was used because it was relatively easy to implement and provides a smooth transition from the start of the clipped region to the end of the clipped region. It does not however produce a sine curve, so reconstructing a sine wave will produce harmonics (both odd and even harmonics).

When analysing the results of ClipFix, one thing to be aware of is that if a clipped region occurs on a multiple of 100000 samples, it does not get fixed. This is due to ClipFix processing the audio in blocks of 100000 samples. This does not matter if you are processing short selections of under a couple of seconds.

I suppose that “smoother sounding” curves could be used, but it cannot be assumed that this will be any more accurate than the curve that is used. I seem to remember that a clip repair effect in another program (possibly CEP) analysed the waveform either side of detected clips and reconstructed the waveform so that it had a similar harmonic content. I’m not sure that it actually sounded any better than a more simple spline interpolation.

There is another inherent shortcoming in ClipFix in that it assumes that the clipping is symmetrical for both +ve and -ve peaks. It would perhaps be better if it set independent thresholds for +ve and -ve.

Why not run clipfix 3 times by default?
ClipFix is a fairly slow plug-in already, do you want it to be 3 times slower?

<<<ClipFix is a fairly slow plug-in already, do you want it to be 3 times slower?>>>

If the show sounds better, I don’t care if it runs all night, remembering that the only other option is the reshoot filter.

All this came from my observation that I applied the tool multiple times and the show kept sounding, to my ears, better. Multi-thousand dollar Neumann condenser microphones are not flat or particularly accurate. They do, however make passionate skillful love to most human voices and that’s their value.

Stuff in the high range of a spectrum analysis doesn’t always sound bad. Just if you do it wrong. We recall the Tube Sound.

I wrote a while back that a tool like this should do a self exam. Iterate until the analysis spikes and trash stop decreasing. They’ll probably never stop changing, but the amount of pain energy will stop decreasing at the sweet spot.

Someone up the thread advanced the idea of a lopez filter to get rid of the harshness after the first correction. Apply only during the tiny corrected portion. Pick a swag number like 5KHz graceful roll-off to get rid of the last cracking snapping quality.


That’s not going to work. The "tiny corrected portion is just the top (/bottom) of a peak and strange things happen if you try to filter just part of a wave. However I get the idea - reduce the amount of upper harmonics (particularly odd harmonics) in the curve and it should sound sweeter.

I’ve had a play with adjusting the parameters of the spline curve, but it seems to be pretty well optimised for low levels of upper harmonics. Put another way - I can make it sound worse by adjusting the parameters (more harmonics) but I’ve not been able to produce a curve with less harmonics. If anyone is brilliant at mathematics and can come up with a better formula for the curve then I may be able to implement it in Nyquist code. There is undoubtedly room for improvement, but I’m finding the curve generated by the code as is, to be difficult to improve on.

I have doubts as to whether it actually improves the sound, but to make ClipFix do 3 passes, find the following lines at the bottom of clipfix.ny

(if (arrayp s)
  (dotimes (j (length s))
	(setf (aref s j) (declip (aref s j))))
  (setq s (declip s)))


and replace it with

(if (arrayp s)
   (vector (declip (declip (declip (aref s 0))))
      (declip (declip (declip (aref s 1)))))
   (declip (declip (declip s))))

Maybe there is a way to improve it.
On the first pass, I would keep the threshold level high, to retain as much of the original waveform as possible. However, on subsequent passes, it could be changed so as to reconstruct a bigger or smaller amount of the wave that was constructed on the first pass. As long as it is not set too low, it will not affect the original part of the waveform.

The obvious drawback is that it could take a lot of trial and error to see what settings improve the sound (if any!), plus the fact that there is no way of knowing (I think), for a given sound, whether a higher or lower threshold would give an improvement.

“If I had used a lower value the last time and a higher one this time …” etc., etc.

Maybe (or maybe not).