<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.hydrogenaudio.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=SebastianG</id>
	<title>Hydrogenaudio Knowledgebase - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.hydrogenaudio.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=SebastianG"/>
	<link rel="alternate" type="text/html" href="https://wiki.hydrogenaudio.org/index.php?title=Special:Contributions/SebastianG"/>
	<updated>2026-04-30T22:35:44Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.44.2</generator>
	<entry>
		<id>https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=19030</id>
		<title>Noise shaping</title>
		<link rel="alternate" type="text/html" href="https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=19030"/>
		<updated>2008-08-20T13:05:17Z</updated>

		<summary type="html">&lt;p&gt;SebastianG: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Noise shaping is a technique that is often used in combination with&lt;br /&gt;
[[Dithering|dithering]] in the context of [[Quantize|quantization]]. The purpose of noise shaping&lt;br /&gt;
is to alter the spectral &#039;&#039;shape&#039;&#039; of the error that is introduced due&lt;br /&gt;
to [[Dithering|dithering]] and [[Quantize|quantization]]. Sometimes it may be desirable to&lt;br /&gt;
concentrate most of the noise power in a certain frequency band so&lt;br /&gt;
that the overall noise becomes less perceivable.&lt;br /&gt;
&lt;br /&gt;
== Applications ==&lt;br /&gt;
&lt;br /&gt;
In the field of digital image processing a popular noise shaping&lt;br /&gt;
technique is known as &amp;quot;Floyd Steinberg dithering&amp;quot;. This algorithm is&lt;br /&gt;
used to reduce the noise power at lower spatial frequencies which&lt;br /&gt;
results in an improved perceptual quality.&lt;br /&gt;
&lt;br /&gt;
For word length reductions of audio signals (i.e conversion from a 24bit&lt;br /&gt;
signal to a 16bit signal) noise shaping can be used to push quantization&lt;br /&gt;
noise out of perceptually important frequency bands to perceptually less&lt;br /&gt;
important frequency bands (&amp;gt;20 kHz) in order to reduce the perceived&lt;br /&gt;
loudness of the quantization noise.&lt;br /&gt;
&lt;br /&gt;
Another example is the &lt;br /&gt;
[[Super Audio Compact Disc|Super Audio CD]]. The SACD format represents&lt;br /&gt;
the most extreme trade-off between word length (1bit/sample) and&lt;br /&gt;
sampling frequency (2.8 Mhz). Due to the low word length high power&lt;br /&gt;
noise is introduced. With the help of noise shaping this noise is&lt;br /&gt;
usually rejected by about 120 dB in the audible band (0-20 kHz) at the&lt;br /&gt;
cost of a higher noise power in the ultrasonic range (+3.5dB for&lt;br /&gt;
frequencies above 100 kHz).&lt;br /&gt;
&lt;br /&gt;
== Limits &amp;amp; filter design restrictions ==&lt;br /&gt;
&lt;br /&gt;
Noise Shaping without [[Dithering|&#039;&#039;dithered&#039;&#039;]] quantization is also possible but&lt;br /&gt;
less useful because the spectral characteristics of &#039;&#039;undithered&#039;&#039;&lt;br /&gt;
quantization noise is not easily predictable and depends heavily on the&lt;br /&gt;
digital signal that is to be quantized. The SACD case is an example where&lt;br /&gt;
[[Dithering|dithering]] can&#039;t be properly applied which may lead to a spectral&lt;br /&gt;
noise power distribution that deviates from the indented distribution. This has&lt;br /&gt;
been demonstrated by Lipshitz and Vanderkooy in&lt;br /&gt;
[http://www.sjeng.org/ftp/SACD.pdf their] [1] paper. &lt;br /&gt;
&lt;br /&gt;
It&#039;s not possible to lower the noise power for &#039;&#039;all&#039;&#039; frequencies&lt;br /&gt;
via noise shaping because this would be in violation of the&lt;br /&gt;
rate/distortion principles. But it &#039;&#039;is&#039;&#039; possible to lower the noise&lt;br /&gt;
in certain frequency bands at the cost of increased noise power in other&lt;br /&gt;
frequency bands. This property is also known as the&lt;br /&gt;
&#039;&#039;noise shaping theorem&#039;&#039;. It states that the average logarithmic magnitude response of&lt;br /&gt;
the corresponding noise shaping &#039;&#039;filter&#039;&#039; over the whole linear&lt;br /&gt;
frequency axis cannot drop below 0dB.&lt;br /&gt;
&lt;br /&gt;
The noise shaping application poses some restrictions on the fiter&lt;br /&gt;
design. The first sample of the filter&#039;s impulse response always equals&lt;br /&gt;
one. Also, interesting filters for noise shaping are so-called&lt;br /&gt;
&#039;&#039;minimum phase filters&#039;&#039;. This type of filter performs optimal in the sense of&lt;br /&gt;
the noise shaping theorem because the average logarithmig magnitude&lt;br /&gt;
response &#039;&#039;is&#039;&#039; 0dB.&lt;br /&gt;
&lt;br /&gt;
For the special [[Super Audio Compact Disc|SACD]] case an additional&lt;br /&gt;
constraint comes into play: The magnitude response should not exceed&lt;br /&gt;
3.5dB. This is enforced to prevent so-called limit cycles.&lt;br /&gt;
&lt;br /&gt;
The design of these kinds of filters is however not trivial. This is not&lt;br /&gt;
covered by this article.&lt;br /&gt;
&lt;br /&gt;
== Digital filter reminder ==&lt;br /&gt;
&lt;br /&gt;
A common representation of digital filters is their &#039;&#039;transfer function&#039;&#039;&lt;br /&gt;
in form of a rational polynomial in z^-1. These polynomials are defined&lt;br /&gt;
by their coefficients b0,b1,b2,...,a1,a2,...:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{B(z)}{A(z)} = \frac{b_0 + b_1 z^{-1} + b_2 z^{-2} + \ldots + b_m z^{-m}}&lt;br /&gt;
{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots + a_m z^{-m}} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &#039;N&#039; is the transfer function and &#039;m&#039; the filter&#039;s &#039;&#039;order&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
=== FIR filters ===&lt;br /&gt;
&lt;br /&gt;
In case a1=a2=a3=...=0 the filter&#039;s [http://en.wikipedia.org/wiki/Impulse_response impulse response] is finite which is&lt;br /&gt;
why these filters are called FIR ([[Finite Impulse Response Filter|finite impulse response]])&lt;br /&gt;
filters as opposed to IIR&lt;br /&gt;
[[Infinite Impulse Response Filter|infinite impulse response]] filters. Note that in case of&lt;br /&gt;
FIR filters the coefficients b0, b1, ... correspond to the impulse&lt;br /&gt;
response. The impulse response is another way a filter can be&lt;br /&gt;
characterized and describes what the filter does to a unit pulse. Let&#039;s&lt;br /&gt;
assume for the moment that a1=a2=a3=...=0. The the impulse response&lt;br /&gt;
would be:&lt;br /&gt;
&lt;br /&gt;
 x:  0  0  1  0  0  0  0  ... 0  0  0 ... (input signal)&lt;br /&gt;
 y:  0  0  b0 b1 b2 b3 b4 ... 0  0  0 ... (output signal)&lt;br /&gt;
&lt;br /&gt;
It helps to think of negative powers of z in transfer functions as a&lt;br /&gt;
delay: z^-1 = delay of one sample. z^-2 = delay of two samples etc.&lt;br /&gt;
Since a digital filter is a&lt;br /&gt;
[http://en.wikipedia.org/wiki/Discrete_system discrete-time]&lt;br /&gt;
[http://en.wikipedia.org/wiki/LTI_system_theory linear and time-invariant]&lt;br /&gt;
system it is&lt;br /&gt;
possible to determine the filter&#039;s output by a superposition of scaled&lt;br /&gt;
and shifted impulse responses. This leads to the following relationship&lt;br /&gt;
between input and output in the case of FIR filters:&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(z) = X(z) * B(z)&lt;br /&gt;
&lt;br /&gt;
where x[] is the input signal, y[] the output signal and k the current&lt;br /&gt;
sample index. This is a simple convolution directly computed using the&lt;br /&gt;
given impulse response. The convolution in the time domain corresponds&lt;br /&gt;
to a multiplication in the Z-domain.&lt;br /&gt;
&lt;br /&gt;
=== IIR filters ===&lt;br /&gt;
&lt;br /&gt;
IIR filters are a simple extension to the FIR case:&lt;br /&gt;
&lt;br /&gt;
 time domain                                Z-transform&lt;br /&gt;
 -----------                                -----------&lt;br /&gt;
      y[k] + a1 y[k-1] + a2 y[k-2] + ...    Y(z) * A(Z) = X(Z) * B(Z)&lt;br /&gt;
 = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Solving for Y we get&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(Z) = X(Z) * N(z)&lt;br /&gt;
                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
which is the well known &#039;&#039;difference formula&#039;&#039;. Its signal flow graph&lt;br /&gt;
representation is known as&lt;br /&gt;
[http://ccrma.stanford.edu/~jos/filters/Direct_Form_I.html direct form I].&lt;br /&gt;
&lt;br /&gt;
== Noise Shaper Implementation ==&lt;br /&gt;
&lt;br /&gt;
Usually a filter&#039;s transfer function is called &#039;H&#039;. In this article &#039;N&#039;&lt;br /&gt;
has been used because this filter is also known as noise transfer&lt;br /&gt;
function (NTF) in the context of noise shaping. Later, another filter&lt;br /&gt;
&#039;H&#039; is introduced that has a special relation to &#039;N&#039;.&lt;br /&gt;
&lt;br /&gt;
=== Direct Form I ===&lt;br /&gt;
&lt;br /&gt;
For convenience let us substitute the actual difference in the&lt;br /&gt;
difference formula for &#039;u&#039;. Also, b0 must be equal to one for noise&lt;br /&gt;
shaping filters as mentioned above. This leads to&lt;br /&gt;
&lt;br /&gt;
 y[k] = x[k] + u[k]   with u[k] = b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
                                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Note that since the filter is supposed to be applied on the quantization&lt;br /&gt;
noise y[] corresponds to the filtered noise and x[] to the unfiltered&lt;br /&gt;
noise. Suppose s[] is the signal we&#039;re trying to quantize. The following&lt;br /&gt;
pseudo code snippet shows the inner loop over k of &#039;&#039;one possible&#039;&#039;&lt;br /&gt;
noise shaping implementation:&lt;br /&gt;
&lt;br /&gt;
 (1) compute u[k]&lt;br /&gt;
 (2) wanted[k] = s[k] + u[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) y[k] = quantized[k] - s[k]&lt;br /&gt;
 (5) x[k] = y[k] - u[k]&lt;br /&gt;
&lt;br /&gt;
Note that the following is true&lt;br /&gt;
&lt;br /&gt;
 time domain                          Z-transform&lt;br /&gt;
 -----------                          -----------&lt;br /&gt;
 quantized[k] = s[k] + u[k] + x[k]    Q(z) = S(z) + X(z) * N(z)&lt;br /&gt;
              = s[k] + y[k]                = S(z) + Y(z)&lt;br /&gt;
&lt;br /&gt;
u[k], wanted[k] are temporary variables. The values are not needed&lt;br /&gt;
outside this iteration. But the past filtered and unfiltered error&lt;br /&gt;
samples need to be remembered (y[] &amp;amp; x[]) upto some amount of samples&lt;br /&gt;
determined by the filter order.&lt;br /&gt;
&lt;br /&gt;
=== Decoupling filtering and quantization ===&lt;br /&gt;
&lt;br /&gt;
[[Image:Noiseshaper3.png|thumb|right|200px]]&lt;br /&gt;
As mentioned, the previous example is &#039;&#039;one possible&#039;&#039; implementation.&lt;br /&gt;
It&#039;s not the best possible one. But it is the first version that comes to mind.&lt;br /&gt;
There&#039;s another common formulation which requires&lt;br /&gt;
some more transfer function fiddeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt; N(z) = \frac{B(z)}{A(z)} = 1 - \frac{A(z)}{A(z)} + \frac{B(z)}{A(z)}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The two fractions have a common denominator so they can be easily&lt;br /&gt;
combined to one fraction. It&#039;s also possible to factor z^-1 out of the fraction:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = 1 - \frac{A(z)-B(z)}{A(z)} = 1 - z^{-1} \frac{z ( A(z)-B(z) )}{A(z)} =: 1 - z^{-1} H(z)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Substituting z*(A(Z)-B(Z)) for H(z) leads to a new filter that is related to N.&lt;br /&gt;
Note that with b0=1 it follows that z*(1-b0)=0. So, H(z) can be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{c_0 + c_1 z^{-1} + c_2 z^{-2} + \ldots}{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
with &amp;lt;math&amp;gt;c_{k-1} = a_k - b_k&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;k = 1...m&amp;lt;/math&amp;gt;. H(z)&lt;br /&gt;
is some &#039;&#039;arbitrary&#039;&#039; filter. The big advantage now is that it is&lt;br /&gt;
possible to plug nearly any digital filter implementation for H(z) into&lt;br /&gt;
the noise shaping loop. One obvious approach is the difference formula&lt;br /&gt;
from above but there are other alternatives with desirable properties.&lt;br /&gt;
However, the purpose of this article is not to discuss digital filters&lt;br /&gt;
in general but their application as noise shaping filters. So,&lt;br /&gt;
throughout the rest of this article H(z) is simply treated as a black&lt;br /&gt;
box. This black box has a certain state and gives us an output sample&lt;br /&gt;
for each input sample. The modified pseudo code loop looks like this:&lt;br /&gt;
&lt;br /&gt;
 (1) h_out[k] = H.filter(last_error)&lt;br /&gt;
 (2) wanted[k] = s[k] - h_out[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) last_error = quantized[k] - wanted[k]&lt;br /&gt;
&lt;br /&gt;
which is equivalent to the shown signal flow graph where the unit&lt;br /&gt;
sample delay z^-1 is implemented by the state variable &#039;last_error&#039;:&lt;br /&gt;
Remember that this black box filter H(z) doesn&#039;t correspond the the&lt;br /&gt;
actual noise shaping filter N(z). Depending on the context published&lt;br /&gt;
coefficients for noise shaping correspond either to N(z), H(z), or&lt;br /&gt;
-H(z).&lt;br /&gt;
&lt;br /&gt;
== Example filters ==&lt;br /&gt;
&lt;br /&gt;
The following two filters have been designed by Sebastian Gesemann&lt;br /&gt;
via non-linear optimization to match a slightly modified version of&lt;br /&gt;
the LAME MP3 encoder&#039;s ATH (absolute threshold of hearing) curve. They&lt;br /&gt;
were first published in the&lt;br /&gt;
[http://www.hydrogenaudio.org/forums/index.php?showtopic=47980&amp;amp;pid=551558&amp;amp;st=0 Hydrogenaudio forum]&lt;br /&gt;
including the noise transfer function&#039;s second order factorizations.&lt;br /&gt;
&lt;br /&gt;
=== Sampling rate of 44100 Hz, derived from LAME&#039;s ATH formula ===&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.1474 z^{-1} +0.5383 z^{-2} -0.3520 z^{-3} +0.3475 z^{-4}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2061 -0.4706 z^{-1} -0.2534 z^{-2} -0.6214 z^{-3}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sampling rate of 48000 Hz, derived from LAME&#039;s ATH formula ===&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.3344 z^{-1} +0.7455 z^{-2} -0.4602 z^{-3} +0.3463 z^{-4}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2374 -0.7339 z^{-1} -0.1251 z^{-2} -0.6033 z^{-3}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
==References== &lt;br /&gt;
* Lipshitz. P, Stanley and John Vanderkooy. &amp;quot;Why 1-bit Sigma-Delta Conversion is Unsuitable for High-Quality Applications&amp;quot;. 110th Convention.  &amp;lt;i&amp;gt;Audio Engineering Society&amp;lt;/i&amp;gt; (May 2001). 2-4.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Signal Processing]]&lt;/div&gt;</summary>
		<author><name>SebastianG</name></author>
	</entry>
	<entry>
		<id>https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=19029</id>
		<title>Noise shaping</title>
		<link rel="alternate" type="text/html" href="https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=19029"/>
		<updated>2008-08-20T13:04:50Z</updated>

		<summary type="html">&lt;p&gt;SebastianG: /* Notes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Noise shaping is a technique that is often used in combination with&lt;br /&gt;
[[Dithering|dithering]] in the context of [[Quantize|quantization]]. The purpose of noise shaping&lt;br /&gt;
is to alter the spectral &#039;&#039;shape&#039;&#039; of the error that is introduced due&lt;br /&gt;
to [[Dithering|dithering]] and [[Quantize|quantization]]. Sometimes it may be desirable to&lt;br /&gt;
concentrate most of the noise power in a certain frequency band so&lt;br /&gt;
that the overall noise becomes less perceivable.&lt;br /&gt;
&lt;br /&gt;
== Applications ==&lt;br /&gt;
&lt;br /&gt;
In the field of digital image processing a popular noise shaping&lt;br /&gt;
technique is known as &amp;quot;Floyd Steinberg dithering&amp;quot;. This algorithm is&lt;br /&gt;
used to reduce the noise power at lower spatial frequencies which&lt;br /&gt;
results in an improved perceptual quality.&lt;br /&gt;
&lt;br /&gt;
For word length reductions of audio signals (i.e conversion from a 24bit&lt;br /&gt;
signal to a 16bit signal) noise shaping can be used to push quantization&lt;br /&gt;
noise out of perceptually important frequency bands to perceptually less&lt;br /&gt;
important frequency bands (&amp;gt;20 kHz) in order to reduce the perceived&lt;br /&gt;
loudness of the quantization noise.&lt;br /&gt;
&lt;br /&gt;
Another example is the &lt;br /&gt;
[[Super Audio Compact Disc|Super Audio CD]]. The SACD format represents&lt;br /&gt;
the most extreme trade-off between word length (1bit/sample) and&lt;br /&gt;
sampling frequency (2.8 Mhz). Due to the low word length high power&lt;br /&gt;
noise is introduced. With the help of noise shaping this noise is&lt;br /&gt;
usually rejected by about 120 dB in the audible band (0-20 kHz) at the&lt;br /&gt;
cost of a higher noise power in the ultrasonic range (+3.5dB for&lt;br /&gt;
frequencies above 100 kHz).&lt;br /&gt;
&lt;br /&gt;
== Limits &amp;amp; filter design restrictions ==&lt;br /&gt;
&lt;br /&gt;
Noise Shaping without [[Dithering|&#039;&#039;dithered&#039;&#039;]] quantization is also possible but&lt;br /&gt;
less useful because the spectral characteristics of &#039;&#039;undithered&#039;&#039;&lt;br /&gt;
quantization noise is not easily predictable and depends heavily on the&lt;br /&gt;
digital signal that is to be quantized. The SACD case is an example where&lt;br /&gt;
[[Dithering|dithering]] can&#039;t be properly applied which may lead to a spectral&lt;br /&gt;
noise power distribution that deviates from the indented distribution. This has&lt;br /&gt;
been demonstrated by Lipshitz and Vanderkooy in&lt;br /&gt;
[http://www.sjeng.org/ftp/SACD.pdf their] [1] paper. &lt;br /&gt;
&lt;br /&gt;
It&#039;s not possible to lower the noise power for &#039;&#039;all&#039;&#039; frequencies&lt;br /&gt;
via noise shaping because this would be in violation of the&lt;br /&gt;
rate/distortion principles. But it &#039;&#039;is&#039;&#039; possible to lower the noise&lt;br /&gt;
in certain frequency bands at the cost of increased noise power in other&lt;br /&gt;
frequency bands. This property is also known as the&lt;br /&gt;
&#039;&#039;noise shaping theorem&#039;&#039;. It states that the average logarithmic magnitude response of&lt;br /&gt;
the corresponding noise shaping &#039;&#039;filter&#039;&#039; over the whole linear&lt;br /&gt;
frequency axis cannot drop below 0dB.&lt;br /&gt;
&lt;br /&gt;
The noise shaping application poses some restrictions on the fiter&lt;br /&gt;
design. The first sample of the filter&#039;s impulse response always equals&lt;br /&gt;
one. Also, interesting filters for noise shaping are so-called&lt;br /&gt;
&#039;&#039;minimum phase filters&#039;&#039;. This type of filter performs optimal in the sense of&lt;br /&gt;
the noise shaping theorem because the average logarithmig magnitude&lt;br /&gt;
response &#039;&#039;is&#039;&#039; 0dB.&lt;br /&gt;
&lt;br /&gt;
For the special [[Super Audio Compact Disc|SACD]] case an additional&lt;br /&gt;
constraint comes into play: The magnitude response should not exceed&lt;br /&gt;
3.5dB. This is enforced to prevent so-called limit cycles.&lt;br /&gt;
&lt;br /&gt;
The design of these kinds of filters is however not trivial. This is not&lt;br /&gt;
covered by this article.&lt;br /&gt;
&lt;br /&gt;
== Digital filter reminder ==&lt;br /&gt;
&lt;br /&gt;
A common representation of digital filters is their &#039;&#039;transfer function&#039;&#039;&lt;br /&gt;
in form of a rational polynomial in z^-1. These polynomials are defined&lt;br /&gt;
by their coefficients b0,b1,b2,...,a1,a2,...:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{B(z)}{A(z)} = \frac{b_0 + b_1 z^{-1} + b_2 z^{-2} + \ldots + b_m z^{-m}}&lt;br /&gt;
{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots + a_m z^{-m}} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &#039;N&#039; is the transfer function and &#039;m&#039; the filter&#039;s &#039;&#039;order&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
=== FIR filters ===&lt;br /&gt;
&lt;br /&gt;
In case a1=a2=a3=...=0 the filter&#039;s [http://en.wikipedia.org/wiki/Impulse_response impulse response] is finite which is&lt;br /&gt;
why these filters are called FIR ([[Finite Impulse Response Filter|finite impulse response]])&lt;br /&gt;
filters as opposed to IIR&lt;br /&gt;
[[Infinite Impulse Response Filter|infinite impulse response]] filters. Note that in case of&lt;br /&gt;
FIR filters the coefficients b0, b1, ... correspond to the impulse&lt;br /&gt;
response. The impulse response is another way a filter can be&lt;br /&gt;
characterized and describes what the filter does to a unit pulse. Let&#039;s&lt;br /&gt;
assume for the moment that a1=a2=a3=...=0. The the impulse response&lt;br /&gt;
would be:&lt;br /&gt;
&lt;br /&gt;
 x:  0  0  1  0  0  0  0  ... 0  0  0 ... (input signal)&lt;br /&gt;
 y:  0  0  b0 b1 b2 b3 b4 ... 0  0  0 ... (output signal)&lt;br /&gt;
&lt;br /&gt;
It helps to think of negative powers of z in transfer functions as a&lt;br /&gt;
delay: z^-1 = delay of one sample. z^-2 = delay of two samples etc.&lt;br /&gt;
Since a digital filter is a&lt;br /&gt;
[http://en.wikipedia.org/wiki/Discrete_system discrete-time]&lt;br /&gt;
[http://en.wikipedia.org/wiki/LTI_system_theory linear and time-invariant]&lt;br /&gt;
system it is&lt;br /&gt;
possible to determine the filter&#039;s output by a superposition of scaled&lt;br /&gt;
and shifted impulse responses. This leads to the following relationship&lt;br /&gt;
between input and output in the case of FIR filters:&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(z) = X(z) * B(z)&lt;br /&gt;
&lt;br /&gt;
where x[] is the input signal, y[] the output signal and k the current&lt;br /&gt;
sample index. This is a simple convolution directly computed using the&lt;br /&gt;
given impulse response. The convolution in the time domain corresponds&lt;br /&gt;
to a multiplication in the Z-domain.&lt;br /&gt;
&lt;br /&gt;
=== IIR filters ===&lt;br /&gt;
&lt;br /&gt;
IIR filters are a simple extension to the FIR case:&lt;br /&gt;
&lt;br /&gt;
 time domain                                Z-transform&lt;br /&gt;
 -----------                                -----------&lt;br /&gt;
      y[k] + a1 y[k-1] + a2 y[k-2] + ...    Y(z) * A(Z) = X(Z) * B(Z)&lt;br /&gt;
 = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Solving for Y we get&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(Z) = X(Z) * N(z)&lt;br /&gt;
                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
which is the well known &#039;&#039;difference formula&#039;&#039;. Its signal flow graph&lt;br /&gt;
representation is known as&lt;br /&gt;
[http://ccrma.stanford.edu/~jos/filters/Direct_Form_I.html direct form I].&lt;br /&gt;
&lt;br /&gt;
== Noise Shaper Implementation ==&lt;br /&gt;
&lt;br /&gt;
Usually a filter&#039;s transfer function is called &#039;H&#039;. In this article &#039;N&#039;&lt;br /&gt;
has been used because this filter is also known as noise transfer&lt;br /&gt;
function (NTF) in the context of noise shaping. Later, another filter&lt;br /&gt;
&#039;H&#039; is introduced that has a special relation to &#039;N&#039;.&lt;br /&gt;
&lt;br /&gt;
=== Direct Form I ===&lt;br /&gt;
&lt;br /&gt;
For convenience let us substitute the actual difference in the&lt;br /&gt;
difference formula for &#039;u&#039;. Also, b0 must be equal to one for noise&lt;br /&gt;
shaping filters as mentioned above. This leads to&lt;br /&gt;
&lt;br /&gt;
 y[k] = x[k] + u[k]   with u[k] = b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
                                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Note that since the filter is supposed to be applied on the quantization&lt;br /&gt;
noise y[] corresponds to the filtered noise and x[] to the unfiltered&lt;br /&gt;
noise. Suppose s[] is the signal we&#039;re trying to quantize. The following&lt;br /&gt;
pseudo code snippet shows the inner loop over k of &#039;&#039;one possible&#039;&#039;&lt;br /&gt;
noise shaping implementation:&lt;br /&gt;
&lt;br /&gt;
 (1) compute u[k]&lt;br /&gt;
 (2) wanted[k] = s[k] + u[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) y[k] = quantized[k] - s[k]&lt;br /&gt;
 (5) x[k] = y[k] - u[k]&lt;br /&gt;
&lt;br /&gt;
Note that the following is true&lt;br /&gt;
&lt;br /&gt;
 time domain                          Z-transform&lt;br /&gt;
 -----------                          -----------&lt;br /&gt;
 quantized[k] = s[k] + u[k] + x[k]    Q(z) = S(z) + X(z) * N(z)&lt;br /&gt;
              = s[k] + y[k]                = S(z) + Y(z)&lt;br /&gt;
&lt;br /&gt;
u[k], wanted[k] are temporary variables. The values are not needed&lt;br /&gt;
outside this iteration. But the past filtered and unfiltered error&lt;br /&gt;
samples need to be remembered (y[] &amp;amp; x[]) upto some amount of samples&lt;br /&gt;
determined by the filter order.&lt;br /&gt;
&lt;br /&gt;
=== Decoupling filtering and quantization ===&lt;br /&gt;
&lt;br /&gt;
[[Image:Noiseshaper3.png|thumb|right|200px]]&lt;br /&gt;
As mentioned, the previous example is &#039;&#039;one possible&#039;&#039; implementation.&lt;br /&gt;
It&#039;s not the best possible one. But it is the first version that comes to mind.&lt;br /&gt;
There&#039;s another common formulation which requires&lt;br /&gt;
some more transfer function fiddeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt; N(z) = \frac{B(z)}{A(z)} = 1 - \frac{A(z)}{A(z)} + \frac{B(z)}{A(z)}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The two fractions have a common denominator so they can be easily&lt;br /&gt;
combined to one fraction. It&#039;s also possible to factor z^-1 out of the fraction:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = 1 - \frac{A(z)-B(z)}{A(z)} = 1 - z^{-1} \frac{z ( A(z)-B(z) )}{A(z)} =: 1 - z^{-1} H(z)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Substituting z*(A(Z)-B(Z)) for H(z) leads to a new filter that is related to N.&lt;br /&gt;
Note that with b0=1 it follows that z*(1-b0)=0. So, H(z) can be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{c_0 + c_1 z^{-1} + c_2 z^{-2} + \ldots}{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
with &amp;lt;math&amp;gt;c_{k-1} = a_k - b_k&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;k = 1...m&amp;lt;/math&amp;gt;. H(z)&lt;br /&gt;
is some &#039;&#039;arbitrary&#039;&#039; filter. The big advantage now is that it is&lt;br /&gt;
possible to plug nearly any digital filter implementation for H(z) into&lt;br /&gt;
the noise shaping loop. One obvious approach is the difference formula&lt;br /&gt;
from above but there are other alternatives with desirable properties.&lt;br /&gt;
However, the purpose of this article is not to discuss digital filters&lt;br /&gt;
in general but their application as noise shaping filters. So,&lt;br /&gt;
throughout the rest of this article H(z) is simply treated as a black&lt;br /&gt;
box. This black box has a certain state and gives us an output sample&lt;br /&gt;
for each input sample. The modified pseudo code loop looks like this:&lt;br /&gt;
&lt;br /&gt;
 (1) h_out[k] = H.filter(last_error)&lt;br /&gt;
 (2) wanted[k] = s[k] - h_out[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) last_error = quantized[k] - wanted[k]&lt;br /&gt;
&lt;br /&gt;
which is equivalent to the shown signal flow graph where the unit&lt;br /&gt;
sample delay z^-1 is implemented by the state variable &#039;last_error&#039;:&lt;br /&gt;
Remember that this black box filter H(z) doesn&#039;t correspond the the&lt;br /&gt;
actual noise shaping filter N(z). Depending on the context published&lt;br /&gt;
coefficients for noise shaping correspond either to N(z), H(z), or&lt;br /&gt;
-H(z).&lt;br /&gt;
&lt;br /&gt;
== Example filters ==&lt;br /&gt;
&lt;br /&gt;
The following two filters have been designed by Sebastian Gesemann&lt;br /&gt;
via non-linear optimization to match a slightly modified version of&lt;br /&gt;
the LAME MP3 encoder&#039;s ATH (absolute threshold of hearing) curve. They&lt;br /&gt;
were first published in the&lt;br /&gt;
[http://www.hydrogenaudio.org/forums/index.php?showtopic=47980&amp;amp;pid=551558&amp;amp;st=0 Hydrogenaudio forum]&lt;br /&gt;
including the noise transfer function&#039;s second order factorizations.&lt;br /&gt;
&lt;br /&gt;
=== Sampling rate of 44100 Hz, derived from LAME&#039;s ATH formula ===&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.1474 z^{-1} +0.5383 z^{-2} -0.3520 z^{-3} +0.3475 z^{-4}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2061 -0.4706 z^{-1} -0.2534 z^{-2} -0.6214 z^{-3}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sampling rate of 48000 Hz, derived from LAME&#039;s ATH formula ===&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.3344 z^{-1} +0.7455 z^{-2} -0.4602 z^{-3} +0.3463 z^{-4}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2374 -0.7339 z^{-1} -0.1251 z^{-2} -0.6033 z^{-3}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
==References== &lt;br /&gt;
* Lipshitz. P, Stanley and John Vanderkooy. &amp;quot;Why 1-bit Sigma-Delta Conversion is Unsuitable for High-Quality Applications&amp;quot;. 110th Convention.  &amp;lt;i&amp;gt;Audio Engineering Society&amp;lt;/i&amp;gt; (May 2001). 2-4. &lt;br /&gt;
* &lt;br /&gt;
&lt;br /&gt;
[[Category:Signal Processing]]&lt;/div&gt;</summary>
		<author><name>SebastianG</name></author>
	</entry>
	<entry>
		<id>https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=19028</id>
		<title>Noise shaping</title>
		<link rel="alternate" type="text/html" href="https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=19028"/>
		<updated>2008-08-20T13:04:33Z</updated>

		<summary type="html">&lt;p&gt;SebastianG: /* Example filters */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Noise shaping is a technique that is often used in combination with&lt;br /&gt;
[[Dithering|dithering]] in the context of [[Quantize|quantization]]. The purpose of noise shaping&lt;br /&gt;
is to alter the spectral &#039;&#039;shape&#039;&#039; of the error that is introduced due&lt;br /&gt;
to [[Dithering|dithering]] and [[Quantize|quantization]]. Sometimes it may be desirable to&lt;br /&gt;
concentrate most of the noise power in a certain frequency band so&lt;br /&gt;
that the overall noise becomes less perceivable.&lt;br /&gt;
&lt;br /&gt;
== Applications ==&lt;br /&gt;
&lt;br /&gt;
In the field of digital image processing a popular noise shaping&lt;br /&gt;
technique is known as &amp;quot;Floyd Steinberg dithering&amp;quot;. This algorithm is&lt;br /&gt;
used to reduce the noise power at lower spatial frequencies which&lt;br /&gt;
results in an improved perceptual quality.&lt;br /&gt;
&lt;br /&gt;
For word length reductions of audio signals (i.e conversion from a 24bit&lt;br /&gt;
signal to a 16bit signal) noise shaping can be used to push quantization&lt;br /&gt;
noise out of perceptually important frequency bands to perceptually less&lt;br /&gt;
important frequency bands (&amp;gt;20 kHz) in order to reduce the perceived&lt;br /&gt;
loudness of the quantization noise.&lt;br /&gt;
&lt;br /&gt;
Another example is the &lt;br /&gt;
[[Super Audio Compact Disc|Super Audio CD]]. The SACD format represents&lt;br /&gt;
the most extreme trade-off between word length (1bit/sample) and&lt;br /&gt;
sampling frequency (2.8 Mhz). Due to the low word length high power&lt;br /&gt;
noise is introduced. With the help of noise shaping this noise is&lt;br /&gt;
usually rejected by about 120 dB in the audible band (0-20 kHz) at the&lt;br /&gt;
cost of a higher noise power in the ultrasonic range (+3.5dB for&lt;br /&gt;
frequencies above 100 kHz).&lt;br /&gt;
&lt;br /&gt;
== Limits &amp;amp; filter design restrictions ==&lt;br /&gt;
&lt;br /&gt;
Noise Shaping without [[Dithering|&#039;&#039;dithered&#039;&#039;]] quantization is also possible but&lt;br /&gt;
less useful because the spectral characteristics of &#039;&#039;undithered&#039;&#039;&lt;br /&gt;
quantization noise is not easily predictable and depends heavily on the&lt;br /&gt;
digital signal that is to be quantized. The SACD case is an example where&lt;br /&gt;
[[Dithering|dithering]] can&#039;t be properly applied which may lead to a spectral&lt;br /&gt;
noise power distribution that deviates from the indented distribution. This has&lt;br /&gt;
been demonstrated by Lipshitz and Vanderkooy in&lt;br /&gt;
[http://www.sjeng.org/ftp/SACD.pdf their] [1] paper. &lt;br /&gt;
&lt;br /&gt;
It&#039;s not possible to lower the noise power for &#039;&#039;all&#039;&#039; frequencies&lt;br /&gt;
via noise shaping because this would be in violation of the&lt;br /&gt;
rate/distortion principles. But it &#039;&#039;is&#039;&#039; possible to lower the noise&lt;br /&gt;
in certain frequency bands at the cost of increased noise power in other&lt;br /&gt;
frequency bands. This property is also known as the&lt;br /&gt;
&#039;&#039;noise shaping theorem&#039;&#039;. It states that the average logarithmic magnitude response of&lt;br /&gt;
the corresponding noise shaping &#039;&#039;filter&#039;&#039; over the whole linear&lt;br /&gt;
frequency axis cannot drop below 0dB.&lt;br /&gt;
&lt;br /&gt;
The noise shaping application poses some restrictions on the fiter&lt;br /&gt;
design. The first sample of the filter&#039;s impulse response always equals&lt;br /&gt;
one. Also, interesting filters for noise shaping are so-called&lt;br /&gt;
&#039;&#039;minimum phase filters&#039;&#039;. This type of filter performs optimal in the sense of&lt;br /&gt;
the noise shaping theorem because the average logarithmig magnitude&lt;br /&gt;
response &#039;&#039;is&#039;&#039; 0dB.&lt;br /&gt;
&lt;br /&gt;
For the special [[Super Audio Compact Disc|SACD]] case an additional&lt;br /&gt;
constraint comes into play: The magnitude response should not exceed&lt;br /&gt;
3.5dB. This is enforced to prevent so-called limit cycles.&lt;br /&gt;
&lt;br /&gt;
The design of these kinds of filters is however not trivial. This is not&lt;br /&gt;
covered by this article.&lt;br /&gt;
&lt;br /&gt;
== Digital filter reminder ==&lt;br /&gt;
&lt;br /&gt;
A common representation of digital filters is their &#039;&#039;transfer function&#039;&#039;&lt;br /&gt;
in form of a rational polynomial in z^-1. These polynomials are defined&lt;br /&gt;
by their coefficients b0,b1,b2,...,a1,a2,...:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{B(z)}{A(z)} = \frac{b_0 + b_1 z^{-1} + b_2 z^{-2} + \ldots + b_m z^{-m}}&lt;br /&gt;
{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots + a_m z^{-m}} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &#039;N&#039; is the transfer function and &#039;m&#039; the filter&#039;s &#039;&#039;order&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
=== FIR filters ===&lt;br /&gt;
&lt;br /&gt;
In case a1=a2=a3=...=0 the filter&#039;s [http://en.wikipedia.org/wiki/Impulse_response impulse response] is finite which is&lt;br /&gt;
why these filters are called FIR ([[Finite Impulse Response Filter|finite impulse response]])&lt;br /&gt;
filters as opposed to IIR&lt;br /&gt;
[[Infinite Impulse Response Filter|infinite impulse response]] filters. Note that in case of&lt;br /&gt;
FIR filters the coefficients b0, b1, ... correspond to the impulse&lt;br /&gt;
response. The impulse response is another way a filter can be&lt;br /&gt;
characterized and describes what the filter does to a unit pulse. Let&#039;s&lt;br /&gt;
assume for the moment that a1=a2=a3=...=0. The the impulse response&lt;br /&gt;
would be:&lt;br /&gt;
&lt;br /&gt;
 x:  0  0  1  0  0  0  0  ... 0  0  0 ... (input signal)&lt;br /&gt;
 y:  0  0  b0 b1 b2 b3 b4 ... 0  0  0 ... (output signal)&lt;br /&gt;
&lt;br /&gt;
It helps to think of negative powers of z in transfer functions as a&lt;br /&gt;
delay: z^-1 = delay of one sample. z^-2 = delay of two samples etc.&lt;br /&gt;
Since a digital filter is a&lt;br /&gt;
[http://en.wikipedia.org/wiki/Discrete_system discrete-time]&lt;br /&gt;
[http://en.wikipedia.org/wiki/LTI_system_theory linear and time-invariant]&lt;br /&gt;
system it is&lt;br /&gt;
possible to determine the filter&#039;s output by a superposition of scaled&lt;br /&gt;
and shifted impulse responses. This leads to the following relationship&lt;br /&gt;
between input and output in the case of FIR filters:&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(z) = X(z) * B(z)&lt;br /&gt;
&lt;br /&gt;
where x[] is the input signal, y[] the output signal and k the current&lt;br /&gt;
sample index. This is a simple convolution directly computed using the&lt;br /&gt;
given impulse response. The convolution in the time domain corresponds&lt;br /&gt;
to a multiplication in the Z-domain.&lt;br /&gt;
&lt;br /&gt;
=== IIR filters ===&lt;br /&gt;
&lt;br /&gt;
IIR filters are a simple extension to the FIR case:&lt;br /&gt;
&lt;br /&gt;
 time domain                                Z-transform&lt;br /&gt;
 -----------                                -----------&lt;br /&gt;
      y[k] + a1 y[k-1] + a2 y[k-2] + ...    Y(z) * A(Z) = X(Z) * B(Z)&lt;br /&gt;
 = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Solving for Y we get&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(Z) = X(Z) * N(z)&lt;br /&gt;
                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
which is the well known &#039;&#039;difference formula&#039;&#039;. Its signal flow graph&lt;br /&gt;
representation is known as&lt;br /&gt;
[http://ccrma.stanford.edu/~jos/filters/Direct_Form_I.html direct form I].&lt;br /&gt;
&lt;br /&gt;
== Noise Shaper Implementation ==&lt;br /&gt;
&lt;br /&gt;
Usually a filter&#039;s transfer function is called &#039;H&#039;. In this article &#039;N&#039;&lt;br /&gt;
has been used because this filter is also known as noise transfer&lt;br /&gt;
function (NTF) in the context of noise shaping. Later, another filter&lt;br /&gt;
&#039;H&#039; is introduced that has a special relation to &#039;N&#039;.&lt;br /&gt;
&lt;br /&gt;
=== Direct Form I ===&lt;br /&gt;
&lt;br /&gt;
For convenience let us substitute the actual difference in the&lt;br /&gt;
difference formula for &#039;u&#039;. Also, b0 must be equal to one for noise&lt;br /&gt;
shaping filters as mentioned above. This leads to&lt;br /&gt;
&lt;br /&gt;
 y[k] = x[k] + u[k]   with u[k] = b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
                                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Note that since the filter is supposed to be applied on the quantization&lt;br /&gt;
noise y[] corresponds to the filtered noise and x[] to the unfiltered&lt;br /&gt;
noise. Suppose s[] is the signal we&#039;re trying to quantize. The following&lt;br /&gt;
pseudo code snippet shows the inner loop over k of &#039;&#039;one possible&#039;&#039;&lt;br /&gt;
noise shaping implementation:&lt;br /&gt;
&lt;br /&gt;
 (1) compute u[k]&lt;br /&gt;
 (2) wanted[k] = s[k] + u[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) y[k] = quantized[k] - s[k]&lt;br /&gt;
 (5) x[k] = y[k] - u[k]&lt;br /&gt;
&lt;br /&gt;
Note that the following is true&lt;br /&gt;
&lt;br /&gt;
 time domain                          Z-transform&lt;br /&gt;
 -----------                          -----------&lt;br /&gt;
 quantized[k] = s[k] + u[k] + x[k]    Q(z) = S(z) + X(z) * N(z)&lt;br /&gt;
              = s[k] + y[k]                = S(z) + Y(z)&lt;br /&gt;
&lt;br /&gt;
u[k], wanted[k] are temporary variables. The values are not needed&lt;br /&gt;
outside this iteration. But the past filtered and unfiltered error&lt;br /&gt;
samples need to be remembered (y[] &amp;amp; x[]) upto some amount of samples&lt;br /&gt;
determined by the filter order.&lt;br /&gt;
&lt;br /&gt;
=== Decoupling filtering and quantization ===&lt;br /&gt;
&lt;br /&gt;
[[Image:Noiseshaper3.png|thumb|right|200px]]&lt;br /&gt;
As mentioned, the previous example is &#039;&#039;one possible&#039;&#039; implementation.&lt;br /&gt;
It&#039;s not the best possible one. But it is the first version that comes to mind.&lt;br /&gt;
There&#039;s another common formulation which requires&lt;br /&gt;
some more transfer function fiddeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt; N(z) = \frac{B(z)}{A(z)} = 1 - \frac{A(z)}{A(z)} + \frac{B(z)}{A(z)}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The two fractions have a common denominator so they can be easily&lt;br /&gt;
combined to one fraction. It&#039;s also possible to factor z^-1 out of the fraction:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = 1 - \frac{A(z)-B(z)}{A(z)} = 1 - z^{-1} \frac{z ( A(z)-B(z) )}{A(z)} =: 1 - z^{-1} H(z)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Substituting z*(A(Z)-B(Z)) for H(z) leads to a new filter that is related to N.&lt;br /&gt;
Note that with b0=1 it follows that z*(1-b0)=0. So, H(z) can be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{c_0 + c_1 z^{-1} + c_2 z^{-2} + \ldots}{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
with &amp;lt;math&amp;gt;c_{k-1} = a_k - b_k&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;k = 1...m&amp;lt;/math&amp;gt;. H(z)&lt;br /&gt;
is some &#039;&#039;arbitrary&#039;&#039; filter. The big advantage now is that it is&lt;br /&gt;
possible to plug nearly any digital filter implementation for H(z) into&lt;br /&gt;
the noise shaping loop. One obvious approach is the difference formula&lt;br /&gt;
from above but there are other alternatives with desirable properties.&lt;br /&gt;
However, the purpose of this article is not to discuss digital filters&lt;br /&gt;
in general but their application as noise shaping filters. So,&lt;br /&gt;
throughout the rest of this article H(z) is simply treated as a black&lt;br /&gt;
box. This black box has a certain state and gives us an output sample&lt;br /&gt;
for each input sample. The modified pseudo code loop looks like this:&lt;br /&gt;
&lt;br /&gt;
 (1) h_out[k] = H.filter(last_error)&lt;br /&gt;
 (2) wanted[k] = s[k] - h_out[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) last_error = quantized[k] - wanted[k]&lt;br /&gt;
&lt;br /&gt;
which is equivalent to the shown signal flow graph where the unit&lt;br /&gt;
sample delay z^-1 is implemented by the state variable &#039;last_error&#039;:&lt;br /&gt;
Remember that this black box filter H(z) doesn&#039;t correspond the the&lt;br /&gt;
actual noise shaping filter N(z). Depending on the context published&lt;br /&gt;
coefficients for noise shaping correspond either to N(z), H(z), or&lt;br /&gt;
-H(z).&lt;br /&gt;
&lt;br /&gt;
== Example filters ==&lt;br /&gt;
&lt;br /&gt;
The following two filters have been designed by Sebastian Gesemann&lt;br /&gt;
via non-linear optimization to match a slightly modified version of&lt;br /&gt;
the LAME MP3 encoder&#039;s ATH (absolute threshold of hearing) curve. They&lt;br /&gt;
were first published in the&lt;br /&gt;
[http://www.hydrogenaudio.org/forums/index.php?showtopic=47980&amp;amp;pid=551558&amp;amp;st=0 Hydrogenaudio forum]&lt;br /&gt;
including the noise transfer function&#039;s second order factorizations.&lt;br /&gt;
&lt;br /&gt;
=== Sampling rate of 44100 Hz, derived from LAME&#039;s ATH formula ===&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.1474 z^{-1} +0.5383 z^{-2} -0.3520 z^{-3} +0.3475 z^{-4}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2061 -0.4706 z^{-1} -0.2534 z^{-2} -0.6214 z^{-3}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sampling rate of 48000 Hz, derived from LAME&#039;s ATH formula ===&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.3344 z^{-1} +0.7455 z^{-2} -0.4602 z^{-3} +0.3463 z^{-4}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2374 -0.7339 z^{-1} -0.1251 z^{-2} -0.6033 z^{-3}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Notes=&lt;br /&gt;
&lt;br /&gt;
==References== &lt;br /&gt;
* Lipshitz. P, Stanley and John Vanderkooy. &amp;quot;Why 1-bit Sigma-Delta Conversion is Unsuitable for High-Quality Applications&amp;quot;. 110th Convention.  &amp;lt;i&amp;gt;Audio Engineering Society&amp;lt;/i&amp;gt; (May 2001). 2-4. &lt;br /&gt;
* &lt;br /&gt;
&lt;br /&gt;
[[Category:Signal Processing]]&lt;/div&gt;</summary>
		<author><name>SebastianG</name></author>
	</entry>
	<entry>
		<id>https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=19027</id>
		<title>Noise shaping</title>
		<link rel="alternate" type="text/html" href="https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=19027"/>
		<updated>2008-08-20T13:04:02Z</updated>

		<summary type="html">&lt;p&gt;SebastianG: /* Noise Shaper Implementation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Noise shaping is a technique that is often used in combination with&lt;br /&gt;
[[Dithering|dithering]] in the context of [[Quantize|quantization]]. The purpose of noise shaping&lt;br /&gt;
is to alter the spectral &#039;&#039;shape&#039;&#039; of the error that is introduced due&lt;br /&gt;
to [[Dithering|dithering]] and [[Quantize|quantization]]. Sometimes it may be desirable to&lt;br /&gt;
concentrate most of the noise power in a certain frequency band so&lt;br /&gt;
that the overall noise becomes less perceivable.&lt;br /&gt;
&lt;br /&gt;
== Applications ==&lt;br /&gt;
&lt;br /&gt;
In the field of digital image processing a popular noise shaping&lt;br /&gt;
technique is known as &amp;quot;Floyd Steinberg dithering&amp;quot;. This algorithm is&lt;br /&gt;
used to reduce the noise power at lower spatial frequencies which&lt;br /&gt;
results in an improved perceptual quality.&lt;br /&gt;
&lt;br /&gt;
For word length reductions of audio signals (i.e conversion from a 24bit&lt;br /&gt;
signal to a 16bit signal) noise shaping can be used to push quantization&lt;br /&gt;
noise out of perceptually important frequency bands to perceptually less&lt;br /&gt;
important frequency bands (&amp;gt;20 kHz) in order to reduce the perceived&lt;br /&gt;
loudness of the quantization noise.&lt;br /&gt;
&lt;br /&gt;
Another example is the &lt;br /&gt;
[[Super Audio Compact Disc|Super Audio CD]]. The SACD format represents&lt;br /&gt;
the most extreme trade-off between word length (1bit/sample) and&lt;br /&gt;
sampling frequency (2.8 Mhz). Due to the low word length high power&lt;br /&gt;
noise is introduced. With the help of noise shaping this noise is&lt;br /&gt;
usually rejected by about 120 dB in the audible band (0-20 kHz) at the&lt;br /&gt;
cost of a higher noise power in the ultrasonic range (+3.5dB for&lt;br /&gt;
frequencies above 100 kHz).&lt;br /&gt;
&lt;br /&gt;
== Limits &amp;amp; filter design restrictions ==&lt;br /&gt;
&lt;br /&gt;
Noise Shaping without [[Dithering|&#039;&#039;dithered&#039;&#039;]] quantization is also possible but&lt;br /&gt;
less useful because the spectral characteristics of &#039;&#039;undithered&#039;&#039;&lt;br /&gt;
quantization noise is not easily predictable and depends heavily on the&lt;br /&gt;
digital signal that is to be quantized. The SACD case is an example where&lt;br /&gt;
[[Dithering|dithering]] can&#039;t be properly applied which may lead to a spectral&lt;br /&gt;
noise power distribution that deviates from the indented distribution. This has&lt;br /&gt;
been demonstrated by Lipshitz and Vanderkooy in&lt;br /&gt;
[http://www.sjeng.org/ftp/SACD.pdf their] [1] paper. &lt;br /&gt;
&lt;br /&gt;
It&#039;s not possible to lower the noise power for &#039;&#039;all&#039;&#039; frequencies&lt;br /&gt;
via noise shaping because this would be in violation of the&lt;br /&gt;
rate/distortion principles. But it &#039;&#039;is&#039;&#039; possible to lower the noise&lt;br /&gt;
in certain frequency bands at the cost of increased noise power in other&lt;br /&gt;
frequency bands. This property is also known as the&lt;br /&gt;
&#039;&#039;noise shaping theorem&#039;&#039;. It states that the average logarithmic magnitude response of&lt;br /&gt;
the corresponding noise shaping &#039;&#039;filter&#039;&#039; over the whole linear&lt;br /&gt;
frequency axis cannot drop below 0dB.&lt;br /&gt;
&lt;br /&gt;
The noise shaping application poses some restrictions on the fiter&lt;br /&gt;
design. The first sample of the filter&#039;s impulse response always equals&lt;br /&gt;
one. Also, interesting filters for noise shaping are so-called&lt;br /&gt;
&#039;&#039;minimum phase filters&#039;&#039;. This type of filter performs optimal in the sense of&lt;br /&gt;
the noise shaping theorem because the average logarithmig magnitude&lt;br /&gt;
response &#039;&#039;is&#039;&#039; 0dB.&lt;br /&gt;
&lt;br /&gt;
For the special [[Super Audio Compact Disc|SACD]] case an additional&lt;br /&gt;
constraint comes into play: The magnitude response should not exceed&lt;br /&gt;
3.5dB. This is enforced to prevent so-called limit cycles.&lt;br /&gt;
&lt;br /&gt;
The design of these kinds of filters is however not trivial. This is not&lt;br /&gt;
covered by this article.&lt;br /&gt;
&lt;br /&gt;
== Digital filter reminder ==&lt;br /&gt;
&lt;br /&gt;
A common representation of digital filters is their &#039;&#039;transfer function&#039;&#039;&lt;br /&gt;
in form of a rational polynomial in z^-1. These polynomials are defined&lt;br /&gt;
by their coefficients b0,b1,b2,...,a1,a2,...:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{B(z)}{A(z)} = \frac{b_0 + b_1 z^{-1} + b_2 z^{-2} + \ldots + b_m z^{-m}}&lt;br /&gt;
{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots + a_m z^{-m}} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &#039;N&#039; is the transfer function and &#039;m&#039; the filter&#039;s &#039;&#039;order&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
=== FIR filters ===&lt;br /&gt;
&lt;br /&gt;
In case a1=a2=a3=...=0 the filter&#039;s [http://en.wikipedia.org/wiki/Impulse_response impulse response] is finite which is&lt;br /&gt;
why these filters are called FIR ([[Finite Impulse Response Filter|finite impulse response]])&lt;br /&gt;
filters as opposed to IIR&lt;br /&gt;
[[Infinite Impulse Response Filter|infinite impulse response]] filters. Note that in case of&lt;br /&gt;
FIR filters the coefficients b0, b1, ... correspond to the impulse&lt;br /&gt;
response. The impulse response is another way a filter can be&lt;br /&gt;
characterized and describes what the filter does to a unit pulse. Let&#039;s&lt;br /&gt;
assume for the moment that a1=a2=a3=...=0. The the impulse response&lt;br /&gt;
would be:&lt;br /&gt;
&lt;br /&gt;
 x:  0  0  1  0  0  0  0  ... 0  0  0 ... (input signal)&lt;br /&gt;
 y:  0  0  b0 b1 b2 b3 b4 ... 0  0  0 ... (output signal)&lt;br /&gt;
&lt;br /&gt;
It helps to think of negative powers of z in transfer functions as a&lt;br /&gt;
delay: z^-1 = delay of one sample. z^-2 = delay of two samples etc.&lt;br /&gt;
Since a digital filter is a&lt;br /&gt;
[http://en.wikipedia.org/wiki/Discrete_system discrete-time]&lt;br /&gt;
[http://en.wikipedia.org/wiki/LTI_system_theory linear and time-invariant]&lt;br /&gt;
system it is&lt;br /&gt;
possible to determine the filter&#039;s output by a superposition of scaled&lt;br /&gt;
and shifted impulse responses. This leads to the following relationship&lt;br /&gt;
between input and output in the case of FIR filters:&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(z) = X(z) * B(z)&lt;br /&gt;
&lt;br /&gt;
where x[] is the input signal, y[] the output signal and k the current&lt;br /&gt;
sample index. This is a simple convolution directly computed using the&lt;br /&gt;
given impulse response. The convolution in the time domain corresponds&lt;br /&gt;
to a multiplication in the Z-domain.&lt;br /&gt;
&lt;br /&gt;
=== IIR filters ===&lt;br /&gt;
&lt;br /&gt;
IIR filters are a simple extension to the FIR case:&lt;br /&gt;
&lt;br /&gt;
 time domain                                Z-transform&lt;br /&gt;
 -----------                                -----------&lt;br /&gt;
      y[k] + a1 y[k-1] + a2 y[k-2] + ...    Y(z) * A(Z) = X(Z) * B(Z)&lt;br /&gt;
 = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Solving for Y we get&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(Z) = X(Z) * N(z)&lt;br /&gt;
                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
which is the well known &#039;&#039;difference formula&#039;&#039;. Its signal flow graph&lt;br /&gt;
representation is known as&lt;br /&gt;
[http://ccrma.stanford.edu/~jos/filters/Direct_Form_I.html direct form I].&lt;br /&gt;
&lt;br /&gt;
== Noise Shaper Implementation ==&lt;br /&gt;
&lt;br /&gt;
Usually a filter&#039;s transfer function is called &#039;H&#039;. In this article &#039;N&#039;&lt;br /&gt;
has been used because this filter is also known as noise transfer&lt;br /&gt;
function (NTF) in the context of noise shaping. Later, another filter&lt;br /&gt;
&#039;H&#039; is introduced that has a special relation to &#039;N&#039;.&lt;br /&gt;
&lt;br /&gt;
=== Direct Form I ===&lt;br /&gt;
&lt;br /&gt;
For convenience let us substitute the actual difference in the&lt;br /&gt;
difference formula for &#039;u&#039;. Also, b0 must be equal to one for noise&lt;br /&gt;
shaping filters as mentioned above. This leads to&lt;br /&gt;
&lt;br /&gt;
 y[k] = x[k] + u[k]   with u[k] = b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
                                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Note that since the filter is supposed to be applied on the quantization&lt;br /&gt;
noise y[] corresponds to the filtered noise and x[] to the unfiltered&lt;br /&gt;
noise. Suppose s[] is the signal we&#039;re trying to quantize. The following&lt;br /&gt;
pseudo code snippet shows the inner loop over k of &#039;&#039;one possible&#039;&#039;&lt;br /&gt;
noise shaping implementation:&lt;br /&gt;
&lt;br /&gt;
 (1) compute u[k]&lt;br /&gt;
 (2) wanted[k] = s[k] + u[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) y[k] = quantized[k] - s[k]&lt;br /&gt;
 (5) x[k] = y[k] - u[k]&lt;br /&gt;
&lt;br /&gt;
Note that the following is true&lt;br /&gt;
&lt;br /&gt;
 time domain                          Z-transform&lt;br /&gt;
 -----------                          -----------&lt;br /&gt;
 quantized[k] = s[k] + u[k] + x[k]    Q(z) = S(z) + X(z) * N(z)&lt;br /&gt;
              = s[k] + y[k]                = S(z) + Y(z)&lt;br /&gt;
&lt;br /&gt;
u[k], wanted[k] are temporary variables. The values are not needed&lt;br /&gt;
outside this iteration. But the past filtered and unfiltered error&lt;br /&gt;
samples need to be remembered (y[] &amp;amp; x[]) upto some amount of samples&lt;br /&gt;
determined by the filter order.&lt;br /&gt;
&lt;br /&gt;
=== Decoupling filtering and quantization ===&lt;br /&gt;
&lt;br /&gt;
[[Image:Noiseshaper3.png|thumb|right|200px]]&lt;br /&gt;
As mentioned, the previous example is &#039;&#039;one possible&#039;&#039; implementation.&lt;br /&gt;
It&#039;s not the best possible one. But it is the first version that comes to mind.&lt;br /&gt;
There&#039;s another common formulation which requires&lt;br /&gt;
some more transfer function fiddeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt; N(z) = \frac{B(z)}{A(z)} = 1 - \frac{A(z)}{A(z)} + \frac{B(z)}{A(z)}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The two fractions have a common denominator so they can be easily&lt;br /&gt;
combined to one fraction. It&#039;s also possible to factor z^-1 out of the fraction:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = 1 - \frac{A(z)-B(z)}{A(z)} = 1 - z^{-1} \frac{z ( A(z)-B(z) )}{A(z)} =: 1 - z^{-1} H(z)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Substituting z*(A(Z)-B(Z)) for H(z) leads to a new filter that is related to N.&lt;br /&gt;
Note that with b0=1 it follows that z*(1-b0)=0. So, H(z) can be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{c_0 + c_1 z^{-1} + c_2 z^{-2} + \ldots}{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
with &amp;lt;math&amp;gt;c_{k-1} = a_k - b_k&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;k = 1...m&amp;lt;/math&amp;gt;. H(z)&lt;br /&gt;
is some &#039;&#039;arbitrary&#039;&#039; filter. The big advantage now is that it is&lt;br /&gt;
possible to plug nearly any digital filter implementation for H(z) into&lt;br /&gt;
the noise shaping loop. One obvious approach is the difference formula&lt;br /&gt;
from above but there are other alternatives with desirable properties.&lt;br /&gt;
However, the purpose of this article is not to discuss digital filters&lt;br /&gt;
in general but their application as noise shaping filters. So,&lt;br /&gt;
throughout the rest of this article H(z) is simply treated as a black&lt;br /&gt;
box. This black box has a certain state and gives us an output sample&lt;br /&gt;
for each input sample. The modified pseudo code loop looks like this:&lt;br /&gt;
&lt;br /&gt;
 (1) h_out[k] = H.filter(last_error)&lt;br /&gt;
 (2) wanted[k] = s[k] - h_out[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) last_error = quantized[k] - wanted[k]&lt;br /&gt;
&lt;br /&gt;
which is equivalent to the shown signal flow graph where the unit&lt;br /&gt;
sample delay z^-1 is implemented by the state variable &#039;last_error&#039;:&lt;br /&gt;
Remember that this black box filter H(z) doesn&#039;t correspond the the&lt;br /&gt;
actual noise shaping filter N(z). Depending on the context published&lt;br /&gt;
coefficients for noise shaping correspond either to N(z), H(z), or&lt;br /&gt;
-H(z).&lt;br /&gt;
&lt;br /&gt;
= Example filters =&lt;br /&gt;
&lt;br /&gt;
The following two filters have been designed by Sebastian Gesemann&lt;br /&gt;
via non-linear optimization to match a slightly modified version of&lt;br /&gt;
the LAME MP3 encoder&#039;s ATH (absolute threshold of hearing) curve. They&lt;br /&gt;
were first published in the&lt;br /&gt;
[http://www.hydrogenaudio.org/forums/index.php?showtopic=47980&amp;amp;pid=551558&amp;amp;st=0 Hydrogenaudio forum]&lt;br /&gt;
including the noise transfer function&#039;s second order factorizations.&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 44100 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.1474 z^{-1} +0.5383 z^{-2} -0.3520 z^{-3} +0.3475 z^{-4}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2061 -0.4706 z^{-1} -0.2534 z^{-2} -0.6214 z^{-3}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 48000 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.3344 z^{-1} +0.7455 z^{-2} -0.4602 z^{-3} +0.3463 z^{-4}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2374 -0.7339 z^{-1} -0.1251 z^{-2} -0.6033 z^{-3}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Notes=&lt;br /&gt;
&lt;br /&gt;
==References== &lt;br /&gt;
* Lipshitz. P, Stanley and John Vanderkooy. &amp;quot;Why 1-bit Sigma-Delta Conversion is Unsuitable for High-Quality Applications&amp;quot;. 110th Convention.  &amp;lt;i&amp;gt;Audio Engineering Society&amp;lt;/i&amp;gt; (May 2001). 2-4. &lt;br /&gt;
* &lt;br /&gt;
&lt;br /&gt;
[[Category:Signal Processing]]&lt;/div&gt;</summary>
		<author><name>SebastianG</name></author>
	</entry>
	<entry>
		<id>https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=19026</id>
		<title>Noise shaping</title>
		<link rel="alternate" type="text/html" href="https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=19026"/>
		<updated>2008-08-20T13:03:36Z</updated>

		<summary type="html">&lt;p&gt;SebastianG: /* IIR filters */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Noise shaping is a technique that is often used in combination with&lt;br /&gt;
[[Dithering|dithering]] in the context of [[Quantize|quantization]]. The purpose of noise shaping&lt;br /&gt;
is to alter the spectral &#039;&#039;shape&#039;&#039; of the error that is introduced due&lt;br /&gt;
to [[Dithering|dithering]] and [[Quantize|quantization]]. Sometimes it may be desirable to&lt;br /&gt;
concentrate most of the noise power in a certain frequency band so&lt;br /&gt;
that the overall noise becomes less perceivable.&lt;br /&gt;
&lt;br /&gt;
== Applications ==&lt;br /&gt;
&lt;br /&gt;
In the field of digital image processing a popular noise shaping&lt;br /&gt;
technique is known as &amp;quot;Floyd Steinberg dithering&amp;quot;. This algorithm is&lt;br /&gt;
used to reduce the noise power at lower spatial frequencies which&lt;br /&gt;
results in an improved perceptual quality.&lt;br /&gt;
&lt;br /&gt;
For word length reductions of audio signals (i.e conversion from a 24bit&lt;br /&gt;
signal to a 16bit signal) noise shaping can be used to push quantization&lt;br /&gt;
noise out of perceptually important frequency bands to perceptually less&lt;br /&gt;
important frequency bands (&amp;gt;20 kHz) in order to reduce the perceived&lt;br /&gt;
loudness of the quantization noise.&lt;br /&gt;
&lt;br /&gt;
Another example is the &lt;br /&gt;
[[Super Audio Compact Disc|Super Audio CD]]. The SACD format represents&lt;br /&gt;
the most extreme trade-off between word length (1bit/sample) and&lt;br /&gt;
sampling frequency (2.8 Mhz). Due to the low word length high power&lt;br /&gt;
noise is introduced. With the help of noise shaping this noise is&lt;br /&gt;
usually rejected by about 120 dB in the audible band (0-20 kHz) at the&lt;br /&gt;
cost of a higher noise power in the ultrasonic range (+3.5dB for&lt;br /&gt;
frequencies above 100 kHz).&lt;br /&gt;
&lt;br /&gt;
== Limits &amp;amp; filter design restrictions ==&lt;br /&gt;
&lt;br /&gt;
Noise Shaping without [[Dithering|&#039;&#039;dithered&#039;&#039;]] quantization is also possible but&lt;br /&gt;
less useful because the spectral characteristics of &#039;&#039;undithered&#039;&#039;&lt;br /&gt;
quantization noise is not easily predictable and depends heavily on the&lt;br /&gt;
digital signal that is to be quantized. The SACD case is an example where&lt;br /&gt;
[[Dithering|dithering]] can&#039;t be properly applied which may lead to a spectral&lt;br /&gt;
noise power distribution that deviates from the indented distribution. This has&lt;br /&gt;
been demonstrated by Lipshitz and Vanderkooy in&lt;br /&gt;
[http://www.sjeng.org/ftp/SACD.pdf their] [1] paper. &lt;br /&gt;
&lt;br /&gt;
It&#039;s not possible to lower the noise power for &#039;&#039;all&#039;&#039; frequencies&lt;br /&gt;
via noise shaping because this would be in violation of the&lt;br /&gt;
rate/distortion principles. But it &#039;&#039;is&#039;&#039; possible to lower the noise&lt;br /&gt;
in certain frequency bands at the cost of increased noise power in other&lt;br /&gt;
frequency bands. This property is also known as the&lt;br /&gt;
&#039;&#039;noise shaping theorem&#039;&#039;. It states that the average logarithmic magnitude response of&lt;br /&gt;
the corresponding noise shaping &#039;&#039;filter&#039;&#039; over the whole linear&lt;br /&gt;
frequency axis cannot drop below 0dB.&lt;br /&gt;
&lt;br /&gt;
The noise shaping application poses some restrictions on the fiter&lt;br /&gt;
design. The first sample of the filter&#039;s impulse response always equals&lt;br /&gt;
one. Also, interesting filters for noise shaping are so-called&lt;br /&gt;
&#039;&#039;minimum phase filters&#039;&#039;. This type of filter performs optimal in the sense of&lt;br /&gt;
the noise shaping theorem because the average logarithmig magnitude&lt;br /&gt;
response &#039;&#039;is&#039;&#039; 0dB.&lt;br /&gt;
&lt;br /&gt;
For the special [[Super Audio Compact Disc|SACD]] case an additional&lt;br /&gt;
constraint comes into play: The magnitude response should not exceed&lt;br /&gt;
3.5dB. This is enforced to prevent so-called limit cycles.&lt;br /&gt;
&lt;br /&gt;
The design of these kinds of filters is however not trivial. This is not&lt;br /&gt;
covered by this article.&lt;br /&gt;
&lt;br /&gt;
== Digital filter reminder ==&lt;br /&gt;
&lt;br /&gt;
A common representation of digital filters is their &#039;&#039;transfer function&#039;&#039;&lt;br /&gt;
in form of a rational polynomial in z^-1. These polynomials are defined&lt;br /&gt;
by their coefficients b0,b1,b2,...,a1,a2,...:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{B(z)}{A(z)} = \frac{b_0 + b_1 z^{-1} + b_2 z^{-2} + \ldots + b_m z^{-m}}&lt;br /&gt;
{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots + a_m z^{-m}} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &#039;N&#039; is the transfer function and &#039;m&#039; the filter&#039;s &#039;&#039;order&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
=== FIR filters ===&lt;br /&gt;
&lt;br /&gt;
In case a1=a2=a3=...=0 the filter&#039;s [http://en.wikipedia.org/wiki/Impulse_response impulse response] is finite which is&lt;br /&gt;
why these filters are called FIR ([[Finite Impulse Response Filter|finite impulse response]])&lt;br /&gt;
filters as opposed to IIR&lt;br /&gt;
[[Infinite Impulse Response Filter|infinite impulse response]] filters. Note that in case of&lt;br /&gt;
FIR filters the coefficients b0, b1, ... correspond to the impulse&lt;br /&gt;
response. The impulse response is another way a filter can be&lt;br /&gt;
characterized and describes what the filter does to a unit pulse. Let&#039;s&lt;br /&gt;
assume for the moment that a1=a2=a3=...=0. The the impulse response&lt;br /&gt;
would be:&lt;br /&gt;
&lt;br /&gt;
 x:  0  0  1  0  0  0  0  ... 0  0  0 ... (input signal)&lt;br /&gt;
 y:  0  0  b0 b1 b2 b3 b4 ... 0  0  0 ... (output signal)&lt;br /&gt;
&lt;br /&gt;
It helps to think of negative powers of z in transfer functions as a&lt;br /&gt;
delay: z^-1 = delay of one sample. z^-2 = delay of two samples etc.&lt;br /&gt;
Since a digital filter is a&lt;br /&gt;
[http://en.wikipedia.org/wiki/Discrete_system discrete-time]&lt;br /&gt;
[http://en.wikipedia.org/wiki/LTI_system_theory linear and time-invariant]&lt;br /&gt;
system it is&lt;br /&gt;
possible to determine the filter&#039;s output by a superposition of scaled&lt;br /&gt;
and shifted impulse responses. This leads to the following relationship&lt;br /&gt;
between input and output in the case of FIR filters:&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(z) = X(z) * B(z)&lt;br /&gt;
&lt;br /&gt;
where x[] is the input signal, y[] the output signal and k the current&lt;br /&gt;
sample index. This is a simple convolution directly computed using the&lt;br /&gt;
given impulse response. The convolution in the time domain corresponds&lt;br /&gt;
to a multiplication in the Z-domain.&lt;br /&gt;
&lt;br /&gt;
=== IIR filters ===&lt;br /&gt;
&lt;br /&gt;
IIR filters are a simple extension to the FIR case:&lt;br /&gt;
&lt;br /&gt;
 time domain                                Z-transform&lt;br /&gt;
 -----------                                -----------&lt;br /&gt;
      y[k] + a1 y[k-1] + a2 y[k-2] + ...    Y(z) * A(Z) = X(Z) * B(Z)&lt;br /&gt;
 = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Solving for Y we get&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(Z) = X(Z) * N(z)&lt;br /&gt;
                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
which is the well known &#039;&#039;difference formula&#039;&#039;. Its signal flow graph&lt;br /&gt;
representation is known as&lt;br /&gt;
[http://ccrma.stanford.edu/~jos/filters/Direct_Form_I.html direct form I].&lt;br /&gt;
&lt;br /&gt;
= Noise Shaper Implementation =&lt;br /&gt;
&lt;br /&gt;
Usually a filter&#039;s transfer function is called &#039;H&#039;. In this article &#039;N&#039;&lt;br /&gt;
has been used because this filter is also known as noise transfer&lt;br /&gt;
function (NTF) in the context of noise shaping. Later, another filter&lt;br /&gt;
&#039;H&#039; is introduced that has a special relation to &#039;N&#039;.&lt;br /&gt;
&lt;br /&gt;
== Direct Form I ==&lt;br /&gt;
&lt;br /&gt;
For convenience let us substitute the actual difference in the&lt;br /&gt;
difference formula for &#039;u&#039;. Also, b0 must be equal to one for noise&lt;br /&gt;
shaping filters as mentioned above. This leads to&lt;br /&gt;
&lt;br /&gt;
 y[k] = x[k] + u[k]   with u[k] = b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
                                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Note that since the filter is supposed to be applied on the quantization&lt;br /&gt;
noise y[] corresponds to the filtered noise and x[] to the unfiltered&lt;br /&gt;
noise. Suppose s[] is the signal we&#039;re trying to quantize. The following&lt;br /&gt;
pseudo code snippet shows the inner loop over k of &#039;&#039;one possible&#039;&#039;&lt;br /&gt;
noise shaping implementation:&lt;br /&gt;
&lt;br /&gt;
 (1) compute u[k]&lt;br /&gt;
 (2) wanted[k] = s[k] + u[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) y[k] = quantized[k] - s[k]&lt;br /&gt;
 (5) x[k] = y[k] - u[k]&lt;br /&gt;
&lt;br /&gt;
Note that the following is true&lt;br /&gt;
&lt;br /&gt;
 time domain                          Z-transform&lt;br /&gt;
 -----------                          -----------&lt;br /&gt;
 quantized[k] = s[k] + u[k] + x[k]    Q(z) = S(z) + X(z) * N(z)&lt;br /&gt;
              = s[k] + y[k]                = S(z) + Y(z)&lt;br /&gt;
&lt;br /&gt;
u[k], wanted[k] are temporary variables. The values are not needed&lt;br /&gt;
outside this iteration. But the past filtered and unfiltered error&lt;br /&gt;
samples need to be remembered (y[] &amp;amp; x[]) upto some amount of samples&lt;br /&gt;
determined by the filter order.&lt;br /&gt;
&lt;br /&gt;
== Decoupling filtering and quantization ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Noiseshaper3.png|thumb|right|200px]]&lt;br /&gt;
As mentioned, the previous example is &#039;&#039;one possible&#039;&#039; implementation.&lt;br /&gt;
It&#039;s not the best possible one. But it is the first version that comes to mind.&lt;br /&gt;
There&#039;s another common formulation which requires&lt;br /&gt;
some more transfer function fiddeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt; N(z) = \frac{B(z)}{A(z)} = 1 - \frac{A(z)}{A(z)} + \frac{B(z)}{A(z)}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The two fractions have a common denominator so they can be easily&lt;br /&gt;
combined to one fraction. It&#039;s also possible to factor z^-1 out of the fraction:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = 1 - \frac{A(z)-B(z)}{A(z)} = 1 - z^{-1} \frac{z ( A(z)-B(z) )}{A(z)} =: 1 - z^{-1} H(z)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Substituting z*(A(Z)-B(Z)) for H(z) leads to a new filter that is related to N.&lt;br /&gt;
Note that with b0=1 it follows that z*(1-b0)=0. So, H(z) can be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{c_0 + c_1 z^{-1} + c_2 z^{-2} + \ldots}{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
with &amp;lt;math&amp;gt;c_{k-1} = a_k - b_k&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;k = 1...m&amp;lt;/math&amp;gt;. H(z)&lt;br /&gt;
is some &#039;&#039;arbitrary&#039;&#039; filter. The big advantage now is that it is&lt;br /&gt;
possible to plug nearly any digital filter implementation for H(z) into&lt;br /&gt;
the noise shaping loop. One obvious approach is the difference formula&lt;br /&gt;
from above but there are other alternatives with desirable properties.&lt;br /&gt;
However, the purpose of this article is not to discuss digital filters&lt;br /&gt;
in general but their application as noise shaping filters. So,&lt;br /&gt;
throughout the rest of this article H(z) is simply treated as a black&lt;br /&gt;
box. This black box has a certain state and gives us an output sample&lt;br /&gt;
for each input sample. The modified pseudo code loop looks like this:&lt;br /&gt;
&lt;br /&gt;
 (1) h_out[k] = H.filter(last_error)&lt;br /&gt;
 (2) wanted[k] = s[k] - h_out[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) last_error = quantized[k] - wanted[k]&lt;br /&gt;
&lt;br /&gt;
which is equivalent to the shown signal flow graph where the unit&lt;br /&gt;
sample delay z^-1 is implemented by the state variable &#039;last_error&#039;:&lt;br /&gt;
Remember that this black box filter H(z) doesn&#039;t correspond the the&lt;br /&gt;
actual noise shaping filter N(z). Depending on the context published&lt;br /&gt;
coefficients for noise shaping correspond either to N(z), H(z), or&lt;br /&gt;
-H(z).&lt;br /&gt;
&lt;br /&gt;
= Example filters =&lt;br /&gt;
&lt;br /&gt;
The following two filters have been designed by Sebastian Gesemann&lt;br /&gt;
via non-linear optimization to match a slightly modified version of&lt;br /&gt;
the LAME MP3 encoder&#039;s ATH (absolute threshold of hearing) curve. They&lt;br /&gt;
were first published in the&lt;br /&gt;
[http://www.hydrogenaudio.org/forums/index.php?showtopic=47980&amp;amp;pid=551558&amp;amp;st=0 Hydrogenaudio forum]&lt;br /&gt;
including the noise transfer function&#039;s second order factorizations.&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 44100 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.1474 z^{-1} +0.5383 z^{-2} -0.3520 z^{-3} +0.3475 z^{-4}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2061 -0.4706 z^{-1} -0.2534 z^{-2} -0.6214 z^{-3}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 48000 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.3344 z^{-1} +0.7455 z^{-2} -0.4602 z^{-3} +0.3463 z^{-4}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2374 -0.7339 z^{-1} -0.1251 z^{-2} -0.6033 z^{-3}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Notes=&lt;br /&gt;
&lt;br /&gt;
==References== &lt;br /&gt;
* Lipshitz. P, Stanley and John Vanderkooy. &amp;quot;Why 1-bit Sigma-Delta Conversion is Unsuitable for High-Quality Applications&amp;quot;. 110th Convention.  &amp;lt;i&amp;gt;Audio Engineering Society&amp;lt;/i&amp;gt; (May 2001). 2-4. &lt;br /&gt;
* &lt;br /&gt;
&lt;br /&gt;
[[Category:Signal Processing]]&lt;/div&gt;</summary>
		<author><name>SebastianG</name></author>
	</entry>
	<entry>
		<id>https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=19025</id>
		<title>Noise shaping</title>
		<link rel="alternate" type="text/html" href="https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=19025"/>
		<updated>2008-08-20T13:03:08Z</updated>

		<summary type="html">&lt;p&gt;SebastianG: /* FIR filters */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Noise shaping is a technique that is often used in combination with&lt;br /&gt;
[[Dithering|dithering]] in the context of [[Quantize|quantization]]. The purpose of noise shaping&lt;br /&gt;
is to alter the spectral &#039;&#039;shape&#039;&#039; of the error that is introduced due&lt;br /&gt;
to [[Dithering|dithering]] and [[Quantize|quantization]]. Sometimes it may be desirable to&lt;br /&gt;
concentrate most of the noise power in a certain frequency band so&lt;br /&gt;
that the overall noise becomes less perceivable.&lt;br /&gt;
&lt;br /&gt;
== Applications ==&lt;br /&gt;
&lt;br /&gt;
In the field of digital image processing a popular noise shaping&lt;br /&gt;
technique is known as &amp;quot;Floyd Steinberg dithering&amp;quot;. This algorithm is&lt;br /&gt;
used to reduce the noise power at lower spatial frequencies which&lt;br /&gt;
results in an improved perceptual quality.&lt;br /&gt;
&lt;br /&gt;
For word length reductions of audio signals (i.e conversion from a 24bit&lt;br /&gt;
signal to a 16bit signal) noise shaping can be used to push quantization&lt;br /&gt;
noise out of perceptually important frequency bands to perceptually less&lt;br /&gt;
important frequency bands (&amp;gt;20 kHz) in order to reduce the perceived&lt;br /&gt;
loudness of the quantization noise.&lt;br /&gt;
&lt;br /&gt;
Another example is the &lt;br /&gt;
[[Super Audio Compact Disc|Super Audio CD]]. The SACD format represents&lt;br /&gt;
the most extreme trade-off between word length (1bit/sample) and&lt;br /&gt;
sampling frequency (2.8 Mhz). Due to the low word length high power&lt;br /&gt;
noise is introduced. With the help of noise shaping this noise is&lt;br /&gt;
usually rejected by about 120 dB in the audible band (0-20 kHz) at the&lt;br /&gt;
cost of a higher noise power in the ultrasonic range (+3.5dB for&lt;br /&gt;
frequencies above 100 kHz).&lt;br /&gt;
&lt;br /&gt;
== Limits &amp;amp; filter design restrictions ==&lt;br /&gt;
&lt;br /&gt;
Noise Shaping without [[Dithering|&#039;&#039;dithered&#039;&#039;]] quantization is also possible but&lt;br /&gt;
less useful because the spectral characteristics of &#039;&#039;undithered&#039;&#039;&lt;br /&gt;
quantization noise is not easily predictable and depends heavily on the&lt;br /&gt;
digital signal that is to be quantized. The SACD case is an example where&lt;br /&gt;
[[Dithering|dithering]] can&#039;t be properly applied which may lead to a spectral&lt;br /&gt;
noise power distribution that deviates from the indented distribution. This has&lt;br /&gt;
been demonstrated by Lipshitz and Vanderkooy in&lt;br /&gt;
[http://www.sjeng.org/ftp/SACD.pdf their] [1] paper. &lt;br /&gt;
&lt;br /&gt;
It&#039;s not possible to lower the noise power for &#039;&#039;all&#039;&#039; frequencies&lt;br /&gt;
via noise shaping because this would be in violation of the&lt;br /&gt;
rate/distortion principles. But it &#039;&#039;is&#039;&#039; possible to lower the noise&lt;br /&gt;
in certain frequency bands at the cost of increased noise power in other&lt;br /&gt;
frequency bands. This property is also known as the&lt;br /&gt;
&#039;&#039;noise shaping theorem&#039;&#039;. It states that the average logarithmic magnitude response of&lt;br /&gt;
the corresponding noise shaping &#039;&#039;filter&#039;&#039; over the whole linear&lt;br /&gt;
frequency axis cannot drop below 0dB.&lt;br /&gt;
&lt;br /&gt;
The noise shaping application poses some restrictions on the fiter&lt;br /&gt;
design. The first sample of the filter&#039;s impulse response always equals&lt;br /&gt;
one. Also, interesting filters for noise shaping are so-called&lt;br /&gt;
&#039;&#039;minimum phase filters&#039;&#039;. This type of filter performs optimal in the sense of&lt;br /&gt;
the noise shaping theorem because the average logarithmig magnitude&lt;br /&gt;
response &#039;&#039;is&#039;&#039; 0dB.&lt;br /&gt;
&lt;br /&gt;
For the special [[Super Audio Compact Disc|SACD]] case an additional&lt;br /&gt;
constraint comes into play: The magnitude response should not exceed&lt;br /&gt;
3.5dB. This is enforced to prevent so-called limit cycles.&lt;br /&gt;
&lt;br /&gt;
The design of these kinds of filters is however not trivial. This is not&lt;br /&gt;
covered by this article.&lt;br /&gt;
&lt;br /&gt;
== Digital filter reminder ==&lt;br /&gt;
&lt;br /&gt;
A common representation of digital filters is their &#039;&#039;transfer function&#039;&#039;&lt;br /&gt;
in form of a rational polynomial in z^-1. These polynomials are defined&lt;br /&gt;
by their coefficients b0,b1,b2,...,a1,a2,...:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{B(z)}{A(z)} = \frac{b_0 + b_1 z^{-1} + b_2 z^{-2} + \ldots + b_m z^{-m}}&lt;br /&gt;
{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots + a_m z^{-m}} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &#039;N&#039; is the transfer function and &#039;m&#039; the filter&#039;s &#039;&#039;order&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
=== FIR filters ===&lt;br /&gt;
&lt;br /&gt;
In case a1=a2=a3=...=0 the filter&#039;s [http://en.wikipedia.org/wiki/Impulse_response impulse response] is finite which is&lt;br /&gt;
why these filters are called FIR ([[Finite Impulse Response Filter|finite impulse response]])&lt;br /&gt;
filters as opposed to IIR&lt;br /&gt;
[[Infinite Impulse Response Filter|infinite impulse response]] filters. Note that in case of&lt;br /&gt;
FIR filters the coefficients b0, b1, ... correspond to the impulse&lt;br /&gt;
response. The impulse response is another way a filter can be&lt;br /&gt;
characterized and describes what the filter does to a unit pulse. Let&#039;s&lt;br /&gt;
assume for the moment that a1=a2=a3=...=0. The the impulse response&lt;br /&gt;
would be:&lt;br /&gt;
&lt;br /&gt;
 x:  0  0  1  0  0  0  0  ... 0  0  0 ... (input signal)&lt;br /&gt;
 y:  0  0  b0 b1 b2 b3 b4 ... 0  0  0 ... (output signal)&lt;br /&gt;
&lt;br /&gt;
It helps to think of negative powers of z in transfer functions as a&lt;br /&gt;
delay: z^-1 = delay of one sample. z^-2 = delay of two samples etc.&lt;br /&gt;
Since a digital filter is a&lt;br /&gt;
[http://en.wikipedia.org/wiki/Discrete_system discrete-time]&lt;br /&gt;
[http://en.wikipedia.org/wiki/LTI_system_theory linear and time-invariant]&lt;br /&gt;
system it is&lt;br /&gt;
possible to determine the filter&#039;s output by a superposition of scaled&lt;br /&gt;
and shifted impulse responses. This leads to the following relationship&lt;br /&gt;
between input and output in the case of FIR filters:&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(z) = X(z) * B(z)&lt;br /&gt;
&lt;br /&gt;
where x[] is the input signal, y[] the output signal and k the current&lt;br /&gt;
sample index. This is a simple convolution directly computed using the&lt;br /&gt;
given impulse response. The convolution in the time domain corresponds&lt;br /&gt;
to a multiplication in the Z-domain.&lt;br /&gt;
&lt;br /&gt;
== IIR filters ==&lt;br /&gt;
&lt;br /&gt;
IIR filters are a simple extension to the FIR case:&lt;br /&gt;
&lt;br /&gt;
 time domain                                Z-transform&lt;br /&gt;
 -----------                                -----------&lt;br /&gt;
      y[k] + a1 y[k-1] + a2 y[k-2] + ...    Y(z) * A(Z) = X(Z) * B(Z)&lt;br /&gt;
 = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Solving for Y we get&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(Z) = X(Z) * N(z)&lt;br /&gt;
                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
which is the well known &#039;&#039;difference formula&#039;&#039;. Its signal flow graph&lt;br /&gt;
representation is known as&lt;br /&gt;
[http://ccrma.stanford.edu/~jos/filters/Direct_Form_I.html direct form I].&lt;br /&gt;
&lt;br /&gt;
= Noise Shaper Implementation =&lt;br /&gt;
&lt;br /&gt;
Usually a filter&#039;s transfer function is called &#039;H&#039;. In this article &#039;N&#039;&lt;br /&gt;
has been used because this filter is also known as noise transfer&lt;br /&gt;
function (NTF) in the context of noise shaping. Later, another filter&lt;br /&gt;
&#039;H&#039; is introduced that has a special relation to &#039;N&#039;.&lt;br /&gt;
&lt;br /&gt;
== Direct Form I ==&lt;br /&gt;
&lt;br /&gt;
For convenience let us substitute the actual difference in the&lt;br /&gt;
difference formula for &#039;u&#039;. Also, b0 must be equal to one for noise&lt;br /&gt;
shaping filters as mentioned above. This leads to&lt;br /&gt;
&lt;br /&gt;
 y[k] = x[k] + u[k]   with u[k] = b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
                                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Note that since the filter is supposed to be applied on the quantization&lt;br /&gt;
noise y[] corresponds to the filtered noise and x[] to the unfiltered&lt;br /&gt;
noise. Suppose s[] is the signal we&#039;re trying to quantize. The following&lt;br /&gt;
pseudo code snippet shows the inner loop over k of &#039;&#039;one possible&#039;&#039;&lt;br /&gt;
noise shaping implementation:&lt;br /&gt;
&lt;br /&gt;
 (1) compute u[k]&lt;br /&gt;
 (2) wanted[k] = s[k] + u[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) y[k] = quantized[k] - s[k]&lt;br /&gt;
 (5) x[k] = y[k] - u[k]&lt;br /&gt;
&lt;br /&gt;
Note that the following is true&lt;br /&gt;
&lt;br /&gt;
 time domain                          Z-transform&lt;br /&gt;
 -----------                          -----------&lt;br /&gt;
 quantized[k] = s[k] + u[k] + x[k]    Q(z) = S(z) + X(z) * N(z)&lt;br /&gt;
              = s[k] + y[k]                = S(z) + Y(z)&lt;br /&gt;
&lt;br /&gt;
u[k], wanted[k] are temporary variables. The values are not needed&lt;br /&gt;
outside this iteration. But the past filtered and unfiltered error&lt;br /&gt;
samples need to be remembered (y[] &amp;amp; x[]) upto some amount of samples&lt;br /&gt;
determined by the filter order.&lt;br /&gt;
&lt;br /&gt;
== Decoupling filtering and quantization ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Noiseshaper3.png|thumb|right|200px]]&lt;br /&gt;
As mentioned, the previous example is &#039;&#039;one possible&#039;&#039; implementation.&lt;br /&gt;
It&#039;s not the best possible one. But it is the first version that comes to mind.&lt;br /&gt;
There&#039;s another common formulation which requires&lt;br /&gt;
some more transfer function fiddeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt; N(z) = \frac{B(z)}{A(z)} = 1 - \frac{A(z)}{A(z)} + \frac{B(z)}{A(z)}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The two fractions have a common denominator so they can be easily&lt;br /&gt;
combined to one fraction. It&#039;s also possible to factor z^-1 out of the fraction:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = 1 - \frac{A(z)-B(z)}{A(z)} = 1 - z^{-1} \frac{z ( A(z)-B(z) )}{A(z)} =: 1 - z^{-1} H(z)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Substituting z*(A(Z)-B(Z)) for H(z) leads to a new filter that is related to N.&lt;br /&gt;
Note that with b0=1 it follows that z*(1-b0)=0. So, H(z) can be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{c_0 + c_1 z^{-1} + c_2 z^{-2} + \ldots}{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
with &amp;lt;math&amp;gt;c_{k-1} = a_k - b_k&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;k = 1...m&amp;lt;/math&amp;gt;. H(z)&lt;br /&gt;
is some &#039;&#039;arbitrary&#039;&#039; filter. The big advantage now is that it is&lt;br /&gt;
possible to plug nearly any digital filter implementation for H(z) into&lt;br /&gt;
the noise shaping loop. One obvious approach is the difference formula&lt;br /&gt;
from above but there are other alternatives with desirable properties.&lt;br /&gt;
However, the purpose of this article is not to discuss digital filters&lt;br /&gt;
in general but their application as noise shaping filters. So,&lt;br /&gt;
throughout the rest of this article H(z) is simply treated as a black&lt;br /&gt;
box. This black box has a certain state and gives us an output sample&lt;br /&gt;
for each input sample. The modified pseudo code loop looks like this:&lt;br /&gt;
&lt;br /&gt;
 (1) h_out[k] = H.filter(last_error)&lt;br /&gt;
 (2) wanted[k] = s[k] - h_out[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) last_error = quantized[k] - wanted[k]&lt;br /&gt;
&lt;br /&gt;
which is equivalent to the shown signal flow graph where the unit&lt;br /&gt;
sample delay z^-1 is implemented by the state variable &#039;last_error&#039;:&lt;br /&gt;
Remember that this black box filter H(z) doesn&#039;t correspond the the&lt;br /&gt;
actual noise shaping filter N(z). Depending on the context published&lt;br /&gt;
coefficients for noise shaping correspond either to N(z), H(z), or&lt;br /&gt;
-H(z).&lt;br /&gt;
&lt;br /&gt;
= Example filters =&lt;br /&gt;
&lt;br /&gt;
The following two filters have been designed by Sebastian Gesemann&lt;br /&gt;
via non-linear optimization to match a slightly modified version of&lt;br /&gt;
the LAME MP3 encoder&#039;s ATH (absolute threshold of hearing) curve. They&lt;br /&gt;
were first published in the&lt;br /&gt;
[http://www.hydrogenaudio.org/forums/index.php?showtopic=47980&amp;amp;pid=551558&amp;amp;st=0 Hydrogenaudio forum]&lt;br /&gt;
including the noise transfer function&#039;s second order factorizations.&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 44100 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.1474 z^{-1} +0.5383 z^{-2} -0.3520 z^{-3} +0.3475 z^{-4}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2061 -0.4706 z^{-1} -0.2534 z^{-2} -0.6214 z^{-3}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 48000 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.3344 z^{-1} +0.7455 z^{-2} -0.4602 z^{-3} +0.3463 z^{-4}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2374 -0.7339 z^{-1} -0.1251 z^{-2} -0.6033 z^{-3}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Notes=&lt;br /&gt;
&lt;br /&gt;
==References== &lt;br /&gt;
* Lipshitz. P, Stanley and John Vanderkooy. &amp;quot;Why 1-bit Sigma-Delta Conversion is Unsuitable for High-Quality Applications&amp;quot;. 110th Convention.  &amp;lt;i&amp;gt;Audio Engineering Society&amp;lt;/i&amp;gt; (May 2001). 2-4. &lt;br /&gt;
* &lt;br /&gt;
&lt;br /&gt;
[[Category:Signal Processing]]&lt;/div&gt;</summary>
		<author><name>SebastianG</name></author>
	</entry>
	<entry>
		<id>https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=19024</id>
		<title>Noise shaping</title>
		<link rel="alternate" type="text/html" href="https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=19024"/>
		<updated>2008-08-20T13:02:43Z</updated>

		<summary type="html">&lt;p&gt;SebastianG: /* Digital filter reminder */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Noise shaping is a technique that is often used in combination with&lt;br /&gt;
[[Dithering|dithering]] in the context of [[Quantize|quantization]]. The purpose of noise shaping&lt;br /&gt;
is to alter the spectral &#039;&#039;shape&#039;&#039; of the error that is introduced due&lt;br /&gt;
to [[Dithering|dithering]] and [[Quantize|quantization]]. Sometimes it may be desirable to&lt;br /&gt;
concentrate most of the noise power in a certain frequency band so&lt;br /&gt;
that the overall noise becomes less perceivable.&lt;br /&gt;
&lt;br /&gt;
== Applications ==&lt;br /&gt;
&lt;br /&gt;
In the field of digital image processing a popular noise shaping&lt;br /&gt;
technique is known as &amp;quot;Floyd Steinberg dithering&amp;quot;. This algorithm is&lt;br /&gt;
used to reduce the noise power at lower spatial frequencies which&lt;br /&gt;
results in an improved perceptual quality.&lt;br /&gt;
&lt;br /&gt;
For word length reductions of audio signals (i.e conversion from a 24bit&lt;br /&gt;
signal to a 16bit signal) noise shaping can be used to push quantization&lt;br /&gt;
noise out of perceptually important frequency bands to perceptually less&lt;br /&gt;
important frequency bands (&amp;gt;20 kHz) in order to reduce the perceived&lt;br /&gt;
loudness of the quantization noise.&lt;br /&gt;
&lt;br /&gt;
Another example is the &lt;br /&gt;
[[Super Audio Compact Disc|Super Audio CD]]. The SACD format represents&lt;br /&gt;
the most extreme trade-off between word length (1bit/sample) and&lt;br /&gt;
sampling frequency (2.8 Mhz). Due to the low word length high power&lt;br /&gt;
noise is introduced. With the help of noise shaping this noise is&lt;br /&gt;
usually rejected by about 120 dB in the audible band (0-20 kHz) at the&lt;br /&gt;
cost of a higher noise power in the ultrasonic range (+3.5dB for&lt;br /&gt;
frequencies above 100 kHz).&lt;br /&gt;
&lt;br /&gt;
== Limits &amp;amp; filter design restrictions ==&lt;br /&gt;
&lt;br /&gt;
Noise Shaping without [[Dithering|&#039;&#039;dithered&#039;&#039;]] quantization is also possible but&lt;br /&gt;
less useful because the spectral characteristics of &#039;&#039;undithered&#039;&#039;&lt;br /&gt;
quantization noise is not easily predictable and depends heavily on the&lt;br /&gt;
digital signal that is to be quantized. The SACD case is an example where&lt;br /&gt;
[[Dithering|dithering]] can&#039;t be properly applied which may lead to a spectral&lt;br /&gt;
noise power distribution that deviates from the indented distribution. This has&lt;br /&gt;
been demonstrated by Lipshitz and Vanderkooy in&lt;br /&gt;
[http://www.sjeng.org/ftp/SACD.pdf their] [1] paper. &lt;br /&gt;
&lt;br /&gt;
It&#039;s not possible to lower the noise power for &#039;&#039;all&#039;&#039; frequencies&lt;br /&gt;
via noise shaping because this would be in violation of the&lt;br /&gt;
rate/distortion principles. But it &#039;&#039;is&#039;&#039; possible to lower the noise&lt;br /&gt;
in certain frequency bands at the cost of increased noise power in other&lt;br /&gt;
frequency bands. This property is also known as the&lt;br /&gt;
&#039;&#039;noise shaping theorem&#039;&#039;. It states that the average logarithmic magnitude response of&lt;br /&gt;
the corresponding noise shaping &#039;&#039;filter&#039;&#039; over the whole linear&lt;br /&gt;
frequency axis cannot drop below 0dB.&lt;br /&gt;
&lt;br /&gt;
The noise shaping application poses some restrictions on the fiter&lt;br /&gt;
design. The first sample of the filter&#039;s impulse response always equals&lt;br /&gt;
one. Also, interesting filters for noise shaping are so-called&lt;br /&gt;
&#039;&#039;minimum phase filters&#039;&#039;. This type of filter performs optimal in the sense of&lt;br /&gt;
the noise shaping theorem because the average logarithmig magnitude&lt;br /&gt;
response &#039;&#039;is&#039;&#039; 0dB.&lt;br /&gt;
&lt;br /&gt;
For the special [[Super Audio Compact Disc|SACD]] case an additional&lt;br /&gt;
constraint comes into play: The magnitude response should not exceed&lt;br /&gt;
3.5dB. This is enforced to prevent so-called limit cycles.&lt;br /&gt;
&lt;br /&gt;
The design of these kinds of filters is however not trivial. This is not&lt;br /&gt;
covered by this article.&lt;br /&gt;
&lt;br /&gt;
== Digital filter reminder ==&lt;br /&gt;
&lt;br /&gt;
A common representation of digital filters is their &#039;&#039;transfer function&#039;&#039;&lt;br /&gt;
in form of a rational polynomial in z^-1. These polynomials are defined&lt;br /&gt;
by their coefficients b0,b1,b2,...,a1,a2,...:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{B(z)}{A(z)} = \frac{b_0 + b_1 z^{-1} + b_2 z^{-2} + \ldots + b_m z^{-m}}&lt;br /&gt;
{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots + a_m z^{-m}} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &#039;N&#039; is the transfer function and &#039;m&#039; the filter&#039;s &#039;&#039;order&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
== FIR filters ==&lt;br /&gt;
&lt;br /&gt;
In case a1=a2=a3=...=0 the filter&#039;s [http://en.wikipedia.org/wiki/Impulse_response impulse response] is finite which is&lt;br /&gt;
why these filters are called FIR ([[Finite Impulse Response Filter|finite impulse response]])&lt;br /&gt;
filters as opposed to IIR&lt;br /&gt;
[[Infinite Impulse Response Filter|infinite impulse response]] filters. Note that in case of&lt;br /&gt;
FIR filters the coefficients b0, b1, ... correspond to the impulse&lt;br /&gt;
response. The impulse response is another way a filter can be&lt;br /&gt;
characterized and describes what the filter does to a unit pulse. Let&#039;s&lt;br /&gt;
assume for the moment that a1=a2=a3=...=0. The the impulse response&lt;br /&gt;
would be:&lt;br /&gt;
&lt;br /&gt;
 x:  0  0  1  0  0  0  0  ... 0  0  0 ... (input signal)&lt;br /&gt;
 y:  0  0  b0 b1 b2 b3 b4 ... 0  0  0 ... (output signal)&lt;br /&gt;
&lt;br /&gt;
It helps to think of negative powers of z in transfer functions as a&lt;br /&gt;
delay: z^-1 = delay of one sample. z^-2 = delay of two samples etc.&lt;br /&gt;
Since a digital filter is a&lt;br /&gt;
[http://en.wikipedia.org/wiki/Discrete_system discrete-time]&lt;br /&gt;
[http://en.wikipedia.org/wiki/LTI_system_theory linear and time-invariant]&lt;br /&gt;
system it is&lt;br /&gt;
possible to determine the filter&#039;s output by a superposition of scaled&lt;br /&gt;
and shifted impulse responses. This leads to the following relationship&lt;br /&gt;
between input and output in the case of FIR filters:&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(z) = X(z) * B(z)&lt;br /&gt;
&lt;br /&gt;
where x[] is the input signal, y[] the output signal and k the current&lt;br /&gt;
sample index. This is a simple convolution directly computed using the&lt;br /&gt;
given impulse response. The convolution in the time domain corresponds&lt;br /&gt;
to a multiplication in the Z-domain.&lt;br /&gt;
&lt;br /&gt;
== IIR filters ==&lt;br /&gt;
&lt;br /&gt;
IIR filters are a simple extension to the FIR case:&lt;br /&gt;
&lt;br /&gt;
 time domain                                Z-transform&lt;br /&gt;
 -----------                                -----------&lt;br /&gt;
      y[k] + a1 y[k-1] + a2 y[k-2] + ...    Y(z) * A(Z) = X(Z) * B(Z)&lt;br /&gt;
 = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Solving for Y we get&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(Z) = X(Z) * N(z)&lt;br /&gt;
                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
which is the well known &#039;&#039;difference formula&#039;&#039;. Its signal flow graph&lt;br /&gt;
representation is known as&lt;br /&gt;
[http://ccrma.stanford.edu/~jos/filters/Direct_Form_I.html direct form I].&lt;br /&gt;
&lt;br /&gt;
= Noise Shaper Implementation =&lt;br /&gt;
&lt;br /&gt;
Usually a filter&#039;s transfer function is called &#039;H&#039;. In this article &#039;N&#039;&lt;br /&gt;
has been used because this filter is also known as noise transfer&lt;br /&gt;
function (NTF) in the context of noise shaping. Later, another filter&lt;br /&gt;
&#039;H&#039; is introduced that has a special relation to &#039;N&#039;.&lt;br /&gt;
&lt;br /&gt;
== Direct Form I ==&lt;br /&gt;
&lt;br /&gt;
For convenience let us substitute the actual difference in the&lt;br /&gt;
difference formula for &#039;u&#039;. Also, b0 must be equal to one for noise&lt;br /&gt;
shaping filters as mentioned above. This leads to&lt;br /&gt;
&lt;br /&gt;
 y[k] = x[k] + u[k]   with u[k] = b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
                                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Note that since the filter is supposed to be applied on the quantization&lt;br /&gt;
noise y[] corresponds to the filtered noise and x[] to the unfiltered&lt;br /&gt;
noise. Suppose s[] is the signal we&#039;re trying to quantize. The following&lt;br /&gt;
pseudo code snippet shows the inner loop over k of &#039;&#039;one possible&#039;&#039;&lt;br /&gt;
noise shaping implementation:&lt;br /&gt;
&lt;br /&gt;
 (1) compute u[k]&lt;br /&gt;
 (2) wanted[k] = s[k] + u[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) y[k] = quantized[k] - s[k]&lt;br /&gt;
 (5) x[k] = y[k] - u[k]&lt;br /&gt;
&lt;br /&gt;
Note that the following is true&lt;br /&gt;
&lt;br /&gt;
 time domain                          Z-transform&lt;br /&gt;
 -----------                          -----------&lt;br /&gt;
 quantized[k] = s[k] + u[k] + x[k]    Q(z) = S(z) + X(z) * N(z)&lt;br /&gt;
              = s[k] + y[k]                = S(z) + Y(z)&lt;br /&gt;
&lt;br /&gt;
u[k], wanted[k] are temporary variables. The values are not needed&lt;br /&gt;
outside this iteration. But the past filtered and unfiltered error&lt;br /&gt;
samples need to be remembered (y[] &amp;amp; x[]) upto some amount of samples&lt;br /&gt;
determined by the filter order.&lt;br /&gt;
&lt;br /&gt;
== Decoupling filtering and quantization ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Noiseshaper3.png|thumb|right|200px]]&lt;br /&gt;
As mentioned, the previous example is &#039;&#039;one possible&#039;&#039; implementation.&lt;br /&gt;
It&#039;s not the best possible one. But it is the first version that comes to mind.&lt;br /&gt;
There&#039;s another common formulation which requires&lt;br /&gt;
some more transfer function fiddeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt; N(z) = \frac{B(z)}{A(z)} = 1 - \frac{A(z)}{A(z)} + \frac{B(z)}{A(z)}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The two fractions have a common denominator so they can be easily&lt;br /&gt;
combined to one fraction. It&#039;s also possible to factor z^-1 out of the fraction:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = 1 - \frac{A(z)-B(z)}{A(z)} = 1 - z^{-1} \frac{z ( A(z)-B(z) )}{A(z)} =: 1 - z^{-1} H(z)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Substituting z*(A(Z)-B(Z)) for H(z) leads to a new filter that is related to N.&lt;br /&gt;
Note that with b0=1 it follows that z*(1-b0)=0. So, H(z) can be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{c_0 + c_1 z^{-1} + c_2 z^{-2} + \ldots}{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
with &amp;lt;math&amp;gt;c_{k-1} = a_k - b_k&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;k = 1...m&amp;lt;/math&amp;gt;. H(z)&lt;br /&gt;
is some &#039;&#039;arbitrary&#039;&#039; filter. The big advantage now is that it is&lt;br /&gt;
possible to plug nearly any digital filter implementation for H(z) into&lt;br /&gt;
the noise shaping loop. One obvious approach is the difference formula&lt;br /&gt;
from above but there are other alternatives with desirable properties.&lt;br /&gt;
However, the purpose of this article is not to discuss digital filters&lt;br /&gt;
in general but their application as noise shaping filters. So,&lt;br /&gt;
throughout the rest of this article H(z) is simply treated as a black&lt;br /&gt;
box. This black box has a certain state and gives us an output sample&lt;br /&gt;
for each input sample. The modified pseudo code loop looks like this:&lt;br /&gt;
&lt;br /&gt;
 (1) h_out[k] = H.filter(last_error)&lt;br /&gt;
 (2) wanted[k] = s[k] - h_out[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) last_error = quantized[k] - wanted[k]&lt;br /&gt;
&lt;br /&gt;
which is equivalent to the shown signal flow graph where the unit&lt;br /&gt;
sample delay z^-1 is implemented by the state variable &#039;last_error&#039;:&lt;br /&gt;
Remember that this black box filter H(z) doesn&#039;t correspond the the&lt;br /&gt;
actual noise shaping filter N(z). Depending on the context published&lt;br /&gt;
coefficients for noise shaping correspond either to N(z), H(z), or&lt;br /&gt;
-H(z).&lt;br /&gt;
&lt;br /&gt;
= Example filters =&lt;br /&gt;
&lt;br /&gt;
The following two filters have been designed by Sebastian Gesemann&lt;br /&gt;
via non-linear optimization to match a slightly modified version of&lt;br /&gt;
the LAME MP3 encoder&#039;s ATH (absolute threshold of hearing) curve. They&lt;br /&gt;
were first published in the&lt;br /&gt;
[http://www.hydrogenaudio.org/forums/index.php?showtopic=47980&amp;amp;pid=551558&amp;amp;st=0 Hydrogenaudio forum]&lt;br /&gt;
including the noise transfer function&#039;s second order factorizations.&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 44100 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.1474 z^{-1} +0.5383 z^{-2} -0.3520 z^{-3} +0.3475 z^{-4}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2061 -0.4706 z^{-1} -0.2534 z^{-2} -0.6214 z^{-3}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 48000 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.3344 z^{-1} +0.7455 z^{-2} -0.4602 z^{-3} +0.3463 z^{-4}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2374 -0.7339 z^{-1} -0.1251 z^{-2} -0.6033 z^{-3}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Notes=&lt;br /&gt;
&lt;br /&gt;
==References== &lt;br /&gt;
* Lipshitz. P, Stanley and John Vanderkooy. &amp;quot;Why 1-bit Sigma-Delta Conversion is Unsuitable for High-Quality Applications&amp;quot;. 110th Convention.  &amp;lt;i&amp;gt;Audio Engineering Society&amp;lt;/i&amp;gt; (May 2001). 2-4. &lt;br /&gt;
* &lt;br /&gt;
&lt;br /&gt;
[[Category:Signal Processing]]&lt;/div&gt;</summary>
		<author><name>SebastianG</name></author>
	</entry>
	<entry>
		<id>https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=19023</id>
		<title>Noise shaping</title>
		<link rel="alternate" type="text/html" href="https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=19023"/>
		<updated>2008-08-20T13:02:29Z</updated>

		<summary type="html">&lt;p&gt;SebastianG: /* Limits &amp;amp; filter design restrictions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Noise shaping is a technique that is often used in combination with&lt;br /&gt;
[[Dithering|dithering]] in the context of [[Quantize|quantization]]. The purpose of noise shaping&lt;br /&gt;
is to alter the spectral &#039;&#039;shape&#039;&#039; of the error that is introduced due&lt;br /&gt;
to [[Dithering|dithering]] and [[Quantize|quantization]]. Sometimes it may be desirable to&lt;br /&gt;
concentrate most of the noise power in a certain frequency band so&lt;br /&gt;
that the overall noise becomes less perceivable.&lt;br /&gt;
&lt;br /&gt;
== Applications ==&lt;br /&gt;
&lt;br /&gt;
In the field of digital image processing a popular noise shaping&lt;br /&gt;
technique is known as &amp;quot;Floyd Steinberg dithering&amp;quot;. This algorithm is&lt;br /&gt;
used to reduce the noise power at lower spatial frequencies which&lt;br /&gt;
results in an improved perceptual quality.&lt;br /&gt;
&lt;br /&gt;
For word length reductions of audio signals (i.e conversion from a 24bit&lt;br /&gt;
signal to a 16bit signal) noise shaping can be used to push quantization&lt;br /&gt;
noise out of perceptually important frequency bands to perceptually less&lt;br /&gt;
important frequency bands (&amp;gt;20 kHz) in order to reduce the perceived&lt;br /&gt;
loudness of the quantization noise.&lt;br /&gt;
&lt;br /&gt;
Another example is the &lt;br /&gt;
[[Super Audio Compact Disc|Super Audio CD]]. The SACD format represents&lt;br /&gt;
the most extreme trade-off between word length (1bit/sample) and&lt;br /&gt;
sampling frequency (2.8 Mhz). Due to the low word length high power&lt;br /&gt;
noise is introduced. With the help of noise shaping this noise is&lt;br /&gt;
usually rejected by about 120 dB in the audible band (0-20 kHz) at the&lt;br /&gt;
cost of a higher noise power in the ultrasonic range (+3.5dB for&lt;br /&gt;
frequencies above 100 kHz).&lt;br /&gt;
&lt;br /&gt;
== Limits &amp;amp; filter design restrictions ==&lt;br /&gt;
&lt;br /&gt;
Noise Shaping without [[Dithering|&#039;&#039;dithered&#039;&#039;]] quantization is also possible but&lt;br /&gt;
less useful because the spectral characteristics of &#039;&#039;undithered&#039;&#039;&lt;br /&gt;
quantization noise is not easily predictable and depends heavily on the&lt;br /&gt;
digital signal that is to be quantized. The SACD case is an example where&lt;br /&gt;
[[Dithering|dithering]] can&#039;t be properly applied which may lead to a spectral&lt;br /&gt;
noise power distribution that deviates from the indented distribution. This has&lt;br /&gt;
been demonstrated by Lipshitz and Vanderkooy in&lt;br /&gt;
[http://www.sjeng.org/ftp/SACD.pdf their] [1] paper. &lt;br /&gt;
&lt;br /&gt;
It&#039;s not possible to lower the noise power for &#039;&#039;all&#039;&#039; frequencies&lt;br /&gt;
via noise shaping because this would be in violation of the&lt;br /&gt;
rate/distortion principles. But it &#039;&#039;is&#039;&#039; possible to lower the noise&lt;br /&gt;
in certain frequency bands at the cost of increased noise power in other&lt;br /&gt;
frequency bands. This property is also known as the&lt;br /&gt;
&#039;&#039;noise shaping theorem&#039;&#039;. It states that the average logarithmic magnitude response of&lt;br /&gt;
the corresponding noise shaping &#039;&#039;filter&#039;&#039; over the whole linear&lt;br /&gt;
frequency axis cannot drop below 0dB.&lt;br /&gt;
&lt;br /&gt;
The noise shaping application poses some restrictions on the fiter&lt;br /&gt;
design. The first sample of the filter&#039;s impulse response always equals&lt;br /&gt;
one. Also, interesting filters for noise shaping are so-called&lt;br /&gt;
&#039;&#039;minimum phase filters&#039;&#039;. This type of filter performs optimal in the sense of&lt;br /&gt;
the noise shaping theorem because the average logarithmig magnitude&lt;br /&gt;
response &#039;&#039;is&#039;&#039; 0dB.&lt;br /&gt;
&lt;br /&gt;
For the special [[Super Audio Compact Disc|SACD]] case an additional&lt;br /&gt;
constraint comes into play: The magnitude response should not exceed&lt;br /&gt;
3.5dB. This is enforced to prevent so-called limit cycles.&lt;br /&gt;
&lt;br /&gt;
The design of these kinds of filters is however not trivial. This is not&lt;br /&gt;
covered by this article.&lt;br /&gt;
&lt;br /&gt;
= Digital filter reminder =&lt;br /&gt;
&lt;br /&gt;
A common representation of digital filters is their &#039;&#039;transfer function&#039;&#039;&lt;br /&gt;
in form of a rational polynomial in z^-1. These polynomials are defined&lt;br /&gt;
by their coefficients b0,b1,b2,...,a1,a2,...:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{B(z)}{A(z)} = \frac{b_0 + b_1 z^{-1} + b_2 z^{-2} + \ldots + b_m z^{-m}}&lt;br /&gt;
{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots + a_m z^{-m}} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &#039;N&#039; is the transfer function and &#039;m&#039; the filter&#039;s &#039;&#039;order&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
== FIR filters ==&lt;br /&gt;
&lt;br /&gt;
In case a1=a2=a3=...=0 the filter&#039;s [http://en.wikipedia.org/wiki/Impulse_response impulse response] is finite which is&lt;br /&gt;
why these filters are called FIR ([[Finite Impulse Response Filter|finite impulse response]])&lt;br /&gt;
filters as opposed to IIR&lt;br /&gt;
[[Infinite Impulse Response Filter|infinite impulse response]] filters. Note that in case of&lt;br /&gt;
FIR filters the coefficients b0, b1, ... correspond to the impulse&lt;br /&gt;
response. The impulse response is another way a filter can be&lt;br /&gt;
characterized and describes what the filter does to a unit pulse. Let&#039;s&lt;br /&gt;
assume for the moment that a1=a2=a3=...=0. The the impulse response&lt;br /&gt;
would be:&lt;br /&gt;
&lt;br /&gt;
 x:  0  0  1  0  0  0  0  ... 0  0  0 ... (input signal)&lt;br /&gt;
 y:  0  0  b0 b1 b2 b3 b4 ... 0  0  0 ... (output signal)&lt;br /&gt;
&lt;br /&gt;
It helps to think of negative powers of z in transfer functions as a&lt;br /&gt;
delay: z^-1 = delay of one sample. z^-2 = delay of two samples etc.&lt;br /&gt;
Since a digital filter is a&lt;br /&gt;
[http://en.wikipedia.org/wiki/Discrete_system discrete-time]&lt;br /&gt;
[http://en.wikipedia.org/wiki/LTI_system_theory linear and time-invariant]&lt;br /&gt;
system it is&lt;br /&gt;
possible to determine the filter&#039;s output by a superposition of scaled&lt;br /&gt;
and shifted impulse responses. This leads to the following relationship&lt;br /&gt;
between input and output in the case of FIR filters:&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(z) = X(z) * B(z)&lt;br /&gt;
&lt;br /&gt;
where x[] is the input signal, y[] the output signal and k the current&lt;br /&gt;
sample index. This is a simple convolution directly computed using the&lt;br /&gt;
given impulse response. The convolution in the time domain corresponds&lt;br /&gt;
to a multiplication in the Z-domain.&lt;br /&gt;
&lt;br /&gt;
== IIR filters ==&lt;br /&gt;
&lt;br /&gt;
IIR filters are a simple extension to the FIR case:&lt;br /&gt;
&lt;br /&gt;
 time domain                                Z-transform&lt;br /&gt;
 -----------                                -----------&lt;br /&gt;
      y[k] + a1 y[k-1] + a2 y[k-2] + ...    Y(z) * A(Z) = X(Z) * B(Z)&lt;br /&gt;
 = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Solving for Y we get&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(Z) = X(Z) * N(z)&lt;br /&gt;
                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
which is the well known &#039;&#039;difference formula&#039;&#039;. Its signal flow graph&lt;br /&gt;
representation is known as&lt;br /&gt;
[http://ccrma.stanford.edu/~jos/filters/Direct_Form_I.html direct form I].&lt;br /&gt;
&lt;br /&gt;
= Noise Shaper Implementation =&lt;br /&gt;
&lt;br /&gt;
Usually a filter&#039;s transfer function is called &#039;H&#039;. In this article &#039;N&#039;&lt;br /&gt;
has been used because this filter is also known as noise transfer&lt;br /&gt;
function (NTF) in the context of noise shaping. Later, another filter&lt;br /&gt;
&#039;H&#039; is introduced that has a special relation to &#039;N&#039;.&lt;br /&gt;
&lt;br /&gt;
== Direct Form I ==&lt;br /&gt;
&lt;br /&gt;
For convenience let us substitute the actual difference in the&lt;br /&gt;
difference formula for &#039;u&#039;. Also, b0 must be equal to one for noise&lt;br /&gt;
shaping filters as mentioned above. This leads to&lt;br /&gt;
&lt;br /&gt;
 y[k] = x[k] + u[k]   with u[k] = b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
                                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Note that since the filter is supposed to be applied on the quantization&lt;br /&gt;
noise y[] corresponds to the filtered noise and x[] to the unfiltered&lt;br /&gt;
noise. Suppose s[] is the signal we&#039;re trying to quantize. The following&lt;br /&gt;
pseudo code snippet shows the inner loop over k of &#039;&#039;one possible&#039;&#039;&lt;br /&gt;
noise shaping implementation:&lt;br /&gt;
&lt;br /&gt;
 (1) compute u[k]&lt;br /&gt;
 (2) wanted[k] = s[k] + u[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) y[k] = quantized[k] - s[k]&lt;br /&gt;
 (5) x[k] = y[k] - u[k]&lt;br /&gt;
&lt;br /&gt;
Note that the following is true&lt;br /&gt;
&lt;br /&gt;
 time domain                          Z-transform&lt;br /&gt;
 -----------                          -----------&lt;br /&gt;
 quantized[k] = s[k] + u[k] + x[k]    Q(z) = S(z) + X(z) * N(z)&lt;br /&gt;
              = s[k] + y[k]                = S(z) + Y(z)&lt;br /&gt;
&lt;br /&gt;
u[k], wanted[k] are temporary variables. The values are not needed&lt;br /&gt;
outside this iteration. But the past filtered and unfiltered error&lt;br /&gt;
samples need to be remembered (y[] &amp;amp; x[]) upto some amount of samples&lt;br /&gt;
determined by the filter order.&lt;br /&gt;
&lt;br /&gt;
== Decoupling filtering and quantization ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Noiseshaper3.png|thumb|right|200px]]&lt;br /&gt;
As mentioned, the previous example is &#039;&#039;one possible&#039;&#039; implementation.&lt;br /&gt;
It&#039;s not the best possible one. But it is the first version that comes to mind.&lt;br /&gt;
There&#039;s another common formulation which requires&lt;br /&gt;
some more transfer function fiddeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt; N(z) = \frac{B(z)}{A(z)} = 1 - \frac{A(z)}{A(z)} + \frac{B(z)}{A(z)}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The two fractions have a common denominator so they can be easily&lt;br /&gt;
combined to one fraction. It&#039;s also possible to factor z^-1 out of the fraction:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = 1 - \frac{A(z)-B(z)}{A(z)} = 1 - z^{-1} \frac{z ( A(z)-B(z) )}{A(z)} =: 1 - z^{-1} H(z)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Substituting z*(A(Z)-B(Z)) for H(z) leads to a new filter that is related to N.&lt;br /&gt;
Note that with b0=1 it follows that z*(1-b0)=0. So, H(z) can be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{c_0 + c_1 z^{-1} + c_2 z^{-2} + \ldots}{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
with &amp;lt;math&amp;gt;c_{k-1} = a_k - b_k&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;k = 1...m&amp;lt;/math&amp;gt;. H(z)&lt;br /&gt;
is some &#039;&#039;arbitrary&#039;&#039; filter. The big advantage now is that it is&lt;br /&gt;
possible to plug nearly any digital filter implementation for H(z) into&lt;br /&gt;
the noise shaping loop. One obvious approach is the difference formula&lt;br /&gt;
from above but there are other alternatives with desirable properties.&lt;br /&gt;
However, the purpose of this article is not to discuss digital filters&lt;br /&gt;
in general but their application as noise shaping filters. So,&lt;br /&gt;
throughout the rest of this article H(z) is simply treated as a black&lt;br /&gt;
box. This black box has a certain state and gives us an output sample&lt;br /&gt;
for each input sample. The modified pseudo code loop looks like this:&lt;br /&gt;
&lt;br /&gt;
 (1) h_out[k] = H.filter(last_error)&lt;br /&gt;
 (2) wanted[k] = s[k] - h_out[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) last_error = quantized[k] - wanted[k]&lt;br /&gt;
&lt;br /&gt;
which is equivalent to the shown signal flow graph where the unit&lt;br /&gt;
sample delay z^-1 is implemented by the state variable &#039;last_error&#039;:&lt;br /&gt;
Remember that this black box filter H(z) doesn&#039;t correspond the the&lt;br /&gt;
actual noise shaping filter N(z). Depending on the context published&lt;br /&gt;
coefficients for noise shaping correspond either to N(z), H(z), or&lt;br /&gt;
-H(z).&lt;br /&gt;
&lt;br /&gt;
= Example filters =&lt;br /&gt;
&lt;br /&gt;
The following two filters have been designed by Sebastian Gesemann&lt;br /&gt;
via non-linear optimization to match a slightly modified version of&lt;br /&gt;
the LAME MP3 encoder&#039;s ATH (absolute threshold of hearing) curve. They&lt;br /&gt;
were first published in the&lt;br /&gt;
[http://www.hydrogenaudio.org/forums/index.php?showtopic=47980&amp;amp;pid=551558&amp;amp;st=0 Hydrogenaudio forum]&lt;br /&gt;
including the noise transfer function&#039;s second order factorizations.&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 44100 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.1474 z^{-1} +0.5383 z^{-2} -0.3520 z^{-3} +0.3475 z^{-4}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2061 -0.4706 z^{-1} -0.2534 z^{-2} -0.6214 z^{-3}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 48000 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.3344 z^{-1} +0.7455 z^{-2} -0.4602 z^{-3} +0.3463 z^{-4}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2374 -0.7339 z^{-1} -0.1251 z^{-2} -0.6033 z^{-3}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Notes=&lt;br /&gt;
&lt;br /&gt;
==References== &lt;br /&gt;
* Lipshitz. P, Stanley and John Vanderkooy. &amp;quot;Why 1-bit Sigma-Delta Conversion is Unsuitable for High-Quality Applications&amp;quot;. 110th Convention.  &amp;lt;i&amp;gt;Audio Engineering Society&amp;lt;/i&amp;gt; (May 2001). 2-4. &lt;br /&gt;
* &lt;br /&gt;
&lt;br /&gt;
[[Category:Signal Processing]]&lt;/div&gt;</summary>
		<author><name>SebastianG</name></author>
	</entry>
	<entry>
		<id>https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=19022</id>
		<title>Noise shaping</title>
		<link rel="alternate" type="text/html" href="https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=19022"/>
		<updated>2008-08-20T13:02:15Z</updated>

		<summary type="html">&lt;p&gt;SebastianG: /* Applications */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Noise shaping is a technique that is often used in combination with&lt;br /&gt;
[[Dithering|dithering]] in the context of [[Quantize|quantization]]. The purpose of noise shaping&lt;br /&gt;
is to alter the spectral &#039;&#039;shape&#039;&#039; of the error that is introduced due&lt;br /&gt;
to [[Dithering|dithering]] and [[Quantize|quantization]]. Sometimes it may be desirable to&lt;br /&gt;
concentrate most of the noise power in a certain frequency band so&lt;br /&gt;
that the overall noise becomes less perceivable.&lt;br /&gt;
&lt;br /&gt;
== Applications ==&lt;br /&gt;
&lt;br /&gt;
In the field of digital image processing a popular noise shaping&lt;br /&gt;
technique is known as &amp;quot;Floyd Steinberg dithering&amp;quot;. This algorithm is&lt;br /&gt;
used to reduce the noise power at lower spatial frequencies which&lt;br /&gt;
results in an improved perceptual quality.&lt;br /&gt;
&lt;br /&gt;
For word length reductions of audio signals (i.e conversion from a 24bit&lt;br /&gt;
signal to a 16bit signal) noise shaping can be used to push quantization&lt;br /&gt;
noise out of perceptually important frequency bands to perceptually less&lt;br /&gt;
important frequency bands (&amp;gt;20 kHz) in order to reduce the perceived&lt;br /&gt;
loudness of the quantization noise.&lt;br /&gt;
&lt;br /&gt;
Another example is the &lt;br /&gt;
[[Super Audio Compact Disc|Super Audio CD]]. The SACD format represents&lt;br /&gt;
the most extreme trade-off between word length (1bit/sample) and&lt;br /&gt;
sampling frequency (2.8 Mhz). Due to the low word length high power&lt;br /&gt;
noise is introduced. With the help of noise shaping this noise is&lt;br /&gt;
usually rejected by about 120 dB in the audible band (0-20 kHz) at the&lt;br /&gt;
cost of a higher noise power in the ultrasonic range (+3.5dB for&lt;br /&gt;
frequencies above 100 kHz).&lt;br /&gt;
&lt;br /&gt;
= Limits &amp;amp; filter design restrictions =&lt;br /&gt;
&lt;br /&gt;
Noise Shaping without [[Dithering|&#039;&#039;dithered&#039;&#039;]] quantization is also possible but&lt;br /&gt;
less useful because the spectral characteristics of &#039;&#039;undithered&#039;&#039;&lt;br /&gt;
quantization noise is not easily predictable and depends heavily on the&lt;br /&gt;
digital signal that is to be quantized. The SACD case is an example where&lt;br /&gt;
[[Dithering|dithering]] can&#039;t be properly applied which may lead to a spectral&lt;br /&gt;
noise power distribution that deviates from the indented distribution. This has&lt;br /&gt;
been demonstrated by Lipshitz and Vanderkooy in&lt;br /&gt;
[http://www.sjeng.org/ftp/SACD.pdf their] [1] paper. &lt;br /&gt;
&lt;br /&gt;
It&#039;s not possible to lower the noise power for &#039;&#039;all&#039;&#039; frequencies&lt;br /&gt;
via noise shaping because this would be in violation of the&lt;br /&gt;
rate/distortion principles. But it &#039;&#039;is&#039;&#039; possible to lower the noise&lt;br /&gt;
in certain frequency bands at the cost of increased noise power in other&lt;br /&gt;
frequency bands. This property is also known as the&lt;br /&gt;
&#039;&#039;noise shaping theorem&#039;&#039;. It states that the average logarithmic magnitude response of&lt;br /&gt;
the corresponding noise shaping &#039;&#039;filter&#039;&#039; over the whole linear&lt;br /&gt;
frequency axis cannot drop below 0dB.&lt;br /&gt;
&lt;br /&gt;
The noise shaping application poses some restrictions on the fiter&lt;br /&gt;
design. The first sample of the filter&#039;s impulse response always equals&lt;br /&gt;
one. Also, interesting filters for noise shaping are so-called&lt;br /&gt;
&#039;&#039;minimum phase filters&#039;&#039;. This type of filter performs optimal in the sense of&lt;br /&gt;
the noise shaping theorem because the average logarithmig magnitude&lt;br /&gt;
response &#039;&#039;is&#039;&#039; 0dB.&lt;br /&gt;
&lt;br /&gt;
For the special [[Super Audio Compact Disc|SACD]] case an additional&lt;br /&gt;
constraint comes into play: The magnitude response should not exceed&lt;br /&gt;
3.5dB. This is enforced to prevent so-called limit cycles.&lt;br /&gt;
&lt;br /&gt;
The design of these kinds of filters is however not trivial. This is not&lt;br /&gt;
covered by this article.&lt;br /&gt;
&lt;br /&gt;
= Digital filter reminder =&lt;br /&gt;
&lt;br /&gt;
A common representation of digital filters is their &#039;&#039;transfer function&#039;&#039;&lt;br /&gt;
in form of a rational polynomial in z^-1. These polynomials are defined&lt;br /&gt;
by their coefficients b0,b1,b2,...,a1,a2,...:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{B(z)}{A(z)} = \frac{b_0 + b_1 z^{-1} + b_2 z^{-2} + \ldots + b_m z^{-m}}&lt;br /&gt;
{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots + a_m z^{-m}} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &#039;N&#039; is the transfer function and &#039;m&#039; the filter&#039;s &#039;&#039;order&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
== FIR filters ==&lt;br /&gt;
&lt;br /&gt;
In case a1=a2=a3=...=0 the filter&#039;s [http://en.wikipedia.org/wiki/Impulse_response impulse response] is finite which is&lt;br /&gt;
why these filters are called FIR ([[Finite Impulse Response Filter|finite impulse response]])&lt;br /&gt;
filters as opposed to IIR&lt;br /&gt;
[[Infinite Impulse Response Filter|infinite impulse response]] filters. Note that in case of&lt;br /&gt;
FIR filters the coefficients b0, b1, ... correspond to the impulse&lt;br /&gt;
response. The impulse response is another way a filter can be&lt;br /&gt;
characterized and describes what the filter does to a unit pulse. Let&#039;s&lt;br /&gt;
assume for the moment that a1=a2=a3=...=0. The the impulse response&lt;br /&gt;
would be:&lt;br /&gt;
&lt;br /&gt;
 x:  0  0  1  0  0  0  0  ... 0  0  0 ... (input signal)&lt;br /&gt;
 y:  0  0  b0 b1 b2 b3 b4 ... 0  0  0 ... (output signal)&lt;br /&gt;
&lt;br /&gt;
It helps to think of negative powers of z in transfer functions as a&lt;br /&gt;
delay: z^-1 = delay of one sample. z^-2 = delay of two samples etc.&lt;br /&gt;
Since a digital filter is a&lt;br /&gt;
[http://en.wikipedia.org/wiki/Discrete_system discrete-time]&lt;br /&gt;
[http://en.wikipedia.org/wiki/LTI_system_theory linear and time-invariant]&lt;br /&gt;
system it is&lt;br /&gt;
possible to determine the filter&#039;s output by a superposition of scaled&lt;br /&gt;
and shifted impulse responses. This leads to the following relationship&lt;br /&gt;
between input and output in the case of FIR filters:&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(z) = X(z) * B(z)&lt;br /&gt;
&lt;br /&gt;
where x[] is the input signal, y[] the output signal and k the current&lt;br /&gt;
sample index. This is a simple convolution directly computed using the&lt;br /&gt;
given impulse response. The convolution in the time domain corresponds&lt;br /&gt;
to a multiplication in the Z-domain.&lt;br /&gt;
&lt;br /&gt;
== IIR filters ==&lt;br /&gt;
&lt;br /&gt;
IIR filters are a simple extension to the FIR case:&lt;br /&gt;
&lt;br /&gt;
 time domain                                Z-transform&lt;br /&gt;
 -----------                                -----------&lt;br /&gt;
      y[k] + a1 y[k-1] + a2 y[k-2] + ...    Y(z) * A(Z) = X(Z) * B(Z)&lt;br /&gt;
 = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Solving for Y we get&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(Z) = X(Z) * N(z)&lt;br /&gt;
                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
which is the well known &#039;&#039;difference formula&#039;&#039;. Its signal flow graph&lt;br /&gt;
representation is known as&lt;br /&gt;
[http://ccrma.stanford.edu/~jos/filters/Direct_Form_I.html direct form I].&lt;br /&gt;
&lt;br /&gt;
= Noise Shaper Implementation =&lt;br /&gt;
&lt;br /&gt;
Usually a filter&#039;s transfer function is called &#039;H&#039;. In this article &#039;N&#039;&lt;br /&gt;
has been used because this filter is also known as noise transfer&lt;br /&gt;
function (NTF) in the context of noise shaping. Later, another filter&lt;br /&gt;
&#039;H&#039; is introduced that has a special relation to &#039;N&#039;.&lt;br /&gt;
&lt;br /&gt;
== Direct Form I ==&lt;br /&gt;
&lt;br /&gt;
For convenience let us substitute the actual difference in the&lt;br /&gt;
difference formula for &#039;u&#039;. Also, b0 must be equal to one for noise&lt;br /&gt;
shaping filters as mentioned above. This leads to&lt;br /&gt;
&lt;br /&gt;
 y[k] = x[k] + u[k]   with u[k] = b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
                                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Note that since the filter is supposed to be applied on the quantization&lt;br /&gt;
noise y[] corresponds to the filtered noise and x[] to the unfiltered&lt;br /&gt;
noise. Suppose s[] is the signal we&#039;re trying to quantize. The following&lt;br /&gt;
pseudo code snippet shows the inner loop over k of &#039;&#039;one possible&#039;&#039;&lt;br /&gt;
noise shaping implementation:&lt;br /&gt;
&lt;br /&gt;
 (1) compute u[k]&lt;br /&gt;
 (2) wanted[k] = s[k] + u[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) y[k] = quantized[k] - s[k]&lt;br /&gt;
 (5) x[k] = y[k] - u[k]&lt;br /&gt;
&lt;br /&gt;
Note that the following is true&lt;br /&gt;
&lt;br /&gt;
 time domain                          Z-transform&lt;br /&gt;
 -----------                          -----------&lt;br /&gt;
 quantized[k] = s[k] + u[k] + x[k]    Q(z) = S(z) + X(z) * N(z)&lt;br /&gt;
              = s[k] + y[k]                = S(z) + Y(z)&lt;br /&gt;
&lt;br /&gt;
u[k], wanted[k] are temporary variables. The values are not needed&lt;br /&gt;
outside this iteration. But the past filtered and unfiltered error&lt;br /&gt;
samples need to be remembered (y[] &amp;amp; x[]) upto some amount of samples&lt;br /&gt;
determined by the filter order.&lt;br /&gt;
&lt;br /&gt;
== Decoupling filtering and quantization ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Noiseshaper3.png|thumb|right|200px]]&lt;br /&gt;
As mentioned, the previous example is &#039;&#039;one possible&#039;&#039; implementation.&lt;br /&gt;
It&#039;s not the best possible one. But it is the first version that comes to mind.&lt;br /&gt;
There&#039;s another common formulation which requires&lt;br /&gt;
some more transfer function fiddeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt; N(z) = \frac{B(z)}{A(z)} = 1 - \frac{A(z)}{A(z)} + \frac{B(z)}{A(z)}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The two fractions have a common denominator so they can be easily&lt;br /&gt;
combined to one fraction. It&#039;s also possible to factor z^-1 out of the fraction:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = 1 - \frac{A(z)-B(z)}{A(z)} = 1 - z^{-1} \frac{z ( A(z)-B(z) )}{A(z)} =: 1 - z^{-1} H(z)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Substituting z*(A(Z)-B(Z)) for H(z) leads to a new filter that is related to N.&lt;br /&gt;
Note that with b0=1 it follows that z*(1-b0)=0. So, H(z) can be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{c_0 + c_1 z^{-1} + c_2 z^{-2} + \ldots}{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
with &amp;lt;math&amp;gt;c_{k-1} = a_k - b_k&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;k = 1...m&amp;lt;/math&amp;gt;. H(z)&lt;br /&gt;
is some &#039;&#039;arbitrary&#039;&#039; filter. The big advantage now is that it is&lt;br /&gt;
possible to plug nearly any digital filter implementation for H(z) into&lt;br /&gt;
the noise shaping loop. One obvious approach is the difference formula&lt;br /&gt;
from above but there are other alternatives with desirable properties.&lt;br /&gt;
However, the purpose of this article is not to discuss digital filters&lt;br /&gt;
in general but their application as noise shaping filters. So,&lt;br /&gt;
throughout the rest of this article H(z) is simply treated as a black&lt;br /&gt;
box. This black box has a certain state and gives us an output sample&lt;br /&gt;
for each input sample. The modified pseudo code loop looks like this:&lt;br /&gt;
&lt;br /&gt;
 (1) h_out[k] = H.filter(last_error)&lt;br /&gt;
 (2) wanted[k] = s[k] - h_out[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) last_error = quantized[k] - wanted[k]&lt;br /&gt;
&lt;br /&gt;
which is equivalent to the shown signal flow graph where the unit&lt;br /&gt;
sample delay z^-1 is implemented by the state variable &#039;last_error&#039;:&lt;br /&gt;
Remember that this black box filter H(z) doesn&#039;t correspond the the&lt;br /&gt;
actual noise shaping filter N(z). Depending on the context published&lt;br /&gt;
coefficients for noise shaping correspond either to N(z), H(z), or&lt;br /&gt;
-H(z).&lt;br /&gt;
&lt;br /&gt;
= Example filters =&lt;br /&gt;
&lt;br /&gt;
The following two filters have been designed by Sebastian Gesemann&lt;br /&gt;
via non-linear optimization to match a slightly modified version of&lt;br /&gt;
the LAME MP3 encoder&#039;s ATH (absolute threshold of hearing) curve. They&lt;br /&gt;
were first published in the&lt;br /&gt;
[http://www.hydrogenaudio.org/forums/index.php?showtopic=47980&amp;amp;pid=551558&amp;amp;st=0 Hydrogenaudio forum]&lt;br /&gt;
including the noise transfer function&#039;s second order factorizations.&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 44100 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.1474 z^{-1} +0.5383 z^{-2} -0.3520 z^{-3} +0.3475 z^{-4}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2061 -0.4706 z^{-1} -0.2534 z^{-2} -0.6214 z^{-3}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 48000 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.3344 z^{-1} +0.7455 z^{-2} -0.4602 z^{-3} +0.3463 z^{-4}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2374 -0.7339 z^{-1} -0.1251 z^{-2} -0.6033 z^{-3}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Notes=&lt;br /&gt;
&lt;br /&gt;
==References== &lt;br /&gt;
* Lipshitz. P, Stanley and John Vanderkooy. &amp;quot;Why 1-bit Sigma-Delta Conversion is Unsuitable for High-Quality Applications&amp;quot;. 110th Convention.  &amp;lt;i&amp;gt;Audio Engineering Society&amp;lt;/i&amp;gt; (May 2001). 2-4. &lt;br /&gt;
* &lt;br /&gt;
&lt;br /&gt;
[[Category:Signal Processing]]&lt;/div&gt;</summary>
		<author><name>SebastianG</name></author>
	</entry>
	<entry>
		<id>https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=19021</id>
		<title>Noise shaping</title>
		<link rel="alternate" type="text/html" href="https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=19021"/>
		<updated>2008-08-20T13:01:57Z</updated>

		<summary type="html">&lt;p&gt;SebastianG: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Noise shaping is a technique that is often used in combination with&lt;br /&gt;
[[Dithering|dithering]] in the context of [[Quantize|quantization]]. The purpose of noise shaping&lt;br /&gt;
is to alter the spectral &#039;&#039;shape&#039;&#039; of the error that is introduced due&lt;br /&gt;
to [[Dithering|dithering]] and [[Quantize|quantization]]. Sometimes it may be desirable to&lt;br /&gt;
concentrate most of the noise power in a certain frequency band so&lt;br /&gt;
that the overall noise becomes less perceivable.&lt;br /&gt;
&lt;br /&gt;
= Applications =&lt;br /&gt;
&lt;br /&gt;
In the field of digital image processing a popular noise shaping&lt;br /&gt;
technique is known as &amp;quot;Floyd Steinberg dithering&amp;quot;. This algorithm is&lt;br /&gt;
used to reduce the noise power at lower spatial frequencies which&lt;br /&gt;
results in an improved perceptual quality.&lt;br /&gt;
&lt;br /&gt;
For word length reductions of audio signals (i.e conversion from a 24bit&lt;br /&gt;
signal to a 16bit signal) noise shaping can be used to push quantization&lt;br /&gt;
noise out of perceptually important frequency bands to perceptually less&lt;br /&gt;
important frequency bands (&amp;gt;20 kHz) in order to reduce the perceived&lt;br /&gt;
loudness of the quantization noise.&lt;br /&gt;
&lt;br /&gt;
Another example is the &lt;br /&gt;
[[Super Audio Compact Disc|Super Audio CD]]. The SACD format represents&lt;br /&gt;
the most extreme trade-off between word length (1bit/sample) and&lt;br /&gt;
sampling frequency (2.8 Mhz). Due to the low word length high power&lt;br /&gt;
noise is introduced. With the help of noise shaping this noise is&lt;br /&gt;
usually rejected by about 120 dB in the audible band (0-20 kHz) at the&lt;br /&gt;
cost of a higher noise power in the ultrasonic range (+3.5dB for&lt;br /&gt;
frequencies above 100 kHz).&lt;br /&gt;
&lt;br /&gt;
= Limits &amp;amp; filter design restrictions =&lt;br /&gt;
&lt;br /&gt;
Noise Shaping without [[Dithering|&#039;&#039;dithered&#039;&#039;]] quantization is also possible but&lt;br /&gt;
less useful because the spectral characteristics of &#039;&#039;undithered&#039;&#039;&lt;br /&gt;
quantization noise is not easily predictable and depends heavily on the&lt;br /&gt;
digital signal that is to be quantized. The SACD case is an example where&lt;br /&gt;
[[Dithering|dithering]] can&#039;t be properly applied which may lead to a spectral&lt;br /&gt;
noise power distribution that deviates from the indented distribution. This has&lt;br /&gt;
been demonstrated by Lipshitz and Vanderkooy in&lt;br /&gt;
[http://www.sjeng.org/ftp/SACD.pdf their] [1] paper. &lt;br /&gt;
&lt;br /&gt;
It&#039;s not possible to lower the noise power for &#039;&#039;all&#039;&#039; frequencies&lt;br /&gt;
via noise shaping because this would be in violation of the&lt;br /&gt;
rate/distortion principles. But it &#039;&#039;is&#039;&#039; possible to lower the noise&lt;br /&gt;
in certain frequency bands at the cost of increased noise power in other&lt;br /&gt;
frequency bands. This property is also known as the&lt;br /&gt;
&#039;&#039;noise shaping theorem&#039;&#039;. It states that the average logarithmic magnitude response of&lt;br /&gt;
the corresponding noise shaping &#039;&#039;filter&#039;&#039; over the whole linear&lt;br /&gt;
frequency axis cannot drop below 0dB.&lt;br /&gt;
&lt;br /&gt;
The noise shaping application poses some restrictions on the fiter&lt;br /&gt;
design. The first sample of the filter&#039;s impulse response always equals&lt;br /&gt;
one. Also, interesting filters for noise shaping are so-called&lt;br /&gt;
&#039;&#039;minimum phase filters&#039;&#039;. This type of filter performs optimal in the sense of&lt;br /&gt;
the noise shaping theorem because the average logarithmig magnitude&lt;br /&gt;
response &#039;&#039;is&#039;&#039; 0dB.&lt;br /&gt;
&lt;br /&gt;
For the special [[Super Audio Compact Disc|SACD]] case an additional&lt;br /&gt;
constraint comes into play: The magnitude response should not exceed&lt;br /&gt;
3.5dB. This is enforced to prevent so-called limit cycles.&lt;br /&gt;
&lt;br /&gt;
The design of these kinds of filters is however not trivial. This is not&lt;br /&gt;
covered by this article.&lt;br /&gt;
&lt;br /&gt;
= Digital filter reminder =&lt;br /&gt;
&lt;br /&gt;
A common representation of digital filters is their &#039;&#039;transfer function&#039;&#039;&lt;br /&gt;
in form of a rational polynomial in z^-1. These polynomials are defined&lt;br /&gt;
by their coefficients b0,b1,b2,...,a1,a2,...:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{B(z)}{A(z)} = \frac{b_0 + b_1 z^{-1} + b_2 z^{-2} + \ldots + b_m z^{-m}}&lt;br /&gt;
{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots + a_m z^{-m}} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &#039;N&#039; is the transfer function and &#039;m&#039; the filter&#039;s &#039;&#039;order&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
== FIR filters ==&lt;br /&gt;
&lt;br /&gt;
In case a1=a2=a3=...=0 the filter&#039;s [http://en.wikipedia.org/wiki/Impulse_response impulse response] is finite which is&lt;br /&gt;
why these filters are called FIR ([[Finite Impulse Response Filter|finite impulse response]])&lt;br /&gt;
filters as opposed to IIR&lt;br /&gt;
[[Infinite Impulse Response Filter|infinite impulse response]] filters. Note that in case of&lt;br /&gt;
FIR filters the coefficients b0, b1, ... correspond to the impulse&lt;br /&gt;
response. The impulse response is another way a filter can be&lt;br /&gt;
characterized and describes what the filter does to a unit pulse. Let&#039;s&lt;br /&gt;
assume for the moment that a1=a2=a3=...=0. The the impulse response&lt;br /&gt;
would be:&lt;br /&gt;
&lt;br /&gt;
 x:  0  0  1  0  0  0  0  ... 0  0  0 ... (input signal)&lt;br /&gt;
 y:  0  0  b0 b1 b2 b3 b4 ... 0  0  0 ... (output signal)&lt;br /&gt;
&lt;br /&gt;
It helps to think of negative powers of z in transfer functions as a&lt;br /&gt;
delay: z^-1 = delay of one sample. z^-2 = delay of two samples etc.&lt;br /&gt;
Since a digital filter is a&lt;br /&gt;
[http://en.wikipedia.org/wiki/Discrete_system discrete-time]&lt;br /&gt;
[http://en.wikipedia.org/wiki/LTI_system_theory linear and time-invariant]&lt;br /&gt;
system it is&lt;br /&gt;
possible to determine the filter&#039;s output by a superposition of scaled&lt;br /&gt;
and shifted impulse responses. This leads to the following relationship&lt;br /&gt;
between input and output in the case of FIR filters:&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(z) = X(z) * B(z)&lt;br /&gt;
&lt;br /&gt;
where x[] is the input signal, y[] the output signal and k the current&lt;br /&gt;
sample index. This is a simple convolution directly computed using the&lt;br /&gt;
given impulse response. The convolution in the time domain corresponds&lt;br /&gt;
to a multiplication in the Z-domain.&lt;br /&gt;
&lt;br /&gt;
== IIR filters ==&lt;br /&gt;
&lt;br /&gt;
IIR filters are a simple extension to the FIR case:&lt;br /&gt;
&lt;br /&gt;
 time domain                                Z-transform&lt;br /&gt;
 -----------                                -----------&lt;br /&gt;
      y[k] + a1 y[k-1] + a2 y[k-2] + ...    Y(z) * A(Z) = X(Z) * B(Z)&lt;br /&gt;
 = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Solving for Y we get&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(Z) = X(Z) * N(z)&lt;br /&gt;
                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
which is the well known &#039;&#039;difference formula&#039;&#039;. Its signal flow graph&lt;br /&gt;
representation is known as&lt;br /&gt;
[http://ccrma.stanford.edu/~jos/filters/Direct_Form_I.html direct form I].&lt;br /&gt;
&lt;br /&gt;
= Noise Shaper Implementation =&lt;br /&gt;
&lt;br /&gt;
Usually a filter&#039;s transfer function is called &#039;H&#039;. In this article &#039;N&#039;&lt;br /&gt;
has been used because this filter is also known as noise transfer&lt;br /&gt;
function (NTF) in the context of noise shaping. Later, another filter&lt;br /&gt;
&#039;H&#039; is introduced that has a special relation to &#039;N&#039;.&lt;br /&gt;
&lt;br /&gt;
== Direct Form I ==&lt;br /&gt;
&lt;br /&gt;
For convenience let us substitute the actual difference in the&lt;br /&gt;
difference formula for &#039;u&#039;. Also, b0 must be equal to one for noise&lt;br /&gt;
shaping filters as mentioned above. This leads to&lt;br /&gt;
&lt;br /&gt;
 y[k] = x[k] + u[k]   with u[k] = b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
                                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Note that since the filter is supposed to be applied on the quantization&lt;br /&gt;
noise y[] corresponds to the filtered noise and x[] to the unfiltered&lt;br /&gt;
noise. Suppose s[] is the signal we&#039;re trying to quantize. The following&lt;br /&gt;
pseudo code snippet shows the inner loop over k of &#039;&#039;one possible&#039;&#039;&lt;br /&gt;
noise shaping implementation:&lt;br /&gt;
&lt;br /&gt;
 (1) compute u[k]&lt;br /&gt;
 (2) wanted[k] = s[k] + u[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) y[k] = quantized[k] - s[k]&lt;br /&gt;
 (5) x[k] = y[k] - u[k]&lt;br /&gt;
&lt;br /&gt;
Note that the following is true&lt;br /&gt;
&lt;br /&gt;
 time domain                          Z-transform&lt;br /&gt;
 -----------                          -----------&lt;br /&gt;
 quantized[k] = s[k] + u[k] + x[k]    Q(z) = S(z) + X(z) * N(z)&lt;br /&gt;
              = s[k] + y[k]                = S(z) + Y(z)&lt;br /&gt;
&lt;br /&gt;
u[k], wanted[k] are temporary variables. The values are not needed&lt;br /&gt;
outside this iteration. But the past filtered and unfiltered error&lt;br /&gt;
samples need to be remembered (y[] &amp;amp; x[]) upto some amount of samples&lt;br /&gt;
determined by the filter order.&lt;br /&gt;
&lt;br /&gt;
== Decoupling filtering and quantization ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Noiseshaper3.png|thumb|right|200px]]&lt;br /&gt;
As mentioned, the previous example is &#039;&#039;one possible&#039;&#039; implementation.&lt;br /&gt;
It&#039;s not the best possible one. But it is the first version that comes to mind.&lt;br /&gt;
There&#039;s another common formulation which requires&lt;br /&gt;
some more transfer function fiddeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt; N(z) = \frac{B(z)}{A(z)} = 1 - \frac{A(z)}{A(z)} + \frac{B(z)}{A(z)}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The two fractions have a common denominator so they can be easily&lt;br /&gt;
combined to one fraction. It&#039;s also possible to factor z^-1 out of the fraction:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = 1 - \frac{A(z)-B(z)}{A(z)} = 1 - z^{-1} \frac{z ( A(z)-B(z) )}{A(z)} =: 1 - z^{-1} H(z)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Substituting z*(A(Z)-B(Z)) for H(z) leads to a new filter that is related to N.&lt;br /&gt;
Note that with b0=1 it follows that z*(1-b0)=0. So, H(z) can be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{c_0 + c_1 z^{-1} + c_2 z^{-2} + \ldots}{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
with &amp;lt;math&amp;gt;c_{k-1} = a_k - b_k&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;k = 1...m&amp;lt;/math&amp;gt;. H(z)&lt;br /&gt;
is some &#039;&#039;arbitrary&#039;&#039; filter. The big advantage now is that it is&lt;br /&gt;
possible to plug nearly any digital filter implementation for H(z) into&lt;br /&gt;
the noise shaping loop. One obvious approach is the difference formula&lt;br /&gt;
from above but there are other alternatives with desirable properties.&lt;br /&gt;
However, the purpose of this article is not to discuss digital filters&lt;br /&gt;
in general but their application as noise shaping filters. So,&lt;br /&gt;
throughout the rest of this article H(z) is simply treated as a black&lt;br /&gt;
box. This black box has a certain state and gives us an output sample&lt;br /&gt;
for each input sample. The modified pseudo code loop looks like this:&lt;br /&gt;
&lt;br /&gt;
 (1) h_out[k] = H.filter(last_error)&lt;br /&gt;
 (2) wanted[k] = s[k] - h_out[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) last_error = quantized[k] - wanted[k]&lt;br /&gt;
&lt;br /&gt;
which is equivalent to the shown signal flow graph where the unit&lt;br /&gt;
sample delay z^-1 is implemented by the state variable &#039;last_error&#039;:&lt;br /&gt;
Remember that this black box filter H(z) doesn&#039;t correspond the the&lt;br /&gt;
actual noise shaping filter N(z). Depending on the context published&lt;br /&gt;
coefficients for noise shaping correspond either to N(z), H(z), or&lt;br /&gt;
-H(z).&lt;br /&gt;
&lt;br /&gt;
= Example filters =&lt;br /&gt;
&lt;br /&gt;
The following two filters have been designed by Sebastian Gesemann&lt;br /&gt;
via non-linear optimization to match a slightly modified version of&lt;br /&gt;
the LAME MP3 encoder&#039;s ATH (absolute threshold of hearing) curve. They&lt;br /&gt;
were first published in the&lt;br /&gt;
[http://www.hydrogenaudio.org/forums/index.php?showtopic=47980&amp;amp;pid=551558&amp;amp;st=0 Hydrogenaudio forum]&lt;br /&gt;
including the noise transfer function&#039;s second order factorizations.&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 44100 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.1474 z^{-1} +0.5383 z^{-2} -0.3520 z^{-3} +0.3475 z^{-4}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2061 -0.4706 z^{-1} -0.2534 z^{-2} -0.6214 z^{-3}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 48000 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.3344 z^{-1} +0.7455 z^{-2} -0.4602 z^{-3} +0.3463 z^{-4}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2374 -0.7339 z^{-1} -0.1251 z^{-2} -0.6033 z^{-3}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Notes=&lt;br /&gt;
&lt;br /&gt;
==References== &lt;br /&gt;
* Lipshitz. P, Stanley and John Vanderkooy. &amp;quot;Why 1-bit Sigma-Delta Conversion is Unsuitable for High-Quality Applications&amp;quot;. 110th Convention.  &amp;lt;i&amp;gt;Audio Engineering Society&amp;lt;/i&amp;gt; (May 2001). 2-4. &lt;br /&gt;
* &lt;br /&gt;
&lt;br /&gt;
[[Category:Signal Processing]]&lt;/div&gt;</summary>
		<author><name>SebastianG</name></author>
	</entry>
	<entry>
		<id>https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=19020</id>
		<title>Noise shaping</title>
		<link rel="alternate" type="text/html" href="https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=19020"/>
		<updated>2008-08-20T13:01:09Z</updated>

		<summary type="html">&lt;p&gt;SebastianG: /* Notes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
Noise shaping is a technique that is often used in combination with&lt;br /&gt;
[[Dithering|dithering]] in the context of [[Quantize|quantization]]. The purpose of noise shaping&lt;br /&gt;
is to alter the spectral &#039;&#039;shape&#039;&#039; of the error that is introduced due&lt;br /&gt;
to [[Dithering|dithering]] and [[Quantize|quantization]]. Sometimes it may be desirable to&lt;br /&gt;
concentrate most of the noise power in a certain frequency band so&lt;br /&gt;
that the overall noise becomes less perceivable.&lt;br /&gt;
&lt;br /&gt;
= Applications =&lt;br /&gt;
&lt;br /&gt;
In the field of digital image processing a popular noise shaping&lt;br /&gt;
technique is known as &amp;quot;Floyd Steinberg dithering&amp;quot;. This algorithm is&lt;br /&gt;
used to reduce the noise power at lower spatial frequencies which&lt;br /&gt;
results in an improved perceptual quality.&lt;br /&gt;
&lt;br /&gt;
For word length reductions of audio signals (i.e conversion from a 24bit&lt;br /&gt;
signal to a 16bit signal) noise shaping can be used to push quantization&lt;br /&gt;
noise out of perceptually important frequency bands to perceptually less&lt;br /&gt;
important frequency bands (&amp;gt;20 kHz) in order to reduce the perceived&lt;br /&gt;
loudness of the quantization noise.&lt;br /&gt;
&lt;br /&gt;
Another example is the &lt;br /&gt;
[[Super Audio Compact Disc|Super Audio CD]]. The SACD format represents&lt;br /&gt;
the most extreme trade-off between word length (1bit/sample) and&lt;br /&gt;
sampling frequency (2.8 Mhz). Due to the low word length high power&lt;br /&gt;
noise is introduced. With the help of noise shaping this noise is&lt;br /&gt;
usually rejected by about 120 dB in the audible band (0-20 kHz) at the&lt;br /&gt;
cost of a higher noise power in the ultrasonic range (+3.5dB for&lt;br /&gt;
frequencies above 100 kHz).&lt;br /&gt;
&lt;br /&gt;
= Limits &amp;amp; filter design restrictions =&lt;br /&gt;
&lt;br /&gt;
Noise Shaping without [[Dithering|&#039;&#039;dithered&#039;&#039;]] quantization is also possible but&lt;br /&gt;
less useful because the spectral characteristics of &#039;&#039;undithered&#039;&#039;&lt;br /&gt;
quantization noise is not easily predictable and depends heavily on the&lt;br /&gt;
digital signal that is to be quantized. The SACD case is an example where&lt;br /&gt;
[[Dithering|dithering]] can&#039;t be properly applied which may lead to a spectral&lt;br /&gt;
noise power distribution that deviates from the indented distribution. This has&lt;br /&gt;
been demonstrated by Lipshitz and Vanderkooy in&lt;br /&gt;
[http://www.sjeng.org/ftp/SACD.pdf their] [1] paper. &lt;br /&gt;
&lt;br /&gt;
It&#039;s not possible to lower the noise power for &#039;&#039;all&#039;&#039; frequencies&lt;br /&gt;
via noise shaping because this would be in violation of the&lt;br /&gt;
rate/distortion principles. But it &#039;&#039;is&#039;&#039; possible to lower the noise&lt;br /&gt;
in certain frequency bands at the cost of increased noise power in other&lt;br /&gt;
frequency bands. This property is also known as the&lt;br /&gt;
&#039;&#039;noise shaping theorem&#039;&#039;. It states that the average logarithmic magnitude response of&lt;br /&gt;
the corresponding noise shaping &#039;&#039;filter&#039;&#039; over the whole linear&lt;br /&gt;
frequency axis cannot drop below 0dB.&lt;br /&gt;
&lt;br /&gt;
The noise shaping application poses some restrictions on the fiter&lt;br /&gt;
design. The first sample of the filter&#039;s impulse response always equals&lt;br /&gt;
one. Also, interesting filters for noise shaping are so-called&lt;br /&gt;
&#039;&#039;minimum phase filters&#039;&#039;. This type of filter performs optimal in the sense of&lt;br /&gt;
the noise shaping theorem because the average logarithmig magnitude&lt;br /&gt;
response &#039;&#039;is&#039;&#039; 0dB.&lt;br /&gt;
&lt;br /&gt;
For the special [[Super Audio Compact Disc|SACD]] case an additional&lt;br /&gt;
constraint comes into play: The magnitude response should not exceed&lt;br /&gt;
3.5dB. This is enforced to prevent so-called limit cycles.&lt;br /&gt;
&lt;br /&gt;
The design of these kinds of filters is however not trivial. This is not&lt;br /&gt;
covered by this article.&lt;br /&gt;
&lt;br /&gt;
= Digital filter reminder =&lt;br /&gt;
&lt;br /&gt;
A common representation of digital filters is their &#039;&#039;transfer function&#039;&#039;&lt;br /&gt;
in form of a rational polynomial in z^-1. These polynomials are defined&lt;br /&gt;
by their coefficients b0,b1,b2,...,a1,a2,...:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{B(z)}{A(z)} = \frac{b_0 + b_1 z^{-1} + b_2 z^{-2} + \ldots + b_m z^{-m}}&lt;br /&gt;
{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots + a_m z^{-m}} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &#039;N&#039; is the transfer function and &#039;m&#039; the filter&#039;s &#039;&#039;order&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
== FIR filters ==&lt;br /&gt;
&lt;br /&gt;
In case a1=a2=a3=...=0 the filter&#039;s [http://en.wikipedia.org/wiki/Impulse_response impulse response] is finite which is&lt;br /&gt;
why these filters are called FIR ([[Finite Impulse Response Filter|finite impulse response]])&lt;br /&gt;
filters as opposed to IIR&lt;br /&gt;
[[Infinite Impulse Response Filter|infinite impulse response]] filters. Note that in case of&lt;br /&gt;
FIR filters the coefficients b0, b1, ... correspond to the impulse&lt;br /&gt;
response. The impulse response is another way a filter can be&lt;br /&gt;
characterized and describes what the filter does to a unit pulse. Let&#039;s&lt;br /&gt;
assume for the moment that a1=a2=a3=...=0. The the impulse response&lt;br /&gt;
would be:&lt;br /&gt;
&lt;br /&gt;
 x:  0  0  1  0  0  0  0  ... 0  0  0 ... (input signal)&lt;br /&gt;
 y:  0  0  b0 b1 b2 b3 b4 ... 0  0  0 ... (output signal)&lt;br /&gt;
&lt;br /&gt;
It helps to think of negative powers of z in transfer functions as a&lt;br /&gt;
delay: z^-1 = delay of one sample. z^-2 = delay of two samples etc.&lt;br /&gt;
Since a digital filter is a&lt;br /&gt;
[http://en.wikipedia.org/wiki/Discrete_system discrete-time]&lt;br /&gt;
[http://en.wikipedia.org/wiki/LTI_system_theory linear and time-invariant]&lt;br /&gt;
system it is&lt;br /&gt;
possible to determine the filter&#039;s output by a superposition of scaled&lt;br /&gt;
and shifted impulse responses. This leads to the following relationship&lt;br /&gt;
between input and output in the case of FIR filters:&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(z) = X(z) * B(z)&lt;br /&gt;
&lt;br /&gt;
where x[] is the input signal, y[] the output signal and k the current&lt;br /&gt;
sample index. This is a simple convolution directly computed using the&lt;br /&gt;
given impulse response. The convolution in the time domain corresponds&lt;br /&gt;
to a multiplication in the Z-domain.&lt;br /&gt;
&lt;br /&gt;
== IIR filters ==&lt;br /&gt;
&lt;br /&gt;
IIR filters are a simple extension to the FIR case:&lt;br /&gt;
&lt;br /&gt;
 time domain                                Z-transform&lt;br /&gt;
 -----------                                -----------&lt;br /&gt;
      y[k] + a1 y[k-1] + a2 y[k-2] + ...    Y(z) * A(Z) = X(Z) * B(Z)&lt;br /&gt;
 = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Solving for Y we get&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(Z) = X(Z) * N(z)&lt;br /&gt;
                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
which is the well known &#039;&#039;difference formula&#039;&#039;. Its signal flow graph&lt;br /&gt;
representation is known as&lt;br /&gt;
[http://ccrma.stanford.edu/~jos/filters/Direct_Form_I.html direct form I].&lt;br /&gt;
&lt;br /&gt;
= Noise Shaper Implementation =&lt;br /&gt;
&lt;br /&gt;
Usually a filter&#039;s transfer function is called &#039;H&#039;. In this article &#039;N&#039;&lt;br /&gt;
has been used because this filter is also known as noise transfer&lt;br /&gt;
function (NTF) in the context of noise shaping. Later, another filter&lt;br /&gt;
&#039;H&#039; is introduced that has a special relation to &#039;N&#039;.&lt;br /&gt;
&lt;br /&gt;
== Direct Form I ==&lt;br /&gt;
&lt;br /&gt;
For convenience let us substitute the actual difference in the&lt;br /&gt;
difference formula for &#039;u&#039;. Also, b0 must be equal to one for noise&lt;br /&gt;
shaping filters as mentioned above. This leads to&lt;br /&gt;
&lt;br /&gt;
 y[k] = x[k] + u[k]   with u[k] = b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
                                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Note that since the filter is supposed to be applied on the quantization&lt;br /&gt;
noise y[] corresponds to the filtered noise and x[] to the unfiltered&lt;br /&gt;
noise. Suppose s[] is the signal we&#039;re trying to quantize. The following&lt;br /&gt;
pseudo code snippet shows the inner loop over k of &#039;&#039;one possible&#039;&#039;&lt;br /&gt;
noise shaping implementation:&lt;br /&gt;
&lt;br /&gt;
 (1) compute u[k]&lt;br /&gt;
 (2) wanted[k] = s[k] + u[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) y[k] = quantized[k] - s[k]&lt;br /&gt;
 (5) x[k] = y[k] - u[k]&lt;br /&gt;
&lt;br /&gt;
Note that the following is true&lt;br /&gt;
&lt;br /&gt;
 time domain                          Z-transform&lt;br /&gt;
 -----------                          -----------&lt;br /&gt;
 quantized[k] = s[k] + u[k] + x[k]    Q(z) = S(z) + X(z) * N(z)&lt;br /&gt;
              = s[k] + y[k]                = S(z) + Y(z)&lt;br /&gt;
&lt;br /&gt;
u[k], wanted[k] are temporary variables. The values are not needed&lt;br /&gt;
outside this iteration. But the past filtered and unfiltered error&lt;br /&gt;
samples need to be remembered (y[] &amp;amp; x[]) upto some amount of samples&lt;br /&gt;
determined by the filter order.&lt;br /&gt;
&lt;br /&gt;
== Decoupling filtering and quantization ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Noiseshaper3.png|thumb|right|200px]]&lt;br /&gt;
As mentioned, the previous example is &#039;&#039;one possible&#039;&#039; implementation.&lt;br /&gt;
It&#039;s not the best possible one. But it is the first version that comes to mind.&lt;br /&gt;
There&#039;s another common formulation which requires&lt;br /&gt;
some more transfer function fiddeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt; N(z) = \frac{B(z)}{A(z)} = 1 - \frac{A(z)}{A(z)} + \frac{B(z)}{A(z)}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The two fractions have a common denominator so they can be easily&lt;br /&gt;
combined to one fraction. It&#039;s also possible to factor z^-1 out of the fraction:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = 1 - \frac{A(z)-B(z)}{A(z)} = 1 - z^{-1} \frac{z ( A(z)-B(z) )}{A(z)} =: 1 - z^{-1} H(z)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Substituting z*(A(Z)-B(Z)) for H(z) leads to a new filter that is related to N.&lt;br /&gt;
Note that with b0=1 it follows that z*(1-b0)=0. So, H(z) can be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{c_0 + c_1 z^{-1} + c_2 z^{-2} + \ldots}{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
with &amp;lt;math&amp;gt;c_{k-1} = a_k - b_k&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;k = 1...m&amp;lt;/math&amp;gt;. H(z)&lt;br /&gt;
is some &#039;&#039;arbitrary&#039;&#039; filter. The big advantage now is that it is&lt;br /&gt;
possible to plug nearly any digital filter implementation for H(z) into&lt;br /&gt;
the noise shaping loop. One obvious approach is the difference formula&lt;br /&gt;
from above but there are other alternatives with desirable properties.&lt;br /&gt;
However, the purpose of this article is not to discuss digital filters&lt;br /&gt;
in general but their application as noise shaping filters. So,&lt;br /&gt;
throughout the rest of this article H(z) is simply treated as a black&lt;br /&gt;
box. This black box has a certain state and gives us an output sample&lt;br /&gt;
for each input sample. The modified pseudo code loop looks like this:&lt;br /&gt;
&lt;br /&gt;
 (1) h_out[k] = H.filter(last_error)&lt;br /&gt;
 (2) wanted[k] = s[k] - h_out[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) last_error = quantized[k] - wanted[k]&lt;br /&gt;
&lt;br /&gt;
which is equivalent to the shown signal flow graph where the unit&lt;br /&gt;
sample delay z^-1 is implemented by the state variable &#039;last_error&#039;:&lt;br /&gt;
Remember that this black box filter H(z) doesn&#039;t correspond the the&lt;br /&gt;
actual noise shaping filter N(z). Depending on the context published&lt;br /&gt;
coefficients for noise shaping correspond either to N(z), H(z), or&lt;br /&gt;
-H(z).&lt;br /&gt;
&lt;br /&gt;
= Example filters =&lt;br /&gt;
&lt;br /&gt;
The following two filters have been designed by Sebastian Gesemann&lt;br /&gt;
via non-linear optimization to match a slightly modified version of&lt;br /&gt;
the LAME MP3 encoder&#039;s ATH (absolute threshold of hearing) curve. They&lt;br /&gt;
were first published in the&lt;br /&gt;
[http://www.hydrogenaudio.org/forums/index.php?showtopic=47980&amp;amp;pid=551558&amp;amp;st=0 Hydrogenaudio forum]&lt;br /&gt;
including the noise transfer function&#039;s second order factorizations.&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 44100 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.1474 z^{-1} +0.5383 z^{-2} -0.3520 z^{-3} +0.3475 z^{-4}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2061 -0.4706 z^{-1} -0.2534 z^{-2} -0.6214 z^{-3}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 48000 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.3344 z^{-1} +0.7455 z^{-2} -0.4602 z^{-3} +0.3463 z^{-4}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2374 -0.7339 z^{-1} -0.1251 z^{-2} -0.6033 z^{-3}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Notes=&lt;br /&gt;
&lt;br /&gt;
==References== &lt;br /&gt;
* Lipshitz. P, Stanley and John Vanderkooy. &amp;quot;Why 1-bit Sigma-Delta Conversion is Unsuitable for High-Quality Applications&amp;quot;. 110th Convention.  &amp;lt;i&amp;gt;Audio Engineering Society&amp;lt;/i&amp;gt; (May 2001). 2-4. &lt;br /&gt;
* &lt;br /&gt;
&lt;br /&gt;
[[Category:Signal Processing]]&lt;/div&gt;</summary>
		<author><name>SebastianG</name></author>
	</entry>
	<entry>
		<id>https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=19019</id>
		<title>Noise shaping</title>
		<link rel="alternate" type="text/html" href="https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=19019"/>
		<updated>2008-08-20T13:00:50Z</updated>

		<summary type="html">&lt;p&gt;SebastianG: /* Notes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
Noise shaping is a technique that is often used in combination with&lt;br /&gt;
[[Dithering|dithering]] in the context of [[Quantize|quantization]]. The purpose of noise shaping&lt;br /&gt;
is to alter the spectral &#039;&#039;shape&#039;&#039; of the error that is introduced due&lt;br /&gt;
to [[Dithering|dithering]] and [[Quantize|quantization]]. Sometimes it may be desirable to&lt;br /&gt;
concentrate most of the noise power in a certain frequency band so&lt;br /&gt;
that the overall noise becomes less perceivable.&lt;br /&gt;
&lt;br /&gt;
= Applications =&lt;br /&gt;
&lt;br /&gt;
In the field of digital image processing a popular noise shaping&lt;br /&gt;
technique is known as &amp;quot;Floyd Steinberg dithering&amp;quot;. This algorithm is&lt;br /&gt;
used to reduce the noise power at lower spatial frequencies which&lt;br /&gt;
results in an improved perceptual quality.&lt;br /&gt;
&lt;br /&gt;
For word length reductions of audio signals (i.e conversion from a 24bit&lt;br /&gt;
signal to a 16bit signal) noise shaping can be used to push quantization&lt;br /&gt;
noise out of perceptually important frequency bands to perceptually less&lt;br /&gt;
important frequency bands (&amp;gt;20 kHz) in order to reduce the perceived&lt;br /&gt;
loudness of the quantization noise.&lt;br /&gt;
&lt;br /&gt;
Another example is the &lt;br /&gt;
[[Super Audio Compact Disc|Super Audio CD]]. The SACD format represents&lt;br /&gt;
the most extreme trade-off between word length (1bit/sample) and&lt;br /&gt;
sampling frequency (2.8 Mhz). Due to the low word length high power&lt;br /&gt;
noise is introduced. With the help of noise shaping this noise is&lt;br /&gt;
usually rejected by about 120 dB in the audible band (0-20 kHz) at the&lt;br /&gt;
cost of a higher noise power in the ultrasonic range (+3.5dB for&lt;br /&gt;
frequencies above 100 kHz).&lt;br /&gt;
&lt;br /&gt;
= Limits &amp;amp; filter design restrictions =&lt;br /&gt;
&lt;br /&gt;
Noise Shaping without [[Dithering|&#039;&#039;dithered&#039;&#039;]] quantization is also possible but&lt;br /&gt;
less useful because the spectral characteristics of &#039;&#039;undithered&#039;&#039;&lt;br /&gt;
quantization noise is not easily predictable and depends heavily on the&lt;br /&gt;
digital signal that is to be quantized. The SACD case is an example where&lt;br /&gt;
[[Dithering|dithering]] can&#039;t be properly applied which may lead to a spectral&lt;br /&gt;
noise power distribution that deviates from the indented distribution. This has&lt;br /&gt;
been demonstrated by Lipshitz and Vanderkooy in&lt;br /&gt;
[http://www.sjeng.org/ftp/SACD.pdf their] [1] paper. &lt;br /&gt;
&lt;br /&gt;
It&#039;s not possible to lower the noise power for &#039;&#039;all&#039;&#039; frequencies&lt;br /&gt;
via noise shaping because this would be in violation of the&lt;br /&gt;
rate/distortion principles. But it &#039;&#039;is&#039;&#039; possible to lower the noise&lt;br /&gt;
in certain frequency bands at the cost of increased noise power in other&lt;br /&gt;
frequency bands. This property is also known as the&lt;br /&gt;
&#039;&#039;noise shaping theorem&#039;&#039;. It states that the average logarithmic magnitude response of&lt;br /&gt;
the corresponding noise shaping &#039;&#039;filter&#039;&#039; over the whole linear&lt;br /&gt;
frequency axis cannot drop below 0dB.&lt;br /&gt;
&lt;br /&gt;
The noise shaping application poses some restrictions on the fiter&lt;br /&gt;
design. The first sample of the filter&#039;s impulse response always equals&lt;br /&gt;
one. Also, interesting filters for noise shaping are so-called&lt;br /&gt;
&#039;&#039;minimum phase filters&#039;&#039;. This type of filter performs optimal in the sense of&lt;br /&gt;
the noise shaping theorem because the average logarithmig magnitude&lt;br /&gt;
response &#039;&#039;is&#039;&#039; 0dB.&lt;br /&gt;
&lt;br /&gt;
For the special [[Super Audio Compact Disc|SACD]] case an additional&lt;br /&gt;
constraint comes into play: The magnitude response should not exceed&lt;br /&gt;
3.5dB. This is enforced to prevent so-called limit cycles.&lt;br /&gt;
&lt;br /&gt;
The design of these kinds of filters is however not trivial. This is not&lt;br /&gt;
covered by this article.&lt;br /&gt;
&lt;br /&gt;
= Digital filter reminder =&lt;br /&gt;
&lt;br /&gt;
A common representation of digital filters is their &#039;&#039;transfer function&#039;&#039;&lt;br /&gt;
in form of a rational polynomial in z^-1. These polynomials are defined&lt;br /&gt;
by their coefficients b0,b1,b2,...,a1,a2,...:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{B(z)}{A(z)} = \frac{b_0 + b_1 z^{-1} + b_2 z^{-2} + \ldots + b_m z^{-m}}&lt;br /&gt;
{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots + a_m z^{-m}} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &#039;N&#039; is the transfer function and &#039;m&#039; the filter&#039;s &#039;&#039;order&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
== FIR filters ==&lt;br /&gt;
&lt;br /&gt;
In case a1=a2=a3=...=0 the filter&#039;s [http://en.wikipedia.org/wiki/Impulse_response impulse response] is finite which is&lt;br /&gt;
why these filters are called FIR ([[Finite Impulse Response Filter|finite impulse response]])&lt;br /&gt;
filters as opposed to IIR&lt;br /&gt;
[[Infinite Impulse Response Filter|infinite impulse response]] filters. Note that in case of&lt;br /&gt;
FIR filters the coefficients b0, b1, ... correspond to the impulse&lt;br /&gt;
response. The impulse response is another way a filter can be&lt;br /&gt;
characterized and describes what the filter does to a unit pulse. Let&#039;s&lt;br /&gt;
assume for the moment that a1=a2=a3=...=0. The the impulse response&lt;br /&gt;
would be:&lt;br /&gt;
&lt;br /&gt;
 x:  0  0  1  0  0  0  0  ... 0  0  0 ... (input signal)&lt;br /&gt;
 y:  0  0  b0 b1 b2 b3 b4 ... 0  0  0 ... (output signal)&lt;br /&gt;
&lt;br /&gt;
It helps to think of negative powers of z in transfer functions as a&lt;br /&gt;
delay: z^-1 = delay of one sample. z^-2 = delay of two samples etc.&lt;br /&gt;
Since a digital filter is a&lt;br /&gt;
[http://en.wikipedia.org/wiki/Discrete_system discrete-time]&lt;br /&gt;
[http://en.wikipedia.org/wiki/LTI_system_theory linear and time-invariant]&lt;br /&gt;
system it is&lt;br /&gt;
possible to determine the filter&#039;s output by a superposition of scaled&lt;br /&gt;
and shifted impulse responses. This leads to the following relationship&lt;br /&gt;
between input and output in the case of FIR filters:&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(z) = X(z) * B(z)&lt;br /&gt;
&lt;br /&gt;
where x[] is the input signal, y[] the output signal and k the current&lt;br /&gt;
sample index. This is a simple convolution directly computed using the&lt;br /&gt;
given impulse response. The convolution in the time domain corresponds&lt;br /&gt;
to a multiplication in the Z-domain.&lt;br /&gt;
&lt;br /&gt;
== IIR filters ==&lt;br /&gt;
&lt;br /&gt;
IIR filters are a simple extension to the FIR case:&lt;br /&gt;
&lt;br /&gt;
 time domain                                Z-transform&lt;br /&gt;
 -----------                                -----------&lt;br /&gt;
      y[k] + a1 y[k-1] + a2 y[k-2] + ...    Y(z) * A(Z) = X(Z) * B(Z)&lt;br /&gt;
 = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Solving for Y we get&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(Z) = X(Z) * N(z)&lt;br /&gt;
                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
which is the well known &#039;&#039;difference formula&#039;&#039;. Its signal flow graph&lt;br /&gt;
representation is known as&lt;br /&gt;
[http://ccrma.stanford.edu/~jos/filters/Direct_Form_I.html direct form I].&lt;br /&gt;
&lt;br /&gt;
= Noise Shaper Implementation =&lt;br /&gt;
&lt;br /&gt;
Usually a filter&#039;s transfer function is called &#039;H&#039;. In this article &#039;N&#039;&lt;br /&gt;
has been used because this filter is also known as noise transfer&lt;br /&gt;
function (NTF) in the context of noise shaping. Later, another filter&lt;br /&gt;
&#039;H&#039; is introduced that has a special relation to &#039;N&#039;.&lt;br /&gt;
&lt;br /&gt;
== Direct Form I ==&lt;br /&gt;
&lt;br /&gt;
For convenience let us substitute the actual difference in the&lt;br /&gt;
difference formula for &#039;u&#039;. Also, b0 must be equal to one for noise&lt;br /&gt;
shaping filters as mentioned above. This leads to&lt;br /&gt;
&lt;br /&gt;
 y[k] = x[k] + u[k]   with u[k] = b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
                                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Note that since the filter is supposed to be applied on the quantization&lt;br /&gt;
noise y[] corresponds to the filtered noise and x[] to the unfiltered&lt;br /&gt;
noise. Suppose s[] is the signal we&#039;re trying to quantize. The following&lt;br /&gt;
pseudo code snippet shows the inner loop over k of &#039;&#039;one possible&#039;&#039;&lt;br /&gt;
noise shaping implementation:&lt;br /&gt;
&lt;br /&gt;
 (1) compute u[k]&lt;br /&gt;
 (2) wanted[k] = s[k] + u[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) y[k] = quantized[k] - s[k]&lt;br /&gt;
 (5) x[k] = y[k] - u[k]&lt;br /&gt;
&lt;br /&gt;
Note that the following is true&lt;br /&gt;
&lt;br /&gt;
 time domain                          Z-transform&lt;br /&gt;
 -----------                          -----------&lt;br /&gt;
 quantized[k] = s[k] + u[k] + x[k]    Q(z) = S(z) + X(z) * N(z)&lt;br /&gt;
              = s[k] + y[k]                = S(z) + Y(z)&lt;br /&gt;
&lt;br /&gt;
u[k], wanted[k] are temporary variables. The values are not needed&lt;br /&gt;
outside this iteration. But the past filtered and unfiltered error&lt;br /&gt;
samples need to be remembered (y[] &amp;amp; x[]) upto some amount of samples&lt;br /&gt;
determined by the filter order.&lt;br /&gt;
&lt;br /&gt;
== Decoupling filtering and quantization ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Noiseshaper3.png|thumb|right|200px]]&lt;br /&gt;
As mentioned, the previous example is &#039;&#039;one possible&#039;&#039; implementation.&lt;br /&gt;
It&#039;s not the best possible one. But it is the first version that comes to mind.&lt;br /&gt;
There&#039;s another common formulation which requires&lt;br /&gt;
some more transfer function fiddeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt; N(z) = \frac{B(z)}{A(z)} = 1 - \frac{A(z)}{A(z)} + \frac{B(z)}{A(z)}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The two fractions have a common denominator so they can be easily&lt;br /&gt;
combined to one fraction. It&#039;s also possible to factor z^-1 out of the fraction:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = 1 - \frac{A(z)-B(z)}{A(z)} = 1 - z^{-1} \frac{z ( A(z)-B(z) )}{A(z)} =: 1 - z^{-1} H(z)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Substituting z*(A(Z)-B(Z)) for H(z) leads to a new filter that is related to N.&lt;br /&gt;
Note that with b0=1 it follows that z*(1-b0)=0. So, H(z) can be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{c_0 + c_1 z^{-1} + c_2 z^{-2} + \ldots}{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
with &amp;lt;math&amp;gt;c_{k-1} = a_k - b_k&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;k = 1...m&amp;lt;/math&amp;gt;. H(z)&lt;br /&gt;
is some &#039;&#039;arbitrary&#039;&#039; filter. The big advantage now is that it is&lt;br /&gt;
possible to plug nearly any digital filter implementation for H(z) into&lt;br /&gt;
the noise shaping loop. One obvious approach is the difference formula&lt;br /&gt;
from above but there are other alternatives with desirable properties.&lt;br /&gt;
However, the purpose of this article is not to discuss digital filters&lt;br /&gt;
in general but their application as noise shaping filters. So,&lt;br /&gt;
throughout the rest of this article H(z) is simply treated as a black&lt;br /&gt;
box. This black box has a certain state and gives us an output sample&lt;br /&gt;
for each input sample. The modified pseudo code loop looks like this:&lt;br /&gt;
&lt;br /&gt;
 (1) h_out[k] = H.filter(last_error)&lt;br /&gt;
 (2) wanted[k] = s[k] - h_out[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) last_error = quantized[k] - wanted[k]&lt;br /&gt;
&lt;br /&gt;
which is equivalent to the shown signal flow graph where the unit&lt;br /&gt;
sample delay z^-1 is implemented by the state variable &#039;last_error&#039;:&lt;br /&gt;
Remember that this black box filter H(z) doesn&#039;t correspond the the&lt;br /&gt;
actual noise shaping filter N(z). Depending on the context published&lt;br /&gt;
coefficients for noise shaping correspond either to N(z), H(z), or&lt;br /&gt;
-H(z).&lt;br /&gt;
&lt;br /&gt;
= Example filters =&lt;br /&gt;
&lt;br /&gt;
The following two filters have been designed by Sebastian Gesemann&lt;br /&gt;
via non-linear optimization to match a slightly modified version of&lt;br /&gt;
the LAME MP3 encoder&#039;s ATH (absolute threshold of hearing) curve. They&lt;br /&gt;
were first published in the&lt;br /&gt;
[http://www.hydrogenaudio.org/forums/index.php?showtopic=47980&amp;amp;pid=551558&amp;amp;st=0 Hydrogenaudio forum]&lt;br /&gt;
including the noise transfer function&#039;s second order factorizations.&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 44100 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.1474 z^{-1} +0.5383 z^{-2} -0.3520 z^{-3} +0.3475 z^{-4}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2061 -0.4706 z^{-1} -0.2534 z^{-2} -0.6214 z^{-3}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 48000 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.3344 z^{-1} +0.7455 z^{-2} -0.4602 z^{-3} +0.3463 z^{-4}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2374 -0.7339 z^{-1} -0.1251 z^{-2} -0.6033 z^{-3}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Notes===&lt;br /&gt;
&lt;br /&gt;
==References== &lt;br /&gt;
* Lipshitz. P, Stanley and John Vanderkooy. &amp;quot;Why 1-bit Sigma-Delta Conversion is Unsuitable for High-Quality Applications&amp;quot;. 110th Convention.  &amp;lt;i&amp;gt;Audio Engineering Society&amp;lt;/i&amp;gt; (May 2001). 2-4. &lt;br /&gt;
* &lt;br /&gt;
&lt;br /&gt;
[[Category:Signal Processing]]&lt;/div&gt;</summary>
		<author><name>SebastianG</name></author>
	</entry>
	<entry>
		<id>https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=19018</id>
		<title>Noise shaping</title>
		<link rel="alternate" type="text/html" href="https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=19018"/>
		<updated>2008-08-20T12:59:34Z</updated>

		<summary type="html">&lt;p&gt;SebastianG: /* Decoupling filtering and quantization */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
Noise shaping is a technique that is often used in combination with&lt;br /&gt;
[[Dithering|dithering]] in the context of [[Quantize|quantization]]. The purpose of noise shaping&lt;br /&gt;
is to alter the spectral &#039;&#039;shape&#039;&#039; of the error that is introduced due&lt;br /&gt;
to [[Dithering|dithering]] and [[Quantize|quantization]]. Sometimes it may be desirable to&lt;br /&gt;
concentrate most of the noise power in a certain frequency band so&lt;br /&gt;
that the overall noise becomes less perceivable.&lt;br /&gt;
&lt;br /&gt;
= Applications =&lt;br /&gt;
&lt;br /&gt;
In the field of digital image processing a popular noise shaping&lt;br /&gt;
technique is known as &amp;quot;Floyd Steinberg dithering&amp;quot;. This algorithm is&lt;br /&gt;
used to reduce the noise power at lower spatial frequencies which&lt;br /&gt;
results in an improved perceptual quality.&lt;br /&gt;
&lt;br /&gt;
For word length reductions of audio signals (i.e conversion from a 24bit&lt;br /&gt;
signal to a 16bit signal) noise shaping can be used to push quantization&lt;br /&gt;
noise out of perceptually important frequency bands to perceptually less&lt;br /&gt;
important frequency bands (&amp;gt;20 kHz) in order to reduce the perceived&lt;br /&gt;
loudness of the quantization noise.&lt;br /&gt;
&lt;br /&gt;
Another example is the &lt;br /&gt;
[[Super Audio Compact Disc|Super Audio CD]]. The SACD format represents&lt;br /&gt;
the most extreme trade-off between word length (1bit/sample) and&lt;br /&gt;
sampling frequency (2.8 Mhz). Due to the low word length high power&lt;br /&gt;
noise is introduced. With the help of noise shaping this noise is&lt;br /&gt;
usually rejected by about 120 dB in the audible band (0-20 kHz) at the&lt;br /&gt;
cost of a higher noise power in the ultrasonic range (+3.5dB for&lt;br /&gt;
frequencies above 100 kHz).&lt;br /&gt;
&lt;br /&gt;
= Limits &amp;amp; filter design restrictions =&lt;br /&gt;
&lt;br /&gt;
Noise Shaping without [[Dithering|&#039;&#039;dithered&#039;&#039;]] quantization is also possible but&lt;br /&gt;
less useful because the spectral characteristics of &#039;&#039;undithered&#039;&#039;&lt;br /&gt;
quantization noise is not easily predictable and depends heavily on the&lt;br /&gt;
digital signal that is to be quantized. The SACD case is an example where&lt;br /&gt;
[[Dithering|dithering]] can&#039;t be properly applied which may lead to a spectral&lt;br /&gt;
noise power distribution that deviates from the indented distribution. This has&lt;br /&gt;
been demonstrated by Lipshitz and Vanderkooy in&lt;br /&gt;
[http://www.sjeng.org/ftp/SACD.pdf their] [1] paper. &lt;br /&gt;
&lt;br /&gt;
It&#039;s not possible to lower the noise power for &#039;&#039;all&#039;&#039; frequencies&lt;br /&gt;
via noise shaping because this would be in violation of the&lt;br /&gt;
rate/distortion principles. But it &#039;&#039;is&#039;&#039; possible to lower the noise&lt;br /&gt;
in certain frequency bands at the cost of increased noise power in other&lt;br /&gt;
frequency bands. This property is also known as the&lt;br /&gt;
&#039;&#039;noise shaping theorem&#039;&#039;. It states that the average logarithmic magnitude response of&lt;br /&gt;
the corresponding noise shaping &#039;&#039;filter&#039;&#039; over the whole linear&lt;br /&gt;
frequency axis cannot drop below 0dB.&lt;br /&gt;
&lt;br /&gt;
The noise shaping application poses some restrictions on the fiter&lt;br /&gt;
design. The first sample of the filter&#039;s impulse response always equals&lt;br /&gt;
one. Also, interesting filters for noise shaping are so-called&lt;br /&gt;
&#039;&#039;minimum phase filters&#039;&#039;. This type of filter performs optimal in the sense of&lt;br /&gt;
the noise shaping theorem because the average logarithmig magnitude&lt;br /&gt;
response &#039;&#039;is&#039;&#039; 0dB.&lt;br /&gt;
&lt;br /&gt;
For the special [[Super Audio Compact Disc|SACD]] case an additional&lt;br /&gt;
constraint comes into play: The magnitude response should not exceed&lt;br /&gt;
3.5dB. This is enforced to prevent so-called limit cycles.&lt;br /&gt;
&lt;br /&gt;
The design of these kinds of filters is however not trivial. This is not&lt;br /&gt;
covered by this article.&lt;br /&gt;
&lt;br /&gt;
= Digital filter reminder =&lt;br /&gt;
&lt;br /&gt;
A common representation of digital filters is their &#039;&#039;transfer function&#039;&#039;&lt;br /&gt;
in form of a rational polynomial in z^-1. These polynomials are defined&lt;br /&gt;
by their coefficients b0,b1,b2,...,a1,a2,...:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{B(z)}{A(z)} = \frac{b_0 + b_1 z^{-1} + b_2 z^{-2} + \ldots + b_m z^{-m}}&lt;br /&gt;
{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots + a_m z^{-m}} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &#039;N&#039; is the transfer function and &#039;m&#039; the filter&#039;s &#039;&#039;order&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
== FIR filters ==&lt;br /&gt;
&lt;br /&gt;
In case a1=a2=a3=...=0 the filter&#039;s [http://en.wikipedia.org/wiki/Impulse_response impulse response] is finite which is&lt;br /&gt;
why these filters are called FIR ([[Finite Impulse Response Filter|finite impulse response]])&lt;br /&gt;
filters as opposed to IIR&lt;br /&gt;
[[Infinite Impulse Response Filter|infinite impulse response]] filters. Note that in case of&lt;br /&gt;
FIR filters the coefficients b0, b1, ... correspond to the impulse&lt;br /&gt;
response. The impulse response is another way a filter can be&lt;br /&gt;
characterized and describes what the filter does to a unit pulse. Let&#039;s&lt;br /&gt;
assume for the moment that a1=a2=a3=...=0. The the impulse response&lt;br /&gt;
would be:&lt;br /&gt;
&lt;br /&gt;
 x:  0  0  1  0  0  0  0  ... 0  0  0 ... (input signal)&lt;br /&gt;
 y:  0  0  b0 b1 b2 b3 b4 ... 0  0  0 ... (output signal)&lt;br /&gt;
&lt;br /&gt;
It helps to think of negative powers of z in transfer functions as a&lt;br /&gt;
delay: z^-1 = delay of one sample. z^-2 = delay of two samples etc.&lt;br /&gt;
Since a digital filter is a&lt;br /&gt;
[http://en.wikipedia.org/wiki/Discrete_system discrete-time]&lt;br /&gt;
[http://en.wikipedia.org/wiki/LTI_system_theory linear and time-invariant]&lt;br /&gt;
system it is&lt;br /&gt;
possible to determine the filter&#039;s output by a superposition of scaled&lt;br /&gt;
and shifted impulse responses. This leads to the following relationship&lt;br /&gt;
between input and output in the case of FIR filters:&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(z) = X(z) * B(z)&lt;br /&gt;
&lt;br /&gt;
where x[] is the input signal, y[] the output signal and k the current&lt;br /&gt;
sample index. This is a simple convolution directly computed using the&lt;br /&gt;
given impulse response. The convolution in the time domain corresponds&lt;br /&gt;
to a multiplication in the Z-domain.&lt;br /&gt;
&lt;br /&gt;
== IIR filters ==&lt;br /&gt;
&lt;br /&gt;
IIR filters are a simple extension to the FIR case:&lt;br /&gt;
&lt;br /&gt;
 time domain                                Z-transform&lt;br /&gt;
 -----------                                -----------&lt;br /&gt;
      y[k] + a1 y[k-1] + a2 y[k-2] + ...    Y(z) * A(Z) = X(Z) * B(Z)&lt;br /&gt;
 = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Solving for Y we get&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(Z) = X(Z) * N(z)&lt;br /&gt;
                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
which is the well known &#039;&#039;difference formula&#039;&#039;. Its signal flow graph&lt;br /&gt;
representation is known as&lt;br /&gt;
[http://ccrma.stanford.edu/~jos/filters/Direct_Form_I.html direct form I].&lt;br /&gt;
&lt;br /&gt;
= Noise Shaper Implementation =&lt;br /&gt;
&lt;br /&gt;
Usually a filter&#039;s transfer function is called &#039;H&#039;. In this article &#039;N&#039;&lt;br /&gt;
has been used because this filter is also known as noise transfer&lt;br /&gt;
function (NTF) in the context of noise shaping. Later, another filter&lt;br /&gt;
&#039;H&#039; is introduced that has a special relation to &#039;N&#039;.&lt;br /&gt;
&lt;br /&gt;
== Direct Form I ==&lt;br /&gt;
&lt;br /&gt;
For convenience let us substitute the actual difference in the&lt;br /&gt;
difference formula for &#039;u&#039;. Also, b0 must be equal to one for noise&lt;br /&gt;
shaping filters as mentioned above. This leads to&lt;br /&gt;
&lt;br /&gt;
 y[k] = x[k] + u[k]   with u[k] = b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
                                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Note that since the filter is supposed to be applied on the quantization&lt;br /&gt;
noise y[] corresponds to the filtered noise and x[] to the unfiltered&lt;br /&gt;
noise. Suppose s[] is the signal we&#039;re trying to quantize. The following&lt;br /&gt;
pseudo code snippet shows the inner loop over k of &#039;&#039;one possible&#039;&#039;&lt;br /&gt;
noise shaping implementation:&lt;br /&gt;
&lt;br /&gt;
 (1) compute u[k]&lt;br /&gt;
 (2) wanted[k] = s[k] + u[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) y[k] = quantized[k] - s[k]&lt;br /&gt;
 (5) x[k] = y[k] - u[k]&lt;br /&gt;
&lt;br /&gt;
Note that the following is true&lt;br /&gt;
&lt;br /&gt;
 time domain                          Z-transform&lt;br /&gt;
 -----------                          -----------&lt;br /&gt;
 quantized[k] = s[k] + u[k] + x[k]    Q(z) = S(z) + X(z) * N(z)&lt;br /&gt;
              = s[k] + y[k]                = S(z) + Y(z)&lt;br /&gt;
&lt;br /&gt;
u[k], wanted[k] are temporary variables. The values are not needed&lt;br /&gt;
outside this iteration. But the past filtered and unfiltered error&lt;br /&gt;
samples need to be remembered (y[] &amp;amp; x[]) upto some amount of samples&lt;br /&gt;
determined by the filter order.&lt;br /&gt;
&lt;br /&gt;
== Decoupling filtering and quantization ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Noiseshaper3.png|thumb|right|200px]]&lt;br /&gt;
As mentioned, the previous example is &#039;&#039;one possible&#039;&#039; implementation.&lt;br /&gt;
It&#039;s not the best possible one. But it is the first version that comes to mind.&lt;br /&gt;
There&#039;s another common formulation which requires&lt;br /&gt;
some more transfer function fiddeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt; N(z) = \frac{B(z)}{A(z)} = 1 - \frac{A(z)}{A(z)} + \frac{B(z)}{A(z)}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The two fractions have a common denominator so they can be easily&lt;br /&gt;
combined to one fraction. It&#039;s also possible to factor z^-1 out of the fraction:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = 1 - \frac{A(z)-B(z)}{A(z)} = 1 - z^{-1} \frac{z ( A(z)-B(z) )}{A(z)} =: 1 - z^{-1} H(z)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Substituting z*(A(Z)-B(Z)) for H(z) leads to a new filter that is related to N.&lt;br /&gt;
Note that with b0=1 it follows that z*(1-b0)=0. So, H(z) can be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{c_0 + c_1 z^{-1} + c_2 z^{-2} + \ldots}{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
with &amp;lt;math&amp;gt;c_{k-1} = a_k - b_k&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;k = 1...m&amp;lt;/math&amp;gt;. H(z)&lt;br /&gt;
is some &#039;&#039;arbitrary&#039;&#039; filter. The big advantage now is that it is&lt;br /&gt;
possible to plug nearly any digital filter implementation for H(z) into&lt;br /&gt;
the noise shaping loop. One obvious approach is the difference formula&lt;br /&gt;
from above but there are other alternatives with desirable properties.&lt;br /&gt;
However, the purpose of this article is not to discuss digital filters&lt;br /&gt;
in general but their application as noise shaping filters. So,&lt;br /&gt;
throughout the rest of this article H(z) is simply treated as a black&lt;br /&gt;
box. This black box has a certain state and gives us an output sample&lt;br /&gt;
for each input sample. The modified pseudo code loop looks like this:&lt;br /&gt;
&lt;br /&gt;
 (1) h_out[k] = H.filter(last_error)&lt;br /&gt;
 (2) wanted[k] = s[k] - h_out[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) last_error = quantized[k] - wanted[k]&lt;br /&gt;
&lt;br /&gt;
which is equivalent to the shown signal flow graph where the unit&lt;br /&gt;
sample delay z^-1 is implemented by the state variable &#039;last_error&#039;:&lt;br /&gt;
Remember that this black box filter H(z) doesn&#039;t correspond the the&lt;br /&gt;
actual noise shaping filter N(z). Depending on the context published&lt;br /&gt;
coefficients for noise shaping correspond either to N(z), H(z), or&lt;br /&gt;
-H(z).&lt;br /&gt;
&lt;br /&gt;
= Example filters =&lt;br /&gt;
&lt;br /&gt;
The following two filters have been designed by Sebastian Gesemann&lt;br /&gt;
via non-linear optimization to match a slightly modified version of&lt;br /&gt;
the LAME MP3 encoder&#039;s ATH (absolute threshold of hearing) curve. They&lt;br /&gt;
were first published in the&lt;br /&gt;
[http://www.hydrogenaudio.org/forums/index.php?showtopic=47980&amp;amp;pid=551558&amp;amp;st=0 Hydrogenaudio forum]&lt;br /&gt;
including the noise transfer function&#039;s second order factorizations.&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 44100 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.1474 z^{-1} +0.5383 z^{-2} -0.3520 z^{-3} +0.3475 z^{-4}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2061 -0.4706 z^{-1} -0.2534 z^{-2} -0.6214 z^{-3}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 48000 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.3344 z^{-1} +0.7455 z^{-2} -0.4602 z^{-3} +0.3463 z^{-4}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2374 -0.7339 z^{-1} -0.1251 z^{-2} -0.6033 z^{-3}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Notes==  &lt;br /&gt;
&lt;br /&gt;
==References== &lt;br /&gt;
* Lipshitz. P, Stanley and John Vanderkooy. &amp;quot;Why 1-bit Sigma-Delta Conversion is Unsuitable for High-Quality Applications&amp;quot;. 110th Convention.  &amp;lt;i&amp;gt;Audio Engineering Society&amp;lt;/i&amp;gt; (May 2001). 2-4. &lt;br /&gt;
* &lt;br /&gt;
&lt;br /&gt;
[[Category:Signal Processing]]&lt;/div&gt;</summary>
		<author><name>SebastianG</name></author>
	</entry>
	<entry>
		<id>https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=19017</id>
		<title>Noise shaping</title>
		<link rel="alternate" type="text/html" href="https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=19017"/>
		<updated>2008-08-20T12:57:38Z</updated>

		<summary type="html">&lt;p&gt;SebastianG: /* IIR filters */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
Noise shaping is a technique that is often used in combination with&lt;br /&gt;
[[Dithering|dithering]] in the context of [[Quantize|quantization]]. The purpose of noise shaping&lt;br /&gt;
is to alter the spectral &#039;&#039;shape&#039;&#039; of the error that is introduced due&lt;br /&gt;
to [[Dithering|dithering]] and [[Quantize|quantization]]. Sometimes it may be desirable to&lt;br /&gt;
concentrate most of the noise power in a certain frequency band so&lt;br /&gt;
that the overall noise becomes less perceivable.&lt;br /&gt;
&lt;br /&gt;
= Applications =&lt;br /&gt;
&lt;br /&gt;
In the field of digital image processing a popular noise shaping&lt;br /&gt;
technique is known as &amp;quot;Floyd Steinberg dithering&amp;quot;. This algorithm is&lt;br /&gt;
used to reduce the noise power at lower spatial frequencies which&lt;br /&gt;
results in an improved perceptual quality.&lt;br /&gt;
&lt;br /&gt;
For word length reductions of audio signals (i.e conversion from a 24bit&lt;br /&gt;
signal to a 16bit signal) noise shaping can be used to push quantization&lt;br /&gt;
noise out of perceptually important frequency bands to perceptually less&lt;br /&gt;
important frequency bands (&amp;gt;20 kHz) in order to reduce the perceived&lt;br /&gt;
loudness of the quantization noise.&lt;br /&gt;
&lt;br /&gt;
Another example is the &lt;br /&gt;
[[Super Audio Compact Disc|Super Audio CD]]. The SACD format represents&lt;br /&gt;
the most extreme trade-off between word length (1bit/sample) and&lt;br /&gt;
sampling frequency (2.8 Mhz). Due to the low word length high power&lt;br /&gt;
noise is introduced. With the help of noise shaping this noise is&lt;br /&gt;
usually rejected by about 120 dB in the audible band (0-20 kHz) at the&lt;br /&gt;
cost of a higher noise power in the ultrasonic range (+3.5dB for&lt;br /&gt;
frequencies above 100 kHz).&lt;br /&gt;
&lt;br /&gt;
= Limits &amp;amp; filter design restrictions =&lt;br /&gt;
&lt;br /&gt;
Noise Shaping without [[Dithering|&#039;&#039;dithered&#039;&#039;]] quantization is also possible but&lt;br /&gt;
less useful because the spectral characteristics of &#039;&#039;undithered&#039;&#039;&lt;br /&gt;
quantization noise is not easily predictable and depends heavily on the&lt;br /&gt;
digital signal that is to be quantized. The SACD case is an example where&lt;br /&gt;
[[Dithering|dithering]] can&#039;t be properly applied which may lead to a spectral&lt;br /&gt;
noise power distribution that deviates from the indented distribution. This has&lt;br /&gt;
been demonstrated by Lipshitz and Vanderkooy in&lt;br /&gt;
[http://www.sjeng.org/ftp/SACD.pdf their] [1] paper. &lt;br /&gt;
&lt;br /&gt;
It&#039;s not possible to lower the noise power for &#039;&#039;all&#039;&#039; frequencies&lt;br /&gt;
via noise shaping because this would be in violation of the&lt;br /&gt;
rate/distortion principles. But it &#039;&#039;is&#039;&#039; possible to lower the noise&lt;br /&gt;
in certain frequency bands at the cost of increased noise power in other&lt;br /&gt;
frequency bands. This property is also known as the&lt;br /&gt;
&#039;&#039;noise shaping theorem&#039;&#039;. It states that the average logarithmic magnitude response of&lt;br /&gt;
the corresponding noise shaping &#039;&#039;filter&#039;&#039; over the whole linear&lt;br /&gt;
frequency axis cannot drop below 0dB.&lt;br /&gt;
&lt;br /&gt;
The noise shaping application poses some restrictions on the fiter&lt;br /&gt;
design. The first sample of the filter&#039;s impulse response always equals&lt;br /&gt;
one. Also, interesting filters for noise shaping are so-called&lt;br /&gt;
&#039;&#039;minimum phase filters&#039;&#039;. This type of filter performs optimal in the sense of&lt;br /&gt;
the noise shaping theorem because the average logarithmig magnitude&lt;br /&gt;
response &#039;&#039;is&#039;&#039; 0dB.&lt;br /&gt;
&lt;br /&gt;
For the special [[Super Audio Compact Disc|SACD]] case an additional&lt;br /&gt;
constraint comes into play: The magnitude response should not exceed&lt;br /&gt;
3.5dB. This is enforced to prevent so-called limit cycles.&lt;br /&gt;
&lt;br /&gt;
The design of these kinds of filters is however not trivial. This is not&lt;br /&gt;
covered by this article.&lt;br /&gt;
&lt;br /&gt;
= Digital filter reminder =&lt;br /&gt;
&lt;br /&gt;
A common representation of digital filters is their &#039;&#039;transfer function&#039;&#039;&lt;br /&gt;
in form of a rational polynomial in z^-1. These polynomials are defined&lt;br /&gt;
by their coefficients b0,b1,b2,...,a1,a2,...:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{B(z)}{A(z)} = \frac{b_0 + b_1 z^{-1} + b_2 z^{-2} + \ldots + b_m z^{-m}}&lt;br /&gt;
{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots + a_m z^{-m}} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &#039;N&#039; is the transfer function and &#039;m&#039; the filter&#039;s &#039;&#039;order&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
== FIR filters ==&lt;br /&gt;
&lt;br /&gt;
In case a1=a2=a3=...=0 the filter&#039;s [http://en.wikipedia.org/wiki/Impulse_response impulse response] is finite which is&lt;br /&gt;
why these filters are called FIR ([[Finite Impulse Response Filter|finite impulse response]])&lt;br /&gt;
filters as opposed to IIR&lt;br /&gt;
[[Infinite Impulse Response Filter|infinite impulse response]] filters. Note that in case of&lt;br /&gt;
FIR filters the coefficients b0, b1, ... correspond to the impulse&lt;br /&gt;
response. The impulse response is another way a filter can be&lt;br /&gt;
characterized and describes what the filter does to a unit pulse. Let&#039;s&lt;br /&gt;
assume for the moment that a1=a2=a3=...=0. The the impulse response&lt;br /&gt;
would be:&lt;br /&gt;
&lt;br /&gt;
 x:  0  0  1  0  0  0  0  ... 0  0  0 ... (input signal)&lt;br /&gt;
 y:  0  0  b0 b1 b2 b3 b4 ... 0  0  0 ... (output signal)&lt;br /&gt;
&lt;br /&gt;
It helps to think of negative powers of z in transfer functions as a&lt;br /&gt;
delay: z^-1 = delay of one sample. z^-2 = delay of two samples etc.&lt;br /&gt;
Since a digital filter is a&lt;br /&gt;
[http://en.wikipedia.org/wiki/Discrete_system discrete-time]&lt;br /&gt;
[http://en.wikipedia.org/wiki/LTI_system_theory linear and time-invariant]&lt;br /&gt;
system it is&lt;br /&gt;
possible to determine the filter&#039;s output by a superposition of scaled&lt;br /&gt;
and shifted impulse responses. This leads to the following relationship&lt;br /&gt;
between input and output in the case of FIR filters:&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(z) = X(z) * B(z)&lt;br /&gt;
&lt;br /&gt;
where x[] is the input signal, y[] the output signal and k the current&lt;br /&gt;
sample index. This is a simple convolution directly computed using the&lt;br /&gt;
given impulse response. The convolution in the time domain corresponds&lt;br /&gt;
to a multiplication in the Z-domain.&lt;br /&gt;
&lt;br /&gt;
== IIR filters ==&lt;br /&gt;
&lt;br /&gt;
IIR filters are a simple extension to the FIR case:&lt;br /&gt;
&lt;br /&gt;
 time domain                                Z-transform&lt;br /&gt;
 -----------                                -----------&lt;br /&gt;
      y[k] + a1 y[k-1] + a2 y[k-2] + ...    Y(z) * A(Z) = X(Z) * B(Z)&lt;br /&gt;
 = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Solving for Y we get&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(Z) = X(Z) * N(z)&lt;br /&gt;
                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
which is the well known &#039;&#039;difference formula&#039;&#039;. Its signal flow graph&lt;br /&gt;
representation is known as&lt;br /&gt;
[http://ccrma.stanford.edu/~jos/filters/Direct_Form_I.html direct form I].&lt;br /&gt;
&lt;br /&gt;
= Noise Shaper Implementation =&lt;br /&gt;
&lt;br /&gt;
Usually a filter&#039;s transfer function is called &#039;H&#039;. In this article &#039;N&#039;&lt;br /&gt;
has been used because this filter is also known as noise transfer&lt;br /&gt;
function (NTF) in the context of noise shaping. Later, another filter&lt;br /&gt;
&#039;H&#039; is introduced that has a special relation to &#039;N&#039;.&lt;br /&gt;
&lt;br /&gt;
== Direct Form I ==&lt;br /&gt;
&lt;br /&gt;
For convenience let us substitute the actual difference in the&lt;br /&gt;
difference formula for &#039;u&#039;. Also, b0 must be equal to one for noise&lt;br /&gt;
shaping filters as mentioned above. This leads to&lt;br /&gt;
&lt;br /&gt;
 y[k] = x[k] + u[k]   with u[k] = b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
                                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Note that since the filter is supposed to be applied on the quantization&lt;br /&gt;
noise y[] corresponds to the filtered noise and x[] to the unfiltered&lt;br /&gt;
noise. Suppose s[] is the signal we&#039;re trying to quantize. The following&lt;br /&gt;
pseudo code snippet shows the inner loop over k of &#039;&#039;one possible&#039;&#039;&lt;br /&gt;
noise shaping implementation:&lt;br /&gt;
&lt;br /&gt;
 (1) compute u[k]&lt;br /&gt;
 (2) wanted[k] = s[k] + u[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) y[k] = quantized[k] - s[k]&lt;br /&gt;
 (5) x[k] = y[k] - u[k]&lt;br /&gt;
&lt;br /&gt;
Note that the following is true&lt;br /&gt;
&lt;br /&gt;
 time domain                          Z-transform&lt;br /&gt;
 -----------                          -----------&lt;br /&gt;
 quantized[k] = s[k] + u[k] + x[k]    Q(z) = S(z) + X(z) * N(z)&lt;br /&gt;
              = s[k] + y[k]                = S(z) + Y(z)&lt;br /&gt;
&lt;br /&gt;
u[k], wanted[k] are temporary variables. The values are not needed&lt;br /&gt;
outside this iteration. But the past filtered and unfiltered error&lt;br /&gt;
samples need to be remembered (y[] &amp;amp; x[]) upto some amount of samples&lt;br /&gt;
determined by the filter order.&lt;br /&gt;
&lt;br /&gt;
== Decoupling filtering and quantization ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Noiseshaper3.png|thumb|right|200px]]&lt;br /&gt;
As mentioned, the previous example is &#039;&#039;one possible&#039;&#039; implementation.&lt;br /&gt;
It&#039;s not the best possible one. But it is the first version that comes to mind.&lt;br /&gt;
There&#039;s another common formulation which requires&lt;br /&gt;
some more transfer function fiddeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt; N(z) = \frac{B(z)}{A(z)} = 1 - \frac{A(z)}{A(z)} + \frac{B(z)}{A(z)}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since now the two fractions have a common denominator they can be easily&lt;br /&gt;
combined to one fraction. It&#039;s also possible to factor z^-1 out of the fraction:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = 1 - \frac{A(z)-B(z)}{A(z)} = 1 - z^{-1} \frac{z ( A(z)-B(z) )}{A(z)} =: 1 - z^{-1} H(z)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Substituting z*(A(Z)-B(Z)) for H(z) leads to a new filter that is related to N.&lt;br /&gt;
Note that with b0=1 it follows that z*(1-b0)=0. So, H(z) can be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{c_0 + c_1 z^{-1} + c_2 z^{-2} + \ldots}{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
with &amp;lt;math&amp;gt;c_{k-1} = a_k - b_k&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;k = 1...m&amp;lt;/math&amp;gt;. H(z)&lt;br /&gt;
is some &#039;&#039;arbitrary&#039;&#039; filter. The big advantage now is that it is&lt;br /&gt;
possible to plug nearly any digital filter implementation for H(z) into&lt;br /&gt;
the noise shaping loop. One obvious approach is the difference formula&lt;br /&gt;
from above but there are other alternatives with desirable properties.&lt;br /&gt;
However, the purpose of this article is not to discuss digital filters&lt;br /&gt;
in general but their application as noise shaping filters. So,&lt;br /&gt;
throughout the rest of this article H(z) is simply treated as a black&lt;br /&gt;
box. This black box has a certain state and gives us an output sample&lt;br /&gt;
for each input sample. The modified pseudo code loop looks like this:&lt;br /&gt;
&lt;br /&gt;
 (1) h_out[k] = H.filter(last_error)&lt;br /&gt;
 (2) wanted[k] = s[k] - h_out[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) last_error = quantized[k] - wanted[k]&lt;br /&gt;
&lt;br /&gt;
which is equivalent to the shown signal flow graph where the unit&lt;br /&gt;
sample delay z^-1 is implemented by the state variable &#039;last_error&#039;:&lt;br /&gt;
Remember that this black box filter H(z) doesn&#039;t correspond the the&lt;br /&gt;
actual noise shaping filter N(z). Depending on the context published&lt;br /&gt;
coefficients for noise shaping correspond either to N(z), H(z), or&lt;br /&gt;
-H(z).&lt;br /&gt;
&lt;br /&gt;
= Example filters =&lt;br /&gt;
&lt;br /&gt;
The following two filters have been designed by Sebastian Gesemann&lt;br /&gt;
via non-linear optimization to match a slightly modified version of&lt;br /&gt;
the LAME MP3 encoder&#039;s ATH (absolute threshold of hearing) curve. They&lt;br /&gt;
were first published in the&lt;br /&gt;
[http://www.hydrogenaudio.org/forums/index.php?showtopic=47980&amp;amp;pid=551558&amp;amp;st=0 Hydrogenaudio forum]&lt;br /&gt;
including the noise transfer function&#039;s second order factorizations.&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 44100 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.1474 z^{-1} +0.5383 z^{-2} -0.3520 z^{-3} +0.3475 z^{-4}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2061 -0.4706 z^{-1} -0.2534 z^{-2} -0.6214 z^{-3}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 48000 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.3344 z^{-1} +0.7455 z^{-2} -0.4602 z^{-3} +0.3463 z^{-4}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2374 -0.7339 z^{-1} -0.1251 z^{-2} -0.6033 z^{-3}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Notes==  &lt;br /&gt;
&lt;br /&gt;
==References== &lt;br /&gt;
* Lipshitz. P, Stanley and John Vanderkooy. &amp;quot;Why 1-bit Sigma-Delta Conversion is Unsuitable for High-Quality Applications&amp;quot;. 110th Convention.  &amp;lt;i&amp;gt;Audio Engineering Society&amp;lt;/i&amp;gt; (May 2001). 2-4. &lt;br /&gt;
* &lt;br /&gt;
&lt;br /&gt;
[[Category:Signal Processing]]&lt;/div&gt;</summary>
		<author><name>SebastianG</name></author>
	</entry>
	<entry>
		<id>https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=18359</id>
		<title>Noise shaping</title>
		<link rel="alternate" type="text/html" href="https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=18359"/>
		<updated>2008-04-08T13:54:01Z</updated>

		<summary type="html">&lt;p&gt;SebastianG: /* Limits &amp;amp; filter design restrictions */  Note on SACD dithering problems added&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
Noise shaping is a technique that is often used in combination with&lt;br /&gt;
[[Dithering|dithering]] in the context of [[Quantize|quantization]]. The purpose of noise shaping&lt;br /&gt;
is to alter the spectral &#039;&#039;shape&#039;&#039; of the error that is introduced due&lt;br /&gt;
to [[Dithering|dithering]] and [[Quantize|quantization]]. Sometimes it may be desirable to&lt;br /&gt;
concentrate most of the noise power in a certain frequency band so&lt;br /&gt;
that the overall noise becomes less perceivable.&lt;br /&gt;
&lt;br /&gt;
= Applications =&lt;br /&gt;
&lt;br /&gt;
In the field of digital image processing a popular noise shaping&lt;br /&gt;
technique is known as &amp;quot;Floyd Steinberg dithering&amp;quot;. This algorithm is&lt;br /&gt;
used to reduce the noise power at lower spatial frequencies which&lt;br /&gt;
results in an improved perceptual quality.&lt;br /&gt;
&lt;br /&gt;
For word length reductions of audio signals (i.e conversion from a 24bit&lt;br /&gt;
signal to a 16bit signal) noise shaping can be used to push quantization&lt;br /&gt;
noise out of perceptually important frequency bands to perceptually less&lt;br /&gt;
important frequency bands (&amp;gt;20 kHz) in order to reduce the perceived&lt;br /&gt;
loudness of the quantization noise.&lt;br /&gt;
&lt;br /&gt;
Another example is the &lt;br /&gt;
[[Super Audio Compact Disc|Super Audio CD]]. The SACD format represents&lt;br /&gt;
the most extreme trade-off between word length (1bit/sample) and&lt;br /&gt;
sampling frequency (2.8 Mhz). Due to the low word length high power&lt;br /&gt;
noise is introduced. With the help of noise shaping this noise is&lt;br /&gt;
usually rejected by about 120 dB in the audible band (0-20 kHz) at the&lt;br /&gt;
cost of a higher noise power in the ultrasonic range (+3.5dB for&lt;br /&gt;
frequencies above 100 kHz).&lt;br /&gt;
&lt;br /&gt;
= Limits &amp;amp; filter design restrictions =&lt;br /&gt;
&lt;br /&gt;
Noise Shaping without [[Dithering|&#039;&#039;dithered&#039;&#039;]] quantization is also possible but&lt;br /&gt;
less useful because the spectral characteristics of &#039;&#039;undithered&#039;&#039;&lt;br /&gt;
quantization noise is not easily predictable and depends heavily on the&lt;br /&gt;
digital signal that is to be quantized. The SACD case is an example where&lt;br /&gt;
[[Dithering|dithering]] can&#039;t be properly applied which may lead to a spectral&lt;br /&gt;
noise power distribution that deviates from the indented distribution. This has&lt;br /&gt;
been demonstrated by Lipshitz and Vanderkooy in their paper&lt;br /&gt;
[http://www.sjeng.org/ftp/SACD.pdf &amp;quot;Why 1-Bit Sigma-Delta Conversion is Unsuitable for High-Quality Applications&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
It&#039;s not possible to lower the noise power for &#039;&#039;all&#039;&#039; frequencies&lt;br /&gt;
via noise shaping because this would be in violation of the&lt;br /&gt;
rate/distortion principles. But it &#039;&#039;is&#039;&#039; possible to lower the noise&lt;br /&gt;
in certain frequency bands at the cost of increased noise power in other&lt;br /&gt;
frequency bands. This property is also known as the&lt;br /&gt;
&#039;&#039;noise shaping theorem&#039;&#039;. It states that the average logarithmic magnitude response of&lt;br /&gt;
the corresponding noise shaping &#039;&#039;filter&#039;&#039; over the whole linear&lt;br /&gt;
frequency axis cannot drop below 0dB.&lt;br /&gt;
&lt;br /&gt;
The noise shaping application poses some restrictions on the fiter&lt;br /&gt;
design. The first sample of the filter&#039;s impulse response always equals&lt;br /&gt;
one. Also, interesting filters for noise shaping are so-called&lt;br /&gt;
&#039;&#039;minimum phase filters&#039;&#039;. This type of filter performs optimal in the sense of&lt;br /&gt;
the noise shaping theorem because the average logarithmig magnitude&lt;br /&gt;
response &#039;&#039;is&#039;&#039; 0dB.&lt;br /&gt;
&lt;br /&gt;
For the special [[Super Audio Compact Disc|SACD]] case an additional&lt;br /&gt;
constraint comes into play: The magnitude response should not exceed&lt;br /&gt;
3.5dB. This is enforced to prevent so-called limit cycles.&lt;br /&gt;
&lt;br /&gt;
The design of these kinds of filters is however not trivial. This is not&lt;br /&gt;
covered by this article.&lt;br /&gt;
&lt;br /&gt;
= Digital filter reminder =&lt;br /&gt;
&lt;br /&gt;
A common representation of digital filters is their &#039;&#039;transfer function&#039;&#039;&lt;br /&gt;
in form of a rational polynomial in z^-1. These polynomials are defined&lt;br /&gt;
by their coefficients b0,b1,b2,...,a1,a2,...:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{B(z)}{A(z)} = \frac{b_0 + b_1 z^{-1} + b_2 z^{-2} + \ldots + b_m z^{-m}}&lt;br /&gt;
{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots + a_m z^{-m}} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &#039;N&#039; is the transfer function and &#039;m&#039; the filter&#039;s &#039;&#039;order&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
== FIR filters ==&lt;br /&gt;
&lt;br /&gt;
In case a1=a2=a3=...=0 the filter&#039;s [http://en.wikipedia.org/wiki/Impulse_response impulse response] is finite which is&lt;br /&gt;
why these filters are called FIR ([[Finite Impulse Response Filter|finite impulse response]])&lt;br /&gt;
filters as opposed to IIR&lt;br /&gt;
[[Infinite Impulse Response Filter|infinite impulse response]] filters. Note that in case of&lt;br /&gt;
FIR filters the coefficients b0, b1, ... correspond to the impulse&lt;br /&gt;
response. The impulse response is another way a filter can be&lt;br /&gt;
characterized and describes what the filter does to a unit pulse. Let&#039;s&lt;br /&gt;
assume for the moment that a1=a2=a3=...=0. The the impulse response&lt;br /&gt;
would be:&lt;br /&gt;
&lt;br /&gt;
 x:  0  0  1  0  0  0  0  ... 0  0  0 ... (input signal)&lt;br /&gt;
 y:  0  0  b0 b1 b2 b3 b4 ... 0  0  0 ... (output signal)&lt;br /&gt;
&lt;br /&gt;
It helps to think of negative powers of z in transfer functions as a&lt;br /&gt;
delay: z^-1 = delay of one sample. z^-2 = delay of two samples etc.&lt;br /&gt;
Since a digital filter is a&lt;br /&gt;
[http://en.wikipedia.org/wiki/Discrete_system discrete-time]&lt;br /&gt;
[http://en.wikipedia.org/wiki/LTI_system_theory linear and time-invariant]&lt;br /&gt;
system it is&lt;br /&gt;
possible to determine the filter&#039;s output by a superposition of scaled&lt;br /&gt;
and shifted impulse responses. This leads to the following relationship&lt;br /&gt;
between input and output in the case of FIR filters:&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(z) = X(z) * B(z)&lt;br /&gt;
&lt;br /&gt;
where x[] is the input signal, y[] the output signal and k the current&lt;br /&gt;
sample index. This is a simple convolution directly computed using the&lt;br /&gt;
given impulse response. The convolution in the time domain corresponds&lt;br /&gt;
to a multiplication in the Z-domain.&lt;br /&gt;
&lt;br /&gt;
== IIR filters ==&lt;br /&gt;
&lt;br /&gt;
IIR filters are now a simple extension to the FIR case:&lt;br /&gt;
&lt;br /&gt;
 time domain                                Z-transform&lt;br /&gt;
 -----------                                -----------&lt;br /&gt;
      y[k] + a1 y[k-1] + a2 y[k-2] + ...    Y(z) * A(Z) = X(Z) * B(Z)&lt;br /&gt;
 = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Solving for Y we get&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(Z) = X(Z) * N(z)&lt;br /&gt;
                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
which is the well known &#039;&#039;difference formula&#039;&#039;. Its signal flow graph&lt;br /&gt;
representation is known as&lt;br /&gt;
[http://ccrma.stanford.edu/~jos/filters/Direct_Form_I.html direct form I].&lt;br /&gt;
Now, let&#039;s see how we can apply this kind of filter to quantization&lt;br /&gt;
errors...&lt;br /&gt;
&lt;br /&gt;
= Noise Shaper Implementation =&lt;br /&gt;
&lt;br /&gt;
Usually a filter&#039;s transfer function is called &#039;H&#039;. In this article &#039;N&#039;&lt;br /&gt;
has been used because this filter is also known as noise transfer&lt;br /&gt;
function (NTF) in the context of noise shaping. Later, another filter&lt;br /&gt;
&#039;H&#039; is introduced that has a special relation to &#039;N&#039;.&lt;br /&gt;
&lt;br /&gt;
== Direct Form I ==&lt;br /&gt;
&lt;br /&gt;
For convenience let us substitute the actual difference in the&lt;br /&gt;
difference formula for &#039;u&#039;. Also, b0 must be equal to one for noise&lt;br /&gt;
shaping filters as mentioned above. This leads to&lt;br /&gt;
&lt;br /&gt;
 y[k] = x[k] + u[k]   with u[k] = b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
                                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Note that since the filter is supposed to be applied on the quantization&lt;br /&gt;
noise y[] corresponds to the filtered noise and x[] to the unfiltered&lt;br /&gt;
noise. Suppose s[] is the signal we&#039;re trying to quantize. The following&lt;br /&gt;
pseudo code snippet shows the inner loop over k of &#039;&#039;one possible&#039;&#039;&lt;br /&gt;
noise shaping implementation:&lt;br /&gt;
&lt;br /&gt;
 (1) compute u[k]&lt;br /&gt;
 (2) wanted[k] = s[k] + u[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) y[k] = quantized[k] - s[k]&lt;br /&gt;
 (5) x[k] = y[k] - u[k]&lt;br /&gt;
&lt;br /&gt;
Note that the following is true&lt;br /&gt;
&lt;br /&gt;
 time domain                          Z-transform&lt;br /&gt;
 -----------                          -----------&lt;br /&gt;
 quantized[k] = s[k] + u[k] + x[k]    Q(z) = S(z) + X(z) * N(z)&lt;br /&gt;
              = s[k] + y[k]                = S(z) + Y(z)&lt;br /&gt;
&lt;br /&gt;
u[k], wanted[k] are temporary variables. The values are not needed&lt;br /&gt;
outside this iteration. But the past filtered and unfiltered error&lt;br /&gt;
samples need to be remembered (y[] &amp;amp; x[]) upto some amount of samples&lt;br /&gt;
determined by the filter order.&lt;br /&gt;
&lt;br /&gt;
== Decoupling filtering and quantization ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Noiseshaper3.png|thumb|right|200px]]&lt;br /&gt;
As mentioned, the previous example is &#039;&#039;one possible&#039;&#039; implementation.&lt;br /&gt;
It&#039;s not the best possible one. But it is the first version that comes to mind.&lt;br /&gt;
There&#039;s another common formulation which requires&lt;br /&gt;
some more transfer function fiddeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt; N(z) = \frac{B(z)}{A(z)} = 1 - \frac{A(z)}{A(z)} + \frac{B(z)}{A(z)}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since now the two fractions have a common denominator they can be easily&lt;br /&gt;
combined to one fraction. It&#039;s also possible to factor z^-1 out of the fraction:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = 1 - \frac{A(z)-B(z)}{A(z)} = 1 - z^{-1} \frac{z ( A(z)-B(z) )}{A(z)} =: 1 - z^{-1} H(z)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Substituting z*(A(Z)-B(Z)) for H(z) leads to a new filter that is related to N.&lt;br /&gt;
Note that with b0=1 it follows that z*(1-b0)=0. So, H(z) can be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{c_0 + c_1 z^{-1} + c_2 z^{-2} + \ldots}{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
with &amp;lt;math&amp;gt;c_{k-1} = a_k - b_k&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;k = 1...m&amp;lt;/math&amp;gt;. H(z)&lt;br /&gt;
is some &#039;&#039;arbitrary&#039;&#039; filter. The big advantage now is that it is&lt;br /&gt;
possible to plug nearly any digital filter implementation for H(z) into&lt;br /&gt;
the noise shaping loop. One obvious approach is the difference formula&lt;br /&gt;
from above but there are other alternatives with desirable properties.&lt;br /&gt;
However, the purpose of this article is not to discuss digital filters&lt;br /&gt;
in general but their application as noise shaping filters. So,&lt;br /&gt;
throughout the rest of this article H(z) is simply treated as a black&lt;br /&gt;
box. This black box has a certain state and gives us an output sample&lt;br /&gt;
for each input sample. The modified pseudo code loop looks like this:&lt;br /&gt;
&lt;br /&gt;
 (1) h_out[k] = H.filter(last_error)&lt;br /&gt;
 (2) wanted[k] = s[k] - h_out[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) last_error = quantized[k] - wanted[k]&lt;br /&gt;
&lt;br /&gt;
which is equivalent to the shown signal flow graph where the unit&lt;br /&gt;
sample delay z^-1 is implemented by the state variable &#039;last_error&#039;:&lt;br /&gt;
Remember that this black box filter H(z) doesn&#039;t correspond the the&lt;br /&gt;
actual noise shaping filter N(z). Depending on the context published&lt;br /&gt;
coefficients for noise shaping correspond either to N(z), H(z), or&lt;br /&gt;
-H(z).&lt;br /&gt;
&lt;br /&gt;
= Example filters =&lt;br /&gt;
&lt;br /&gt;
The following two filters have been designed by Sebastian Gesemann&lt;br /&gt;
via non-linear optimization to match a slightly modified version of&lt;br /&gt;
the LAME MP3 encoder&#039;s ATH (absolute threshold of hearing) curve. They&lt;br /&gt;
were first published in the&lt;br /&gt;
[http://www.hydrogenaudio.org/forums/index.php?showtopic=47980&amp;amp;pid=551558&amp;amp;st=0 Hydrogenaudio forum]&lt;br /&gt;
including the noise transfer function&#039;s second order factorizations.&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 44100 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.1474 z^{-1} +0.5383 z^{-2} -0.3520 z^{-3} +0.3475 z^{-4}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2061 -0.4706 z^{-1} -0.2534 z^{-2} -0.6214 z^{-3}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 48000 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.3344 z^{-1} +0.7455 z^{-2} -0.4602 z^{-3} +0.3463 z^{-4}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2374 -0.7339 z^{-1} -0.1251 z^{-2} -0.6033 z^{-3}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Signal Processing]]&lt;/div&gt;</summary>
		<author><name>SebastianG</name></author>
	</entry>
	<entry>
		<id>https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=18317</id>
		<title>Noise shaping</title>
		<link rel="alternate" type="text/html" href="https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=18317"/>
		<updated>2008-03-14T14:20:21Z</updated>

		<summary type="html">&lt;p&gt;SebastianG: /* FIR filters */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
Noise shaping is a technique that is often used in combination with&lt;br /&gt;
[[Dithering|dithering]] in the context of [[Quantize|quantization]]. The purpose of noise shaping&lt;br /&gt;
is to alter the spectral &#039;&#039;shape&#039;&#039; of the error that is introduced due&lt;br /&gt;
to [[Dithering|dithering]] and [[Quantize|quantization]]. Sometimes it may be desirable to&lt;br /&gt;
concentrate most of the noise power in a certain frequency band so&lt;br /&gt;
that the overall noise becomes less perceivable.&lt;br /&gt;
&lt;br /&gt;
= Applications =&lt;br /&gt;
&lt;br /&gt;
In the field of digital image processing a popular noise shaping&lt;br /&gt;
technique is known as &amp;quot;Floyd Steinberg dithering&amp;quot;. This algorithm is&lt;br /&gt;
used to reduce the noise power at lower spatial frequencies which&lt;br /&gt;
results in an improved perceptual quality.&lt;br /&gt;
&lt;br /&gt;
For word length reductions of audio signals (i.e conversion from a 24bit&lt;br /&gt;
signal to a 16bit signal) noise shaping can be used to push quantization&lt;br /&gt;
noise out of perceptually important frequency bands to perceptually less&lt;br /&gt;
important frequency bands (&amp;gt;20 kHz) in order to reduce the perceived&lt;br /&gt;
loudness of the quantization noise.&lt;br /&gt;
&lt;br /&gt;
Another example is the &lt;br /&gt;
[[Super Audio Compact Disc|Super Audio CD]]. The SACD format represents&lt;br /&gt;
the most extreme trade-off between word length (1bit/sample) and&lt;br /&gt;
sampling frequency (2.8 Mhz). Due to the low word length high power&lt;br /&gt;
noise is introduced. With the help of noise shaping this noise is&lt;br /&gt;
usually rejected by about 120 dB in the audible band (0-20 kHz) at the&lt;br /&gt;
cost of a higher noise power in the ultrasonic range (+3.5dB for&lt;br /&gt;
frequencies above 100 kHz).&lt;br /&gt;
&lt;br /&gt;
= Limits &amp;amp; filter design restrictions =&lt;br /&gt;
&lt;br /&gt;
Noise Shaping without [[Dithering|&#039;&#039;dithered&#039;&#039;]] quantization is also possible but&lt;br /&gt;
less useful because the spectral characteristics of &#039;&#039;undithered&#039;&#039;&lt;br /&gt;
quantization noise is not easily predictable and depends heavily on the&lt;br /&gt;
digital signal that is to be quantized.&lt;br /&gt;
&lt;br /&gt;
It&#039;s not possible to lower the noise power for &#039;&#039;all&#039;&#039; frequencies&lt;br /&gt;
via noise shaping because this would be in violation of the&lt;br /&gt;
rate/distortion principles. But it &#039;&#039;is&#039;&#039; possible to lower the noise&lt;br /&gt;
in certain frequency bands at the cost of increased noise power in other&lt;br /&gt;
frequency bands. This property is also known as the&lt;br /&gt;
&#039;&#039;noise shaping theorem&#039;&#039;. It states that the average logarithmic magnitude response of&lt;br /&gt;
the corresponding noise shaping &#039;&#039;filter&#039;&#039; over the whole linear&lt;br /&gt;
frequency axis cannot drop below 0dB.&lt;br /&gt;
&lt;br /&gt;
The noise shaping application poses some restrictions on the fiter&lt;br /&gt;
design. The first sample of the filter&#039;s impulse response always equals&lt;br /&gt;
one. Also, interesting filters for noise shaping are so-called&lt;br /&gt;
&#039;&#039;minimum phase filters&#039;&#039;. This type of filter performs optimal in the sense of&lt;br /&gt;
the noise shaping theorem because the average logarithmig magnitude&lt;br /&gt;
response &#039;&#039;is&#039;&#039; 0dB.&lt;br /&gt;
&lt;br /&gt;
For the special [[Super Audio Compact Disc|SACD]] case an additional&lt;br /&gt;
constraint comes into play: The magnitude response should not exceed&lt;br /&gt;
3.5dB. This is enforced to prevent so-called limit cycles.&lt;br /&gt;
&lt;br /&gt;
The design of these kinds of filters is however not trivial. This is not&lt;br /&gt;
covered by this article.&lt;br /&gt;
&lt;br /&gt;
= Digital filter reminder =&lt;br /&gt;
&lt;br /&gt;
A common representation of digital filters is their &#039;&#039;transfer function&#039;&#039;&lt;br /&gt;
in form of a rational polynomial in z^-1. These polynomials are defined&lt;br /&gt;
by their coefficients b0,b1,b2,...,a1,a2,...:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{B(z)}{A(z)} = \frac{b_0 + b_1 z^{-1} + b_2 z^{-2} + \ldots + b_m z^{-m}}&lt;br /&gt;
{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots + a_m z^{-m}} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &#039;N&#039; is the transfer function and &#039;m&#039; the filter&#039;s &#039;&#039;order&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
== FIR filters ==&lt;br /&gt;
&lt;br /&gt;
In case a1=a2=a3=...=0 the filter&#039;s [http://en.wikipedia.org/wiki/Impulse_response impulse response] is finite which is&lt;br /&gt;
why these filters are called FIR ([[Finite Impulse Response Filter|finite impulse response]])&lt;br /&gt;
filters as opposed to IIR&lt;br /&gt;
[[Infinite Impulse Response Filter|infinite impulse response]] filters. Note that in case of&lt;br /&gt;
FIR filters the coefficients b0, b1, ... correspond to the impulse&lt;br /&gt;
response. The impulse response is another way a filter can be&lt;br /&gt;
characterized and describes what the filter does to a unit pulse. Let&#039;s&lt;br /&gt;
assume for the moment that a1=a2=a3=...=0. The the impulse response&lt;br /&gt;
would be:&lt;br /&gt;
&lt;br /&gt;
 x:  0  0  1  0  0  0  0  ... 0  0  0 ... (input signal)&lt;br /&gt;
 y:  0  0  b0 b1 b2 b3 b4 ... 0  0  0 ... (output signal)&lt;br /&gt;
&lt;br /&gt;
It helps to think of negative powers of z in transfer functions as a&lt;br /&gt;
delay: z^-1 = delay of one sample. z^-2 = delay of two samples etc.&lt;br /&gt;
Since a digital filter is a&lt;br /&gt;
[http://en.wikipedia.org/wiki/Discrete_system discrete-time]&lt;br /&gt;
[http://en.wikipedia.org/wiki/LTI_system_theory linear and time-invariant]&lt;br /&gt;
system it is&lt;br /&gt;
possible to determine the filter&#039;s output by a superposition of scaled&lt;br /&gt;
and shifted impulse responses. This leads to the following relationship&lt;br /&gt;
between input and output in the case of FIR filters:&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(z) = X(z) * B(z)&lt;br /&gt;
&lt;br /&gt;
where x[] is the input signal, y[] the output signal and k the current&lt;br /&gt;
sample index. This is a simple convolution directly computed using the&lt;br /&gt;
given impulse response. The convolution in the time domain corresponds&lt;br /&gt;
to a multiplication in the Z-domain.&lt;br /&gt;
&lt;br /&gt;
== IIR filters ==&lt;br /&gt;
&lt;br /&gt;
IIR filters are now a simple extension to the FIR case:&lt;br /&gt;
&lt;br /&gt;
 time domain                                Z-transform&lt;br /&gt;
 -----------                                -----------&lt;br /&gt;
      y[k] + a1 y[k-1] + a2 y[k-2] + ...    Y(z) * A(Z) = X(Z) * B(Z)&lt;br /&gt;
 = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Solving for Y we get&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(Z) = X(Z) * N(z)&lt;br /&gt;
                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
which is the well known &#039;&#039;difference formula&#039;&#039;. Its signal flow graph&lt;br /&gt;
representation is known as&lt;br /&gt;
[http://ccrma.stanford.edu/~jos/filters/Direct_Form_I.html direct form I].&lt;br /&gt;
Now, let&#039;s see how we can apply this kind of filter to quantization&lt;br /&gt;
errors...&lt;br /&gt;
&lt;br /&gt;
= Noise Shaper Implementation =&lt;br /&gt;
&lt;br /&gt;
Usually a filter&#039;s transfer function is called &#039;H&#039;. In this article &#039;N&#039;&lt;br /&gt;
has been used because this filter is also known as noise transfer&lt;br /&gt;
function (NTF) in the context of noise shaping. Later, another filter&lt;br /&gt;
&#039;H&#039; is introduced that has a special relation to &#039;N&#039;.&lt;br /&gt;
&lt;br /&gt;
== Direct Form I ==&lt;br /&gt;
&lt;br /&gt;
For convenience let us substitute the actual difference in the&lt;br /&gt;
difference formula for &#039;u&#039;. Also, b0 must be equal to one for noise&lt;br /&gt;
shaping filters as mentioned above. This leads to&lt;br /&gt;
&lt;br /&gt;
 y[k] = x[k] + u[k]   with u[k] = b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
                                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Note that since the filter is supposed to be applied on the quantization&lt;br /&gt;
noise y[] corresponds to the filtered noise and x[] to the unfiltered&lt;br /&gt;
noise. Suppose s[] is the signal we&#039;re trying to quantize. The following&lt;br /&gt;
pseudo code snippet shows the inner loop over k of &#039;&#039;one possible&#039;&#039;&lt;br /&gt;
noise shaping implementation:&lt;br /&gt;
&lt;br /&gt;
 (1) compute u[k]&lt;br /&gt;
 (2) wanted[k] = s[k] + u[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) y[k] = quantized[k] - s[k]&lt;br /&gt;
 (5) x[k] = y[k] - u[k]&lt;br /&gt;
&lt;br /&gt;
Note that the following is true&lt;br /&gt;
&lt;br /&gt;
 time domain                          Z-transform&lt;br /&gt;
 -----------                          -----------&lt;br /&gt;
 quantized[k] = s[k] + u[k] + x[k]    Q(z) = S(z) + X(z) * N(z)&lt;br /&gt;
              = s[k] + y[k]                = S(z) + Y(z)&lt;br /&gt;
&lt;br /&gt;
u[k], wanted[k] are temporary variables. The values are not needed&lt;br /&gt;
outside this iteration. But the past filtered and unfiltered error&lt;br /&gt;
samples need to be remembered (y[] &amp;amp; x[]) upto some amount of samples&lt;br /&gt;
determined by the filter order.&lt;br /&gt;
&lt;br /&gt;
== Decoupling filtering and quantization ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Noiseshaper3.png|thumb|right|200px]]&lt;br /&gt;
As mentioned, the previous example is &#039;&#039;one possible&#039;&#039; implementation.&lt;br /&gt;
It&#039;s not the best possible one. But it is the first version that comes to mind.&lt;br /&gt;
There&#039;s another common formulation which requires&lt;br /&gt;
some more transfer function fiddeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt; N(z) = \frac{B(z)}{A(z)} = 1 - \frac{A(z)}{A(z)} + \frac{B(z)}{A(z)}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since now the two fractions have a common denominator they can be easily&lt;br /&gt;
combined to one fraction. It&#039;s also possible to factor z^-1 out of the fraction:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = 1 - \frac{A(z)-B(z)}{A(z)} = 1 - z^{-1} \frac{z ( A(z)-B(z) )}{A(z)} =: 1 - z^{-1} H(z)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Substituting z*(A(Z)-B(Z)) for H(z) leads to a new filter that is related to N.&lt;br /&gt;
Note that with b0=1 it follows that z*(1-b0)=0. So, H(z) can be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{c_0 + c_1 z^{-1} + c_2 z^{-2} + \ldots}{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
with &amp;lt;math&amp;gt;c_{k-1} = a_k - b_k&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;k = 1...m&amp;lt;/math&amp;gt;. H(z)&lt;br /&gt;
is some &#039;&#039;arbitrary&#039;&#039; filter. The big advantage now is that it is&lt;br /&gt;
possible to plug nearly any digital filter implementation for H(z) into&lt;br /&gt;
the noise shaping loop. One obvious approach is the difference formula&lt;br /&gt;
from above but there are other alternatives with desirable properties.&lt;br /&gt;
However, the purpose of this article is not to discuss digital filters&lt;br /&gt;
in general but their application as noise shaping filters. So,&lt;br /&gt;
throughout the rest of this article H(z) is simply treated as a black&lt;br /&gt;
box. This black box has a certain state and gives us an output sample&lt;br /&gt;
for each input sample. The modified pseudo code loop looks like this:&lt;br /&gt;
&lt;br /&gt;
 (1) h_out[k] = H.filter(last_error)&lt;br /&gt;
 (2) wanted[k] = s[k] - h_out[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) last_error = quantized[k] - wanted[k]&lt;br /&gt;
&lt;br /&gt;
which is equivalent to the shown signal flow graph where the unit&lt;br /&gt;
sample delay z^-1 is implemented by the state variable &#039;last_error&#039;:&lt;br /&gt;
Remember that this black box filter H(z) doesn&#039;t correspond the the&lt;br /&gt;
actual noise shaping filter N(z). Depending on the context published&lt;br /&gt;
coefficients for noise shaping correspond either to N(z), H(z), or&lt;br /&gt;
-H(z).&lt;br /&gt;
&lt;br /&gt;
= Example filters =&lt;br /&gt;
&lt;br /&gt;
The following two filters have been designed by Sebastian Gesemann&lt;br /&gt;
via non-linear optimization to match a slightly modified version of&lt;br /&gt;
the LAME MP3 encoder&#039;s ATH (absolute threshold of hearing) curve. They&lt;br /&gt;
were first published in the&lt;br /&gt;
[http://www.hydrogenaudio.org/forums/index.php?showtopic=47980&amp;amp;pid=551558&amp;amp;st=0 Hydrogenaudio forum]&lt;br /&gt;
including the noise transfer function&#039;s second order factorizations.&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 44100 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.1474 z^{-1} +0.5383 z^{-2} -0.3520 z^{-3} +0.3475 z^{-4}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2061 -0.4706 z^{-1} -0.2534 z^{-2} -0.6214 z^{-3}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 48000 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.3344 z^{-1} +0.7455 z^{-2} -0.4602 z^{-3} +0.3463 z^{-4}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2374 -0.7339 z^{-1} -0.1251 z^{-2} -0.6033 z^{-3}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Signal Processing]]&lt;/div&gt;</summary>
		<author><name>SebastianG</name></author>
	</entry>
	<entry>
		<id>https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=18316</id>
		<title>Noise shaping</title>
		<link rel="alternate" type="text/html" href="https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=18316"/>
		<updated>2008-03-14T14:17:34Z</updated>

		<summary type="html">&lt;p&gt;SebastianG: /* FIR filters - LTI reference added */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
Noise shaping is a technique that is often used in combination with&lt;br /&gt;
[[Dithering|dithering]] in the context of [[Quantize|quantization]]. The purpose of noise shaping&lt;br /&gt;
is to alter the spectral &#039;&#039;shape&#039;&#039; of the error that is introduced due&lt;br /&gt;
to [[Dithering|dithering]] and [[Quantize|quantization]]. Sometimes it may be desirable to&lt;br /&gt;
concentrate most of the noise power in a certain frequency band so&lt;br /&gt;
that the overall noise becomes less perceivable.&lt;br /&gt;
&lt;br /&gt;
= Applications =&lt;br /&gt;
&lt;br /&gt;
In the field of digital image processing a popular noise shaping&lt;br /&gt;
technique is known as &amp;quot;Floyd Steinberg dithering&amp;quot;. This algorithm is&lt;br /&gt;
used to reduce the noise power at lower spatial frequencies which&lt;br /&gt;
results in an improved perceptual quality.&lt;br /&gt;
&lt;br /&gt;
For word length reductions of audio signals (i.e conversion from a 24bit&lt;br /&gt;
signal to a 16bit signal) noise shaping can be used to push quantization&lt;br /&gt;
noise out of perceptually important frequency bands to perceptually less&lt;br /&gt;
important frequency bands (&amp;gt;20 kHz) in order to reduce the perceived&lt;br /&gt;
loudness of the quantization noise.&lt;br /&gt;
&lt;br /&gt;
Another example is the &lt;br /&gt;
[[Super Audio Compact Disc|Super Audio CD]]. The SACD format represents&lt;br /&gt;
the most extreme trade-off between word length (1bit/sample) and&lt;br /&gt;
sampling frequency (2.8 Mhz). Due to the low word length high power&lt;br /&gt;
noise is introduced. With the help of noise shaping this noise is&lt;br /&gt;
usually rejected by about 120 dB in the audible band (0-20 kHz) at the&lt;br /&gt;
cost of a higher noise power in the ultrasonic range (+3.5dB for&lt;br /&gt;
frequencies above 100 kHz).&lt;br /&gt;
&lt;br /&gt;
= Limits &amp;amp; filter design restrictions =&lt;br /&gt;
&lt;br /&gt;
Noise Shaping without [[Dithering|&#039;&#039;dithered&#039;&#039;]] quantization is also possible but&lt;br /&gt;
less useful because the spectral characteristics of &#039;&#039;undithered&#039;&#039;&lt;br /&gt;
quantization noise is not easily predictable and depends heavily on the&lt;br /&gt;
digital signal that is to be quantized.&lt;br /&gt;
&lt;br /&gt;
It&#039;s not possible to lower the noise power for &#039;&#039;all&#039;&#039; frequencies&lt;br /&gt;
via noise shaping because this would be in violation of the&lt;br /&gt;
rate/distortion principles. But it &#039;&#039;is&#039;&#039; possible to lower the noise&lt;br /&gt;
in certain frequency bands at the cost of increased noise power in other&lt;br /&gt;
frequency bands. This property is also known as the&lt;br /&gt;
&#039;&#039;noise shaping theorem&#039;&#039;. It states that the average logarithmic magnitude response of&lt;br /&gt;
the corresponding noise shaping &#039;&#039;filter&#039;&#039; over the whole linear&lt;br /&gt;
frequency axis cannot drop below 0dB.&lt;br /&gt;
&lt;br /&gt;
The noise shaping application poses some restrictions on the fiter&lt;br /&gt;
design. The first sample of the filter&#039;s impulse response always equals&lt;br /&gt;
one. Also, interesting filters for noise shaping are so-called&lt;br /&gt;
&#039;&#039;minimum phase filters&#039;&#039;. This type of filter performs optimal in the sense of&lt;br /&gt;
the noise shaping theorem because the average logarithmig magnitude&lt;br /&gt;
response &#039;&#039;is&#039;&#039; 0dB.&lt;br /&gt;
&lt;br /&gt;
For the special [[Super Audio Compact Disc|SACD]] case an additional&lt;br /&gt;
constraint comes into play: The magnitude response should not exceed&lt;br /&gt;
3.5dB. This is enforced to prevent so-called limit cycles.&lt;br /&gt;
&lt;br /&gt;
The design of these kinds of filters is however not trivial. This is not&lt;br /&gt;
covered by this article.&lt;br /&gt;
&lt;br /&gt;
= Digital filter reminder =&lt;br /&gt;
&lt;br /&gt;
A common representation of digital filters is their &#039;&#039;transfer function&#039;&#039;&lt;br /&gt;
in form of a rational polynomial in z^-1. These polynomials are defined&lt;br /&gt;
by their coefficients b0,b1,b2,...,a1,a2,...:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{B(z)}{A(z)} = \frac{b_0 + b_1 z^{-1} + b_2 z^{-2} + \ldots + b_m z^{-m}}&lt;br /&gt;
{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots + a_m z^{-m}} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &#039;N&#039; is the transfer function and &#039;m&#039; the filter&#039;s &#039;&#039;order&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
== FIR filters ==&lt;br /&gt;
&lt;br /&gt;
In case a1=a2=a3=...=0 the filter&#039;s [http://en.wikipedia.org/wiki/Impulse_response impulse response] is finite which is&lt;br /&gt;
why these filters are called FIR ([[Finite Impulse Response Filter|finite impulse response]])&lt;br /&gt;
filters as opposed to IIR&lt;br /&gt;
[[Infinite Impulse Response Filter|infinite impulse response]] filters. Note that in case of&lt;br /&gt;
FIR filters the coefficients b0, b1, ... correspond to the impulse&lt;br /&gt;
response. The impulse response is another way a filter can be&lt;br /&gt;
characterized and describes what the filter does to a unit pulse. Let&#039;s&lt;br /&gt;
assume for the moment that a1=a2=a3=...=0. The the impulse response&lt;br /&gt;
would be:&lt;br /&gt;
&lt;br /&gt;
 x:  0  0  1  0  0  0  0  ... 0  0  0 ... (input signal)&lt;br /&gt;
 y:  0  0  b0 b1 b2 b3 b4 ... 0  0  0 ... (output signal)&lt;br /&gt;
&lt;br /&gt;
It helps to think of negative powers of z in transfer functions as a&lt;br /&gt;
delay: z^-1 = delay of one sample. z^-2 = delay of two samples etc.&lt;br /&gt;
Since a digital filter is a &lt;br /&gt;
[http://en.wikipedia.org/wiki/LTI_system_theory linear and time-invariant]&lt;br /&gt;
system it is&lt;br /&gt;
possible to determine the filter&#039;s output by a superposition of scaled&lt;br /&gt;
and shifted impulse responses. This leads to the following relationship&lt;br /&gt;
between input and output in the case of FIR filters:&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(z) = X(z) * B(z)&lt;br /&gt;
&lt;br /&gt;
where x[] is the input signal, y[] the output signal and k the current&lt;br /&gt;
sample index. This is a simple convolution directly computed using the&lt;br /&gt;
given impulse response. The convolution in the time domain corresponds&lt;br /&gt;
to a multiplication in the Z-domain.&lt;br /&gt;
&lt;br /&gt;
== IIR filters ==&lt;br /&gt;
&lt;br /&gt;
IIR filters are now a simple extension to the FIR case:&lt;br /&gt;
&lt;br /&gt;
 time domain                                Z-transform&lt;br /&gt;
 -----------                                -----------&lt;br /&gt;
      y[k] + a1 y[k-1] + a2 y[k-2] + ...    Y(z) * A(Z) = X(Z) * B(Z)&lt;br /&gt;
 = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Solving for Y we get&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(Z) = X(Z) * N(z)&lt;br /&gt;
                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
which is the well known &#039;&#039;difference formula&#039;&#039;. Its signal flow graph&lt;br /&gt;
representation is known as&lt;br /&gt;
[http://ccrma.stanford.edu/~jos/filters/Direct_Form_I.html direct form I].&lt;br /&gt;
Now, let&#039;s see how we can apply this kind of filter to quantization&lt;br /&gt;
errors...&lt;br /&gt;
&lt;br /&gt;
= Noise Shaper Implementation =&lt;br /&gt;
&lt;br /&gt;
Usually a filter&#039;s transfer function is called &#039;H&#039;. In this article &#039;N&#039;&lt;br /&gt;
has been used because this filter is also known as noise transfer&lt;br /&gt;
function (NTF) in the context of noise shaping. Later, another filter&lt;br /&gt;
&#039;H&#039; is introduced that has a special relation to &#039;N&#039;.&lt;br /&gt;
&lt;br /&gt;
== Direct Form I ==&lt;br /&gt;
&lt;br /&gt;
For convenience let us substitute the actual difference in the&lt;br /&gt;
difference formula for &#039;u&#039;. Also, b0 must be equal to one for noise&lt;br /&gt;
shaping filters as mentioned above. This leads to&lt;br /&gt;
&lt;br /&gt;
 y[k] = x[k] + u[k]   with u[k] = b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
                                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Note that since the filter is supposed to be applied on the quantization&lt;br /&gt;
noise y[] corresponds to the filtered noise and x[] to the unfiltered&lt;br /&gt;
noise. Suppose s[] is the signal we&#039;re trying to quantize. The following&lt;br /&gt;
pseudo code snippet shows the inner loop over k of &#039;&#039;one possible&#039;&#039;&lt;br /&gt;
noise shaping implementation:&lt;br /&gt;
&lt;br /&gt;
 (1) compute u[k]&lt;br /&gt;
 (2) wanted[k] = s[k] + u[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) y[k] = quantized[k] - s[k]&lt;br /&gt;
 (5) x[k] = y[k] - u[k]&lt;br /&gt;
&lt;br /&gt;
Note that the following is true&lt;br /&gt;
&lt;br /&gt;
 time domain                          Z-transform&lt;br /&gt;
 -----------                          -----------&lt;br /&gt;
 quantized[k] = s[k] + u[k] + x[k]    Q(z) = S(z) + X(z) * N(z)&lt;br /&gt;
              = s[k] + y[k]                = S(z) + Y(z)&lt;br /&gt;
&lt;br /&gt;
u[k], wanted[k] are temporary variables. The values are not needed&lt;br /&gt;
outside this iteration. But the past filtered and unfiltered error&lt;br /&gt;
samples need to be remembered (y[] &amp;amp; x[]) upto some amount of samples&lt;br /&gt;
determined by the filter order.&lt;br /&gt;
&lt;br /&gt;
== Decoupling filtering and quantization ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Noiseshaper3.png|thumb|right|200px]]&lt;br /&gt;
As mentioned, the previous example is &#039;&#039;one possible&#039;&#039; implementation.&lt;br /&gt;
It&#039;s not the best possible one. But it is the first version that comes to mind.&lt;br /&gt;
There&#039;s another common formulation which requires&lt;br /&gt;
some more transfer function fiddeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt; N(z) = \frac{B(z)}{A(z)} = 1 - \frac{A(z)}{A(z)} + \frac{B(z)}{A(z)}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since now the two fractions have a common denominator they can be easily&lt;br /&gt;
combined to one fraction. It&#039;s also possible to factor z^-1 out of the fraction:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = 1 - \frac{A(z)-B(z)}{A(z)} = 1 - z^{-1} \frac{z ( A(z)-B(z) )}{A(z)} =: 1 - z^{-1} H(z)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Substituting z*(A(Z)-B(Z)) for H(z) leads to a new filter that is related to N.&lt;br /&gt;
Note that with b0=1 it follows that z*(1-b0)=0. So, H(z) can be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{c_0 + c_1 z^{-1} + c_2 z^{-2} + \ldots}{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
with &amp;lt;math&amp;gt;c_{k-1} = a_k - b_k&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;k = 1...m&amp;lt;/math&amp;gt;. H(z)&lt;br /&gt;
is some &#039;&#039;arbitrary&#039;&#039; filter. The big advantage now is that it is&lt;br /&gt;
possible to plug nearly any digital filter implementation for H(z) into&lt;br /&gt;
the noise shaping loop. One obvious approach is the difference formula&lt;br /&gt;
from above but there are other alternatives with desirable properties.&lt;br /&gt;
However, the purpose of this article is not to discuss digital filters&lt;br /&gt;
in general but their application as noise shaping filters. So,&lt;br /&gt;
throughout the rest of this article H(z) is simply treated as a black&lt;br /&gt;
box. This black box has a certain state and gives us an output sample&lt;br /&gt;
for each input sample. The modified pseudo code loop looks like this:&lt;br /&gt;
&lt;br /&gt;
 (1) h_out[k] = H.filter(last_error)&lt;br /&gt;
 (2) wanted[k] = s[k] - h_out[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) last_error = quantized[k] - wanted[k]&lt;br /&gt;
&lt;br /&gt;
which is equivalent to the shown signal flow graph where the unit&lt;br /&gt;
sample delay z^-1 is implemented by the state variable &#039;last_error&#039;:&lt;br /&gt;
Remember that this black box filter H(z) doesn&#039;t correspond the the&lt;br /&gt;
actual noise shaping filter N(z). Depending on the context published&lt;br /&gt;
coefficients for noise shaping correspond either to N(z), H(z), or&lt;br /&gt;
-H(z).&lt;br /&gt;
&lt;br /&gt;
= Example filters =&lt;br /&gt;
&lt;br /&gt;
The following two filters have been designed by Sebastian Gesemann&lt;br /&gt;
via non-linear optimization to match a slightly modified version of&lt;br /&gt;
the LAME MP3 encoder&#039;s ATH (absolute threshold of hearing) curve. They&lt;br /&gt;
were first published in the&lt;br /&gt;
[http://www.hydrogenaudio.org/forums/index.php?showtopic=47980&amp;amp;pid=551558&amp;amp;st=0 Hydrogenaudio forum]&lt;br /&gt;
including the noise transfer function&#039;s second order factorizations.&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 44100 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.1474 z^{-1} +0.5383 z^{-2} -0.3520 z^{-3} +0.3475 z^{-4}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2061 -0.4706 z^{-1} -0.2534 z^{-2} -0.6214 z^{-3}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 48000 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.3344 z^{-1} +0.7455 z^{-2} -0.4602 z^{-3} +0.3463 z^{-4}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2374 -0.7339 z^{-1} -0.1251 z^{-2} -0.6033 z^{-3}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Signal Processing]]&lt;/div&gt;</summary>
		<author><name>SebastianG</name></author>
	</entry>
	<entry>
		<id>https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=18315</id>
		<title>Noise shaping</title>
		<link rel="alternate" type="text/html" href="https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=18315"/>
		<updated>2008-03-14T10:41:30Z</updated>

		<summary type="html">&lt;p&gt;SebastianG: /* Decoupling filtering and quantization */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
Noise shaping is a technique that is often used in combination with&lt;br /&gt;
[[Dithering|dithering]] in the context of [[Quantize|quantization]]. The purpose of noise shaping&lt;br /&gt;
is to alter the spectral &#039;&#039;shape&#039;&#039; of the error that is introduced due&lt;br /&gt;
to [[Dithering|dithering]] and [[Quantize|quantization]]. Sometimes it may be desirable to&lt;br /&gt;
concentrate most of the noise power in a certain frequency band so&lt;br /&gt;
that the overall noise becomes less perceivable.&lt;br /&gt;
&lt;br /&gt;
= Applications =&lt;br /&gt;
&lt;br /&gt;
In the field of digital image processing a popular noise shaping&lt;br /&gt;
technique is known as &amp;quot;Floyd Steinberg dithering&amp;quot;. This algorithm is&lt;br /&gt;
used to reduce the noise power at lower spatial frequencies which&lt;br /&gt;
results in an improved perceptual quality.&lt;br /&gt;
&lt;br /&gt;
For word length reductions of audio signals (i.e conversion from a 24bit&lt;br /&gt;
signal to a 16bit signal) noise shaping can be used to push quantization&lt;br /&gt;
noise out of perceptually important frequency bands to perceptually less&lt;br /&gt;
important frequency bands (&amp;gt;20 kHz) in order to reduce the perceived&lt;br /&gt;
loudness of the quantization noise.&lt;br /&gt;
&lt;br /&gt;
Another example is the &lt;br /&gt;
[[Super Audio Compact Disc|Super Audio CD]]. The SACD format represents&lt;br /&gt;
the most extreme trade-off between word length (1bit/sample) and&lt;br /&gt;
sampling frequency (2.8 Mhz). Due to the low word length high power&lt;br /&gt;
noise is introduced. With the help of noise shaping this noise is&lt;br /&gt;
usually rejected by about 120 dB in the audible band (0-20 kHz) at the&lt;br /&gt;
cost of a higher noise power in the ultrasonic range (+3.5dB for&lt;br /&gt;
frequencies above 100 kHz).&lt;br /&gt;
&lt;br /&gt;
= Limits &amp;amp; filter design restrictions =&lt;br /&gt;
&lt;br /&gt;
Noise Shaping without [[Dithering|&#039;&#039;dithered&#039;&#039;]] quantization is also possible but&lt;br /&gt;
less useful because the spectral characteristics of &#039;&#039;undithered&#039;&#039;&lt;br /&gt;
quantization noise is not easily predictable and depends heavily on the&lt;br /&gt;
digital signal that is to be quantized.&lt;br /&gt;
&lt;br /&gt;
It&#039;s not possible to lower the noise power for &#039;&#039;all&#039;&#039; frequencies&lt;br /&gt;
via noise shaping because this would be in violation of the&lt;br /&gt;
rate/distortion principles. But it &#039;&#039;is&#039;&#039; possible to lower the noise&lt;br /&gt;
in certain frequency bands at the cost of increased noise power in other&lt;br /&gt;
frequency bands. This property is also known as the&lt;br /&gt;
&#039;&#039;noise shaping theorem&#039;&#039;. It states that the average logarithmic magnitude response of&lt;br /&gt;
the corresponding noise shaping &#039;&#039;filter&#039;&#039; over the whole linear&lt;br /&gt;
frequency axis cannot drop below 0dB.&lt;br /&gt;
&lt;br /&gt;
The noise shaping application poses some restrictions on the fiter&lt;br /&gt;
design. The first sample of the filter&#039;s impulse response always equals&lt;br /&gt;
one. Also, interesting filters for noise shaping are so-called&lt;br /&gt;
&#039;&#039;minimum phase filters&#039;&#039;. This type of filter performs optimal in the sense of&lt;br /&gt;
the noise shaping theorem because the average logarithmig magnitude&lt;br /&gt;
response &#039;&#039;is&#039;&#039; 0dB.&lt;br /&gt;
&lt;br /&gt;
For the special [[Super Audio Compact Disc|SACD]] case an additional&lt;br /&gt;
constraint comes into play: The magnitude response should not exceed&lt;br /&gt;
3.5dB. This is enforced to prevent so-called limit cycles.&lt;br /&gt;
&lt;br /&gt;
The design of these kinds of filters is however not trivial. This is not&lt;br /&gt;
covered by this article.&lt;br /&gt;
&lt;br /&gt;
= Digital filter reminder =&lt;br /&gt;
&lt;br /&gt;
A common representation of digital filters is their &#039;&#039;transfer function&#039;&#039;&lt;br /&gt;
in form of a rational polynomial in z^-1. These polynomials are defined&lt;br /&gt;
by their coefficients b0,b1,b2,...,a1,a2,...:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{B(z)}{A(z)} = \frac{b_0 + b_1 z^{-1} + b_2 z^{-2} + \ldots + b_m z^{-m}}&lt;br /&gt;
{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots + a_m z^{-m}} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &#039;N&#039; is the transfer function and &#039;m&#039; the filter&#039;s &#039;&#039;order&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
== FIR filters ==&lt;br /&gt;
&lt;br /&gt;
In case a1=a2=a3=...=0 the filter&#039;s [http://en.wikipedia.org/wiki/Impulse_response impulse response] is finite which is&lt;br /&gt;
why these filters are called FIR ([[Finite Impulse Response Filter|finite impulse response]])&lt;br /&gt;
filters as opposed to IIR&lt;br /&gt;
[[Infinite Impulse Response Filter|infinite impulse response]] filters. Note that in case of&lt;br /&gt;
FIR filters the coefficients b0, b1, ... correspond to the impulse&lt;br /&gt;
response. The impulse response is another way a filter can be&lt;br /&gt;
characterized and describes what the filter does to a unit pulse. Let&#039;s&lt;br /&gt;
assume for the moment that a1=a2=a3=...=0. The the impulse response&lt;br /&gt;
would be:&lt;br /&gt;
&lt;br /&gt;
 x:  0  0  1  0  0  0  0  ... 0  0  0 ... (input signal)&lt;br /&gt;
 y:  0  0  b0 b1 b2 b3 b4 ... 0  0  0 ... (output signal)&lt;br /&gt;
&lt;br /&gt;
It helps to think of negative powers of z in transfer functions as a&lt;br /&gt;
delay: z^-1 = delay of one sample. z^-2 = delay of two samples etc.&lt;br /&gt;
Since a digital filter is a linear and time-invariant system (LTI) it is&lt;br /&gt;
possible to determine the filter&#039;s output by a superposition of scaled&lt;br /&gt;
and shifted impulse responses. This leads to the following relationship&lt;br /&gt;
between input and output in the case of FIR filters:&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(z) = X(z) * B(z)&lt;br /&gt;
&lt;br /&gt;
where x[] is the input signal, y[] the output signal and k the current&lt;br /&gt;
sample index. This is a simple convolution directly computed using the&lt;br /&gt;
given impulse response. The convolution in the time domain corresponds&lt;br /&gt;
to a multiplication in the Z-domain.&lt;br /&gt;
&lt;br /&gt;
== IIR filters ==&lt;br /&gt;
&lt;br /&gt;
IIR filters are now a simple extension to the FIR case:&lt;br /&gt;
&lt;br /&gt;
 time domain                                Z-transform&lt;br /&gt;
 -----------                                -----------&lt;br /&gt;
      y[k] + a1 y[k-1] + a2 y[k-2] + ...    Y(z) * A(Z) = X(Z) * B(Z)&lt;br /&gt;
 = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Solving for Y we get&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(Z) = X(Z) * N(z)&lt;br /&gt;
                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
which is the well known &#039;&#039;difference formula&#039;&#039;. Its signal flow graph&lt;br /&gt;
representation is known as&lt;br /&gt;
[http://ccrma.stanford.edu/~jos/filters/Direct_Form_I.html direct form I].&lt;br /&gt;
Now, let&#039;s see how we can apply this kind of filter to quantization&lt;br /&gt;
errors...&lt;br /&gt;
&lt;br /&gt;
= Noise Shaper Implementation =&lt;br /&gt;
&lt;br /&gt;
Usually a filter&#039;s transfer function is called &#039;H&#039;. In this article &#039;N&#039;&lt;br /&gt;
has been used because this filter is also known as noise transfer&lt;br /&gt;
function (NTF) in the context of noise shaping. Later, another filter&lt;br /&gt;
&#039;H&#039; is introduced that has a special relation to &#039;N&#039;.&lt;br /&gt;
&lt;br /&gt;
== Direct Form I ==&lt;br /&gt;
&lt;br /&gt;
For convenience let us substitute the actual difference in the&lt;br /&gt;
difference formula for &#039;u&#039;. Also, b0 must be equal to one for noise&lt;br /&gt;
shaping filters as mentioned above. This leads to&lt;br /&gt;
&lt;br /&gt;
 y[k] = x[k] + u[k]   with u[k] = b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
                                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Note that since the filter is supposed to be applied on the quantization&lt;br /&gt;
noise y[] corresponds to the filtered noise and x[] to the unfiltered&lt;br /&gt;
noise. Suppose s[] is the signal we&#039;re trying to quantize. The following&lt;br /&gt;
pseudo code snippet shows the inner loop over k of &#039;&#039;one possible&#039;&#039;&lt;br /&gt;
noise shaping implementation:&lt;br /&gt;
&lt;br /&gt;
 (1) compute u[k]&lt;br /&gt;
 (2) wanted[k] = s[k] + u[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) y[k] = quantized[k] - s[k]&lt;br /&gt;
 (5) x[k] = y[k] - u[k]&lt;br /&gt;
&lt;br /&gt;
Note that the following is true&lt;br /&gt;
&lt;br /&gt;
 time domain                          Z-transform&lt;br /&gt;
 -----------                          -----------&lt;br /&gt;
 quantized[k] = s[k] + u[k] + x[k]    Q(z) = S(z) + X(z) * N(z)&lt;br /&gt;
              = s[k] + y[k]                = S(z) + Y(z)&lt;br /&gt;
&lt;br /&gt;
u[k], wanted[k] are temporary variables. The values are not needed&lt;br /&gt;
outside this iteration. But the past filtered and unfiltered error&lt;br /&gt;
samples need to be remembered (y[] &amp;amp; x[]) upto some amount of samples&lt;br /&gt;
determined by the filter order.&lt;br /&gt;
&lt;br /&gt;
== Decoupling filtering and quantization ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Noiseshaper3.png|thumb|right|200px]]&lt;br /&gt;
As mentioned, the previous example is &#039;&#039;one possible&#039;&#039; implementation.&lt;br /&gt;
It&#039;s not the best possible one. But it is the first version that comes to mind.&lt;br /&gt;
There&#039;s another common formulation which requires&lt;br /&gt;
some more transfer function fiddeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt; N(z) = \frac{B(z)}{A(z)} = 1 - \frac{A(z)}{A(z)} + \frac{B(z)}{A(z)}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since now the two fractions have a common denominator they can be easily&lt;br /&gt;
combined to one fraction. It&#039;s also possible to factor z^-1 out of the fraction:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = 1 - \frac{A(z)-B(z)}{A(z)} = 1 - z^{-1} \frac{z ( A(z)-B(z) )}{A(z)} =: 1 - z^{-1} H(z)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Substituting z*(A(Z)-B(Z)) for H(z) leads to a new filter that is related to N.&lt;br /&gt;
Note that with b0=1 it follows that z*(1-b0)=0. So, H(z) can be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{c_0 + c_1 z^{-1} + c_2 z^{-2} + \ldots}{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
with &amp;lt;math&amp;gt;c_{k-1} = a_k - b_k&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;k = 1...m&amp;lt;/math&amp;gt;. H(z)&lt;br /&gt;
is some &#039;&#039;arbitrary&#039;&#039; filter. The big advantage now is that it is&lt;br /&gt;
possible to plug nearly any digital filter implementation for H(z) into&lt;br /&gt;
the noise shaping loop. One obvious approach is the difference formula&lt;br /&gt;
from above but there are other alternatives with desirable properties.&lt;br /&gt;
However, the purpose of this article is not to discuss digital filters&lt;br /&gt;
in general but their application as noise shaping filters. So,&lt;br /&gt;
throughout the rest of this article H(z) is simply treated as a black&lt;br /&gt;
box. This black box has a certain state and gives us an output sample&lt;br /&gt;
for each input sample. The modified pseudo code loop looks like this:&lt;br /&gt;
&lt;br /&gt;
 (1) h_out[k] = H.filter(last_error)&lt;br /&gt;
 (2) wanted[k] = s[k] - h_out[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) last_error = quantized[k] - wanted[k]&lt;br /&gt;
&lt;br /&gt;
which is equivalent to the shown signal flow graph where the unit&lt;br /&gt;
sample delay z^-1 is implemented by the state variable &#039;last_error&#039;:&lt;br /&gt;
Remember that this black box filter H(z) doesn&#039;t correspond the the&lt;br /&gt;
actual noise shaping filter N(z). Depending on the context published&lt;br /&gt;
coefficients for noise shaping correspond either to N(z), H(z), or&lt;br /&gt;
-H(z).&lt;br /&gt;
&lt;br /&gt;
= Example filters =&lt;br /&gt;
&lt;br /&gt;
The following two filters have been designed by Sebastian Gesemann&lt;br /&gt;
via non-linear optimization to match a slightly modified version of&lt;br /&gt;
the LAME MP3 encoder&#039;s ATH (absolute threshold of hearing) curve. They&lt;br /&gt;
were first published in the&lt;br /&gt;
[http://www.hydrogenaudio.org/forums/index.php?showtopic=47980&amp;amp;pid=551558&amp;amp;st=0 Hydrogenaudio forum]&lt;br /&gt;
including the noise transfer function&#039;s second order factorizations.&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 44100 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.1474 z^{-1} +0.5383 z^{-2} -0.3520 z^{-3} +0.3475 z^{-4}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2061 -0.4706 z^{-1} -0.2534 z^{-2} -0.6214 z^{-3}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 48000 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.3344 z^{-1} +0.7455 z^{-2} -0.4602 z^{-3} +0.3463 z^{-4}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2374 -0.7339 z^{-1} -0.1251 z^{-2} -0.6033 z^{-3}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Signal Processing]]&lt;/div&gt;</summary>
		<author><name>SebastianG</name></author>
	</entry>
	<entry>
		<id>https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=18314</id>
		<title>Noise shaping</title>
		<link rel="alternate" type="text/html" href="https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=18314"/>
		<updated>2008-03-14T10:39:30Z</updated>

		<summary type="html">&lt;p&gt;SebastianG: /* Decoupling filtering and quantization */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
Noise shaping is a technique that is often used in combination with&lt;br /&gt;
[[Dithering|dithering]] in the context of [[Quantize|quantization]]. The purpose of noise shaping&lt;br /&gt;
is to alter the spectral &#039;&#039;shape&#039;&#039; of the error that is introduced due&lt;br /&gt;
to [[Dithering|dithering]] and [[Quantize|quantization]]. Sometimes it may be desirable to&lt;br /&gt;
concentrate most of the noise power in a certain frequency band so&lt;br /&gt;
that the overall noise becomes less perceivable.&lt;br /&gt;
&lt;br /&gt;
= Applications =&lt;br /&gt;
&lt;br /&gt;
In the field of digital image processing a popular noise shaping&lt;br /&gt;
technique is known as &amp;quot;Floyd Steinberg dithering&amp;quot;. This algorithm is&lt;br /&gt;
used to reduce the noise power at lower spatial frequencies which&lt;br /&gt;
results in an improved perceptual quality.&lt;br /&gt;
&lt;br /&gt;
For word length reductions of audio signals (i.e conversion from a 24bit&lt;br /&gt;
signal to a 16bit signal) noise shaping can be used to push quantization&lt;br /&gt;
noise out of perceptually important frequency bands to perceptually less&lt;br /&gt;
important frequency bands (&amp;gt;20 kHz) in order to reduce the perceived&lt;br /&gt;
loudness of the quantization noise.&lt;br /&gt;
&lt;br /&gt;
Another example is the &lt;br /&gt;
[[Super Audio Compact Disc|Super Audio CD]]. The SACD format represents&lt;br /&gt;
the most extreme trade-off between word length (1bit/sample) and&lt;br /&gt;
sampling frequency (2.8 Mhz). Due to the low word length high power&lt;br /&gt;
noise is introduced. With the help of noise shaping this noise is&lt;br /&gt;
usually rejected by about 120 dB in the audible band (0-20 kHz) at the&lt;br /&gt;
cost of a higher noise power in the ultrasonic range (+3.5dB for&lt;br /&gt;
frequencies above 100 kHz).&lt;br /&gt;
&lt;br /&gt;
= Limits &amp;amp; filter design restrictions =&lt;br /&gt;
&lt;br /&gt;
Noise Shaping without [[Dithering|&#039;&#039;dithered&#039;&#039;]] quantization is also possible but&lt;br /&gt;
less useful because the spectral characteristics of &#039;&#039;undithered&#039;&#039;&lt;br /&gt;
quantization noise is not easily predictable and depends heavily on the&lt;br /&gt;
digital signal that is to be quantized.&lt;br /&gt;
&lt;br /&gt;
It&#039;s not possible to lower the noise power for &#039;&#039;all&#039;&#039; frequencies&lt;br /&gt;
via noise shaping because this would be in violation of the&lt;br /&gt;
rate/distortion principles. But it &#039;&#039;is&#039;&#039; possible to lower the noise&lt;br /&gt;
in certain frequency bands at the cost of increased noise power in other&lt;br /&gt;
frequency bands. This property is also known as the&lt;br /&gt;
&#039;&#039;noise shaping theorem&#039;&#039;. It states that the average logarithmic magnitude response of&lt;br /&gt;
the corresponding noise shaping &#039;&#039;filter&#039;&#039; over the whole linear&lt;br /&gt;
frequency axis cannot drop below 0dB.&lt;br /&gt;
&lt;br /&gt;
The noise shaping application poses some restrictions on the fiter&lt;br /&gt;
design. The first sample of the filter&#039;s impulse response always equals&lt;br /&gt;
one. Also, interesting filters for noise shaping are so-called&lt;br /&gt;
&#039;&#039;minimum phase filters&#039;&#039;. This type of filter performs optimal in the sense of&lt;br /&gt;
the noise shaping theorem because the average logarithmig magnitude&lt;br /&gt;
response &#039;&#039;is&#039;&#039; 0dB.&lt;br /&gt;
&lt;br /&gt;
For the special [[Super Audio Compact Disc|SACD]] case an additional&lt;br /&gt;
constraint comes into play: The magnitude response should not exceed&lt;br /&gt;
3.5dB. This is enforced to prevent so-called limit cycles.&lt;br /&gt;
&lt;br /&gt;
The design of these kinds of filters is however not trivial. This is not&lt;br /&gt;
covered by this article.&lt;br /&gt;
&lt;br /&gt;
= Digital filter reminder =&lt;br /&gt;
&lt;br /&gt;
A common representation of digital filters is their &#039;&#039;transfer function&#039;&#039;&lt;br /&gt;
in form of a rational polynomial in z^-1. These polynomials are defined&lt;br /&gt;
by their coefficients b0,b1,b2,...,a1,a2,...:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{B(z)}{A(z)} = \frac{b_0 + b_1 z^{-1} + b_2 z^{-2} + \ldots + b_m z^{-m}}&lt;br /&gt;
{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots + a_m z^{-m}} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &#039;N&#039; is the transfer function and &#039;m&#039; the filter&#039;s &#039;&#039;order&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
== FIR filters ==&lt;br /&gt;
&lt;br /&gt;
In case a1=a2=a3=...=0 the filter&#039;s [http://en.wikipedia.org/wiki/Impulse_response impulse response] is finite which is&lt;br /&gt;
why these filters are called FIR ([[Finite Impulse Response Filter|finite impulse response]])&lt;br /&gt;
filters as opposed to IIR&lt;br /&gt;
[[Infinite Impulse Response Filter|infinite impulse response]] filters. Note that in case of&lt;br /&gt;
FIR filters the coefficients b0, b1, ... correspond to the impulse&lt;br /&gt;
response. The impulse response is another way a filter can be&lt;br /&gt;
characterized and describes what the filter does to a unit pulse. Let&#039;s&lt;br /&gt;
assume for the moment that a1=a2=a3=...=0. The the impulse response&lt;br /&gt;
would be:&lt;br /&gt;
&lt;br /&gt;
 x:  0  0  1  0  0  0  0  ... 0  0  0 ... (input signal)&lt;br /&gt;
 y:  0  0  b0 b1 b2 b3 b4 ... 0  0  0 ... (output signal)&lt;br /&gt;
&lt;br /&gt;
It helps to think of negative powers of z in transfer functions as a&lt;br /&gt;
delay: z^-1 = delay of one sample. z^-2 = delay of two samples etc.&lt;br /&gt;
Since a digital filter is a linear and time-invariant system (LTI) it is&lt;br /&gt;
possible to determine the filter&#039;s output by a superposition of scaled&lt;br /&gt;
and shifted impulse responses. This leads to the following relationship&lt;br /&gt;
between input and output in the case of FIR filters:&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(z) = X(z) * B(z)&lt;br /&gt;
&lt;br /&gt;
where x[] is the input signal, y[] the output signal and k the current&lt;br /&gt;
sample index. This is a simple convolution directly computed using the&lt;br /&gt;
given impulse response. The convolution in the time domain corresponds&lt;br /&gt;
to a multiplication in the Z-domain.&lt;br /&gt;
&lt;br /&gt;
== IIR filters ==&lt;br /&gt;
&lt;br /&gt;
IIR filters are now a simple extension to the FIR case:&lt;br /&gt;
&lt;br /&gt;
 time domain                                Z-transform&lt;br /&gt;
 -----------                                -----------&lt;br /&gt;
      y[k] + a1 y[k-1] + a2 y[k-2] + ...    Y(z) * A(Z) = X(Z) * B(Z)&lt;br /&gt;
 = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Solving for Y we get&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(Z) = X(Z) * N(z)&lt;br /&gt;
                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
which is the well known &#039;&#039;difference formula&#039;&#039;. Its signal flow graph&lt;br /&gt;
representation is known as&lt;br /&gt;
[http://ccrma.stanford.edu/~jos/filters/Direct_Form_I.html direct form I].&lt;br /&gt;
Now, let&#039;s see how we can apply this kind of filter to quantization&lt;br /&gt;
errors...&lt;br /&gt;
&lt;br /&gt;
= Noise Shaper Implementation =&lt;br /&gt;
&lt;br /&gt;
Usually a filter&#039;s transfer function is called &#039;H&#039;. In this article &#039;N&#039;&lt;br /&gt;
has been used because this filter is also known as noise transfer&lt;br /&gt;
function (NTF) in the context of noise shaping. Later, another filter&lt;br /&gt;
&#039;H&#039; is introduced that has a special relation to &#039;N&#039;.&lt;br /&gt;
&lt;br /&gt;
== Direct Form I ==&lt;br /&gt;
&lt;br /&gt;
For convenience let us substitute the actual difference in the&lt;br /&gt;
difference formula for &#039;u&#039;. Also, b0 must be equal to one for noise&lt;br /&gt;
shaping filters as mentioned above. This leads to&lt;br /&gt;
&lt;br /&gt;
 y[k] = x[k] + u[k]   with u[k] = b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
                                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Note that since the filter is supposed to be applied on the quantization&lt;br /&gt;
noise y[] corresponds to the filtered noise and x[] to the unfiltered&lt;br /&gt;
noise. Suppose s[] is the signal we&#039;re trying to quantize. The following&lt;br /&gt;
pseudo code snippet shows the inner loop over k of &#039;&#039;one possible&#039;&#039;&lt;br /&gt;
noise shaping implementation:&lt;br /&gt;
&lt;br /&gt;
 (1) compute u[k]&lt;br /&gt;
 (2) wanted[k] = s[k] + u[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) y[k] = quantized[k] - s[k]&lt;br /&gt;
 (5) x[k] = y[k] - u[k]&lt;br /&gt;
&lt;br /&gt;
Note that the following is true&lt;br /&gt;
&lt;br /&gt;
 time domain                          Z-transform&lt;br /&gt;
 -----------                          -----------&lt;br /&gt;
 quantized[k] = s[k] + u[k] + x[k]    Q(z) = S(z) + X(z) * N(z)&lt;br /&gt;
              = s[k] + y[k]                = S(z) + Y(z)&lt;br /&gt;
&lt;br /&gt;
u[k], wanted[k] are temporary variables. The values are not needed&lt;br /&gt;
outside this iteration. But the past filtered and unfiltered error&lt;br /&gt;
samples need to be remembered (y[] &amp;amp; x[]) upto some amount of samples&lt;br /&gt;
determined by the filter order.&lt;br /&gt;
&lt;br /&gt;
== Decoupling filtering and quantization ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Noiseshaper3.png|thumb|right|200px]]&lt;br /&gt;
As mentioned, the previous example is &#039;&#039;one possible&#039;&#039; implementation.&lt;br /&gt;
It&#039;s not the best possible one. But it is the first version that comes to mind.&lt;br /&gt;
There&#039;s another common formulation which requires&lt;br /&gt;
some more transfer function fiddeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt; N(z) = \frac{B(z)}{A(z)} = 1 - \frac{A(z)}{A(z)} + \frac{B(z)}{A(z)}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since now the two fractions have a common denominator they can be easily&lt;br /&gt;
combined to one fraction. It&#039;s also possible to factor z^-1 out of the fraction:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = 1 - \frac{A(z)-B(z)}{A(z)} = 1 - z^{-1} \frac{z ( A(z)-B(z) )}{A(z)} =: 1 - z^{-1} H(z)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Substituting z*(A(Z)-B(Z)) for H(z) leads to a new filter that is related to N.&lt;br /&gt;
Note that with b0=1 it follows that z*(1-b0)=0. So, H(z) can be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{c0 + c1 z^{-1} + c2 z^{-2} + \ldots}{1 + a1 z^{-1} + a2 z^{-2} + \ldots} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
with &amp;lt;math&amp;gt;c_{k-1} = a_k - b_k&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;k = 1...m&amp;lt;/math&amp;gt;. H(z)&lt;br /&gt;
is some &#039;&#039;arbitrary&#039;&#039; filter. The big advantage now is that it is&lt;br /&gt;
possible to plug nearly any digital filter implementation for H(z) into&lt;br /&gt;
the noise shaping loop. One obvious approach is the difference formula&lt;br /&gt;
from above but there are other alternatives with desirable properties.&lt;br /&gt;
However, the purpose of this article is not to discuss digital filters&lt;br /&gt;
in general but their application as noise shaping filters. So,&lt;br /&gt;
throughout the rest of this article H(z) is simply treated as a black&lt;br /&gt;
box. This black box has a certain state and gives us an output sample&lt;br /&gt;
for each input sample. The modified pseudo code loop looks like this:&lt;br /&gt;
&lt;br /&gt;
 (1) h_out[k] = H.filter(last_error)&lt;br /&gt;
 (2) wanted[k] = s[k] - h_out[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) last_error = quantized[k] - wanted[k]&lt;br /&gt;
&lt;br /&gt;
which is equivalent to the shown signal flow graph where the unit&lt;br /&gt;
sample delay z^-1 is implemented by the state variable &#039;last_error&#039;:&lt;br /&gt;
Remember that this black box filter H(z) doesn&#039;t correspond the the&lt;br /&gt;
actual noise shaping filter N(z). Depending on the context published&lt;br /&gt;
coefficients for noise shaping correspond either to N(z), H(z), or&lt;br /&gt;
-H(z).&lt;br /&gt;
&lt;br /&gt;
= Example filters =&lt;br /&gt;
&lt;br /&gt;
The following two filters have been designed by Sebastian Gesemann&lt;br /&gt;
via non-linear optimization to match a slightly modified version of&lt;br /&gt;
the LAME MP3 encoder&#039;s ATH (absolute threshold of hearing) curve. They&lt;br /&gt;
were first published in the&lt;br /&gt;
[http://www.hydrogenaudio.org/forums/index.php?showtopic=47980&amp;amp;pid=551558&amp;amp;st=0 Hydrogenaudio forum]&lt;br /&gt;
including the noise transfer function&#039;s second order factorizations.&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 44100 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.1474 z^{-1} +0.5383 z^{-2} -0.3520 z^{-3} +0.3475 z^{-4}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2061 -0.4706 z^{-1} -0.2534 z^{-2} -0.6214 z^{-3}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 48000 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.3344 z^{-1} +0.7455 z^{-2} -0.4602 z^{-3} +0.3463 z^{-4}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2374 -0.7339 z^{-1} -0.1251 z^{-2} -0.6033 z^{-3}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Signal Processing]]&lt;/div&gt;</summary>
		<author><name>SebastianG</name></author>
	</entry>
	<entry>
		<id>https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=18313</id>
		<title>Noise shaping</title>
		<link rel="alternate" type="text/html" href="https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=18313"/>
		<updated>2008-03-14T10:38:12Z</updated>

		<summary type="html">&lt;p&gt;SebastianG: /* Decoupling filtering and quantization */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
Noise shaping is a technique that is often used in combination with&lt;br /&gt;
[[Dithering|dithering]] in the context of [[Quantize|quantization]]. The purpose of noise shaping&lt;br /&gt;
is to alter the spectral &#039;&#039;shape&#039;&#039; of the error that is introduced due&lt;br /&gt;
to [[Dithering|dithering]] and [[Quantize|quantization]]. Sometimes it may be desirable to&lt;br /&gt;
concentrate most of the noise power in a certain frequency band so&lt;br /&gt;
that the overall noise becomes less perceivable.&lt;br /&gt;
&lt;br /&gt;
= Applications =&lt;br /&gt;
&lt;br /&gt;
In the field of digital image processing a popular noise shaping&lt;br /&gt;
technique is known as &amp;quot;Floyd Steinberg dithering&amp;quot;. This algorithm is&lt;br /&gt;
used to reduce the noise power at lower spatial frequencies which&lt;br /&gt;
results in an improved perceptual quality.&lt;br /&gt;
&lt;br /&gt;
For word length reductions of audio signals (i.e conversion from a 24bit&lt;br /&gt;
signal to a 16bit signal) noise shaping can be used to push quantization&lt;br /&gt;
noise out of perceptually important frequency bands to perceptually less&lt;br /&gt;
important frequency bands (&amp;gt;20 kHz) in order to reduce the perceived&lt;br /&gt;
loudness of the quantization noise.&lt;br /&gt;
&lt;br /&gt;
Another example is the &lt;br /&gt;
[[Super Audio Compact Disc|Super Audio CD]]. The SACD format represents&lt;br /&gt;
the most extreme trade-off between word length (1bit/sample) and&lt;br /&gt;
sampling frequency (2.8 Mhz). Due to the low word length high power&lt;br /&gt;
noise is introduced. With the help of noise shaping this noise is&lt;br /&gt;
usually rejected by about 120 dB in the audible band (0-20 kHz) at the&lt;br /&gt;
cost of a higher noise power in the ultrasonic range (+3.5dB for&lt;br /&gt;
frequencies above 100 kHz).&lt;br /&gt;
&lt;br /&gt;
= Limits &amp;amp; filter design restrictions =&lt;br /&gt;
&lt;br /&gt;
Noise Shaping without [[Dithering|&#039;&#039;dithered&#039;&#039;]] quantization is also possible but&lt;br /&gt;
less useful because the spectral characteristics of &#039;&#039;undithered&#039;&#039;&lt;br /&gt;
quantization noise is not easily predictable and depends heavily on the&lt;br /&gt;
digital signal that is to be quantized.&lt;br /&gt;
&lt;br /&gt;
It&#039;s not possible to lower the noise power for &#039;&#039;all&#039;&#039; frequencies&lt;br /&gt;
via noise shaping because this would be in violation of the&lt;br /&gt;
rate/distortion principles. But it &#039;&#039;is&#039;&#039; possible to lower the noise&lt;br /&gt;
in certain frequency bands at the cost of increased noise power in other&lt;br /&gt;
frequency bands. This property is also known as the&lt;br /&gt;
&#039;&#039;noise shaping theorem&#039;&#039;. It states that the average logarithmic magnitude response of&lt;br /&gt;
the corresponding noise shaping &#039;&#039;filter&#039;&#039; over the whole linear&lt;br /&gt;
frequency axis cannot drop below 0dB.&lt;br /&gt;
&lt;br /&gt;
The noise shaping application poses some restrictions on the fiter&lt;br /&gt;
design. The first sample of the filter&#039;s impulse response always equals&lt;br /&gt;
one. Also, interesting filters for noise shaping are so-called&lt;br /&gt;
&#039;&#039;minimum phase filters&#039;&#039;. This type of filter performs optimal in the sense of&lt;br /&gt;
the noise shaping theorem because the average logarithmig magnitude&lt;br /&gt;
response &#039;&#039;is&#039;&#039; 0dB.&lt;br /&gt;
&lt;br /&gt;
For the special [[Super Audio Compact Disc|SACD]] case an additional&lt;br /&gt;
constraint comes into play: The magnitude response should not exceed&lt;br /&gt;
3.5dB. This is enforced to prevent so-called limit cycles.&lt;br /&gt;
&lt;br /&gt;
The design of these kinds of filters is however not trivial. This is not&lt;br /&gt;
covered by this article.&lt;br /&gt;
&lt;br /&gt;
= Digital filter reminder =&lt;br /&gt;
&lt;br /&gt;
A common representation of digital filters is their &#039;&#039;transfer function&#039;&#039;&lt;br /&gt;
in form of a rational polynomial in z^-1. These polynomials are defined&lt;br /&gt;
by their coefficients b0,b1,b2,...,a1,a2,...:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{B(z)}{A(z)} = \frac{b_0 + b_1 z^{-1} + b_2 z^{-2} + \ldots + b_m z^{-m}}&lt;br /&gt;
{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots + a_m z^{-m}} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &#039;N&#039; is the transfer function and &#039;m&#039; the filter&#039;s &#039;&#039;order&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
== FIR filters ==&lt;br /&gt;
&lt;br /&gt;
In case a1=a2=a3=...=0 the filter&#039;s [http://en.wikipedia.org/wiki/Impulse_response impulse response] is finite which is&lt;br /&gt;
why these filters are called FIR ([[Finite Impulse Response Filter|finite impulse response]])&lt;br /&gt;
filters as opposed to IIR&lt;br /&gt;
[[Infinite Impulse Response Filter|infinite impulse response]] filters. Note that in case of&lt;br /&gt;
FIR filters the coefficients b0, b1, ... correspond to the impulse&lt;br /&gt;
response. The impulse response is another way a filter can be&lt;br /&gt;
characterized and describes what the filter does to a unit pulse. Let&#039;s&lt;br /&gt;
assume for the moment that a1=a2=a3=...=0. The the impulse response&lt;br /&gt;
would be:&lt;br /&gt;
&lt;br /&gt;
 x:  0  0  1  0  0  0  0  ... 0  0  0 ... (input signal)&lt;br /&gt;
 y:  0  0  b0 b1 b2 b3 b4 ... 0  0  0 ... (output signal)&lt;br /&gt;
&lt;br /&gt;
It helps to think of negative powers of z in transfer functions as a&lt;br /&gt;
delay: z^-1 = delay of one sample. z^-2 = delay of two samples etc.&lt;br /&gt;
Since a digital filter is a linear and time-invariant system (LTI) it is&lt;br /&gt;
possible to determine the filter&#039;s output by a superposition of scaled&lt;br /&gt;
and shifted impulse responses. This leads to the following relationship&lt;br /&gt;
between input and output in the case of FIR filters:&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(z) = X(z) * B(z)&lt;br /&gt;
&lt;br /&gt;
where x[] is the input signal, y[] the output signal and k the current&lt;br /&gt;
sample index. This is a simple convolution directly computed using the&lt;br /&gt;
given impulse response. The convolution in the time domain corresponds&lt;br /&gt;
to a multiplication in the Z-domain.&lt;br /&gt;
&lt;br /&gt;
== IIR filters ==&lt;br /&gt;
&lt;br /&gt;
IIR filters are now a simple extension to the FIR case:&lt;br /&gt;
&lt;br /&gt;
 time domain                                Z-transform&lt;br /&gt;
 -----------                                -----------&lt;br /&gt;
      y[k] + a1 y[k-1] + a2 y[k-2] + ...    Y(z) * A(Z) = X(Z) * B(Z)&lt;br /&gt;
 = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Solving for Y we get&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(Z) = X(Z) * N(z)&lt;br /&gt;
                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
which is the well known &#039;&#039;difference formula&#039;&#039;. Its signal flow graph&lt;br /&gt;
representation is known as&lt;br /&gt;
[http://ccrma.stanford.edu/~jos/filters/Direct_Form_I.html direct form I].&lt;br /&gt;
Now, let&#039;s see how we can apply this kind of filter to quantization&lt;br /&gt;
errors...&lt;br /&gt;
&lt;br /&gt;
= Noise Shaper Implementation =&lt;br /&gt;
&lt;br /&gt;
Usually a filter&#039;s transfer function is called &#039;H&#039;. In this article &#039;N&#039;&lt;br /&gt;
has been used because this filter is also known as noise transfer&lt;br /&gt;
function (NTF) in the context of noise shaping. Later, another filter&lt;br /&gt;
&#039;H&#039; is introduced that has a special relation to &#039;N&#039;.&lt;br /&gt;
&lt;br /&gt;
== Direct Form I ==&lt;br /&gt;
&lt;br /&gt;
For convenience let us substitute the actual difference in the&lt;br /&gt;
difference formula for &#039;u&#039;. Also, b0 must be equal to one for noise&lt;br /&gt;
shaping filters as mentioned above. This leads to&lt;br /&gt;
&lt;br /&gt;
 y[k] = x[k] + u[k]   with u[k] = b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
                                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Note that since the filter is supposed to be applied on the quantization&lt;br /&gt;
noise y[] corresponds to the filtered noise and x[] to the unfiltered&lt;br /&gt;
noise. Suppose s[] is the signal we&#039;re trying to quantize. The following&lt;br /&gt;
pseudo code snippet shows the inner loop over k of &#039;&#039;one possible&#039;&#039;&lt;br /&gt;
noise shaping implementation:&lt;br /&gt;
&lt;br /&gt;
 (1) compute u[k]&lt;br /&gt;
 (2) wanted[k] = s[k] + u[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) y[k] = quantized[k] - s[k]&lt;br /&gt;
 (5) x[k] = y[k] - u[k]&lt;br /&gt;
&lt;br /&gt;
Note that the following is true&lt;br /&gt;
&lt;br /&gt;
 time domain                          Z-transform&lt;br /&gt;
 -----------                          -----------&lt;br /&gt;
 quantized[k] = s[k] + u[k] + x[k]    Q(z) = S(z) + X(z) * N(z)&lt;br /&gt;
              = s[k] + y[k]                = S(z) + Y(z)&lt;br /&gt;
&lt;br /&gt;
u[k], wanted[k] are temporary variables. The values are not needed&lt;br /&gt;
outside this iteration. But the past filtered and unfiltered error&lt;br /&gt;
samples need to be remembered (y[] &amp;amp; x[]) upto some amount of samples&lt;br /&gt;
determined by the filter order.&lt;br /&gt;
&lt;br /&gt;
== Decoupling filtering and quantization ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Noiseshaper3.png|thumb|right|200px]]&lt;br /&gt;
As mentioned, the previous example is &#039;&#039;one possible&#039;&#039; implementation.&lt;br /&gt;
It&#039;s not the best possible one. But it is the first version that comes to mind.&lt;br /&gt;
There&#039;s another common formulation which requires&lt;br /&gt;
some more transfer function fiddeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt; N(z) = \frac{B(z)}{A(z)} = 1 - \frac{A(z)}{A(z)} + \frac{B(z)}{A(z)}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since now the two fractions have a common denominator they can be easily&lt;br /&gt;
combined to one fraction. It&#039;s also possible to factor z^-1 out of the fraction:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = 1 - \frac{A(z)-B(z)}{A(z)} = 1 - z^{-1} \frac{z ( A(z)-B(z) )}{A(z)} =: 1 - z^{-1} H(z)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Substituting z*(A(Z)-B(Z)) for H(z) leads to a new filter that is related to N.&lt;br /&gt;
Note that with b0=1 it follows that z*(1-b0)=0. So, H(z) can be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{c0 + c1 z^{-1} + c2 z^{-2} + \ldots}{1 + a1 z^{-1} + a2 z^{-2} + \ldots} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
with &amp;lt;math&amp;gt;c_{k-1} = a_k - b_k&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;k = 1 \ldots m&amp;lt;/math&amp;gt;. H(z)&lt;br /&gt;
is some &#039;&#039;arbitrary&#039;&#039; filter. The big advantage now is that it is&lt;br /&gt;
possible to plug nearly any digital filter implementation for H(z) into&lt;br /&gt;
the noise shaping loop. One obvious approach is the difference formula&lt;br /&gt;
from above but there are other alternatives with desirable properties.&lt;br /&gt;
However, the purpose of this article is not to discuss digital filters&lt;br /&gt;
in general but their application as noise shaping filters. So,&lt;br /&gt;
throughout the rest of this article H(z) is simply treated as a black&lt;br /&gt;
box. This black box has a certain state and gives us an output sample&lt;br /&gt;
for each input sample. The modified pseudo code loop looks like this:&lt;br /&gt;
&lt;br /&gt;
 (1) h_out[k] = H.filter(last_error)&lt;br /&gt;
 (2) wanted[k] = s[k] - h_out[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) last_error = quantized[k] - wanted[k]&lt;br /&gt;
&lt;br /&gt;
which is equivalent to the shown signal flow graph where the unit&lt;br /&gt;
sample delay z^-1 is implemented by the state variable &#039;last_error&#039;:&lt;br /&gt;
Remember that this black box filter H(z) doesn&#039;t correspond the the&lt;br /&gt;
actual noise shaping filter N(z). Depending on the context published&lt;br /&gt;
coefficients for noise shaping correspond either to N(z), H(z), or&lt;br /&gt;
-H(z).&lt;br /&gt;
&lt;br /&gt;
= Example filters =&lt;br /&gt;
&lt;br /&gt;
The following two filters have been designed by Sebastian Gesemann&lt;br /&gt;
via non-linear optimization to match a slightly modified version of&lt;br /&gt;
the LAME MP3 encoder&#039;s ATH (absolute threshold of hearing) curve. They&lt;br /&gt;
were first published in the&lt;br /&gt;
[http://www.hydrogenaudio.org/forums/index.php?showtopic=47980&amp;amp;pid=551558&amp;amp;st=0 Hydrogenaudio forum]&lt;br /&gt;
including the noise transfer function&#039;s second order factorizations.&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 44100 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.1474 z^{-1} +0.5383 z^{-2} -0.3520 z^{-3} +0.3475 z^{-4}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2061 -0.4706 z^{-1} -0.2534 z^{-2} -0.6214 z^{-3}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 48000 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.3344 z^{-1} +0.7455 z^{-2} -0.4602 z^{-3} +0.3463 z^{-4}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2374 -0.7339 z^{-1} -0.1251 z^{-2} -0.6033 z^{-3}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Signal Processing]]&lt;/div&gt;</summary>
		<author><name>SebastianG</name></author>
	</entry>
	<entry>
		<id>https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=18312</id>
		<title>Noise shaping</title>
		<link rel="alternate" type="text/html" href="https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=18312"/>
		<updated>2008-03-14T10:32:16Z</updated>

		<summary type="html">&lt;p&gt;SebastianG: /* Digital filter reminder */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
Noise shaping is a technique that is often used in combination with&lt;br /&gt;
[[Dithering|dithering]] in the context of [[Quantize|quantization]]. The purpose of noise shaping&lt;br /&gt;
is to alter the spectral &#039;&#039;shape&#039;&#039; of the error that is introduced due&lt;br /&gt;
to [[Dithering|dithering]] and [[Quantize|quantization]]. Sometimes it may be desirable to&lt;br /&gt;
concentrate most of the noise power in a certain frequency band so&lt;br /&gt;
that the overall noise becomes less perceivable.&lt;br /&gt;
&lt;br /&gt;
= Applications =&lt;br /&gt;
&lt;br /&gt;
In the field of digital image processing a popular noise shaping&lt;br /&gt;
technique is known as &amp;quot;Floyd Steinberg dithering&amp;quot;. This algorithm is&lt;br /&gt;
used to reduce the noise power at lower spatial frequencies which&lt;br /&gt;
results in an improved perceptual quality.&lt;br /&gt;
&lt;br /&gt;
For word length reductions of audio signals (i.e conversion from a 24bit&lt;br /&gt;
signal to a 16bit signal) noise shaping can be used to push quantization&lt;br /&gt;
noise out of perceptually important frequency bands to perceptually less&lt;br /&gt;
important frequency bands (&amp;gt;20 kHz) in order to reduce the perceived&lt;br /&gt;
loudness of the quantization noise.&lt;br /&gt;
&lt;br /&gt;
Another example is the &lt;br /&gt;
[[Super Audio Compact Disc|Super Audio CD]]. The SACD format represents&lt;br /&gt;
the most extreme trade-off between word length (1bit/sample) and&lt;br /&gt;
sampling frequency (2.8 Mhz). Due to the low word length high power&lt;br /&gt;
noise is introduced. With the help of noise shaping this noise is&lt;br /&gt;
usually rejected by about 120 dB in the audible band (0-20 kHz) at the&lt;br /&gt;
cost of a higher noise power in the ultrasonic range (+3.5dB for&lt;br /&gt;
frequencies above 100 kHz).&lt;br /&gt;
&lt;br /&gt;
= Limits &amp;amp; filter design restrictions =&lt;br /&gt;
&lt;br /&gt;
Noise Shaping without [[Dithering|&#039;&#039;dithered&#039;&#039;]] quantization is also possible but&lt;br /&gt;
less useful because the spectral characteristics of &#039;&#039;undithered&#039;&#039;&lt;br /&gt;
quantization noise is not easily predictable and depends heavily on the&lt;br /&gt;
digital signal that is to be quantized.&lt;br /&gt;
&lt;br /&gt;
It&#039;s not possible to lower the noise power for &#039;&#039;all&#039;&#039; frequencies&lt;br /&gt;
via noise shaping because this would be in violation of the&lt;br /&gt;
rate/distortion principles. But it &#039;&#039;is&#039;&#039; possible to lower the noise&lt;br /&gt;
in certain frequency bands at the cost of increased noise power in other&lt;br /&gt;
frequency bands. This property is also known as the&lt;br /&gt;
&#039;&#039;noise shaping theorem&#039;&#039;. It states that the average logarithmic magnitude response of&lt;br /&gt;
the corresponding noise shaping &#039;&#039;filter&#039;&#039; over the whole linear&lt;br /&gt;
frequency axis cannot drop below 0dB.&lt;br /&gt;
&lt;br /&gt;
The noise shaping application poses some restrictions on the fiter&lt;br /&gt;
design. The first sample of the filter&#039;s impulse response always equals&lt;br /&gt;
one. Also, interesting filters for noise shaping are so-called&lt;br /&gt;
&#039;&#039;minimum phase filters&#039;&#039;. This type of filter performs optimal in the sense of&lt;br /&gt;
the noise shaping theorem because the average logarithmig magnitude&lt;br /&gt;
response &#039;&#039;is&#039;&#039; 0dB.&lt;br /&gt;
&lt;br /&gt;
For the special [[Super Audio Compact Disc|SACD]] case an additional&lt;br /&gt;
constraint comes into play: The magnitude response should not exceed&lt;br /&gt;
3.5dB. This is enforced to prevent so-called limit cycles.&lt;br /&gt;
&lt;br /&gt;
The design of these kinds of filters is however not trivial. This is not&lt;br /&gt;
covered by this article.&lt;br /&gt;
&lt;br /&gt;
= Digital filter reminder =&lt;br /&gt;
&lt;br /&gt;
A common representation of digital filters is their &#039;&#039;transfer function&#039;&#039;&lt;br /&gt;
in form of a rational polynomial in z^-1. These polynomials are defined&lt;br /&gt;
by their coefficients b0,b1,b2,...,a1,a2,...:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{B(z)}{A(z)} = \frac{b_0 + b_1 z^{-1} + b_2 z^{-2} + \ldots + b_m z^{-m}}&lt;br /&gt;
{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots + a_m z^{-m}} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &#039;N&#039; is the transfer function and &#039;m&#039; the filter&#039;s &#039;&#039;order&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
== FIR filters ==&lt;br /&gt;
&lt;br /&gt;
In case a1=a2=a3=...=0 the filter&#039;s [http://en.wikipedia.org/wiki/Impulse_response impulse response] is finite which is&lt;br /&gt;
why these filters are called FIR ([[Finite Impulse Response Filter|finite impulse response]])&lt;br /&gt;
filters as opposed to IIR&lt;br /&gt;
[[Infinite Impulse Response Filter|infinite impulse response]] filters. Note that in case of&lt;br /&gt;
FIR filters the coefficients b0, b1, ... correspond to the impulse&lt;br /&gt;
response. The impulse response is another way a filter can be&lt;br /&gt;
characterized and describes what the filter does to a unit pulse. Let&#039;s&lt;br /&gt;
assume for the moment that a1=a2=a3=...=0. The the impulse response&lt;br /&gt;
would be:&lt;br /&gt;
&lt;br /&gt;
 x:  0  0  1  0  0  0  0  ... 0  0  0 ... (input signal)&lt;br /&gt;
 y:  0  0  b0 b1 b2 b3 b4 ... 0  0  0 ... (output signal)&lt;br /&gt;
&lt;br /&gt;
It helps to think of negative powers of z in transfer functions as a&lt;br /&gt;
delay: z^-1 = delay of one sample. z^-2 = delay of two samples etc.&lt;br /&gt;
Since a digital filter is a linear and time-invariant system (LTI) it is&lt;br /&gt;
possible to determine the filter&#039;s output by a superposition of scaled&lt;br /&gt;
and shifted impulse responses. This leads to the following relationship&lt;br /&gt;
between input and output in the case of FIR filters:&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(z) = X(z) * B(z)&lt;br /&gt;
&lt;br /&gt;
where x[] is the input signal, y[] the output signal and k the current&lt;br /&gt;
sample index. This is a simple convolution directly computed using the&lt;br /&gt;
given impulse response. The convolution in the time domain corresponds&lt;br /&gt;
to a multiplication in the Z-domain.&lt;br /&gt;
&lt;br /&gt;
== IIR filters ==&lt;br /&gt;
&lt;br /&gt;
IIR filters are now a simple extension to the FIR case:&lt;br /&gt;
&lt;br /&gt;
 time domain                                Z-transform&lt;br /&gt;
 -----------                                -----------&lt;br /&gt;
      y[k] + a1 y[k-1] + a2 y[k-2] + ...    Y(z) * A(Z) = X(Z) * B(Z)&lt;br /&gt;
 = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Solving for Y we get&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(Z) = X(Z) * N(z)&lt;br /&gt;
                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
which is the well known &#039;&#039;difference formula&#039;&#039;. Its signal flow graph&lt;br /&gt;
representation is known as&lt;br /&gt;
[http://ccrma.stanford.edu/~jos/filters/Direct_Form_I.html direct form I].&lt;br /&gt;
Now, let&#039;s see how we can apply this kind of filter to quantization&lt;br /&gt;
errors...&lt;br /&gt;
&lt;br /&gt;
= Noise Shaper Implementation =&lt;br /&gt;
&lt;br /&gt;
Usually a filter&#039;s transfer function is called &#039;H&#039;. In this article &#039;N&#039;&lt;br /&gt;
has been used because this filter is also known as noise transfer&lt;br /&gt;
function (NTF) in the context of noise shaping. Later, another filter&lt;br /&gt;
&#039;H&#039; is introduced that has a special relation to &#039;N&#039;.&lt;br /&gt;
&lt;br /&gt;
== Direct Form I ==&lt;br /&gt;
&lt;br /&gt;
For convenience let us substitute the actual difference in the&lt;br /&gt;
difference formula for &#039;u&#039;. Also, b0 must be equal to one for noise&lt;br /&gt;
shaping filters as mentioned above. This leads to&lt;br /&gt;
&lt;br /&gt;
 y[k] = x[k] + u[k]   with u[k] = b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
                                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Note that since the filter is supposed to be applied on the quantization&lt;br /&gt;
noise y[] corresponds to the filtered noise and x[] to the unfiltered&lt;br /&gt;
noise. Suppose s[] is the signal we&#039;re trying to quantize. The following&lt;br /&gt;
pseudo code snippet shows the inner loop over k of &#039;&#039;one possible&#039;&#039;&lt;br /&gt;
noise shaping implementation:&lt;br /&gt;
&lt;br /&gt;
 (1) compute u[k]&lt;br /&gt;
 (2) wanted[k] = s[k] + u[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) y[k] = quantized[k] - s[k]&lt;br /&gt;
 (5) x[k] = y[k] - u[k]&lt;br /&gt;
&lt;br /&gt;
Note that the following is true&lt;br /&gt;
&lt;br /&gt;
 time domain                          Z-transform&lt;br /&gt;
 -----------                          -----------&lt;br /&gt;
 quantized[k] = s[k] + u[k] + x[k]    Q(z) = S(z) + X(z) * N(z)&lt;br /&gt;
              = s[k] + y[k]                = S(z) + Y(z)&lt;br /&gt;
&lt;br /&gt;
u[k], wanted[k] are temporary variables. The values are not needed&lt;br /&gt;
outside this iteration. But the past filtered and unfiltered error&lt;br /&gt;
samples need to be remembered (y[] &amp;amp; x[]) upto some amount of samples&lt;br /&gt;
determined by the filter order.&lt;br /&gt;
&lt;br /&gt;
== Decoupling filtering and quantization ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Noiseshaper3.png|thumb|right|200px]]&lt;br /&gt;
As mentioned, the previous example is &#039;&#039;one possible&#039;&#039; implementation.&lt;br /&gt;
It&#039;s not the best possible one. But it is the first version one could&lt;br /&gt;
that comes to mind. There&#039;s another common formulation. We now require&lt;br /&gt;
some more transfer function fiddeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt; N(z) = \frac{B(z)}{A(z)} = 1 - \frac{A(z)}{A(z)} + \frac{B(z)}{A(z)}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since now the two fractions have a common denominator they can be easily&lt;br /&gt;
combined to one fraction. It&#039;s also possible to factor z^-1 out of the fraction:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = 1 - \frac{A(z)-B(z)}{A(z)} = 1 - z^{-1} \frac{z ( A(z)-B(z) )}{A(z)} =: 1 - z^{-1} H(z)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Substituting z*(A(Z)-B(Z)) for H(z) leads to a new filter that is related to N.&lt;br /&gt;
Note that with b0=1 it follows that z*(1-b0)=0. So, H(z) can be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{c0 + c1 z^{-1} + c2 z^{-2} + \ldots}{1 + a1 z^{-1} + a2 z^{-2} + \ldots} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
with &amp;lt;math&amp;gt;c_{k-1} = a_k - b_k&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;k = 1 \ldots m&amp;lt;/math&amp;gt;. H(z)&lt;br /&gt;
is some &#039;&#039;arbitrary&#039;&#039; filter. The big advantage now is that it is&lt;br /&gt;
possible to plug nearly any digital filter implementation for H(z) into&lt;br /&gt;
the noise shaping loop. One obvious approach is the difference formula&lt;br /&gt;
from above but there are other alternatives with desirable properties.&lt;br /&gt;
However, the purpose of this article is not to discuss digital filters&lt;br /&gt;
in general but their application as noise shaping filters. So,&lt;br /&gt;
throughout the rest of this article H(z) is simply treated as a black&lt;br /&gt;
box. This black box has a certain state and gives us an output sample&lt;br /&gt;
for each input sample. The modified pseudo code loop looks like this:&lt;br /&gt;
&lt;br /&gt;
 (1) h_out[k] = H.filter(last_error)&lt;br /&gt;
 (2) wanted[k] = s[k] - h_out[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) last_error = quantized[k] - wanted[k]&lt;br /&gt;
&lt;br /&gt;
which is equivalent to the shown signal flow graph where the unit&lt;br /&gt;
sample delay z^-1 is implemented by the state variable &#039;last_error&#039;:&lt;br /&gt;
Remember that this black box filter H(z) doesn&#039;t correspond the the&lt;br /&gt;
actual noise shaping filter N(z). Depending on the context published&lt;br /&gt;
coefficients for noise shaping correspond either to N(z), H(z), or&lt;br /&gt;
-H(z).&lt;br /&gt;
&lt;br /&gt;
= Example filters =&lt;br /&gt;
&lt;br /&gt;
The following two filters have been designed by Sebastian Gesemann&lt;br /&gt;
via non-linear optimization to match a slightly modified version of&lt;br /&gt;
the LAME MP3 encoder&#039;s ATH (absolute threshold of hearing) curve. They&lt;br /&gt;
were first published in the&lt;br /&gt;
[http://www.hydrogenaudio.org/forums/index.php?showtopic=47980&amp;amp;pid=551558&amp;amp;st=0 Hydrogenaudio forum]&lt;br /&gt;
including the noise transfer function&#039;s second order factorizations.&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 44100 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.1474 z^{-1} +0.5383 z^{-2} -0.3520 z^{-3} +0.3475 z^{-4}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2061 -0.4706 z^{-1} -0.2534 z^{-2} -0.6214 z^{-3}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 48000 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.3344 z^{-1} +0.7455 z^{-2} -0.4602 z^{-3} +0.3463 z^{-4}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2374 -0.7339 z^{-1} -0.1251 z^{-2} -0.6033 z^{-3}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Signal Processing]]&lt;/div&gt;</summary>
		<author><name>SebastianG</name></author>
	</entry>
	<entry>
		<id>https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=18311</id>
		<title>Noise shaping</title>
		<link rel="alternate" type="text/html" href="https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=18311"/>
		<updated>2008-03-14T10:12:51Z</updated>

		<summary type="html">&lt;p&gt;SebastianG: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
Noise shaping is a technique that is often used in combination with&lt;br /&gt;
[[Dithering|dithering]] in the context of [[Quantize|quantization]]. The purpose of noise shaping&lt;br /&gt;
is to alter the spectral &#039;&#039;shape&#039;&#039; of the error that is introduced due&lt;br /&gt;
to [[Dithering|dithering]] and [[Quantize|quantization]]. Sometimes it may be desirable to&lt;br /&gt;
concentrate most of the noise power in a certain frequency band so&lt;br /&gt;
that the overall noise becomes less perceivable.&lt;br /&gt;
&lt;br /&gt;
= Applications =&lt;br /&gt;
&lt;br /&gt;
In the field of digital image processing a popular noise shaping&lt;br /&gt;
technique is known as &amp;quot;Floyd Steinberg dithering&amp;quot;. This algorithm is&lt;br /&gt;
used to reduce the noise power at lower spatial frequencies which&lt;br /&gt;
results in an improved perceptual quality.&lt;br /&gt;
&lt;br /&gt;
For word length reductions of audio signals (i.e conversion from a 24bit&lt;br /&gt;
signal to a 16bit signal) noise shaping can be used to push quantization&lt;br /&gt;
noise out of perceptually important frequency bands to perceptually less&lt;br /&gt;
important frequency bands (&amp;gt;20 kHz) in order to reduce the perceived&lt;br /&gt;
loudness of the quantization noise.&lt;br /&gt;
&lt;br /&gt;
Another example is the &lt;br /&gt;
[[Super Audio Compact Disc|Super Audio CD]]. The SACD format represents&lt;br /&gt;
the most extreme trade-off between word length (1bit/sample) and&lt;br /&gt;
sampling frequency (2.8 Mhz). Due to the low word length high power&lt;br /&gt;
noise is introduced. With the help of noise shaping this noise is&lt;br /&gt;
usually rejected by about 120 dB in the audible band (0-20 kHz) at the&lt;br /&gt;
cost of a higher noise power in the ultrasonic range (+3.5dB for&lt;br /&gt;
frequencies above 100 kHz).&lt;br /&gt;
&lt;br /&gt;
= Limits &amp;amp; filter design restrictions =&lt;br /&gt;
&lt;br /&gt;
Noise Shaping without [[Dithering|&#039;&#039;dithered&#039;&#039;]] quantization is also possible but&lt;br /&gt;
less useful because the spectral characteristics of &#039;&#039;undithered&#039;&#039;&lt;br /&gt;
quantization noise is not easily predictable and depends heavily on the&lt;br /&gt;
digital signal that is to be quantized.&lt;br /&gt;
&lt;br /&gt;
It&#039;s not possible to lower the noise power for &#039;&#039;all&#039;&#039; frequencies&lt;br /&gt;
via noise shaping because this would be in violation of the&lt;br /&gt;
rate/distortion principles. But it &#039;&#039;is&#039;&#039; possible to lower the noise&lt;br /&gt;
in certain frequency bands at the cost of increased noise power in other&lt;br /&gt;
frequency bands. This property is also known as the&lt;br /&gt;
&#039;&#039;noise shaping theorem&#039;&#039;. It states that the average logarithmic magnitude response of&lt;br /&gt;
the corresponding noise shaping &#039;&#039;filter&#039;&#039; over the whole linear&lt;br /&gt;
frequency axis cannot drop below 0dB.&lt;br /&gt;
&lt;br /&gt;
The noise shaping application poses some restrictions on the fiter&lt;br /&gt;
design. The first sample of the filter&#039;s impulse response always equals&lt;br /&gt;
one. Also, interesting filters for noise shaping are so-called&lt;br /&gt;
&#039;&#039;minimum phase filters&#039;&#039;. This type of filter performs optimal in the sense of&lt;br /&gt;
the noise shaping theorem because the average logarithmig magnitude&lt;br /&gt;
response &#039;&#039;is&#039;&#039; 0dB.&lt;br /&gt;
&lt;br /&gt;
For the special [[Super Audio Compact Disc|SACD]] case an additional&lt;br /&gt;
constraint comes into play: The magnitude response should not exceed&lt;br /&gt;
3.5dB. This is enforced to prevent so-called limit cycles.&lt;br /&gt;
&lt;br /&gt;
The design of these kinds of filters is however not trivial. This is not&lt;br /&gt;
covered by this article.&lt;br /&gt;
&lt;br /&gt;
= Digital filter reminder =&lt;br /&gt;
&lt;br /&gt;
A common representation of digital filters is their &#039;&#039;transfer function&#039;&#039;&lt;br /&gt;
in form of a rational polynomial in z^-1. These polynomials are defined&lt;br /&gt;
by their coefficients b0,b1,b2,...,a1,a2,...:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{B(z)}{A(z)} = \frac{b_0 + b_1 z^{-1} + b_2 z^{-2} + \ldots + b_m z^{-m}}&lt;br /&gt;
{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots + a_m z^{-m}} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &#039;N&#039; is the transfer function and &#039;m&#039; the filter&#039;s &#039;&#039;order&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
== FIR filters ==&lt;br /&gt;
&lt;br /&gt;
In case a1=a2=a3=...=0 the filter&#039;s impulse response is finite which is&lt;br /&gt;
why these filters are called FIR ([[Finite Impulse Response Filter|finite impulse response]])&lt;br /&gt;
filters as opposed to IIR&lt;br /&gt;
[[Infinite Impulse Response Filter|infinite impulse response]] filters. Note that in case of&lt;br /&gt;
FIR filters the coefficients b0, b1, ... correspond to the impulse&lt;br /&gt;
response. The impulse response is another way a filter can be&lt;br /&gt;
characterized and describes what the filter does to a unit pulse. Let&#039;s&lt;br /&gt;
assume for the moment that a1=a2=a3=...=0. The the impulse response&lt;br /&gt;
would be:&lt;br /&gt;
&lt;br /&gt;
 x:  0  0  1  0  0  0  0  ... 0  0  0 ... (input signal)&lt;br /&gt;
 y:  0  0  b0 b1 b2 b3 b4 ... 0  0  0 ... (output signal)&lt;br /&gt;
&lt;br /&gt;
It helps to think of negative powers of z in transfer functions as a&lt;br /&gt;
delay: z^-1 = delay of one sample. z^-2 = delay of two samples etc.&lt;br /&gt;
Since a digital filter is a linear and time-invariant system (LTI) it is&lt;br /&gt;
possible to determine the filter&#039;s output by a superposition of scaled&lt;br /&gt;
and shifted impulse responses. This leads to the following relationship&lt;br /&gt;
between input and output in the case of FIR filters:&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(z) = X(z) * B(z)&lt;br /&gt;
&lt;br /&gt;
where x[] is the input signal, y[] the output signal and k the current&lt;br /&gt;
sample index. This is a simple convolution directly computed using the&lt;br /&gt;
given impulse response. The convolution in the time domain corresponds&lt;br /&gt;
to a multiplication in the Z-domain.&lt;br /&gt;
&lt;br /&gt;
== IIR filters ==&lt;br /&gt;
&lt;br /&gt;
IIR filters are now a simple extension to the FIR case:&lt;br /&gt;
&lt;br /&gt;
 time domain                                Z-transform&lt;br /&gt;
 -----------                                -----------&lt;br /&gt;
      y[k] + a1 y[k-1] + a2 y[k-2] + ...    Y(z) * A(Z) = X(Z) * B(Z)&lt;br /&gt;
 = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Solving for Y we get&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(Z) = X(Z) * N(z)&lt;br /&gt;
                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
which is the well known &#039;&#039;difference formula&#039;&#039;. Its signal flow graph&lt;br /&gt;
representation is known as&lt;br /&gt;
[http://ccrma.stanford.edu/~jos/filters/Direct_Form_I.html direct form I].&lt;br /&gt;
Now, let&#039;s see how we can apply this kind of filter to quantization&lt;br /&gt;
errors...&lt;br /&gt;
&lt;br /&gt;
= Noise Shaper Implementation =&lt;br /&gt;
&lt;br /&gt;
Usually a filter&#039;s transfer function is called &#039;H&#039;. In this article &#039;N&#039;&lt;br /&gt;
has been used because this filter is also known as noise transfer&lt;br /&gt;
function (NTF) in the context of noise shaping. Later, another filter&lt;br /&gt;
&#039;H&#039; is introduced that has a special relation to &#039;N&#039;.&lt;br /&gt;
&lt;br /&gt;
== Direct Form I ==&lt;br /&gt;
&lt;br /&gt;
For convenience let us substitute the actual difference in the&lt;br /&gt;
difference formula for &#039;u&#039;. Also, b0 must be equal to one for noise&lt;br /&gt;
shaping filters as mentioned above. This leads to&lt;br /&gt;
&lt;br /&gt;
 y[k] = x[k] + u[k]   with u[k] = b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
                                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Note that since the filter is supposed to be applied on the quantization&lt;br /&gt;
noise y[] corresponds to the filtered noise and x[] to the unfiltered&lt;br /&gt;
noise. Suppose s[] is the signal we&#039;re trying to quantize. The following&lt;br /&gt;
pseudo code snippet shows the inner loop over k of &#039;&#039;one possible&#039;&#039;&lt;br /&gt;
noise shaping implementation:&lt;br /&gt;
&lt;br /&gt;
 (1) compute u[k]&lt;br /&gt;
 (2) wanted[k] = s[k] + u[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) y[k] = quantized[k] - s[k]&lt;br /&gt;
 (5) x[k] = y[k] - u[k]&lt;br /&gt;
&lt;br /&gt;
Note that the following is true&lt;br /&gt;
&lt;br /&gt;
 time domain                          Z-transform&lt;br /&gt;
 -----------                          -----------&lt;br /&gt;
 quantized[k] = s[k] + u[k] + x[k]    Q(z) = S(z) + X(z) * N(z)&lt;br /&gt;
              = s[k] + y[k]                = S(z) + Y(z)&lt;br /&gt;
&lt;br /&gt;
u[k], wanted[k] are temporary variables. The values are not needed&lt;br /&gt;
outside this iteration. But the past filtered and unfiltered error&lt;br /&gt;
samples need to be remembered (y[] &amp;amp; x[]) upto some amount of samples&lt;br /&gt;
determined by the filter order.&lt;br /&gt;
&lt;br /&gt;
== Decoupling filtering and quantization ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Noiseshaper3.png|thumb|right|200px]]&lt;br /&gt;
As mentioned, the previous example is &#039;&#039;one possible&#039;&#039; implementation.&lt;br /&gt;
It&#039;s not the best possible one. But it is the first version one could&lt;br /&gt;
that comes to mind. There&#039;s another common formulation. We now require&lt;br /&gt;
some more transfer function fiddeling:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt; N(z) = \frac{B(z)}{A(z)} = 1 - \frac{A(z)}{A(z)} + \frac{B(z)}{A(z)}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since now the two fractions have a common denominator they can be easily&lt;br /&gt;
combined to one fraction. It&#039;s also possible to factor z^-1 out of the fraction:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = 1 - \frac{A(z)-B(z)}{A(z)} = 1 - z^{-1} \frac{z ( A(z)-B(z) )}{A(z)} =: 1 - z^{-1} H(z)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Substituting z*(A(Z)-B(Z)) for H(z) leads to a new filter that is related to N.&lt;br /&gt;
Note that with b0=1 it follows that z*(1-b0)=0. So, H(z) can be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{c0 + c1 z^{-1} + c2 z^{-2} + \ldots}{1 + a1 z^{-1} + a2 z^{-2} + \ldots} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
with &amp;lt;math&amp;gt;c_{k-1} = a_k - b_k&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;k = 1 \ldots m&amp;lt;/math&amp;gt;. H(z)&lt;br /&gt;
is some &#039;&#039;arbitrary&#039;&#039; filter. The big advantage now is that it is&lt;br /&gt;
possible to plug nearly any digital filter implementation for H(z) into&lt;br /&gt;
the noise shaping loop. One obvious approach is the difference formula&lt;br /&gt;
from above but there are other alternatives with desirable properties.&lt;br /&gt;
However, the purpose of this article is not to discuss digital filters&lt;br /&gt;
in general but their application as noise shaping filters. So,&lt;br /&gt;
throughout the rest of this article H(z) is simply treated as a black&lt;br /&gt;
box. This black box has a certain state and gives us an output sample&lt;br /&gt;
for each input sample. The modified pseudo code loop looks like this:&lt;br /&gt;
&lt;br /&gt;
 (1) h_out[k] = H.filter(last_error)&lt;br /&gt;
 (2) wanted[k] = s[k] - h_out[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) last_error = quantized[k] - wanted[k]&lt;br /&gt;
&lt;br /&gt;
which is equivalent to the shown signal flow graph where the unit&lt;br /&gt;
sample delay z^-1 is implemented by the state variable &#039;last_error&#039;:&lt;br /&gt;
Remember that this black box filter H(z) doesn&#039;t correspond the the&lt;br /&gt;
actual noise shaping filter N(z). Depending on the context published&lt;br /&gt;
coefficients for noise shaping correspond either to N(z), H(z), or&lt;br /&gt;
-H(z).&lt;br /&gt;
&lt;br /&gt;
= Example filters =&lt;br /&gt;
&lt;br /&gt;
The following two filters have been designed by Sebastian Gesemann&lt;br /&gt;
via non-linear optimization to match a slightly modified version of&lt;br /&gt;
the LAME MP3 encoder&#039;s ATH (absolute threshold of hearing) curve. They&lt;br /&gt;
were first published in the&lt;br /&gt;
[http://www.hydrogenaudio.org/forums/index.php?showtopic=47980&amp;amp;pid=551558&amp;amp;st=0 Hydrogenaudio forum]&lt;br /&gt;
including the noise transfer function&#039;s second order factorizations.&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 44100 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.1474 z^{-1} +0.5383 z^{-2} -0.3520 z^{-3} +0.3475 z^{-4}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2061 -0.4706 z^{-1} -0.2534 z^{-2} -0.6214 z^{-3}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 48000 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.3344 z^{-1} +0.7455 z^{-2} -0.4602 z^{-3} +0.3463 z^{-4}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2374 -0.7339 z^{-1} -0.1251 z^{-2} -0.6033 z^{-3}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Signal Processing]]&lt;/div&gt;</summary>
		<author><name>SebastianG</name></author>
	</entry>
	<entry>
		<id>https://wiki.hydrogenaudio.org/index.php?title=User_talk:SebastianG&amp;diff=18310</id>
		<title>User talk:SebastianG</title>
		<link rel="alternate" type="text/html" href="https://wiki.hydrogenaudio.org/index.php?title=User_talk:SebastianG&amp;diff=18310"/>
		<updated>2008-03-14T10:10:31Z</updated>

		<summary type="html">&lt;p&gt;SebastianG: Removing all content from page&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>SebastianG</name></author>
	</entry>
	<entry>
		<id>https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=18309</id>
		<title>Noise shaping</title>
		<link rel="alternate" type="text/html" href="https://wiki.hydrogenaudio.org/index.php?title=Noise_shaping&amp;diff=18309"/>
		<updated>2008-03-14T10:01:51Z</updated>

		<summary type="html">&lt;p&gt;SebastianG: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
Noise shaping is a technique that is often used in combination with&lt;br /&gt;
[[Dithering|dithering]] in the context of [[Quantize|quantization]]. The purpose of noise shaping&lt;br /&gt;
is to alter the spectral &#039;&#039;shape&#039;&#039; of the error that is introduced due&lt;br /&gt;
to [[Dithering|dithering]] and [[Quantize|quantization]]. Sometimes it may be desirable to&lt;br /&gt;
concentrate most of the noise power in a certain frequency band so&lt;br /&gt;
that the overall noise becomes less perceivable.&lt;br /&gt;
&lt;br /&gt;
= Applications =&lt;br /&gt;
&lt;br /&gt;
In the field of digital image processing a popular noise shaping&lt;br /&gt;
technique is known as &amp;quot;Floyd Steinberg dithering&amp;quot;. This algorithm is&lt;br /&gt;
used to reduce the noise power at lower spatial frequencies which&lt;br /&gt;
results in an improved perceptual quality.&lt;br /&gt;
&lt;br /&gt;
For word length reductions of audio signals (i.e conversion from a 24bit&lt;br /&gt;
signal to a 16bit signal) noise shaping can be used to push quantization&lt;br /&gt;
noise out of perceptually important frequency bands to perceptually less&lt;br /&gt;
important frequency bands (&amp;gt;20 kHz) in order to reduce the perceived&lt;br /&gt;
loudness of the quantization noise.&lt;br /&gt;
&lt;br /&gt;
Another example is the &lt;br /&gt;
[[Super Audio Compact Disc|Super Audio CD]]. The SACD format represents&lt;br /&gt;
the most extreme trade-off between word length (1bit/sample) and&lt;br /&gt;
sampling frequency (2.8 Mhz). Due to the low word length high power&lt;br /&gt;
noise is introduced. With the help of noise shaping this noise is&lt;br /&gt;
usually rejected by about 120 dB in the audible band (0-20 kHz) at the&lt;br /&gt;
cost of a higher noise power in the ultrasonic range (+3.5dB for&lt;br /&gt;
frequencies above 100 kHz).&lt;br /&gt;
&lt;br /&gt;
= Limits &amp;amp; filter design restrictions =&lt;br /&gt;
&lt;br /&gt;
Noise Shaping without [[Dithering|&#039;&#039;dithered&#039;&#039;]] quantization is also possible but&lt;br /&gt;
less useful because the spectral characteristics of &#039;&#039;undithered&#039;&#039;&lt;br /&gt;
quantization noise is not easily predictable and depends heavily on the&lt;br /&gt;
digital signal that is to be quantized.&lt;br /&gt;
&lt;br /&gt;
It&#039;s not possible to lower the noise power for &#039;&#039;all&#039;&#039; frequencies&lt;br /&gt;
via noise shaping because this would be in violation of the&lt;br /&gt;
rate/distortion principles. But it &#039;&#039;is&#039;&#039; possible to lower the noise&lt;br /&gt;
in certain frequency bands at the cost of increased noise power in other&lt;br /&gt;
frequency bands. This property is also known as the&lt;br /&gt;
&#039;&#039;noise shaping theorem&#039;&#039;. It states that the average logarithmic magnitude response of&lt;br /&gt;
the corresponding noise shaping &#039;&#039;filter&#039;&#039; over the whole linear&lt;br /&gt;
frequency axis cannot drop below 0dB.&lt;br /&gt;
&lt;br /&gt;
The noise shaping application poses some restrictions on the fiter&lt;br /&gt;
design. The first sample of the filter&#039;s impulse response always equals&lt;br /&gt;
one. Also, interesting filters for noise shaping are so-called&lt;br /&gt;
&#039;&#039;minimum phase filters&#039;&#039;. This type of filter performs optimal in the sense of&lt;br /&gt;
the noise shaping theorem because the average logarithmig magnitude&lt;br /&gt;
response &#039;&#039;is&#039;&#039; 0dB.&lt;br /&gt;
&lt;br /&gt;
For the special [[Super Audio Compact Disc|SACD]] case an additional&lt;br /&gt;
constraint comes into play: The magnitude response should not exceed&lt;br /&gt;
3.5dB. This is enforced to prevent so-called limit cycles.&lt;br /&gt;
&lt;br /&gt;
The design of these kinds of filters is however not trivial. This is not&lt;br /&gt;
covered by this article.&lt;br /&gt;
&lt;br /&gt;
= Digital filter reminder =&lt;br /&gt;
&lt;br /&gt;
A common representation of digital filters is their &#039;&#039;transfer function&#039;&#039;&lt;br /&gt;
in form of a rational polynomial in z^-1. These polynomials are defined&lt;br /&gt;
by their coefficients b0,b1,b2,...,a1,a2,...:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{B(z)}{A(z)} = \frac{b_0 + b_1 z^{-1} + b_2 z^{-2} + \ldots + b_m z^{-m}}&lt;br /&gt;
{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots + a_m z^{-m}} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &#039;N&#039; is the transfer function and &#039;m&#039; the filter&#039;s &#039;&#039;order&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
== FIR filters ==&lt;br /&gt;
&lt;br /&gt;
In case a1=a2=a3=...=0 the filter&#039;s impulse response is finite which is&lt;br /&gt;
why these filters are called FIR ([[Finite Impulse Response Filter|finite impulse response]])&lt;br /&gt;
filters as opposed to IIR&lt;br /&gt;
[[Infinite Impulse Response Filter|infinite impulse response]] filters. Note that in case of&lt;br /&gt;
FIR filters the coefficients b0, b1, ... correspond to the impulse&lt;br /&gt;
response. The impulse response is another way a filter can be&lt;br /&gt;
characterized and describes what the filter does to a unit pulse. Let&#039;s&lt;br /&gt;
assume for the moment that a1=a2=a3=...=0. The the impulse response&lt;br /&gt;
would be:&lt;br /&gt;
&lt;br /&gt;
 x:  0  0  1  0  0  0  0  ... 0  0  0 ... (input signal)&lt;br /&gt;
 y:  0  0  b0 b1 b2 b3 b4 ... 0  0  0 ... (output signal)&lt;br /&gt;
&lt;br /&gt;
It helps to think of negative powers of z in transfer functions as a&lt;br /&gt;
delay: z^-1 = delay of one sample. z^-2 = delay of two samples etc.&lt;br /&gt;
Since a digital filter is a linear and time-invariant system (LTI) it is&lt;br /&gt;
possible to determine the filter&#039;s output by a superposition of scaled&lt;br /&gt;
and shifted impulse responses. This leads to the following relationship&lt;br /&gt;
between input and output in the case of FIR filters:&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(z) = X(z) * B(z)&lt;br /&gt;
&lt;br /&gt;
where x[] is the input signal, y[] the output signal and k the current&lt;br /&gt;
sample index. This is a simple convolution directly computed using the&lt;br /&gt;
given impulse response. The convolution in the time domain corresponds&lt;br /&gt;
to a multiplication in the Z-domain.&lt;br /&gt;
&lt;br /&gt;
== IIR filters ==&lt;br /&gt;
&lt;br /&gt;
IIR filters are now a simple extension to the FIR case:&lt;br /&gt;
&lt;br /&gt;
 time domain                                Z-transform&lt;br /&gt;
 -----------                                -----------&lt;br /&gt;
      y[k] + a1 y[k-1] + a2 y[k-2] + ...    Y(z) * A(Z) = X(Z) * B(Z)&lt;br /&gt;
 = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Solving for Y we get&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(Z) = X(Z) * N(z)&lt;br /&gt;
                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
which is the well known &#039;&#039;difference formula&#039;&#039;. Its signal flow graph&lt;br /&gt;
representation is known as&lt;br /&gt;
[http://ccrma.stanford.edu/~jos/filters/Direct_Form_I.html direct form I].&lt;br /&gt;
Now, let&#039;s see how we can apply this kind of filter to quantization&lt;br /&gt;
errors...&lt;br /&gt;
&lt;br /&gt;
= Noise Shaper Implementation =&lt;br /&gt;
&lt;br /&gt;
Usually a filter&#039;s transfer function is called &#039;H&#039;. In this article &#039;N&#039;&lt;br /&gt;
has been used because this filter is also known as noise transfer&lt;br /&gt;
function (NTF) in the context of noise shaping. Later, another filter&lt;br /&gt;
&#039;H&#039; is introduced that has a special relation to &#039;N&#039;.&lt;br /&gt;
&lt;br /&gt;
== Direct Form I ==&lt;br /&gt;
&lt;br /&gt;
For convenience let us substitute the actual difference in the&lt;br /&gt;
difference formula for &#039;u&#039;. Also, b0 must be equal to one for noise&lt;br /&gt;
shaping filters as mentioned above. This leads to&lt;br /&gt;
&lt;br /&gt;
 y[k] = x[k] + u[k]   with u[k] = b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
                                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Note that since the filter is supposed to be applied on the quantization&lt;br /&gt;
noise y[] corresponds to the filtered noise and x[] to the unfiltered&lt;br /&gt;
noise. Suppose s[] is the signal we&#039;re trying to quantize. The following&lt;br /&gt;
pseudo code snippet shows the inner loop over k of &#039;&#039;one possible&#039;&#039;&lt;br /&gt;
noise shaping implementation:&lt;br /&gt;
&lt;br /&gt;
 (1) compute u[k]&lt;br /&gt;
 (2) wanted[k] = s[k] + u[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) y[k] = quantized[k] - s[k]&lt;br /&gt;
 (5) x[k] = y[k] - u[k]&lt;br /&gt;
&lt;br /&gt;
That&#039;s about it. It may not be obvious at first, though. Take a closer&lt;br /&gt;
look at what happens at step 3. Note that the following is true&lt;br /&gt;
&lt;br /&gt;
 time domain                          Z-transform&lt;br /&gt;
 -----------                          -----------&lt;br /&gt;
 quantized[k] = s[k] + u[k] + x[k]    Q(z) = S(z) + X(z) * N(z)&lt;br /&gt;
              = s[k] + y[k]                = S(z) + Y(z)&lt;br /&gt;
&lt;br /&gt;
u[k], wanted[k] are temporary variables. The values are not needed&lt;br /&gt;
outside this iteration, so you don&#039;t need to remember these. But we&lt;br /&gt;
&#039;&#039;do&#039;&#039; need to remember the past filtered and unfiltered error samples&lt;br /&gt;
y[] &amp;amp; x[] upto some amount of samples determined by the filter order.&lt;br /&gt;
&lt;br /&gt;
== Decoupling filtering and quantization ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Noiseshaper3.png|thumb|right|200px]]&lt;br /&gt;
As mentioned, the previous example is &#039;&#039;one possible&#039;&#039; implementation.&lt;br /&gt;
It&#039;s not the smartest one. But it&#039;s hopefully easy to understand.&lt;br /&gt;
There&#039;s another common formulation. We now require some more transfer&lt;br /&gt;
function fiddeling. First, we add one and subtract one again:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt; N(z) = \frac{B(z)}{A(z)} = 1 - \frac{A(z)}{A(z)} + \frac{B(z)}{A(z)}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since now the two fractions have a common denominator we can combine&lt;br /&gt;
them easily to one fraction. Also, let&#039;s factor z^-1 out of the&lt;br /&gt;
fraction:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = 1 - \frac{A(z)-B(z)}{A(z)} = 1 - z^{-1} \frac{z ( A(z)-B(z) )}{A(z)} =: 1 - z^{-1} H(z)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The last thing we did is writing H(z) for z*(A(Z)-B(Z)). Note that with&lt;br /&gt;
b0=1 it follows that z*(1-b0)=0. So, H(z) can be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{c0 + c1 z^{-1} + c2 z^{-2} + \ldots}{1 + a1 z^{-1} + a2 z^{-2} + \ldots} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
with &amp;lt;math&amp;gt;c_{k-1} = a_k - b_k&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;k = 1 \ldots m&amp;lt;/math&amp;gt;. H(z)&lt;br /&gt;
is some &#039;&#039;arbitrary&#039;&#039; filter. The big advantage now is that it is&lt;br /&gt;
possible to plug nearly any digital filter implementation for H(z) into&lt;br /&gt;
the noise shaping loop. One obvious approach is the difference formula&lt;br /&gt;
from above but there are other alternatives with desirable properties.&lt;br /&gt;
However, the purpose of this article is not to discuss digital filters&lt;br /&gt;
in general but their application as noise shaping filters. So,&lt;br /&gt;
throughout the rest of this article H(z) is simply treated as a black&lt;br /&gt;
box. This black box has a certain state and gives us an output sample&lt;br /&gt;
for each input sample. The modified pseudo code loop looks like this:&lt;br /&gt;
&lt;br /&gt;
 (1) h_out[k] = H.filter(last_error)&lt;br /&gt;
 (2) wanted[k] = s[k] - h_out[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) last_error = quantized[k] - wanted[k]&lt;br /&gt;
&lt;br /&gt;
which is equivalent to the shown signal flow graph where the unit&lt;br /&gt;
sample delay z^-1 is implemented by the state variable &#039;last_error&#039;:&lt;br /&gt;
Remember that this black box filter H(z) doesn&#039;t correspond the the&lt;br /&gt;
actual noise shaping filter N(z). Depending on the context published&lt;br /&gt;
coefficients for noise shaping correspond either to N(z), H(z), or&lt;br /&gt;
-H(z).&lt;br /&gt;
&lt;br /&gt;
= Example filters =&lt;br /&gt;
&lt;br /&gt;
The following two filters have been designed by Sebastian Gesemann&lt;br /&gt;
via non-linear optimization to match a slightly modified version of&lt;br /&gt;
the LAME MP3 encoder&#039;s ATH (absolute threshold of hearing) curve. They&lt;br /&gt;
were first published in the&lt;br /&gt;
[http://www.hydrogenaudio.org/forums/index.php?showtopic=47980&amp;amp;pid=551558&amp;amp;st=0 Hydrogenaudio forum]&lt;br /&gt;
including the noise transfer function&#039;s second order factorizations.&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 44100 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.1474 z^{-1} +0.5383 z^{-2} -0.3520 z^{-3} +0.3475 z^{-4}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2061 -0.4706 z^{-1} -0.2534 z^{-2} -0.6214 z^{-3}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 48000 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.3344 z^{-1} +0.7455 z^{-2} -0.4602 z^{-3} +0.3463 z^{-4}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2374 -0.7339 z^{-1} -0.1251 z^{-2} -0.6033 z^{-3}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Signal Processing]]&lt;/div&gt;</summary>
		<author><name>SebastianG</name></author>
	</entry>
	<entry>
		<id>https://wiki.hydrogenaudio.org/index.php?title=User_talk:SebastianG&amp;diff=18308</id>
		<title>User talk:SebastianG</title>
		<link rel="alternate" type="text/html" href="https://wiki.hydrogenaudio.org/index.php?title=User_talk:SebastianG&amp;diff=18308"/>
		<updated>2008-03-14T09:59:10Z</updated>

		<summary type="html">&lt;p&gt;SebastianG: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
Noise shaping is a technique that is often used in combination with&lt;br /&gt;
[[Dithering|dithering]] in the context of [[Quantize|quantization]]. The purpose of noise shaping&lt;br /&gt;
is to alter the spectral &#039;&#039;shape&#039;&#039; of the error that is introduced due&lt;br /&gt;
to [[Dithering|dithering]] and [[Quantize|quantization]]. Sometimes it may be desirable to&lt;br /&gt;
concentrate most of the noise power in a certain frequency band so&lt;br /&gt;
that the overall noise becomes less perceivable.&lt;br /&gt;
&lt;br /&gt;
= Applications =&lt;br /&gt;
&lt;br /&gt;
In the field of digital image processing a popular noise shaping&lt;br /&gt;
technique is known as &amp;quot;Floyd Steinberg dithering&amp;quot;. This algorithm is&lt;br /&gt;
used to reduce the noise power at lower spatial frequencies which&lt;br /&gt;
results in an improved perceptual quality.&lt;br /&gt;
&lt;br /&gt;
For word length reductions of audio signals (i.e conversion from a 24bit&lt;br /&gt;
signal to a 16bit signal) noise shaping can be used to push quantization&lt;br /&gt;
noise out of perceptually important frequency bands to perceptually less&lt;br /&gt;
important frequency bands (&amp;gt;20 kHz) in order to reduce the perceived&lt;br /&gt;
loudness of the quantization noise.&lt;br /&gt;
&lt;br /&gt;
Another example is the &lt;br /&gt;
[[Super Audio Compact Disc|Super Audio CD]]. The SACD format represents&lt;br /&gt;
the most extreme trade-off between word length (1bit/sample) and&lt;br /&gt;
sampling frequency (2.8 Mhz). Due to the low word length high power&lt;br /&gt;
noise is introduced. With the help of noise shaping this noise is&lt;br /&gt;
usually rejected by about 120 dB in the audible band (0-20 kHz) at the&lt;br /&gt;
cost of a higher noise power in the ultrasonic range (+3.5dB for&lt;br /&gt;
frequencies above 100 kHz).&lt;br /&gt;
&lt;br /&gt;
= Limits &amp;amp; filter design restrictions =&lt;br /&gt;
&lt;br /&gt;
Noise Shaping without [[Dithering|&#039;&#039;dithered&#039;&#039;]] quantization is also possible but&lt;br /&gt;
less useful because the spectral characteristics of &#039;&#039;undithered&#039;&#039;&lt;br /&gt;
quantization noise is not easily predictable and depends heavily on the&lt;br /&gt;
digital signal that is to be quantized.&lt;br /&gt;
&lt;br /&gt;
It&#039;s not possible to lower the noise power for &#039;&#039;all&#039;&#039; frequencies&lt;br /&gt;
via noise shaping because this would be in violation of the&lt;br /&gt;
rate/distortion principles. But it &#039;&#039;is&#039;&#039; possible to lower the noise&lt;br /&gt;
in certain frequency bands at the cost of increased noise power in other&lt;br /&gt;
frequency bands. This property is also known as the&lt;br /&gt;
&#039;&#039;noise shaping theorem&#039;&#039;. It states that the average logarithmic magnitude response of&lt;br /&gt;
the corresponding noise shaping &#039;&#039;filter&#039;&#039; over the whole linear&lt;br /&gt;
frequency axis cannot drop below 0dB.&lt;br /&gt;
&lt;br /&gt;
The noise shaping application poses some restrictions on the fiter&lt;br /&gt;
design. The first sample of the filter&#039;s impulse response always equals&lt;br /&gt;
one. Also, interesting filters for noise shaping are so-called&lt;br /&gt;
&#039;&#039;minimum phase filters&#039;&#039;. This type of filter performs optimal in the sense of&lt;br /&gt;
the noise shaping theorem because the average logarithmig magnitude&lt;br /&gt;
response &#039;&#039;is&#039;&#039; 0dB.&lt;br /&gt;
&lt;br /&gt;
For the special [[Super Audio Compact Disc|SACD]] case an additional&lt;br /&gt;
constraint comes into play: The magnitude response should not exceed&lt;br /&gt;
3.5dB. This is enforced to prevent so-called limit cycles.&lt;br /&gt;
&lt;br /&gt;
The design of these kinds of filters is however not trivial. This is not&lt;br /&gt;
covered by this article.&lt;br /&gt;
&lt;br /&gt;
= Digital filter reminder =&lt;br /&gt;
&lt;br /&gt;
A common representation of digital filters is their &#039;&#039;transfer function&#039;&#039;&lt;br /&gt;
in form of a rational polynomial in z^-1. These polynomials are defined&lt;br /&gt;
by their coefficients b0,b1,b2,...,a1,a2,...:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{B(z)}{A(z)} = \frac{b_0 + b_1 z^{-1} + b_2 z^{-2} + \ldots + b_m z^{-m}}&lt;br /&gt;
{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots + a_m z^{-m}} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &#039;N&#039; is the transfer function and &#039;m&#039; the filter&#039;s &#039;&#039;order&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
== FIR filters ==&lt;br /&gt;
&lt;br /&gt;
In case a1=a2=a3=...=0 the filter&#039;s impulse response is finite which is&lt;br /&gt;
why these filters are called FIR ([[Finite Impulse Response Filter|finite impulse response]])&lt;br /&gt;
filters as opposed to IIR&lt;br /&gt;
[[Infinite Impulse Response Filter|infinite impulse response]] filters. Note that in case of&lt;br /&gt;
FIR filters the coefficients b0, b1, ... correspond to the impulse&lt;br /&gt;
response. The impulse response is another way a filter can be&lt;br /&gt;
characterized and describes what the filter does to a unit pulse. Let&#039;s&lt;br /&gt;
assume for the moment that a1=a2=a3=...=0. The the impulse response&lt;br /&gt;
would be:&lt;br /&gt;
&lt;br /&gt;
 x:  0  0  1  0  0  0  0  ... 0  0  0 ... (input signal)&lt;br /&gt;
 y:  0  0  b0 b1 b2 b3 b4 ... 0  0  0 ... (output signal)&lt;br /&gt;
&lt;br /&gt;
It helps to think of negative powers of z in transfer functions as a&lt;br /&gt;
delay: z^-1 = delay of one sample. z^-2 = delay of two samples etc.&lt;br /&gt;
Since a digital filter is a linear and time-invariant system (LTI) it is&lt;br /&gt;
possible to determine the filter&#039;s output by a superposition of scaled&lt;br /&gt;
and shifted impulse responses. This leads to the following relationship&lt;br /&gt;
between input and output in the case of FIR filters:&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(z) = X(z) * B(z)&lt;br /&gt;
&lt;br /&gt;
where x[] is the input signal, y[] the output signal and k the current&lt;br /&gt;
sample index. This is a simple convolution directly computed using the&lt;br /&gt;
given impulse response. The convolution in the time domain corresponds&lt;br /&gt;
to a multiplication in the Z-domain.&lt;br /&gt;
&lt;br /&gt;
== IIR filters ==&lt;br /&gt;
&lt;br /&gt;
IIR filters are now a simple extension to the FIR case:&lt;br /&gt;
&lt;br /&gt;
 time domain                                Z-transform&lt;br /&gt;
 -----------                                -----------&lt;br /&gt;
      y[k] + a1 y[k-1] + a2 y[k-2] + ...    Y(z) * A(Z) = X(Z) * B(Z)&lt;br /&gt;
 = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Solving for Y we get&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(Z) = X(Z) * N(z)&lt;br /&gt;
                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
which is the well known &#039;&#039;difference formula&#039;&#039;. Its signal flow graph&lt;br /&gt;
representation is known as&lt;br /&gt;
[http://ccrma.stanford.edu/~jos/filters/Direct_Form_I.html direct form I].&lt;br /&gt;
Now, let&#039;s see how we can apply this kind of filter to quantization&lt;br /&gt;
errors...&lt;br /&gt;
&lt;br /&gt;
= Noise Shaper Implementation =&lt;br /&gt;
&lt;br /&gt;
Usually a filter&#039;s transfer function is called &#039;H&#039;. In this article &#039;N&#039;&lt;br /&gt;
has been used because this filter is also known as noise transfer&lt;br /&gt;
function (NTF) in the context of noise shaping. Later, another filter&lt;br /&gt;
&#039;H&#039; is introduced that has a special relation to &#039;N&#039;.&lt;br /&gt;
&lt;br /&gt;
== Direct Form I ==&lt;br /&gt;
&lt;br /&gt;
For convenience let us substitute the actual difference in the&lt;br /&gt;
difference formula for &#039;u&#039;. Also, b0 must be equal to one for noise&lt;br /&gt;
shaping filters as mentioned above. This leads to&lt;br /&gt;
&lt;br /&gt;
 y[k] = x[k] + u[k]   with u[k] = b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
                                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Note that since the filter is supposed to be applied on the quantization&lt;br /&gt;
noise y[] corresponds to the filtered noise and x[] to the unfiltered&lt;br /&gt;
noise. Suppose s[] is the signal we&#039;re trying to quantize. The following&lt;br /&gt;
pseudo code snippet shows the inner loop over k of &#039;&#039;one possible&#039;&#039;&lt;br /&gt;
noise shaping implementation:&lt;br /&gt;
&lt;br /&gt;
 (1) compute u[k]&lt;br /&gt;
 (2) wanted[k] = s[k] + u[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) y[k] = quantized[k] - s[k]&lt;br /&gt;
 (5) x[k] = y[k] - u[k]&lt;br /&gt;
&lt;br /&gt;
That&#039;s about it. It may not be obvious at first, though. Take a closer&lt;br /&gt;
look at what happens at step 3. Note that the following is true&lt;br /&gt;
&lt;br /&gt;
 time domain                          Z-transform&lt;br /&gt;
 -----------                          -----------&lt;br /&gt;
 quantized[k] = s[k] + u[k] + x[k]    Q(z) = S(z) + X(z) * N(z)&lt;br /&gt;
              = s[k] + y[k]                = S(z) + Y(z)&lt;br /&gt;
&lt;br /&gt;
u[k], wanted[k] are temporary variables. The values are not needed&lt;br /&gt;
outside this iteration, so you don&#039;t need to remember these. But we&lt;br /&gt;
&#039;&#039;do&#039;&#039; need to remember the past filtered and unfiltered error samples&lt;br /&gt;
y[] &amp;amp; x[] upto some amount of samples determined by the filter order.&lt;br /&gt;
&lt;br /&gt;
== Decoupling filtering and quantization ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Noiseshaper3.png|thumb|right|200px]]&lt;br /&gt;
As mentioned, the previous example is &#039;&#039;one possible&#039;&#039; implementation.&lt;br /&gt;
It&#039;s not the smartest one. But it&#039;s hopefully easy to understand.&lt;br /&gt;
There&#039;s another common formulation. We now require some more transfer&lt;br /&gt;
function fiddeling. First, we add one and subtract one again:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt; N(z) = \frac{B(z)}{A(z)} = 1 - \frac{A(z)}{A(z)} + \frac{B(z)}{A(z)}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since now the two fractions have a common denominator we can combine&lt;br /&gt;
them easily to one fraction. Also, let&#039;s factor z^-1 out of the&lt;br /&gt;
fraction:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = 1 - \frac{A(z)-B(z)}{A(z)} = 1 - z^{-1} \frac{z ( A(z)-B(z) )}{A(z)} =: 1 - z^{-1} H(z)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The last thing we did is writing H(z) for z*(A(Z)-B(Z)). Note that with&lt;br /&gt;
b0=1 it follows that z*(1-b0)=0. So, H(z) can be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{c0 + c1 z^{-1} + c2 z^{-2} + \ldots}{1 + a1 z^{-1} + a2 z^{-2} + \ldots} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
with &amp;lt;math&amp;gt;c_{k-1} = a_k - b_k&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;k = 1 \ldots m&amp;lt;/math&amp;gt;. H(z)&lt;br /&gt;
is some &#039;&#039;arbitrary&#039;&#039; filter. The big advantage now is that it is&lt;br /&gt;
possible to plug nearly any digital filter implementation for H(z) into&lt;br /&gt;
the noise shaping loop. One obvious approach is the difference formula&lt;br /&gt;
from above but there are other alternatives with desirable properties.&lt;br /&gt;
However, the purpose of this article is not to discuss digital filters&lt;br /&gt;
in general but their application as noise shaping filters. So,&lt;br /&gt;
throughout the rest of this article H(z) is simply treated as a black&lt;br /&gt;
box. This black box has a certain state and gives us an output sample&lt;br /&gt;
for each input sample. The modified pseudo code loop looks like this:&lt;br /&gt;
&lt;br /&gt;
 (1) h_out[k] = H.filter(last_error)&lt;br /&gt;
 (2) wanted[k] = s[k] - h_out[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) last_error = quantized[k] - wanted[k]&lt;br /&gt;
&lt;br /&gt;
which is equivalent to the shown signal flow graph where the unit&lt;br /&gt;
sample delay z^-1 is implemented by the state variable &#039;last_error&#039;:&lt;br /&gt;
Remember that this black box filter H(z) doesn&#039;t correspond the the&lt;br /&gt;
actual noise shaping filter N(z). Depending on the context published&lt;br /&gt;
coefficients for noise shaping correspond either to N(z), H(z), or&lt;br /&gt;
-H(z).&lt;br /&gt;
&lt;br /&gt;
= Example filters =&lt;br /&gt;
&lt;br /&gt;
The following two filters have been designed by Sebastian Gesemann&lt;br /&gt;
via non-linear optimization to match a slightly modified version of&lt;br /&gt;
the LAME MP3 encoder&#039;s ATH (absolute threshold of hearing) curve. They&lt;br /&gt;
were first published in the&lt;br /&gt;
[http://www.hydrogenaudio.org/forums/index.php?showtopic=47980&amp;amp;pid=551558&amp;amp;st=0 Hydrogenaudio forum]&lt;br /&gt;
including the noise transfer function&#039;s second order factorizations.&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 44100 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.1474 z^{-1} +0.5383 z^{-2} -0.3520 z^{-3} +0.3475 z^{-4}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2061 -0.4706 z^{-1} -0.2534 z^{-2} -0.6214 z^{-3}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Sampling rate of 48000 Hz, derived from LAME&#039;s ATH formula ==&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.3344 z^{-1} +0.7455 z^{-2} -0.4602 z^{-3} +0.3463 z^{-4}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2374 -0.7339 z^{-1} -0.1251 z^{-2} -0.6033 z^{-3}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;/div&gt;</summary>
		<author><name>SebastianG</name></author>
	</entry>
	<entry>
		<id>https://wiki.hydrogenaudio.org/index.php?title=File:Noiseshaper3.png&amp;diff=18307</id>
		<title>File:Noiseshaper3.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.hydrogenaudio.org/index.php?title=File:Noiseshaper3.png&amp;diff=18307"/>
		<updated>2008-03-14T09:50:13Z</updated>

		<summary type="html">&lt;p&gt;SebastianG: Signal flow graph for a noise shaped quantizer&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Signal flow graph for a noise shaped quantizer&lt;/div&gt;</summary>
		<author><name>SebastianG</name></author>
	</entry>
	<entry>
		<id>https://wiki.hydrogenaudio.org/index.php?title=User_talk:SebastianG&amp;diff=18306</id>
		<title>User talk:SebastianG</title>
		<link rel="alternate" type="text/html" href="https://wiki.hydrogenaudio.org/index.php?title=User_talk:SebastianG&amp;diff=18306"/>
		<updated>2008-03-14T09:26:47Z</updated>

		<summary type="html">&lt;p&gt;SebastianG: 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...&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Noise shaping is a technique that is often used in combination with&lt;br /&gt;
[[Dithering|dithering]] in the context of [[Quantize|quantization]]. The purpose of noise shaping&lt;br /&gt;
is to alter the spectral &#039;&#039;shape&#039;&#039; of the error that is introduced due&lt;br /&gt;
to [[Dithering|dithering]] and [[Quantize|quantization]]. Sometimes it may be desirable to&lt;br /&gt;
concentrate most of the noise power in a certain frequency band so&lt;br /&gt;
that the overall noise becomes less perceivable.&lt;br /&gt;
&lt;br /&gt;
== Limits ==&lt;br /&gt;
&lt;br /&gt;
Noise Shaping without [[Dithering|&#039;&#039;dithered&#039;&#039;]] quantization is also possible but&lt;br /&gt;
less useful because the spectral characteristics of &#039;&#039;undithered&#039;&#039;&lt;br /&gt;
quantization noise is not easily predictable and depends heavily on the&lt;br /&gt;
digital signal that is to be quantized.&lt;br /&gt;
&lt;br /&gt;
It&#039;s not possible to lower the noise power for &#039;&#039;all&#039;&#039; frequencies&lt;br /&gt;
via noise shaping because this would be in violation of the&lt;br /&gt;
rate/distortion principles. But it &#039;&#039;is&#039;&#039; possible to lower the noise&lt;br /&gt;
in certain frequency bands at the cost of increased noise power in other&lt;br /&gt;
frequency bands. This property is also known as the&lt;br /&gt;
&#039;&#039;noise shaping theorem&#039;&#039;. It states that the average logarithmic magnitude response of&lt;br /&gt;
the corresponding noise shaping &#039;&#039;filter&#039;&#039; over the whole linear&lt;br /&gt;
frequency axis cannot drop below 0dB.&lt;br /&gt;
&lt;br /&gt;
== Applications ==&lt;br /&gt;
&lt;br /&gt;
In the field of digital image processing a popular noise shaping&lt;br /&gt;
technique is known as &amp;quot;Floyd Steinberg dithering&amp;quot;. This algorithm is&lt;br /&gt;
used to reduce the noise power at lower spatial frequencies which&lt;br /&gt;
results in an improved perceptual quality.&lt;br /&gt;
&lt;br /&gt;
For word length reductions of audio signals (i.e conversion from a 24bit&lt;br /&gt;
signal to a 16bit signal) noise shaping can be used to push quantization&lt;br /&gt;
noise out of perceptually important frequency bands to perceptually less&lt;br /&gt;
important frequency bands (&amp;gt;20 kHz) in order to reduce the perceived&lt;br /&gt;
loudness of the quantization noise.&lt;br /&gt;
&lt;br /&gt;
Another example is the &lt;br /&gt;
[[Super Audio Compact Disc|Super Audio CD]]. The SACD format represents&lt;br /&gt;
the most extreme trade-off between word length (1bit/sample) and&lt;br /&gt;
sampling frequency (2.8 Mhz). Due to the low word length high power&lt;br /&gt;
noise is introduced. With the help of noise shaping this noise is&lt;br /&gt;
usually rejected by about 120 dB in the audible band (0-20 kHz) at the&lt;br /&gt;
cost of a higher noise power in the ultrasonic range (+3.5dB for&lt;br /&gt;
frequencies above 100 kHz).&lt;br /&gt;
&lt;br /&gt;
== Filter design restrictions ==&lt;br /&gt;
&lt;br /&gt;
The noise shaping application poses some restrictions on the fiter&lt;br /&gt;
design. The first sample of the filter&#039;s impulse response always equals&lt;br /&gt;
one. Also, interesting filters for noise shaping are so-called&lt;br /&gt;
&#039;&#039;minimum phase filters&#039;&#039;. This type of filter performs optimal in the sense of&lt;br /&gt;
the noise shaping theorem because the average logarithmig magnitude&lt;br /&gt;
response &#039;&#039;is&#039;&#039; 0dB.&lt;br /&gt;
&lt;br /&gt;
For the special [[Super Audio Compact Disc|SACD]] case an additional&lt;br /&gt;
constraint comes into play: The magnitude response should not exceed&lt;br /&gt;
3.5dB. This is enforced to prevent so-called limit cycles.&lt;br /&gt;
&lt;br /&gt;
The design of these kinds of filters is however not trivial. This is not&lt;br /&gt;
covered by this article.&lt;br /&gt;
&lt;br /&gt;
== Digital filter reminder ==&lt;br /&gt;
&lt;br /&gt;
A common representation of digital filters is their &#039;&#039;transfer function&#039;&#039;&lt;br /&gt;
in form of a rational polynomial in z^-1. These polynomials are defined&lt;br /&gt;
by their coefficients b0,b1,b2,...,a1,a2,...:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{B(z)}{A(z)} = \frac{b_0 + b_1 z^{-1} + b_2 z^{-2} + \ldots + b_m z^{-m}}&lt;br /&gt;
{1 + a_1 z^{-1} + a_2 z^{-2} + \ldots + a_m z^{-m}} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &#039;N&#039; is the transfer function and &#039;m&#039; the filter&#039;s &#039;&#039;order&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
=== FIR filters ===&lt;br /&gt;
&lt;br /&gt;
In case a1=a2=a3=...=0 the filter&#039;s impulse response is finite which is&lt;br /&gt;
why these filters are called FIR ([[Finite Impulse Response Filter|finite impulse response]])&lt;br /&gt;
filters as opposed to IIR&lt;br /&gt;
[[Infinite Impulse Response Filter|infinite impulse response]] filters. Note that in case of&lt;br /&gt;
FIR filters the coefficients b0, b1, ... correspond to the impulse&lt;br /&gt;
response. The impulse response is another way a filter can be&lt;br /&gt;
characterized and describes what the filter does to a unit pulse. Let&#039;s&lt;br /&gt;
assume for the moment that a1=a2=a3=...=0. The the impulse response&lt;br /&gt;
would be:&lt;br /&gt;
&lt;br /&gt;
 x:  0  0  1  0  0  0  0  ... 0  0  0 ... (input signal)&lt;br /&gt;
 y:  0  0  b0 b1 b2 b3 b4 ... 0  0  0 ... (output signal)&lt;br /&gt;
&lt;br /&gt;
It helps to think of negative powers of z in transfer functions as a&lt;br /&gt;
delay: z^-1 = delay of one sample. z^-2 = delay of two samples etc.&lt;br /&gt;
Since a digital filter is a linear and time-invariant system (LTI) it is&lt;br /&gt;
possible to determine the filter&#039;s output by a superposition of scaled&lt;br /&gt;
and shifted impulse responses. This leads to the following relationship&lt;br /&gt;
between input and output in the case of FIR filters:&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(z) = X(z) * B(z)&lt;br /&gt;
&lt;br /&gt;
where x[] is the input signal, y[] the output signal and k the current&lt;br /&gt;
sample index. This is a simple convolution directly computed using the&lt;br /&gt;
given impulse response. The convolution in the time domain corresponds&lt;br /&gt;
to a multiplication in the Z-domain.&lt;br /&gt;
&lt;br /&gt;
=== IIR filters ===&lt;br /&gt;
&lt;br /&gt;
IIR filters are now a simple extension to the FIR case:&lt;br /&gt;
&lt;br /&gt;
 time domain                                Z-transform&lt;br /&gt;
 -----------                                -----------&lt;br /&gt;
      y[k] + a1 y[k-1] + a2 y[k-2] + ...    Y(z) * A(Z) = X(Z) * B(Z)&lt;br /&gt;
 = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Solving for Y we get&lt;br /&gt;
&lt;br /&gt;
 time domain                                     Z-transform&lt;br /&gt;
 -----------                                     -----------&lt;br /&gt;
 y[k] = b0 x[k] + b1 x[k-1] + b2 x[k-2] + ...    Y(Z) = X(Z) * N(z)&lt;br /&gt;
                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
which is the well known &#039;&#039;difference formula&#039;&#039;. Its signal flow graph&lt;br /&gt;
representation is known as &#039;&#039;direct form I&#039;&#039;. See&lt;br /&gt;
&amp;lt;http://ccrma.stanford.edu/~jos/filters/Direct_Form_I.html&amp;gt;&lt;br /&gt;
Now, let&#039;s see how we can apply this kind of filter to quantization&lt;br /&gt;
errors...&lt;br /&gt;
&lt;br /&gt;
== Noise Shaper Implementation ==&lt;br /&gt;
&lt;br /&gt;
Usually a filter&#039;s transfer function is called &#039;H&#039;. In this article &#039;N&#039;&lt;br /&gt;
has been used because this filter is also known as noise transfer&lt;br /&gt;
function (NTF) in the context of noise shaping. Later, another filter&lt;br /&gt;
&#039;H&#039; is introduced that has a special relation to &#039;N&#039;.&lt;br /&gt;
&lt;br /&gt;
=== Direct Form I ===&lt;br /&gt;
&lt;br /&gt;
For convenience let us substitute the actual difference in the&lt;br /&gt;
difference formula for &#039;u&#039;. Also, b0 must be equal to one for noise&lt;br /&gt;
shaping filters as mentioned above. This leads to&lt;br /&gt;
&lt;br /&gt;
 y[k] = x[k] + u[k]   with u[k] = b1 x[k-1] + b2 x[k-2] + ...&lt;br /&gt;
                                - a1 y[k-1] - a2 y[k-2] + ...&lt;br /&gt;
&lt;br /&gt;
Note that since the filter is supposed to be applied on the quantization&lt;br /&gt;
noise y[] corresponds to the filtered noise and x[] to the unfiltered&lt;br /&gt;
noise. Suppose s[] is the signal we&#039;re trying to quantize. The following&lt;br /&gt;
pseudo code snippet shows the inner loop over k of &#039;&#039;one possible&#039;&#039;&lt;br /&gt;
noise shaping implementation:&lt;br /&gt;
&lt;br /&gt;
 (1) compute u[k]&lt;br /&gt;
 (2) wanted[k] = s[k] + u[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) y[k] = quantized[k] - s[k]&lt;br /&gt;
 (5) x[k] = y[k] - u[k]&lt;br /&gt;
&lt;br /&gt;
That&#039;s about it. It may not be obvious at first, though. Take a closer&lt;br /&gt;
look at what happens at step 3. Note that the following is true&lt;br /&gt;
&lt;br /&gt;
 time domain                          Z-transform&lt;br /&gt;
 -----------                          -----------&lt;br /&gt;
 quantized[k] = s[k] + u[k] + x[k]    Q(z) = S(z) + X(z) * N(z)&lt;br /&gt;
              = s[k] + y[k]                = S(z) + Y(z)&lt;br /&gt;
&lt;br /&gt;
u[k], wanted[k] are temporary variables. The values are not needed&lt;br /&gt;
outside this iteration, so you don&#039;t need to remember these. But we&lt;br /&gt;
&#039;&#039;do&#039;&#039; need to remember the past filtered and unfiltered error samples&lt;br /&gt;
y[] &amp;amp; x[] upto some amount of samples determined by the filter order.&lt;br /&gt;
&lt;br /&gt;
=== Decoupling filtering and quantization ===&lt;br /&gt;
&lt;br /&gt;
As mentioned, the previous example is &#039;&#039;one possible&#039;&#039; implementation.&lt;br /&gt;
It&#039;s not the smartest one. But it&#039;s hopefully easy to understand.&lt;br /&gt;
There&#039;s another common formulation. We now require some more transfer&lt;br /&gt;
function fiddeling. First, we add one and subtract one again:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt; N(z) = \frac{B(z)}{A(z)} = 1 - \frac{A(z)}{A(z)} + \frac{B(z)}{A(z)}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since now the two fractions have a common denominator we can combine&lt;br /&gt;
them easily to one fraction. Also, let&#039;s factor z^-1 out of the&lt;br /&gt;
fraction:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = 1 - \frac{A(z)-B(z)}{A(z)} = 1 - z^{-1} \frac{z ( A(z)-B(z) )}{A(z)} =: 1 - z^{-1} H(z)&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The last thing we did is writing H(z) for z*(A(Z)-B(Z)). Note that with&lt;br /&gt;
b0=1 it follows that z*(1-b0)=0. So, H(z) can be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{c0 + c1 z^{-1} + c2 z^{-2} + \ldots}{1 + a1 z^{-1} + a2 z^{-2} + \ldots} &amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
with &amp;lt;math&amp;gt;c_{k-1} = a_k - b_k&amp;lt;/math&amp;gt; for &amp;lt;math&amp;gt;k = 1 \ldots m&amp;lt;/math&amp;gt;. H(z)&lt;br /&gt;
is some &#039;&#039;arbitrary&#039;&#039; filter. The big advantage now is that it is&lt;br /&gt;
possible to plug nearly any digital filter implementation for H(z) into&lt;br /&gt;
the noise shaping loop. One obvious approach is the difference formula&lt;br /&gt;
from above but there are other alternatives with desirable properties.&lt;br /&gt;
However, the purpose of this article is not to discuss digital filters&lt;br /&gt;
in general but their application as noise shaping filters. So,&lt;br /&gt;
throughout the rest of this article H(z) is simply treated as a black&lt;br /&gt;
box. This black box has a certain state and gives us an output sample&lt;br /&gt;
for each input sample. The modified pseudo code loop looks like this:&lt;br /&gt;
&lt;br /&gt;
 (1) h_out[k] = H.filter(last_error)&lt;br /&gt;
 (2) wanted[k] = s[k] - h_out[k]&lt;br /&gt;
 (3) quantized[k] = quantize( wanted[k] + dither() )&lt;br /&gt;
 (4) last_error = quantized[k] - wanted[k]&lt;br /&gt;
&lt;br /&gt;
which is equivalent to the following signal flow graph where the unit&lt;br /&gt;
sample delay z^-1 is implemented by the state variable &#039;last_error&#039;:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;link to the signal flow graph I drew&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Remember that this black box filter H(z) doesn&#039;t correspond the the&lt;br /&gt;
actual noise shaping filter N(z). Depending on the context published&lt;br /&gt;
coefficients for noise shaping correspond either to N(z), H(z), or&lt;br /&gt;
-H(z).&lt;br /&gt;
&lt;br /&gt;
== Example filters ==&lt;br /&gt;
&lt;br /&gt;
The following two filters have been designed by Sebastian Gesemann&lt;br /&gt;
via non-linear optimization to match a slightly modified version of&lt;br /&gt;
the LAME MP3 encoder&#039;s ATH (absolute threshold of hearing) curve. They&lt;br /&gt;
were first published in the Hydrogenaudio forum including the noise&lt;br /&gt;
transfer function&#039;s second order factorizations.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;link to the &amp;quot;build your own noise shapers&amp;quot; thread&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Sampling rate of 44100 Hz, derived from LAME&#039;s ATH formula ===&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.1474 z^{-1} +0.5383 z^{-2} -0.3520 z^{-3} +0.3475 z^{-4}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2061 -0.4706 z^{-1} -0.2534 z^{-2} -0.6214 z^{-3}}&lt;br /&gt;
{1 +1.0587 z^{-1} +0.0676 z^{-2} -0.6054 z^{-3} -0.2738 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sampling rate of 48000 Hz, derived from LAME&#039;s ATH formula ===&lt;br /&gt;
&lt;br /&gt;
The noise transfer function is given by&lt;br /&gt;
&amp;lt;math&amp;gt;N(z) = \frac{1 -1.3344 z^{-1} +0.7455 z^{-2} -0.4602 z^{-3} +0.3463 z^{-4}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The corresponding filter H is given by&lt;br /&gt;
&amp;lt;math&amp;gt;H(z) = \frac{2.2374 -0.7339 z^{-1} -0.1251 z^{-2} -0.6033 z^{-3}}&lt;br /&gt;
{1 +0.9030 z^{-1} +0.0116 z^{-2} -0.5853 z^{-3} -0.2571 z^{-4}}&amp;lt;/math&amp;gt;&lt;/div&gt;</summary>
		<author><name>SebastianG</name></author>
	</entry>
</feed>