Page 1 of 3

Improving "Clip Fix"

Posted: Wed May 29, 2013 5:14 pm
by Quant
When I use the "Effects"->"Repair" feature to manually fix clipping, the result is immensely better than what is achieved with "Effects"->"Clip Fix." The only problem is that, when dealing with a heavily clipped audio track, it takes an inordinate amount of time to manually select hundreds or thousands of clipped regions. I would therefore like to suggest that the interpolation used in "Effects"->"Clip Fix" be replaced with the interpolation used in "Effects"->"Repair."

Re: Improving "Clip Fix"

Posted: Sat Jun 01, 2013 8:27 am
by Gale Andrews
Quant wrote:When I use the "Effects"->"Repair" feature to manually fix clipping, the result is immensely better than what is achieved with "Effects"->"Clip Fix." The only problem is that, when dealing with a heavily clipped audio track, it takes an inordinate amount of time to manually select hundreds or thousands of clipped regions. I would therefore like to suggest that the interpolation used in "Effects"->"Clip Fix" be replaced with the interpolation used in "Effects"->"Repair."
Repair is meant for clicks as well as clipping.

If you are asking that Repair or something like if should run on longer selections, that's a popular request.

Personally I find Repair inferior to ClipFix if you were trying to repair by dragging adjacent sections of 128 samples back on each other. Clip Fix seems to me to give a much more musical result and the waveform retains its original shape and dynamics below the peaks to a greater extent.

Are you de-amplifying by a sufficient amount before using Clip Fix?

It is known however that Clip Fix cannot cope with clipping that is so bad that the polarity is inverted. There is an experimental version of it (only for mono tracks) that tries to cope with inverted polarity here .


Gale

Re: Improving "Clip Fix"

Posted: Sat Jun 01, 2013 10:31 am
by steve
For ClipFix there is the additional problem that it has to accurately detect where the clipping occurs as well as fixing it. "Repair" does not need to do that because it assumes that you have selected the damage. For "digital" clipping where peaks are cut off absolutely flat Clipfix can detect the clipping quite well. If the clipping is due to overloading of analogue components, or if the audio has been saved in MP3 format, or if for any other reason the clipping is not simple "digital" clipping, then detecting is very much harder and ClipFix will probably fail.

The best thing (by far) is to avoid getting clipping in the first place by recording with appropriate equipment and appropriate settings. If these are your own recordings, please describe what and how you are recording and we can try and give some tips about how to avoid distortion.

Re: Improving "Clip Fix"

Posted: Sat Jun 01, 2013 7:41 pm
by Quant
Thank you both for your replies!
Gale Andrews wrote:If you are asking that Repair or something like if should run on longer selections, that's a popular request.
No, but that certainly could be useful. If anyone's interested, though, I have an idea (obvious, perhaps) for a workaround for selections longer than 128 samples:

1) Downsample the original track (track A) just enough so that the section in need of repair becomes less than or equal to 128 samples in length. Call the downsampled result track B.
2) Repair the damaged section in track B.
3) Upsample track B to its original rate.
4) Take the repaired section from 3) and overwrite the corresponding section in track A.
5) If necessary, repair the two boundaries where the overwriting in 4) took place.

Of course, some of the higher frequencies will be lost in the repaired section, but it may be acceptable.
Personally I find Repair inferior to ClipFix if you were trying to repair by dragging adjacent sections of 128 samples back on each other. Clip Fix seems to me to give a much more musical result and the waveform retains its original shape and dynamics below the peaks to a greater extent.
I select each clipped region by hand and use "Effects"->"Repair." I don't do any dragging.
Are you de-amplifying by a sufficient amount before using Clip Fix?
Yes, although when working with 32-bit samples, I believe one only has to de-amplify afterwards so that the recovered peaks are no longer clipped.
It is known however that Clip Fix cannot cope with clipping that is so bad that the polarity is inverted. There is an experimental version of it (only for mono tracks) that tries to cope with inverted polarity here .
That's not an issue for me, but thanks.

-------------------------
steve wrote:For ClipFix there is the additional problem that it has to accurately detect where the clipping occurs as well as fixing it. "Repair" does not need to do that because it assumes that you have selected the damage. For "digital" clipping where peaks are cut off absolutely flat Clipfix can detect the clipping quite well. If the clipping is due to overloading of analogue components, or if the audio has been saved in MP3 format, or if for any other reason the clipping is not simple "digital" clipping, then detecting is very much harder and ClipFix will probably fail.
I'm dealing only with digital clipping, so Clip Fix finds the clipped regions sufficiently well for my purposes.
The best thing (by far) is to avoid getting clipping in the first place by recording with appropriate equipment and appropriate settings. If these are your own recordings, please describe what and how you are recording and we can try and give some tips about how to avoid distortion.
The recordings I'm dealing with are simply tracks ripped from purchased CD albums.

