User talk:SebastianG: Difference between revisions

From Hydrogenaudio Knowledgebase
(New page: == Introduction == Noise shaping is a technique that is often used in combination with dithering in the context of quantization. The purpose of noise shaping is...)
 
No edit summary
Line 1: Line 1:
== Introduction ==
= Introduction =


Noise shaping is a technique that is often used in combination with
Noise shaping is a technique that is often used in combination with
Line 8: Line 8:
that the overall noise becomes less perceivable.
that the overall noise becomes less perceivable.


== Limits ==
= Applications =
 
Noise Shaping without [[Dithering|''dithered'']] quantization is also possible but
less useful because the spectral characteristics of ''undithered''
quantization noise is not easily predictable and depends heavily on the
digital signal that is to be quantized.
 
It's not possible to lower the noise power for ''all'' frequencies
via noise shaping because this would be in violation of the
rate/distortion principles. But it ''is'' possible to lower the noise
in certain frequency bands at the cost of increased noise power in other
frequency bands. This property is also known as the
''noise shaping theorem''. It states that the average logarithmic magnitude response of
the corresponding noise shaping ''filter'' over the whole linear
frequency axis cannot drop below 0dB.
 
== Applications ==


In the field of digital image processing a popular noise shaping
In the field of digital image processing a popular noise shaping
Line 46: Line 30:
frequencies above 100 kHz).
frequencies above 100 kHz).


== Filter design restrictions ==
= Limits & filter design restrictions =
 
Noise Shaping without [[Dithering|''dithered'']] quantization is also possible but
less useful because the spectral characteristics of ''undithered''
quantization noise is not easily predictable and depends heavily on the
digital signal that is to be quantized.
 
It's not possible to lower the noise power for ''all'' frequencies
via noise shaping because this would be in violation of the
rate/distortion principles. But it ''is'' possible to lower the noise
in certain frequency bands at the cost of increased noise power in other
frequency bands. This property is also known as the
''noise shaping theorem''. It states that the average logarithmic magnitude response of
the corresponding noise shaping ''filter'' over the whole linear
frequency axis cannot drop below 0dB.


The noise shaping application poses some restrictions on the fiter
The noise shaping application poses some restrictions on the fiter
Line 62: Line 60:
covered by this article.
covered by this article.


== Digital filter reminder ==
= Digital filter reminder =


A common representation of digital filters is their ''transfer function''
A common representation of digital filters is their ''transfer function''
Line 73: Line 71:
where 'N' is the transfer function and 'm' the filter's ''order''.
where 'N' is the transfer function and 'm' the filter's ''order''.


=== FIR filters ===
== FIR filters ==


In case a1=a2=a3=...=0 the filter's impulse response is finite which is
In case a1=a2=a3=...=0 the filter's impulse response is finite which is
Line 104: Line 102:
to a multiplication in the Z-domain.
to a multiplication in the Z-domain.


=== IIR filters ===
== IIR filters ==


IIR filters are now a simple extension to the FIR case:
IIR filters are now a simple extension to the FIR case:
Line 121: Line 119:


which is the well known ''difference formula''. Its signal flow graph
which is the well known ''difference formula''. Its signal flow graph
representation is known as ''direct form I''. See
representation is known as
<http://ccrma.stanford.edu/~jos/filters/Direct_Form_I.html>
[http://ccrma.stanford.edu/~jos/filters/Direct_Form_I.html direct form I].
Now, let's see how we can apply this kind of filter to quantization
Now, let's see how we can apply this kind of filter to quantization
errors...
errors...


== Noise Shaper Implementation ==
= Noise Shaper Implementation =


Usually a filter's transfer function is called 'H'. In this article 'N'
Usually a filter's transfer function is called 'H'. In this article 'N'
Line 133: Line 131:
'H' is introduced that has a special relation to 'N'.
'H' is introduced that has a special relation to 'N'.


=== Direct Form I ===
== Direct Form I ==


For convenience let us substitute the actual difference in the
For convenience let us substitute the actual difference in the
Line 167: Line 165:
y[] & x[] upto some amount of samples determined by the filter order.
y[] & x[] upto some amount of samples determined by the filter order.


=== Decoupling filtering and quantization ===
== Decoupling filtering and quantization ==


[[Image:Noiseshaper3.png|thumb|right|200px]]
As mentioned, the previous example is ''one possible'' implementation.
As mentioned, the previous example is ''one possible'' implementation.
It's not the smartest one. But it's hopefully easy to understand.
It's not the smartest one. But it's hopefully easy to understand.
Line 203: Line 202:
  (4) last_error = quantized[k] - wanted[k]
  (4) last_error = quantized[k] - wanted[k]


which is equivalent to the following signal flow graph where the unit
which is equivalent to the shown signal flow graph where the unit
sample delay z^-1 is implemented by the state variable 'last_error':
sample delay z^-1 is implemented by the state variable 'last_error':
<link to the signal flow graph I drew>
Remember that this black box filter H(z) doesn't correspond the the
Remember that this black box filter H(z) doesn't correspond the the
actual noise shaping filter N(z). Depending on the context published
actual noise shaping filter N(z). Depending on the context published
Line 213: Line 209:
-H(z).
-H(z).


== Example filters ==
= Example filters =


The following two filters have been designed by Sebastian Gesemann
The following two filters have been designed by Sebastian Gesemann
via non-linear optimization to match a slightly modified version of
via non-linear optimization to match a slightly modified version of
the LAME MP3 encoder's ATH (absolute threshold of hearing) curve. They
the LAME MP3 encoder's ATH (absolute threshold of hearing) curve. They
were first published in the Hydrogenaudio forum including the noise
were first published in the
transfer function's second order factorizations.
[http://www.hydrogenaudio.org/forums/index.php?showtopic=47980&pid=551558&st=0 Hydrogenaudio forum]
 
including the noise transfer function's second order factorizations.
    <link to the "build your own noise shapers" thread>
 


=== Sampling rate of 44100 Hz, derived from LAME's ATH formula ===
== Sampling rate of 44100 Hz, derived from LAME's ATH formula ==


The noise transfer function is given by
The noise transfer function is given by
Line 234: Line 228:
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}</math>
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}</math>


