Page 3 of 4

Re: Killing hard clipping

Posted: Sun Aug 28, 2011 6:03 am
by kozikowski
Past the correction of quality sound, it's also necessary to compensate for the level and that can be its own nightmare. It's very common for people to jam a stereo, line-level signal into the Mic-In of a Windows laptop. It's also not uncommon for that to produce painful clipping at 0.5 or 6dB down from maximum.

So if you're waiting for a signal waveform to smack maximum and turn red (if you have that feature turned on) you may have a very long wait.

I think it's a grand idea to correct the clipped area by trying to divine the musical identity of adjacent notes. But this is assuming that they're not distorted, too. Slippery slope.

Koz

Re: Killing hard clipping

Posted: Sun Aug 28, 2011 3:01 pm
by samspots
It is a good thing that I am allowed to clarify here:
Clip.PNG
Clip.PNG (3.43 KiB) Viewed 1176 times
The Pink area is the clip, and the Cyan area is what is used to replace it.
The high frequencies in the cyan area are sampled, and then interpolated linearly, like this:
Catness.jpg
Catness.jpg (44.49 KiB) Viewed 1176 times
But imagine an actual spectrum instead of my cat.

Re: Killing hard clipping

Posted: Sun Aug 28, 2011 7:52 pm
by kozikowski
Actually, I'm happy with the video analogy, being a telelvision type.

Early analog videotape machines did something very similar called the DOC or Drop Out Compensator. If they played over a rough patch of tape, they would replace the visual damage with a closely associated patch of healthy video. If your show was predominately natural video, nobody could tell what you did. Can you tell if that leaf on the tree is the right one? Special effects shows didn't work out too well because there may not be closely associated video.

Under cetain conditions of massive error/bad tape, the show would look like your cat.

The processes are relarkably similar except we did have one one thing that sound doesn't. When we had damage, other systems failed, too, making it dead simple to figure out precisely where the damage was. The only way to figure out if you have audio clipping -- past hearing it -- is the presence of 0dB sound. If the failure isn't in the digital domain, you're dead.

Koz

Re: Killing hard clipping

Posted: Sun Aug 28, 2011 8:39 pm
by samspots
You do make a good argument. I must reiterate, however, by saying that I do not want a solution to clipping. I only want a way to reduce the sound of clipping. If turning a hole in the sound into a smear is what we are doing, it is going to be good enough. A hole draws attention, where a smear just doesn't look as good as the original.

Also, frankly, if there is a clip fix effect that is supposed to make clipping sound better, I might as well have a crack at it, too.

I should learn Nyquist now, shouldn't I?

Re: Killing hard clipping

Posted: Mon Aug 29, 2011 4:49 am
by kozikowski
Don't let me stop you.

How are you going to figure out if there is clipping damage in order to know when to hide it?

I think I may have just named your product. Clip Hide.

Koz

Re: Killing hard clipping

Posted: Mon Aug 29, 2011 11:30 am
by steve
samspots wrote:I should learn Nyquist now, shouldn't I?
Sure, but this is probably not a good first project. There are some parts of your suggested procedure that are extremely difficult to code and one step that may be impossible.
samspots wrote: A Fourier transform will be applied and the new harmonics will be determined by interpolating by them from the non-clipped areas.
This is the step that I think may be impossible, or at least, not work as expected.

FFT is a mathematical shortcut for estimating a "Discrete Fourier transform". DFT / FFT can be used to calculate the frequency content of a periodic waveform - that is that the waveform is assumed to repeat as a cycle. In real world audio we are not dealing with simple periodic waveforms, so a "window size" must be used that is sufficiently large to encompass all of the frequencies that we are interested in - in practice we could probably get away with (a close enough approximation) a window size of about 50 ms.

So lets look at a clipped note:
firsttrack000.png
firsttrack000.png (9.74 KiB) Viewed 1161 times
It is likely that the "attack" part of the note will be clipped (as in this example) so the frequency analysis would need to be taken from the section following (not before) the clipped region.

We then need to reconstruct the tops and bottoms of the waveform that have been clipped off.
Lets zoom in close onto a clipped region:
firsttrack002.png
firsttrack002.png (7.69 KiB) Viewed 1161 times
Let's say that we have successfully analysed the unclipped part of the waveform and found that it has a base frequency of 440 Hz and has a third harmonic that is 6 dB lower and a 5th harmonic that is 9 dB lower.
We do not know the phase relationships of these frequencies, so we can't simply synthesize those tones and "add" them to the clipped region. We would need to calculate the correct phase relationship of each frequency component that will provide a waveform shape to match the slope of the original waveform before/after the clipped region. Even in this highly simplified example (with only three frequencies) the calculations involved are extremely complex. In real world audio there could be thousands of frequencies and the phase angle of each of them would need to be calculated.

