Clipfix (see waveform pic) - plugins available?

Using Nyquist scripts in Audacity.
Post and download new plug-ins.
Forum rules
If you require help using Audacity, please post on the forum board relevant to your operating system:
Windows
Mac OS X
GNU/Linux and Unix-like
Post Reply
Arnie
Posts: 10
Joined: Mon May 12, 2008 11:18 am
Operating System: Please select

Clipfix (see waveform pic) - plugins available?

Post by Arnie » Mon May 12, 2008 11:42 am

Edit: scroll down and read the bold parts for a quick look at the computational model and coding outline.

Ok, so I made a mistake. I gave a mic a bit too much gain and now the reading went off the scale, resulting in this:
Image
You can see the sine-like waves in there still, so I was wondering if there is a plugin that can reconstruct those waves by completing the parts that went off the scale by fitting them in the sine?

This wave produces a typical clip-distortion sound. I tried clipfix.ny which can be found on the Internet but it didn't help. Any suggestions? If there's no plugin available I hope to have inspired someone with math-programming skills to write one :)
Last edited by Arnie on Sun May 18, 2008 12:42 am, edited 1 time in total.

steve
Site Admin
Posts: 80677
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Clipfix (see waveform pic) - plugins available?

Post by steve » Wed May 14, 2008 12:40 am

Arnie wrote:I gave a mic a bit too much gain
A "BIT" too much gain :D

What has happened here is that the signal has gone so far off the scale that it has appeared on the other side.
Actually that should never happen, and is partly due to your sound card handling the overload incorrectly.

The distortion in your example is just too badly damaged to be repaired by ClipFix (or any other clip repair tool that I know of).