=== Sampling rate of 48000 Hz, derived from LAME's ATH formula ===
== Sampling rate of 48000 Hz, derived from LAME's ATH formula ==


The noise transfer function is given by
The noise transfer function is given by

Revision as of 09:59, 14 March 2008

Introduction

Noise shaping is a technique that is often used in combination with dithering in the context of quantization. The purpose of noise shaping is to alter the spectral shape of the error that is introduced due to dithering and quantization. Sometimes it may be desirable to concentrate most of the noise power in a certain frequency band so that the overall noise becomes less perceivable.

Applications

In the field of digital image processing a popular noise shaping technique is known as "Floyd Steinberg dithering". This algorithm is used to reduce the noise power at lower spatial frequencies which results in an improved perceptual quality.

For word length reductions of audio signals (i.e conversion from a 24bit signal to a 16bit signal) noise shaping can be used to push quantization noise out of perceptually important frequency bands to perceptually less important frequency bands (>20 kHz) in order to reduce the perceived loudness of the quantization noise.

Another example is the Super Audio CD. The SACD format represents the most extreme trade-off between word length (1bit/sample) and sampling frequency (2.8 Mhz). Due to the low word length high power noise is introduced. With the help of noise shaping this noise is usually rejected by about 120 dB in the audible band (0-20 kHz) at the cost of a higher noise power in the ultrasonic range (+3.5dB for frequencies above 100 kHz).

Limits & filter design restrictions

Noise Shaping without dithered quantization is also possible but less useful because the spectral characteristics of undithered quantization noise is not easily predictable and depends heavily on the digital signal that is to be quantized.

It's not possible to lower the noise power for all frequencies via noise shaping because this would be in violation of the rate/distortion principles. But it is possible to lower the noise in certain frequency bands at the cost of increased noise power in other frequency bands. This property is also known as the noise shaping theorem. It states that the average logarithmic magnitude response of the corresponding noise shaping filter over the whole linear frequency axis cannot drop below 0dB.

The noise shaping application poses some restrictions on the fiter design. The first sample of the filter's impulse response always equals one. Also, interesting filters for noise shaping are so-called minimum phase filters. This type of filter performs optimal in the sense of the noise shaping theorem because the average logarithmig magnitude response is 0dB.

For the special SACD case an additional constraint comes into play: The magnitude response should not exceed 3.5dB. This is enforced to prevent so-called limit cycles.

The design of these kinds of filters is however not trivial. This is not covered by this article.

Digital filter reminder

A common representation of digital filters is their transfer function in form of a rational polynomial in z^-1. These polynomials are defined by their coefficients b0,b1,b2,...,a1,a2,...:

where 'N' is the transfer function and 'm' the filter's order.

FIR filters

In case a1=a2=a3=...=0 the filter's impulse response is finite which is why these filters are called FIR (finite impulse response) filters as opposed to IIR infinite impulse response filters. Note that in case of FIR filters the coefficients b0, b1, ... correspond to the impulse response. The impulse response is another way a filter can be characterized and describes what the filter does to a unit pulse. Let's assume for the moment that a1=a2=a3=...=0. The the impulse response would be:

x:  0  0  1  0  0  0  0  ... 0  0  0 ... (input signal)
y:  0  0  b0 b1 b2 b3 b4 ... 0  0  0 ... (output signal)