What the current "ClipFix" effect does is to ignore the frequency content and "simply" extrapolate the slope of the original waveform before/after the clipped region. Even this is quite complex without the added difficulty of attempting to match a specified frequency spectrum. This is the part that calculates the interpolated sample values:

Code: Select all

    (let* ((t1 (car exithigh))  ;;exit time
          (t2 (car returnhigh)) ;;return time
          (d1 (max 0 (/ (- (aref r t1) (aref r (- t1 (1- drange)))) (1- drange)))) ;;slope at exit
          (d2 (min 0 (/ (- (aref r (+ t2 (1- drange))) (aref r t2)) (1- drange)))) ;;slope at return
          (m (/ (+ d2 d1) (* (- t2 t1) (- t2 t1)))) ;;interpolation is by (t-t1)(t-t2)(mx+b)
          (b (- (/ d2 (- t2 t1)) (* m t2))) ;;These values of m and b make the cubic seamless
          (j (1+ t1))) ;; j is the index

     (while (< j t2)
         (setf (aref r j) (+ (aref r t1) (* (- j t1) (- j t2) (+ (* m j) b)))) 
	 (setf (aref r j) (+ (* (- t2 j) (/ (aref r t1) (- t2 t1))) (* (- j t1) (/ (aref r t2) (- t2 t1)))  (* (- j t1) (- j t2) (+ (* m j) b))))
         (setq j (1+ j)))) 
(you can view the code of the ClipFix effect by opening "clipfix.ny" in a text editor).

and here is the result of the interpolation:
firsttrack003.png
firsttrack003.png (7.9 KiB) Viewed 1160 times

Re: Killing hard clipping

Posted: Tue Aug 30, 2011 1:40 am
by samspots
kozikowski wrote:Don't let me stop you.

How are you going to figure out if there is clipping damage in order to know when to hide it?

I think I may have just named your product. Clip Hide.

Koz
.

Clip hide is a good name. I think that sticking with that name will make things easier for everybody.

@Steve:
I see, so the frequencies cannot be interpolated using that method, unless I do some nearly-impossible magic.

I did think of an alternative, though, that does not use any spectral wizardry.

Also, the clip fix you made is highly effective for digitally generated "pure" sounds, but for a lot of sounds, especially musical mixes, it is not a viable solution. The one thing that confuses clipfix is when it tries to reconstruct some high sounds riding a wave of bass. It just kills the prediction. If that could be fixed, it would be much closer to what I originally intended this to be.

First, the wave has a splitter-style hi-pass applied at the clip-replacement frequency on each waveform. Then, the waveforms before and after the clip are crossfaded and reversed so that they line up roughly with the seams, and then inverted to make the phases align better in some situations. Then, that is used to replace the clipped area, and the seams are repaired, like in the "repair" function. The High and Low mixes are then combined into the final, clip-hidden mix.

The only issue is that if more than 50% of all samples fit into the "clip" category, it will not sound as good. I don't see why any sensible person would let that happen, though, and any attempts to repair audio that badly damaged would not make sense.

It is much more doable, and though minor phase problems will occur within the clip repairs, I doubt a spectral solution could come much closer.

Re: Killing hard clipping

Posted: Tue Aug 30, 2011 2:50 am
by steve
samspots wrote: the clip fix you made
clipfix.ny was made by Benjamin Schwartz.
samspots wrote:First, the wave has a splitter-style hi-pass applied ...
As soon as you apply a filter to the waveform the shape of the waveform changes completely and the clipped regions become undetectable.
As an example, the the top track is a clipped sine tone, the second track is the same clipped sine tone after applying a high-pass filter and the third track is the clipped sine tone after applying a low pass filter.
tracks002.png
tracks002.png (21.81 KiB) Viewed 1155 times
If you want to experiment, an easy way to create clipping is to run the following code in the Nyquist Prompt effect:

Code: Select all

(clip s 0.6)
The number at the end (0.6) is the level at which the audio will clip.

Re: Killing hard clipping

Posted: Tue Aug 30, 2011 6:38 am
by kozikowski
The only issue is that if more than 50% of all samples fit into the "clip" category, it will not sound as good. I don't see why any sensible person would let that happen, though, and any attempts to repair audio that badly damaged would not make sense.
How long have you been on the forum?

"I took my camcorder to "The Bleeding Pretzels" rock concert but the track I got is all buzzy and clicking. How do I make a nice clear recording? Hurry, I'm on deadline. Thanx."

Koz

Re: Killing hard clipping

Posted: Wed Aug 31, 2011 6:43 am
by kozikowski
Are you likely to be listening to square waves? The bottom sample, the one closest to sine waves is most likely to have been the original show.

There is a Photoshop tool that when you change one brightness point, the rest of the scene changes to what it would likely have been if the whole scene had been lighted to the new value. It's right so often I've forgotten how to turn it off. It, too, works on likely outcomes.

Chris's Compressor. It's likely that you would want that upstage solo a little louder and that tutti orchestra in the next act softer.

Koz