ClipFix is pretty good at repairing "normal" clipping, but in the example you have shown, the clipped regions have change polarity (overly high positive values have become negative.

Here is an example of a badly clipped audio signal which does not have polarity reversal:
bad-clipping.png
bad-clipping.png (10.41 KiB) Viewed 11929 times
And here is the same waveform after repairing with ClipFix:
repaired.png
repaired.png (14.52 KiB) Viewed 11901 times
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Arnie
Posts: 10
Joined: Mon May 12, 2008 11:18 am
Operating System: Please select

Re: Clipfix (see waveform pic) - plugins available?

Post by Arnie » Thu May 15, 2008 9:04 pm

Thank you for your reply.

Actually this recording was made in a more sophisticated setup:
Microphones etc. --> Samson Mixer --> MD Recorder --> Laptop
I was testing a Sennheiser wired mic instead of the AKG wireless set we usually use. Lacking a compressor I tend to use the first available link in the chain to limit the volume of each mic to a certain level, to keep the dynamic range within bounds. (The system isn't only used for recording but also for live amplificaton and I can't blow people's ears off just to get a good recording, y'know.)

For the wireless mic this link is the receiver's volume setting. The receiver would clip where appropriate and its max. output signal to the mixer was set not to clip anything further down the chain. These recordings never gave distortion like this and were straightforward to edit. I guess the AKG box handles clipping nicely.

However the Sennheiser wired mic obviously doesn't have a receiver with a volume setting, but is inserted straight into the mixer by XLR, so it clips there instead. In this situation I know I made sure that the signal sent through to the MD recorder and laptop was at an appropriate level (but of course the damage had already been done there). So I'm suspecting that the mixer just doesn't deal so well with clipping. I wish I had a compressor but that's just no option financially.

Back to the point though: purely from a mathematical point of view I see potential in the graphs of my first post. It has to be possible to reconstruct those sine-like waves if it's possible to reconstruct your example. I mean, in your graphs there is a flat max-signal at the clipping point; replace the bad (sudden negative) data points from my graph with the highest value found right before/after the negative values kick in, and you have exactly the same situation. Alternatively, reversing back the polarity seems like another option. That's a simple -1 multiplication.

In fact, if somebody would be able to do the above, it should be sufficient to make the current clipfix.ny finish the job (it fixed your situation)!


Unfortunately I don't have the appropriate programming skills to convert this idea to code but I was hoping for someone to tag along who does. Of course I don't want to push anyone to code this specially for me :mrgreen: but I guess it would make a good and easily implemented function for the experienced audio programmer. Or would it? You'd certainly have my graditude, for what it's worth... :lol:
Last edited by Arnie on Sun May 18, 2008 12:45 am, edited 2 times in total.

steve
Site Admin
Posts: 80677
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Clipfix (see waveform pic) - plugins available?

Post by steve » Fri May 16, 2008 7:06 pm

Arnie wrote:purely from a mathematical point of view I see potential in the graphs of my first post. It has to be possible to reconstruct those sines if it's possible to reconstruct your example. I mean, in your graphs there is a flat max-signal at the clipping point; replace the bad (sudden negative) data points from my graph with the highest value found right before/after the negative values kick in, and you have exactly the same situation. Alternatively, reversing back the polarity seems like another option. That's a simple -1 multiplication.
It's not quite so simple - What kind of algorithm would you need to be able to detect that the samples highlighted in this image are incorrect? (if indeed they are incorrect ).
clipwave.png
clipwave.png (11.58 KiB) Viewed 11919 times
So you are recording a live gig?

For a good recording, you need to be able to be able to avoid clipping rather than try to correct it. Correcting bad recording is really a last resort which can sometimes work in removing small imperfections in an otherwise good recording.
Arnie wrote:However the Sennheiser wired mic obviously doesn't have a receiver with a volume setting, but is inserted straight into the mixer by XLR, so it clips there
If that is where the clipping occurs, you need to turn down the gain on that channel and use the level fader to bring the volume up to match the other inputs. If the level fader will not go high enough, then you need to turn down everything else.

In the heat of a live session, it is easy to think "oh, the guitar is not loud enough, I'll turn it up. Oh, now the vocal is not loud enough, I'll turn it up. Oh, now the Sax is not loud enough, I'll turn i up. Oh, now the guitar sounds too quiet again...." when in fact you would have been better to turn down the electric bass. Most mixing desks have either a means of metering a single channel "pre-fader", or at least a "peak" or "clip" indicator. To get a clean sound for recording, you must take heed of these and avoid clipping. If it is not possible to get a high enough level through your PA, then you need a more powerful PA.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Arnie
Posts: 10
Joined: Mon May 12, 2008 11:18 am
Operating System: Please select

Re: Clipfix (see waveform pic) - plugins available?

Post by Arnie » Fri May 16, 2008 8:09 pm

Look at the waveform - what is characteristic for the clipping parts from a mathematical point of view? I'd say that the sudden extremely negative slope, i.e. the drop from a very positive to a very negative value of the wave, marks the beginning of the clip area. Likewise, to find the end of such a clip area you could detect the sudden postive slope.

Translating this to a computational definition: considering that the distance Δt between two samples on the x-axis is constant (defined by the sample rate 44.1kHz), the slope Δyt is proportional to Δy. The beginning of the clip (sudden negative slope) can be identified by a very negative Δy and the end of the clip by a very positive. This Δy can simply be found by y(sample) - y(previoussample).

Of course we still have to establish what exact value of Δy is required for something to be identified as a clip. This could be left a variable for the user to set himself while executing the plugin. A second requirement for an area to be identified as clipping (to prevent false positives) is that the y-value must remain in the negative until a sudden high slope is found, marking the end of the clip.

A third characteristic that I distinguish in this particular waveform is that when you revert the polarity of the clipping area back to positive, the line seems to approximate the red line that I drew earlier. Computationally speaking -y(clip) ≈ y(just-before-clip). I'm not sure this should be used in a detection algorithm though. If it would be, a user-set variable determining how much the values can differ for an area to still be identified as 'clip' is recommended. But that would probably amount to a lot of unnecessary work. Also for a waveform that doesn't have its zero line at 0 this is an unusable property. An alternative definition would be y(clip) + 1 ≈ y(just-before-clip). But such advanced ideas don't seem necessary to me.

I hope the characters Δ (delta) and ≈ (approx. equal to) show correctly on your system.
---
The problem during the live recording is, that the mic is used for both soft speech and loud shouts. The soft speech has to be amplified so that it's audible on the system, and at the level required to do that the louder parts would blow the people's ears without a clip limit.
Last edited by Arnie on Sun May 18, 2008 12:41 am, edited 1 time in total.

steve
Site Admin
Posts: 80677
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Clipfix (see waveform pic) - plugins available?

Post by steve » Sat May 17, 2008 9:41 am

A much better solution:
http://www.behringer.com/COM800/?lang=ENG
http://www.dolphinmusic.co.uk/page/shop ... ct_id/5983

(I have a similar problem with the singer in one of my bands - how to prevent him from blasting everything into the red as he ... um... emphasizes certain moments. :D
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Arnie
Posts: 10
Joined: Mon May 12, 2008 11:18 am
Operating System: Please select

Re: Clipfix (see waveform pic) - plugins available?

Post by Arnie » Sat May 17, 2008 12:41 pm

Yeah, I wish I could afford a compressor.

Here's an illustration to my idea of converting the inversed clips to normal clips:
Image

Also note this parallel topic:
http://www.hydrogenaudio.org/forums/ind ... 63302&st=0
Last edited by Arnie on Sun May 18, 2008 1:33 am, edited 1 time in total.

steve
Site Admin
Posts: 80677
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Clipfix (see waveform pic) - plugins available?

Post by steve » Sat May 17, 2008 2:00 pm

I can see the idea of your suggestion, and I guess that it could work to some extent since with real world digital audio, consecutive samples tend to be relatively close to each other. I don't think it very likely that you will find anyone willing to spend a lot of time developing such a tool - as said before, this kind of damage should not happen in the first place, and even after "repair" the output would still be significantly different from what it should have been.

Did you check out the price of that compressor? The box is just plastic, but the price is very good and the electronics are good - probably worth saving up for.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Arnie
Posts: 10
Joined: Mon May 12, 2008 11:18 am
Operating System: Please select

Re: Clipfix (see waveform pic) - plugins available?

Post by Arnie » Sat May 17, 2008 10:21 pm

Well, I sent this topic to the mailing list but I did some research myself and found that Nyquist is better equipped for higher level operations than for sample-by-sample processing. However this particular idea should be possible with the following functions:
http://www.audacity-forum.de/download/e ... l#index164

This thread gives a great start:
http://www.nabble.com/Processing-indivi ... 55625.html
Especially note the framework code for sample-by-sample processing which I'll cite here:

Code: Select all

;nyquist plug-in 
;version 1 
;type process 
;name "DSP Effect..." 
;action "Performing DSP Effect..." 
;control dummy "" int "" 0 0 0 

;; the dummy slider above is only to open a effect window 
;; where you can press the "Debug" button in case of trouble. 

;; Just write TWO semicolons at the beginning of the "control" 
;; line if you want to disable the effect window temporarily. 

;; define a dsp class 
;; 
(setf dsp-class (send class :new '(copy-of-sound))) 

;; initial function of dsp class 
;; 
(send dsp-class :answer :isnew '(sound) 
  '((setf copy-of-sound (snd-copy sound)))) 

;; method to be executed with every call to dsp-class 
;; 
(send dsp-class :answer :next '() 
  '((let ((current-sample (snd-fetch copy-of-sound))) 
     ;; "cond" checks for end-of-samples condition 
     (cond (current-sample 
            ;; 
            ;; replace the following line with your own function(s) 
            ;; 
            (* 0.5 current-sample) 
  ))))) 

;; define a dsp function for mono signals 
;; 
(defun dsp (sound) 
  (let (obj) 
    (setf obj (send dsp-class :new sound)) 
    (snd-fromobject (snd-t0 sound) (snd-srate sound) obj))) 

;; add automatic handling of mono/stereo tracks. Processes both stereo 
;; channels one after the other. To process both channels simultaneously 
;; or whole blocks of samples the object code above needs to be rewritten. 
;; 
(if (arrayp s) 
  (vector 
    (dsp (aref s 0)) 
    (dsp (aref s 1))) 
  (dsp s))
As I said, I personally can't program in Nyquist/Lisp but using the above code it should be straightforward. Here I'll attempt to outline the code structure I have in mind for the "inversed polarity fixer". Being inexperienced with the language I may make mistakes, so correct me where I'm wrong.

Current-sample is a function that returns the numerical value of the current sample, that is the y which I used in my previous posts. We also need a symbol (Lisp-variable, right?) that holds the y-value of the sample just before the one in current-sample, because both values are required to calculate the Δy which is used to identify a clipping area by its large slope. I'll call this symbol previoussample and I suggest to copy current-sample's value to previoussample right before switching to the next sample routine.

Image

Here's the code structure I have in my head, but it may be quite wrong:

Code: Select all

1 WHILE currentsample
2     IF (currentsample-previoussample) < limit
3         SET peakheight = previoussample
4         WHILE (currentsample-previoussample) < -limit
5           SET currentsample = peakheight
6           NEXTSAMPLE... somehow
7         ENDWHILE
8     ELSE
9         return the sample unmodified
10        NEXTSAMPLE
11    ENDIF
12 ENDWHILE
Running you through it:
1 and 12 ensure the routine is carried out from the first to the last selected audio sample.
2 checks the Δy which I mentioned before, used to find a large negative slope (limit being a negative, user-set variable). If more negative than limit the modification code from 3 to 7 is run, otherwise (8) nothing is done and we go on with the next sample (9).
3 writes the y-value of the last 'normal' sample (see picture above) to a storage symbol because it will be used to set the next samples, thus creating the straight red line (see picture in previous post). This value comes from previoussample but must be stored because previoussample will changes value when proceeding to the next sample.
4-7 sets the negative clipping samples to the highest normal sample known, thus creating a 'normal' positive clipping line (the red line) that can be dealt with by regular clip fixing plugins. When currentsample-previoussample (the Δy) suddenly gets mighty positive, the end of the clip is reached and the action is stopped.

Questions:
How to tell the script to go to the NEXTSAMPLE, i.e. read a new sample value with current-sample? How does this fit in the Nyquist DSP framework code I cited above? Where in the code should the previoussample symbol be set? Can I also remember a sample's location and go back to it, instead of just going to the next... next...? The latter would allow an observation-first principle, where we first check if there's an end to the negative clipping area (with high slope) before changing any sample values, instead of rushing on as soon as we find the start and looking for the end on the way.

Some additional thoughts on user parameters.
limit: as seen in the pseudo-code. Any number from -2 to 0 that determines the required shift between two samples for the pair to be identified as starting point of a clip. It's -2 to 0 because Audacity waves are put on a scale between -1 and 1, making the maximum drop -2. Using a -2 limit means the plugin does nothing, using a 0 limit makes it go nuts.

maxduration: amount of samples that each negative clipping area is allowed to contain. In practice this defines the maximum amount of samples that the algorithm will look ahead to find the very high slope (marking the end of the clip) after having found the very low slope (start of the clip). Not implemented in the pseudo-code because this fits in the observation-first principle as described above.

Hopefully this all is specific enough for someone to work with, otherwise I may try doing it myself. But either way I'll need some advice though :)

steve
Site Admin
Posts: 80677
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Clipfix (see waveform pic) - plugins available?

Post by steve » Sun May 18, 2008 3:48 pm

Funny that, I was thinking of something very similar for a code structure.
(this is not in any real language. Lines beginning with # are comments)

Code: Select all

# $limit is set by our "sensitivity" slider
# $dif is the difference between the current sample and the next sample
# we initialise $dif to zero
$dif=0

# some code to grab first sample
$current = GET (sample)

# some code to grab the next sample and start our loop
# assuming that GET(sample) returns "false" at end of selection
WHILE $next = GET (sample)

# calculate difference ($dif) between next sample ($next) and the current one ($current)
$dif = $current - $next

# we want $dif as a positive number irrespective of whether it is a positive or negative going waveform
IF $dif < 0
$dif -= $dif
ENDIF

# compare $dif with $limit and change the sample if greater than our limit
IF $dif > $limit
$next = $current
# else set value for our new current sample
ELSE $current = $next
ENDIF

# end the loop
ENDWHILE 
Almost identical to your scheme except that it allows for both positive and negative going waveforms.

In fact, this would be much better if it could be included in the "clipfix.ny" script since really we want to repair the inverted section, not just set it to flat.
Arnie wrote:How to tell the script to go to the NEXTSAMPLE,
From a quick look at clipfix.ny it looks like Benjamin Schwartz (the author) has copied the track into an array, and then just steps through the array.

Other than playing with the Nyquist prompt in Audacity I've not done any Lisp/Nyquist programming either, but if you are interested in having a go I'll be happy to try and help. It's about time that I started doing a bit more with Nyquist. :)
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Post Reply