-------------------------

I've prepared a simple example that shows how, in my opinion, the interpolation of "Effects"->"Repair" is vastly superior to that of "Effects"->"Clip Fix."

Image

In my example, you'll notice six tracks:

1) Original track
2) Original track with +dB of clipping
3) Clipped track with -3dB of amplification
4) Result of "Effects"->"Clip Fix"
5) Result of "Effects"->"Repair"
6) Result of "Effects"->"Repair" applied four times in an iterative fashion.

I think the results speak for themselves.

Also, you'll notice I did something special with track 6, which provides the best results (in fact, almost perfect). On each iteration, I used "Effects"->"Repair" left to right on each of the original damaged sections. Each new iteration is an improvement over the previous iteration when multiple clipped sections are close to each other.

Again, all is well and good when I only have to repair a small number of clipped sections, but it's not uncommon to find a clipped CD track with thousands of clipped sections. In this case, it would take days of tedious work to repair so many sections. However, I have verified that "Effects"->"Repair" gives very good results as I have repaired just a few seconds of a clipped track (which took me about half an hour), whereas "Effects"->"Clip Fix" was no help and sometimes makes things worse.

Re: Improving "Clip Fix"

Posted: Sat Jun 01, 2013 9:21 pm
by steve
Quant wrote:although when working with 32-bit samples, I believe one only has to de-amplify afterwards so that the recovered peaks are no longer clipped.
That is correct (with recent versions of Audacity).
Quant wrote:I've prepared a simple example that shows how, in my opinion, the interpolation of "Effects"->"Repair" is vastly superior to that of "Effects"->"Clip Fix."
The results that you are getting from ClipFix in that example look to be extraordinarily bad - much worse than I would expect, so I'm wondering what the reason for that is. Could you post the actual audio in WAV format (preferably 32 bit float to ensure that I have exactly the same audio as you) and give details including settings of how I can reproduce your results - I'd like to improve ClipFix if possible, and it should be able to do much better than that.

Re: Improving "Clip Fix"

Posted: Sat Jun 01, 2013 11:48 pm
by Quant
steve wrote:The results that you are getting from ClipFix in that example look to be extraordinarily bad - much worse than I would expect, so I'm wondering what the reason for that is. Could you post the actual audio in WAV format (preferably 32 bit float to ensure that I have exactly the same audio as you) and give details including settings of how I can reproduce your results - I'd like to improve ClipFix if possible, and it should be able to do much better than that.
I've attached the complete Audacity project in a .ZIP archive. When opening the project, the part of the track I worked on starts at around sample 694,605. If you would prefer individual WAV files, just let me know.

With ClipFix, I used a high threshold (99.9%, if I remember correctly), since the clipping in this example is perfectly flat. When lowering the threshold, the reconstructed sections become more rounded, but the results are still nowhere near Repair (especially for music, rather than just this toy example).

In regards to Repair, I simply selected each clipped section, one at a time, from left to right, and performed a Repair operation on each. I should also note that, whenever there was a single sample surrounded by clipping on both sides, I included that sample in the selection when performing the Repair operation.

For the very last track in my example, I simply applied the processing described in the previous paragraph four times. For example, if there were five clipped sections, A B C D E, there would be a total of 5*4 Repair operations, in the following order: (A B C D E) (A B C D E) (A B C D E) (A B C D E).


P.S. I wonder if there is something that can be done so that my posts don't have to wait for moderator approval. I promise not to post any spam. :D

Re: Improving "Clip Fix"

Posted: Sun Jun 02, 2013 12:59 am
by Gale Andrews
Quant wrote:I wonder if there is something that can be done so that my posts don't have to wait for moderator approval. I promise not to post any spam. :D
OK I removed you from moderation. ;)


Gale

Re: Improving "Clip Fix"

Posted: Sun Jun 02, 2013 2:27 pm
by billw58
You seem to have found a case where Clip Fix is bound to fail. I'll leave it to Steve to analyze the code to see exactly what's going wrong, but my understanding is this. Clip Fix looks at each side of the flat section to determine the slope of the waveform. In your example, there are 1) only two or three samples on each side of the flat portion that Clip Fix can use to determine the slope, and 2) within the clipped section there is more than 1 cycle of the high-frequency wave. Point #1 means that if Clip Fix is using more than two samples on each side of the clipped section to determine the slope then it is getting fooled. Point #2 is something Clip Fix can not deal with - it assumes that it is reconstructing one half-cycle of the waveform. Repair looks at each side of the selection then tries to use that waveform to reconstruct the damaged (selected) section. This is why Repair works better with your example.