It helps to think of negative powers of z in transfer functions as a delay: z^-1 = delay of one sample. z^-2 = delay of two samples etc. Since a digital filter is a linear and time-invariant system (LTI) it is possible to determine the filter's output by a superposition of scaled and shifted impulse responses. This leads to the following relationship between input and output in the case of FIR filters:

time domain                                     Z-transform
-----------                                     -----------
y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(z) = X(z) * B(z)

where x[] is the input signal, y[] the output signal and k the current sample index. This is a simple convolution directly computed using the given impulse response. The convolution in the time domain corresponds to a multiplication in the Z-domain.

IIR filters

IIR filters are now a simple extension to the FIR case:

time domain                                Z-transform
-----------                                -----------
     y[k] + a1 y[k-1] + a2 y[k-2] + ...    Y(z) * A(Z) = X(Z) * B(Z)
= b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...

Solving for Y we get

time domain                                     Z-transform
-----------                                     -----------
y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(Z) = X(Z) * N(z)
               - a1 y[k-1] - a2 y[k-2] + ...

which is the well known difference formula. Its signal flow graph representation is known as direct form I. Now, let's see how we can apply this kind of filter to quantization errors...

Noise Shaper Implementation

Usually a filter's transfer function is called 'H'. In this article 'N' has been used because this filter is also known as noise transfer function (NTF) in the context of noise shaping. Later, another filter 'H' is introduced that has a special relation to 'N'.

Direct Form I

For convenience let us substitute the actual difference in the difference formula for 'u'. Also, b0 must be equal to one for noise shaping filters as mentioned above. This leads to

y[k] = x[k] + u[k]   with u[k] = b1 x[k-1] + b2 x[k-2] + ...
                               - a1 y[k-1] - a2 y[k-2] + ...

Note that since the filter is supposed to be applied on the quantization noise y[] corresponds to the filtered noise and x[] to the unfiltered noise. Suppose s[] is the signal we're trying to quantize. The following pseudo code snippet shows the inner loop over k of one possible noise shaping implementation:

(1) compute u[k]
(2) wanted[k] = s[k] + u[k]
(3) quantized[k] = quantize( wanted[k] + dither() )
(4) y[k] = quantized[k] - s[k]
(5) x[k] = y[k] - u[k]

That's about it. It may not be obvious at first, though. Take a closer look at what happens at step 3. Note that the following is true

time domain                          Z-transform
-----------                          -----------
quantized[k] = s[k] + u[k] + x[k]    Q(z) = S(z) + X(z) * N(z)
             = s[k] + y[k]                = S(z) + Y(z)

u[k], wanted[k] are temporary variables. The values are not needed outside this iteration, so you don't need to remember these. But we do need to remember the past filtered and unfiltered error samples y[] & x[] upto some amount of samples determined by the filter order.

Decoupling filtering and quantization

As mentioned, the previous example is one possible implementation. It's not the smartest one. But it's hopefully easy to understand. There's another common formulation. We now require some more transfer function fiddeling. First, we add one and subtract one again:

Since now the two fractions have a common denominator we can combine them easily to one fraction. Also, let's factor z^-1 out of the fraction:

The last thing we did is writing H(z) for z*(A(Z)-B(Z)). Note that with b0=1 it follows that z*(1-b0)=0. So, H(z) can be written as

with for . H(z) is some arbitrary filter. The big advantage now is that it is possible to plug nearly any digital filter implementation for H(z) into the noise shaping loop. One obvious approach is the difference formula from above but there are other alternatives with desirable properties. However, the purpose of this article is not to discuss digital filters in general but their application as noise shaping filters. So, throughout the rest of this article H(z) is simply treated as a black box. This black box has a certain state and gives us an output sample for each input sample. The modified pseudo code loop looks like this:

(1) h_out[k] = H.filter(last_error)
(2) wanted[k] = s[k] - h_out[k]
(3) quantized[k] = quantize( wanted[k] + dither() )
(4) last_error = quantized[k] - wanted[k]

which is equivalent to the shown signal flow graph where the unit sample delay z^-1 is implemented by the state variable 'last_error': Remember that this black box filter H(z) doesn't correspond the the actual noise shaping filter N(z). Depending on the context published coefficients for noise shaping correspond either to N(z), H(z), or -H(z).

Example filters

The following two filters have been designed by Sebastian Gesemann via non-linear optimization to match a slightly modified version of the LAME MP3 encoder's ATH (absolute threshold of hearing) curve. They were first published in the Hydrogenaudio forum including the noise transfer function's second order factorizations.

Sampling rate of 44100 Hz, derived from LAME's ATH formula

The noise transfer function is given by

The corresponding filter H is given by

Sampling rate of 48000 Hz, derived from LAME's ATH formula

The noise transfer function is given by

The corresponding filter H is given by