# What are good ways to detect signal clipping in a recording?

Given a recording I need to detect whether any clipping has occurred.

Can I safely conclude there was clipping if any (one) sample reaches the maximum sample value, or should I look for a series of subsequent samples at the maximum level?

The recording may be taken from 16 or 24-bit A/D converters, and are converted to floating point values ranging from $-1...1$. If this conversion takes the form of a division by $2^{15}-1$ or $2^{23}-1$, then presumably the negative peaks can be somewhat lower than -1, and samples with the value -1 are not clipped?

Obviously one can always create a signal specifically to defeat the clipping detection algorithm, but I'm looking at recordings of speech, music, sine waves or pink/white noise.

• Keep in mind that the clipping level is not always the digital maximum. If the analog circuitry (or even the analog side of the ADC) has a slightly lower clipping level than digital max, it will clip early. If it clips in analog and then passes through some filtering, it will not even be a straight line. Which scenarios do you need to detect? – endolith Aug 18 '11 at 22:10
• The recordings are made with a PC sounddevice (usually connected via USB). Mostly they are responses to a sweep or MLS stimulus and used to calculate a room impulse response. I do not control the hardware, so the clipping might even occur at the stimulus output. Didn't think of that earlier, but I'm glad you thought of it. – Han Aug 19 '11 at 14:29

I was in the middle of typing an answer pretty much exactly like Yoda's. He's is probably the most reliable but, I'll proposed a different solution so you have some options.

If you take a histogram of your signal, you will more than likely a bell or triangle like shape depending on the signal type. Clean signals will tend to follow this pattern. Many recording studios add a "loudness" effect that causes a little bump near the top, but it is still somewhat smooth looking. Here is an example from a real song from a major musician:

Here is the histogram of signal that Yoda gives in his answer:

And now the case of their being clipping:

This method can be fooled at times, but it is at least something to throw in your tool bag for situations that the FFT method doesn't seem to be working for you or is too many computations for your environment.

• That is a crazy awesome affect. Very interesting. – Kortuk Aug 18 '11 at 23:09
• I'm glad you suggested this method. I should have included it myself... – PearsonArtPhoto Aug 18 '11 at 23:43
• I say that specifically because this seems to be the most implementable method. It is an applied form of the other options given, but this looks like the "error" signal is much more clear. – Kortuk Aug 19 '11 at 1:03
• Might as well take the absolute value of the signal first and get a smoother one-sided histogram – endolith Aug 19 '11 at 4:03
• My fingers are itching to try this out on my signals. Thanks. – Han Aug 19 '11 at 14:48

The simplest answer if you're dealing with short recordings is to listen to it and detect "pops" (short spiked sound) in the playback. However, a more robust solution is to analyze the frequency spectrum of the recording.

Recall that when a signal gets clipped at some threshold, it locally resembles a square wave in the clipped regions. This introduces higher harmonics in the frequency spectrum which would not have been there originally. If your signal is bandlimited (most real world signals are) and you're sampling well above the Nyquist rate, then this stands out quite clear as day.

Here's a short example in MATLAB demonstrating this. Here, I create a bandlimited signal of 1s duration, sampled at 1000Hz, and then clip it to between ±0.8 (see the top plot in the figure below)

time = 0:0.001:1;
cleanSignal = sin(2*pi*75*time).*chirp(time,50,1,200);
clippedSignal = min(abs(cleanSignal),0.8).*sign(cleanSignal);


You can clearly see that the frequency spectrum of the original, unclipped waveform is clean and goes to zero outside the bandwidth (bottom left), whereas in the clipped signal, there is a general minor distortion of the spectrum (expected if clipped) and most importantly, higher harmonics/spikes/non-zero contributions in the spectrum outside the bandwidth of the signal (bottom right).

This might generally be a better approach, because detecting clipping by looking at the values is generally not accurate unless if you designed the equipment yourself and know precisely the value of the threshold.

• Some of my signals (particularly the MLS) go right up to the Nyquist frequency. So this method is probably not always applicable for me. – Han Aug 19 '11 at 14:43
• @yoda With the spectrums in hand, how then does one tell that a spectrum is 'dirty' as you have indicated? What test can one perform? – Spacey Mar 21 '12 at 4:07

A bit of this depends on the method of record. It sounds like you are using only 1 convertor, which simplifies things somewhat.

You should look for anything above some threshold, and specifically for more than one point next to each other. Typically, A/D convertors don't actually read to their max value unless you test it very exactly, so realize that the max value might be lower than seems possible.

Given your parameters, I would look for consecutive signals above .98 or below -.98, with some tweaking to determine what the optimal threshold should be (I wouldn't bring it below .9). It might be wise to detect one at the max, and another close by over something like .8.

The reason to ignore 1 specific measurement is that it is common for spikes to occur that have nothing to do with the signal at all. This will be reduced in the case that you are using one known good A/D convertor. It is likely that if you are using an array of detectors, or an image, that some of the detectors will be bad, potentially clipping frequently.

• Very practical advice here. Together with @Kellenjb's approach this should give me enough to get working on an implementation. – Han Aug 19 '11 at 14:58

MLS (maximum length sequences) are particularly tricky to analyze for clipping. Their crest factor (=peak/rms) is very close to 1, which is even three dB smaller than that of a sine wave. Many D/A converters are designed to take a sine wave as the worst case and an MLS played at full amplitude can easily clip the output interpolation circuit of a D/A.

The next problem is that a clipped MLS looks almost identical to a non clipped one since the amplitudes are almost all +-peak in the first place. Also the PDF analysis doesn't work since the PDF of an MLS is simply two large peaks at the edges.

In a typical room impulse response measurement, the most likely clipping point is actually the D/A, the amp, or the speaker. Once it got through the room it looks a lot less like an MLS and therefor it's easier to assess clipping with the methods described above.

In nearly all acoustic measurements the noise floor is determined by the self noise of the microphone or background noises and not the A/D. Therefore it is not very important to optimize the input gain into the A/D and leaving ample head room before clipping (10dB or so) is perfectly fine.

It's typically a good idea to measure with a a number of different excitation levels and look at the SNR of the measurement. At low levels the acoustic background noise dominates and at high levels something will limit, compress or clip. The trick to making a good measurement is to find a good spot in between.