Try this. Generate 5 seconds of 1000 Hz tone, level 1.0. Select one cycle and Amplify by 3 dB (mark it with a label so you can find it later). Select the entire track and Amplify by -4 dB. Duplicate the track. Apply Clip Fix to one track and observe the result. On the other track, apply the Repair effect to the damaged cycle. Clip Fix correctly restores the amplified and clipped cycle. Repair replaces the damaged cycle with the undamaged cycles on either side, resulting in a loss of the amplification.

No time to upload a screenshot right now. If you'd like to see one let me know.

-- Bill

Re: Improving "Clip Fix"

Posted: Sun Jun 02, 2013 3:07 pm
by Quant
Gale Andrews wrote:OK I removed you from moderation. ;)
Many thanks! :D
billw58 wrote:Point #2 is something Clip Fix can not deal with - it assumes that it is reconstructing one half-cycle of the waveform.
This is exactly why I find Repair to be superior. For complex signals, like music, the result of Clip Fix does not sound very different from the clipped version because the vast majority (almost all) of the frequencies that were missing (due to clipping) are still missing after Clip Fix. With Repair, there is at least an attempt made to fill in the missing frequencies with something plausible, and this is why, in every case I've encountered, Repair does a better job than Clip Fix with music.
Repair looks at each side of the selection then tries to use that waveform to reconstruct the damaged (selected) section. This is why Repair works better with your example.
For anyone who is unaware, Repair uses least squares autoregression (LSAR) to interpolate the clipped samples. Basically, my suggestion is to give an option to use LSAR with Clip Fix rather than simple cubic interpolation.
Try this. Generate 5 seconds of 1000 Hz tone, level 1.0. Select one cycle and Amplify by 3 dB (mark it with a label so you can find it later). Select the entire track and Amplify by -4 dB. Duplicate the track. Apply Clip Fix to one track and observe the result. On the other track, apply the Repair effect to the damaged cycle. Clip Fix correctly restores the amplified and clipped cycle. Repair replaces the damaged cycle with the undamaged cycles on either side, resulting in a loss of the amplification.
Yes, Repair will not work well when the clipping is extremely periodic and occurs with a frequency similar to the underlying signal, especially when there was just a single frequency prior to clipping. I'd imagine that most real-world signals that people process with Audacity are much more complex, and that is where Repair does much better than Clip Fix.

Many thanks for your reply, Bill. I appreciate it. :)

Re: Improving "Clip Fix"

Posted: Sun Jun 02, 2013 5:56 pm
by steve
billw58 wrote:You seem to have found a case where Clip Fix is bound to fail.
It's close to a "best case" example for the Repair effect and close to a "worst case" example for ClipFix.

It wasn't particularly obvious from the original picture why, but on close inspection and considering the algorithms used the reason becomes obvious.

Here is an example that is close to a "best case" for ClipFix and close to a "worst case" for the Repair effect:
tracks.png
tracks.png (27.03 KiB) Viewed 18159 times
ClipFix

Detection:
ClipFix looks for consecutive samples that are close to the maximum peak level.

Reconstruction:
  1. ClipFix looks at the sample before the clipped region and the first sample of the clipped region and calculates the slope of the waveform at the start of the clipped region.
  2. The slope at the end of the clipped region is then calculated by looking at the final sample of the clipped region and the next sample.
  3. The waveform of the clipped region is reconstructed by cubic interpolation from the slope at the start to the slope at the end of the clipped regions.

Repair

Detection:
Repair assumes that the "bad" samples are within the selection.

Reconstruction:
The waveform in the selected region is constructed by nth order "least squares auto-regression" from the surrounding audio, where "n" is 3x the selection length to a maximum of 50.


ClipFix failed in Quant's example because the high frequency content of the waveform creates slopes at the start and end of the clipped region that are not in keeping with the low frequency component.

"Repair" failed in my example because the "repaired" audio is based on the surrounding audio, but the surrounding audio is also clipped.

In this final example, we have a region selected that included "not clipped" waveform, surrounded by clipped waveform - After Repair shows that the "not clipped" waveform has been clipped (so as to match the surrounding waveform). This illustrates why we can't just iterate across a longer damaged region 128 samples at a time.
tracks004.png
tracks004.png (19.7 KiB) Viewed 18159 times