Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Embedded Systems

Real-Time Sound Processing


Dr. Dobb's Journal October 1998: Compensating for Poor Hardware Clipping

Dr. Dobb's Journal October 1998

Compensating for Poor Hardware Clipping


Analog-to-digital converters map a voltage linearly into a binary number. For example, they may map -1 volt to -32768 and +1 volt to 32767; see Figure 1. For signals within the (+1/-1 volt range, this works great. What happens if the signal exceeds +1 volt, as in Figure 2? Nothing good, I assure you. This situation is called "clipping," and there are several ways to handle it. For digital audio recording, limiting the signal to a maximum or minimum value is best, as in Figure 3. The signal is slightly distorted, but otherwise fine. Unfortunately, some Macs and Windows sound cards do the unexpected -- they modify the signal as in Figure 4, inverting and then limiting the signal at peak amplitudes. This distorts the signal severely, making for unpleasant playback.

Figure 1: Signals within the digitizer's range are converted to digital form.

Figure 2: What value should be given to samples outside the expressible range?

Figure 3: Limiting the signal to maximum and minimum values is a reasonable approach.

Figure 4: Some sound cards invert and limit the signal, which introduces strong distortions.

Detecting Distortion

To compensate for this, I have developed an algorithm (available electronically) that detects the distortion of Figure 4 and transforms it into the gentler signal of Figure 3. Basically you examine the difference between a sample and its predecessor. If the magnitude of the difference exceeds a certain threshold (indicating that the signal has been inverted), you negate all subsequent samples until you detect another inversion.

Because it has to examine every sample, the first implementation of the algorithm is unacceptably slow. However, by splitting the main processing loop into two cases (inverting and not inverting) and simplifying the test for possible signal inversion to the sign of the result of an exclusive-or operation (also available electronically), a dramatic speedup (over 10 times) can be achieved. Optimizations like this are typical in real-time code.

-- R.C.

Back to Article


Copyright © 1998, Dr. Dobb's Journal

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.