<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE document PUBLIC "-//CNX//DTD CNXML 0.5 plus MathML//EN" "http://cnx.rice.edu/cnxml/0.5/DTD/cnxml_mathml.dtd">
<document xmlns="http://cnx.rice.edu/cnxml" xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="None">
  <name>Signal Processing in Processing: Sampling and Quantization</name>
  <metadata>
  <md:version>1.5</md:version>
  <md:created>2005/09/27 07:13:59 GMT-5</md:created>
  <md:revised>2008/06/05 01:27:48.309 GMT-5</md:revised>
  <md:authorlist>
      <md:author id="drocchesso">
      <md:firstname>Davide</md:firstname>
      
      <md:surname>Rocchesso</md:surname>
      <md:email>roc@iuav.it</md:email>
    </md:author>
      <md:author id="ppolotti">
      <md:firstname>Pietro</md:firstname>
      
      <md:surname>Polotti</md:surname>
      <md:email>polotti@sci.univr.it</md:email>
    </md:author>
  </md:authorlist>

  <md:maintainerlist>
    <md:maintainer id="drocchesso">
      <md:firstname>Davide</md:firstname>
      
      <md:surname>Rocchesso</md:surname>
      <md:email>roc@iuav.it</md:email>
    </md:maintainer>
  </md:maintainerlist>
  
  <md:keywordlist>
    <md:keyword>Quantization of sounds and images</md:keyword>
    <md:keyword>Sampling of 1D and 2D signals</md:keyword>
  </md:keywordlist>

  <md:abstract>Fundamentals of sampling, reconstruction, and quantization of 1D (sounds) and 2D (images) signals, especially oriented at the Processing language.</md:abstract>
</metadata>

  <content>
    <section id="campionamento">
      <name>Sampling</name>
      <para id="intro_campionamento">
	Both sounds and images can be considered as signals, in one or
	two dimensions, respectively. Sound can be described as a
	fluctuation of the acoustic pressure in time, while images are
	spatial distributions of values of luminance or color, the
	latter being described in its RGB or HSB components.  Any
	signal, in order to be processed by numerical computing
	devices, have to be reduced to a sequence of discrete
	<emphasis>samples</emphasis>, and each sample must be
	represented using a finite number of bits. The first operation
	is called <term>sampling</term>, and the second operation is
	called <term>quantization</term> of the domain of real
	numbers.
      </para>
      <section id="campionamento1D">
	<name>1-D: Sounds</name>
	<para id="campionamento1Dp">
	  Sampling is, for one-dimensional signals, the operation that
	  transforms a continuous-time signal (such as, for instance,
	  the air pressure fluctuation at the entrance of the ear
	  canal) into a discrete-time signal, that is a sequence of
	  numbers. The discrete-time signal gives the values of the
	  continuous-time signal read at intervals of <m:math>
	  <m:ci>T</m:ci> </m:math> seconds. The reciprocal of the
	  sampling interval is called <term>sampling rate</term>
	  <m:math>
	    <m:apply>
	      <m:eq/>
	      <m:ci>
		<m:msub>
		  <m:mi>F</m:mi>
		  <m:mn>s</m:mn>
		</m:msub>
	      </m:ci>
	    <m:apply>
	      <m:divide/>
	      <m:cn>1</m:cn>
	      <m:ci>T</m:ci>
	    </m:apply>
	    </m:apply>
	  </m:math>. In this module we do not explain the theory of
	  sampling, but we rather describe its manifestations. For a a
	  more extensive yet accessible treatment, we point to the
	  <cite src="#rocISP">Introduction to Sound Processing</cite>.
	  For our purposes, the process of sampling a 1-D signal can
	  be reduced to three facts and a theorem.
	</para>
	<list id="fatti">
	  <item><name>Fact 1</name> The <cnxn document="m0046">Fourier
	      Transform</cnxn> of a discrete-time signal is a function
	      (called <term>spectrum</term>) of the continuous
	      variable <m:math> <m:ci>ω</m:ci> </m:math>, and it
	      is periodic with period
	      <m:math>
	      <m:apply>
		<m:times/>
		<m:cn>2</m:cn>
		<m:ci>π</m:ci>
	      </m:apply>
 	      </m:math>. Given a value of <m:math><m:ci>ω</m:ci>
	      </m:math>, the Fourier transform gives back a complex
	      number that can be interpreted as magnitude and phase
	      (translation in time) of the sinusoidal component at
	      that frequency.
	  </item>
	  <item><name>Fact 2</name> Sampling the continuous-time signal <m:math>
	      <m:apply>
		<m:ci type="fn">x</m:ci>
		<m:ci>t</m:ci>
	      </m:apply>
	    </m:math>
	    with interval <m:math>
	      <m:ci>T</m:ci> 
	    </m:math> we get the discrete-time signal <m:math>
	      <m:apply>
		<m:eq/>
	      <m:apply>
		<m:ci type="fn">x</m:ci>
		<m:ci>n</m:ci>
	      </m:apply>
	      <m:apply>
		<m:ci type="fn">x</m:ci>
		  <m:apply>
		    <m:times/>
		    <m:ci>n</m:ci>
		    <m:ci>T</m:ci>
		  </m:apply>
	      </m:apply>		
	      </m:apply>
	    </m:math>, which is a function of the discrete variable <m:math>
	      <m:ci>n</m:ci>
	    </m:math>.
	  </item>
	  <item><name>Fact 3</name> Sampling a continuous-time signal
	  with sampling rate <m:math> <m:ci>
		<m:msub>
		  <m:mi>F</m:mi>
		  <m:mn>s</m:mn>
		</m:msub>
	      </m:ci>
	    </m:math> produces a discrete-time signal whose frequency
	    spectrum is the periodic replication of the original
	    signal, and the replication period is <m:math> <m:ci>
		<m:msub>
		  <m:mi>F</m:mi>
		  <m:mn>s</m:mn>
		</m:msub>
	      </m:ci>
	    </m:math>. The Fourier variable <m:math>
	    <m:ci>ω</m:ci> </m:math> for functions of discrete
	    variable is converted into the frequency variable <m:math>
	    <m:ci>f</m:ci> </m:math> (in Hertz) by means of <m:math>
	      <m:apply>
		<m:eq/>
		<m:ci>f</m:ci>
		<m:apply>
		  <m:divide/>
		  <m:ci>ω</m:ci>
		  <m:apply>
		    <m:times/>
		    <m:cn>2</m:cn>
		    <m:ci>π</m:ci>
		    <m:ci>T</m:ci>
		  </m:apply>
		</m:apply>
	      </m:apply>
	    </m:math>.
	  </item>
	</list>
	<para id="spettro_esp">
	  The <cnxn target="repeti"/> shows an example of frequency
	  spectrum of a signal sampled with sampling rate <m:math>
	  <m:ci>
		<m:msub>
		  <m:mi>F</m:mi>
		  <m:mn>s</m:mn>
		</m:msub>
	      </m:ci>
	    </m:math>. In the example, the continuous-time signal had
	    all and only the frequency components between <m:math>
	    <m:apply>
	      <m:minus/>
	      <m:ci>
		<m:msub>
		  <m:mi>F</m:mi>
		  <m:mn>b</m:mn>
		</m:msub>
	      </m:ci> </m:apply> </m:math> and <m:math> <m:ci>
		<m:msub>
		  <m:mi>F</m:mi>
		  <m:mn>b</m:mn>
		</m:msub>
	      </m:ci>
	  </m:math>. The replicas of the original spectrum are
	  sometimes called <emphasis>images</emphasis>.
	</para>
	<figure id="repeti">
	  <name>Frequency spectrum of a sampled signal</name>
	  <media type="image/png" src="repeti.png">
	  </media>
	</figure>
	<para id="teorema_campp">
	  Given the <cnxn target="fatti">facts</cnxn>, we can have an
	  intuitive understanding of the Sampling Theorem,
	  historically attributed to the scientists Nyquist and
	  Shannon.
	</para>
	<rule id="teorema_campionamento" type="theorem">
	  <name>Sampling Theorem</name>
	  <statement>
	    <para id="campiona_statement">
	    A continuous-time signal <m:math>
	      <m:apply>
		<m:ci type="fn">x</m:ci>
		<m:ci>t</m:ci>
	      </m:apply>
	    </m:math>, whose spectral content is limited to
	    frequencies smaller than <m:math> <m:ci>
		<m:msub>
		  <m:mi>F</m:mi>
		  <m:mn>b</m:mn>
		</m:msub>
	      </m:ci>
	    </m:math> (i.e., it is band-limited to <m:math> <m:ci>
		<m:msub>
		  <m:mi>F</m:mi>
		  <m:mn>b</m:mn>
		</m:msub>
	      </m:ci>
	    </m:math>) can be recovered from its sampled version
		<m:math> <m:apply> <m:ci type="fn">x</m:ci>
		<m:ci>n</m:ci>
	      </m:apply>
	    </m:math> if the sampling rate is larger than twice the
	    bandwidth (i.e., if <m:math>
		<m:apply>
		  <m:gt/>
		  <m:ci>
		    <m:msub>
		      <m:mi>F</m:mi>
		      <m:mn>s</m:mn>
		    </m:msub>
		  </m:ci>
		  <m:apply>
		    <m:times/>
		    <m:cn>2</m:cn>
		  <m:ci>
		    <m:msub>
		      <m:mi>F</m:mi>
		      <m:mn>b</m:mn>
		    </m:msub>
		  </m:ci>
		  </m:apply>
		</m:apply>
	      </m:math>)
	    </para>
	  </statement>
	</rule>
	<para id="camp_rico">The reconstruction can only occur by means of a filter that
	  cancels out all spectral images except for the one directly
	  coming from the original continuous-time signal. In other
	  words, the canceled images are those having frequency
	  components higher than the <term>Nyquist frequency</term>
	  defined as <m:math>
	    <m:apply>
	      <m:divide/>
	      <m:ci><m:msub>
		  <m:mi>F</m:mi>
		  <m:mn>s</m:mn>
		</m:msub>
	      </m:ci>
	      <m:cn>2</m:cn>
	    </m:apply>
	  </m:math>. The condition required by the <cnxn target="teorema_campionamento">sampling theorem</cnxn> is equivalent
to saying that no overlaps between spectral images are allowed. If
such superimpositions were present, it wouldn't be possible to design
a filter that eliminates the copies of the original spectrum. In case of overlapping, a filter
that eliminates all frequency components higher than the Nyquist
frequency would produce a signal that is affected by
<term>aliasing</term>. The concept of aliasing is well illustrated in
the <cnxn document="m11448"> Aliasing Applet</cnxn>, where a
continuous-time sinusoid is subject to sampling. If the frequency of
the sinusoid is too high as compared to the sampling rate, we see that
the the waveform that is reconstructed from samples is not the
original sinusoid, as it has a much lower frequency. We all have
familiarity with aliasing as it shows up in moving images, for
instance when the wagon wheels in western movies start spinning
backward. In that case, the sampling rate is given by the <term>frame
rate</term>, or number of pictures per second, and has to be related
with the spinning velocity of the wheels. This is one of several <link src="http://www.michaelbach.de/ot/mot_strob/">
stroboscopic </link> phenomena.
	</para>



<para id="applet_aliasing">
In the case of sound, in order to become aware of the consequences of the 
<m:math>
	      <m:apply>
		<m:times/>
		<m:cn>2</m:cn>
		<m:ci>π</m:ci>
	      </m:apply>
 	      </m:math> periodicity of discrete-time signal spectra
(see <cnxn target="repeti"/>) and of violations of the condition of
the sampling theorem, we examine a simple case.

Let us consider a sound that is generated by a sum of sinusoids that
are harmonics (i.e., integer multiples) of a fundamental. The spectrum
of such sound would display peaks corresponding to the fundamental
frequency and to its integer multiples.
Just to give a concrete example, imagine working at the sampling rate of 
	  <m:math>
	    <m:cn>44100</m:cn> </m:math> Hz and summing <m:math>
<m:cn>10</m:cn> </m:math> sinusoids. From the sampling theorem we know
that, in our case, we can represent without aliasing all frequency
components up to <m:math> <m:cn>22050</m:cn> </m:math> Hz. So, in
order to avoid aliasing, the fundamental frequency should be lower
than <m:math> <m:cn>2205</m:cn> </m:math> Hz. The Processing (+ Sonia)
code reported in table <cnxn target="aliasingTab"/> implements a
generator of sounds formed by <m:math> <m:cn>10</m:cn> </m:math>
harmonic sinusoids. To produce such sounds it is necessary to click on
a point of the display window. The x coordinate would vary with the
fundamental frequency, and the window will show the spectral peaks
corresponding to the generated harmonics. When we click on a point
whose x coordinate is larger than <m:math>
	    <m:apply>
	      <m:divide/>
	      <m:cn>1</m:cn>
	      <m:cn>10</m:cn>
	    </m:apply>
	  </m:math> of the window width, we still see ten spectral
	  peaks. Otherwise, we violate the sampling theorem and
	  aliasing will enter our representation.
</para>


<table id="aliasingTab" frame="all">
<tgroup cols="2" align="left" colsep="1" rowsep="1"><colspec colname="col1" colnum="1" colwidth="60"/> 
		<colspec colname="col2" colnum="2" colwidth="340"/>
		<tbody>
		  <row>
		    <entry>
		      <link src="./aliasingDP.html"> Aliasing test:
		    Applet to experience the effect of aliasing on
		    sounds obtained by summation of 10 sinusoids in
		    harmonic ratio </link> </entry>
		    <entry>
	    <code type="block">
          <![CDATA[       
import pitaru.sonia_v2_9.*;

int L = 1024; // FFT length (positive frequency)
int L2 = int(L*4); // table length
int sr = 44100;
int H = 10; //number of harmonics
int ampiezza;
float[] Data;
float freq = 10.00; // fundamental frequency [Hz]
Sample mySample;
float oneCycle = TWO_PI/L; 

void setup() {
  size(1024,200);
  frameRate(1);
  colorMode(HSB, 360, height, height);
  Sonia.start(this);
  mySample = new Sample(L2); // empty sample with L2 frames.
  Data = new float[L2]; // array with as many frames as samples.
  //populate the array with sample data, i.e. a sine wave 
  for(int k = 1; k <=H; k++){      // Additive Synthesis
    for(int i = 0; i < L2; i++){
      Data[i] = Data[i] + sin(i*oneCycle*freq*k)/10;
    }
  }
  mySample.write(Data); // write from 'Data' array into sample
  mySample.repeat(); // loop the sample */
}

void draw()
{
  background(0,20,0);
  strokeWeight(0);
  stroke(0,230,0);
  mySample.getSpectrum(L);  // FFT
  stroke(255); //println();
  for ( int i = 1; i < L; i++){
    ampiezza = int(mySample.spectrum[i]*L);
    line(i, height, i, height - 2*ampiezza/(0.003*i));
  }
}

void mouseReleased()
{
  freq = (float)(mouseX/2);  // to change the fundamental
  println(freq + " Hz");
  for (int i = 0; i < L2; i++) Data[i] = 0;
  for(int k = 1; k <=H; k++){      // Additive Synthesis
    for(int i = 0; i < L2; i++){
      Data[i] = Data[i] + sin(i*oneCycle*freq*k)/20;
    }
  }
  mySample.write(Data); // write from 'Data' array into sample
  mySample.getSpectrum(L); // initial steps
  mouseReleased();
}

// safely stop the Sonia engine upon shutdown.
public void stop(){
  Sonia.stop();
  super.stop();
}

           ]]>	      
	    </code>
		    </entry>
		  </row>
		</tbody>
	      
</tgroup>
</table>


      </section>

      <section id="campionaimmagini">
	<name>2-D: Images</name>
	<para id="campionamento2Dp">
	  Let us assume we have a continuous distribution, on a plane,
	    of values of luminance or, more simply stated, an
	    image. In order to process it using a computer we have to
	    reduce it to a sequence of numbers by means of
	    sampling. There are several ways to sample an image, or
	    read its values of luminance at discrete points. The
	    simplest way is to use a regular grid, with spatial steps
	    <m:math> <m:ci>X</m:ci> </m:math> e <m:math>
	    <m:ci>Y</m:ci> </m:math>. Similarly to what we did for
	    sounds, we define the spatial sampling rates <m:math>
	    <m:apply>
	      <m:eq/>
	      <m:ci>
		<m:msub>
		  <m:mi>F</m:mi>
		  <m:mn>X</m:mn>
		</m:msub>
	      </m:ci>
	    <m:apply>
	      <m:divide/>
	      <m:cn>1</m:cn>
	      <m:ci>X</m:ci>
	    </m:apply>
	    </m:apply>
	  </m:math> and  <m:math>
	    <m:apply>
	      <m:eq/>
	      <m:ci>
		<m:msub>
		  <m:mi>F</m:mi>
		  <m:mn>Y</m:mn>
		</m:msub>
	      </m:ci>
	    <m:apply>
	      <m:divide/>
	      <m:cn>1</m:cn>
	      <m:ci>Y</m:ci>
	    </m:apply>
	    </m:apply>
	  </m:math>.  As in the one-dimensional case, also for
	  two-dimensional signals, or images, sampling can be
	  described by three facts and a theorem.
	</para>
	<list id="fatti2D">
	  <item><name>Fact 1</name> The Fourier Transform of a
	      discrete-space signal is a function (called
	      <term>spectrum</term>) of two continuous variables
	      <m:math> <m:ci> <m:msub> <m:mi>ω</m:mi>
	      <m:mn>X</m:mn>
		</m:msub>
	      </m:ci> </m:math> and <m:math>
	      <m:ci>		<m:msub>
		  <m:mi>ω</m:mi>
		  <m:mn>Y</m:mn>
		</m:msub>
	      </m:ci> </m:math>, and it is periodic in two dimensions with periods
	      <m:math>
	      <m:apply>
		<m:times/>
		<m:cn>2</m:cn>
		<m:ci>π</m:ci>
	      </m:apply>
 	      </m:math>. Given a couple of values <m:math>
	      <m:ci>		<m:msub>
		  <m:mi>ω</m:mi>
		  <m:mn>X</m:mn>
		</m:msub>
	      </m:ci> </m:math> and <m:math>
	      <m:ci>		<m:msub>
		  <m:mi>ω</m:mi>
		  <m:mn>Y</m:mn>
		</m:msub>
	      </m:ci> </m:math>, the Fourier transform gives back a
	      complex number that can be interpreted as magnitude and
	      phase (translation in space) of the sinusoidal component
	      at such spatial frequencies.
	  </item>
	  <item><name>Fact 2</name> Sampling the continuous-space signal <m:math>
	      <m:apply>
		<m:ci type="fn">s</m:ci>
		<m:ci>x</m:ci>
		<m:ci>y</m:ci>
	      </m:apply>
	    </m:math>
	    with the regular grid of steps <m:math>
	      <m:ci>X</m:ci> 
	    </m:math>, <m:math>
	      <m:ci>Y</m:ci> 
	    </m:math>, gives a discrete-space signal <m:math>
	      <m:apply>
		<m:eq/>
	      <m:apply>
		<m:ci type="fn">s</m:ci>
		<m:ci>m</m:ci>
		<m:ci>n</m:ci>
	      </m:apply>
	      <m:apply>
		<m:ci type="fn">s</m:ci>
		  <m:apply>
		    <m:times/>
		    <m:ci>m</m:ci>
		    <m:ci>X</m:ci>
		  </m:apply>
		  <m:apply>
		    <m:times/>
		    <m:ci>n</m:ci>
		    <m:ci>Y</m:ci>
		  </m:apply>
	      </m:apply>		
	      </m:apply>
	    </m:math>, which is a function of the discrete variables <m:math>
	      <m:ci>m</m:ci>
	    </m:math> and <m:math>
	      <m:ci>n</m:ci>
	    </m:math>.
	  </item>
	  <item><name>Fact 3</name> Sampling a continuous-space signal
	  with spatial frequencies <m:math> <m:ci>
		<m:msub>
		  <m:mi>F</m:mi>
		  <m:mn>X</m:mn>
		</m:msub>
	      </m:ci>
	    </m:math> and <m:math> <m:ci>
		<m:msub>
		  <m:mi>F</m:mi>
		  <m:mn>Y</m:mn>
		</m:msub>
	      </m:ci>
	    </m:math> gives a discrete-space signal whose spectrum is
	    the periodic replication along the grid of steps <m:math>
	    <m:ci>
		<m:msub>
		  <m:mi>F</m:mi>
		  <m:mn>X</m:mn>
		</m:msub>
	      </m:ci>
	    </m:math> and <m:math> <m:ci>
		<m:msub>
		  <m:mi>F</m:mi>
		  <m:mn>Y</m:mn>
		</m:msub>
	      </m:ci>
	    </m:math> of the original signal spectrum. The Fourier variables
	    <m:math>
	      <m:ci> <m:msub> <m:mi>ω</m:mi> <m:mn>X</m:mn>
		</m:msub>
	      </m:ci> </m:math> and <m:math>
	      <m:ci>		<m:msub>
		  <m:mi>ω</m:mi>
		  <m:mn>Y</m:mn>
		</m:msub>
	      </m:ci> </m:math> correspond to the frequencies (in
	      cycles per meter) represented by the variables <m:math>
	      <m:apply>
		<m:eq/>
		<m:ci><m:msub>
		    <m:mi>f</m:mi>
		    <m:mn>X</m:mn>
		  </m:msub> </m:ci>
		<m:apply>
		  <m:divide/>
		  <m:ci><m:msub>
		  <m:mi>ω</m:mi>
		  <m:mn>X</m:mn>
		</m:msub></m:ci>
		  <m:apply>
		    <m:times/>
		    <m:cn>2</m:cn>
		    <m:ci>π</m:ci>
		    <m:ci>X</m:ci>
		  </m:apply>
		</m:apply>
	      </m:apply>
	    </m:math> and <m:math>
	      <m:apply>
		<m:eq/>
		<m:ci><m:msub>
		    <m:mi>f</m:mi>
		    <m:mn>Y</m:mn>
		  </m:msub> </m:ci>
		<m:apply>
		  <m:divide/>
		  <m:ci><m:msub>
		  <m:mi>ω</m:mi>
		  <m:mn>Y</m:mn>
		</m:msub></m:ci>
		  <m:apply>
		    <m:times/>
		    <m:cn>2</m:cn>
		    <m:ci>π</m:ci>
		    <m:ci>Y</m:ci>
		  </m:apply>
		</m:apply>
	      </m:apply>
	    </m:math>.
	  </item>
	</list>
	<para id="spettro_es2Dp">
	  The <cnxn target="repeti2D"/> shows an example of spectrum
	  of a two-dimensional sampled signal. There, the
	  continuous-space signal had all and only the frequency
	  components included in the central hexagon.  The hexagonal
	  shape of the spectral support (region of non-null spectral
	  energy) is merely illustrative. The replicas of the original
	  spectrum are often called spectral
	  <emphasis>images</emphasis>.
	</para>
      <figure id="repeti2D">
	  <name>Spectrum of a sampled image</name>
	  <media type="image/png" src="repeti2D.png">
	  </media>
	</figure>
	<para id="teorema_campp2D">
	  Given the above <cnxn target="fatti2D">facts</cnxn>, we can
	  have an intuitive understanding of the Sampling Theorem.
	</para>
	<rule id="teorema_campionamento2D" type="theorem">
	  <name>Sampling Theorem (in 2D)</name>
	  <statement>
	    <para id="campiona_statement2D">
	    A continuous-space signal <m:math>
	      <m:apply>
		<m:ci type="fn">s</m:ci>
		<m:ci>x</m:ci>
		<m:ci>y</m:ci>
	      </m:apply>
	    </m:math>, whose spectral content is limited to spatial
	    frequencies belonging to the rectangle having semi-edges
	    <m:math> <m:ci>
		<m:msub>
		  <m:mi>F</m:mi>
		  <m:mn>bX</m:mn>
		</m:msub>
	      </m:ci>
	    </m:math> and <m:math> <m:ci>
		<m:msub>
		  <m:mi>F</m:mi>
		  <m:mn>bY</m:mn>
		</m:msub>
	      </m:ci>
	    </m:math> (i.e., bandlimited) can be recovered from its sampled version
	      <m:math>
		<m:apply> <m:ci type="fn">s</m:ci> <m:ci>m</m:ci>
		<m:ci>n</m:ci>
	      </m:apply>
	    </m:math> if the spatial sampling rates are larger than
	    twice the respective bandwidths (i.e., if <m:math>
		<m:apply>
		  <m:gt/>
		  <m:ci>
		    <m:msub>
		      <m:mi>F</m:mi>
		      <m:mn>X</m:mn>
		    </m:msub>
		  </m:ci>
		  <m:apply>
		    <m:times/>
		    <m:cn>2</m:cn>
		  <m:ci>
		    <m:msub>
		      <m:mi>F</m:mi>
		      <m:mn>bX</m:mn>
		    </m:msub>
		  </m:ci>
		  </m:apply>
		</m:apply>
	      </m:math> and <m:math>
		<m:apply>
		  <m:gt/>
		  <m:ci>
		    <m:msub>
		      <m:mi>F</m:mi>
		      <m:mn>Y</m:mn>
		    </m:msub>
		  </m:ci>
		  <m:apply>
		    <m:times/>
		    <m:cn>2</m:cn>
		  <m:ci>
		    <m:msub>
		      <m:mi>F</m:mi>
		      <m:mn>bY</m:mn>
		    </m:msub>
		  </m:ci>
		  </m:apply>
		</m:apply>
	      </m:math>)
	    </para>
	  </statement>
	</rule>
	<para id="camp_rico2D">
	  In practice, the spatial sampling step can not be larger
	  than the semi-period of the finest spatial frequency (or the
	  finest detail) that is represented in the image. The
	  reconstruction can only be done through a filter that
	  eliminates all the spectral images but the one coming
	  directly from the original continuous-space signal. In other
	  words, the filter will cut all images whose frequency
	  components are higher than the <term>Nyquist
	  frequency</term> defined as <m:math>
	    <m:apply>
	      <m:divide/>
	      <m:ci><m:msub>
		  <m:mi>F</m:mi>
		  <m:mn>X</m:mn>
		</m:msub>
	      </m:ci>
	      <m:cn>2</m:cn>
	    </m:apply>
	  </m:math> and <m:math>
	    <m:apply>
	      <m:divide/>
	      <m:ci><m:msub>
		  <m:mi>F</m:mi>
		  <m:mn>Y</m:mn>
		</m:msub>
	      </m:ci>
	      <m:cn>2</m:cn>
	    </m:apply>
	  </m:math> along the two axes. The condition required by the
	  <cnxn target="teorema_campionamento2D">sampling
	  theorem</cnxn> is equivalent to requiring that there are no
	  overlaps between spectral images. If there were such
	  overlaps, it wouldn't be possible to eliminate the copies of
	  the original signal spectrum by means of filtering. In case of overlapping, a filter
	  cutting all frequency components higher than the Nyquist
	  frequency would give back a signal that is affected by
	  aliasing.
	</para>
	<para id="resampling2D">
	  We note how aliasing can be produced by down-sampling (or
	  decimating) a sampled image. Starting from a discrete-space
	  image, we can select only a subset of samples arranged in a
	  regular grid. This will determine the periodic repetition of
	  the spectral images, that will end up overlapping.
	</para>
	<para id="applet_samplingp">
	  In order to explore the concepts of sampling, down-sampling,
	  and aliasing, run the <link src="resampling_ellipse.html">applet drawing ellipses
	  </link>. With the keyboard arrow you can double or halve the
	  horizontal and vertical sampling steps.
	</para>
      </section>

      <para id="image_proc_basics">A simple introduction to the first
elements of image processing is found in <cnxn document="m10973">
Digital Image Processing Basics</cnxn>. </para>
    </section>
    <section id="quantizzazione">
      <name>Quantization</name>
      <para id="quantizzazionep">
	With the adjective "digital" we indicate those systems that
	work on signals that are represented by numbers, with the
	(finite) precision that computing systems allow. Up to now we
	have considered discrete-time and discrete-space signals as if
	they were collections of infinite-precision numbers, or real
	numbers. Unfortunately, computers only allow to represent
	finite subsets of rational numbers. This means that our
	signals are subject to quantization.
      </para>
      <para id="quantlinp">
	For our purposes, the most interesting quantization is the
	  linear one, which is usually occurring in the process of
	  conversion of an analog signal into the digital domain. If
	  the memory word dedicated to storing a number is made of
	  <m:math> <m:ci>b</m:ci> </m:math> bits, then the range of
	  such number is discretized into
	  <m:math>
	  <m:apply>
	    <m:power/>
	    <m:mn>2</m:mn>
	    <m:ci>b</m:ci>
	  </m:apply>
	</m:math> quantization levels. Any value that is found between
	  two quantization levels can be approximated by truncation or
	  rounding to the closest value. The <cnxn target="ottoquanti"/> shows an example of quantization with
	  representation on <m:math> <m:cn>3</m:cn> </m:math> bits in
	  <cnxn document="m10808"> two's complement</cnxn>.
      </para>
	<figure id="ottoquanti">
	  <name>Sampling and quantization of an analog signal</name>
	  <media type="image/png" src="quant.png">
	  </media>
	</figure>
      <para id="quanterrorp">
	The approximation introduced by quantization manifests itself
	as a noise, called <term>quantization noise</term>. Often, for
	the analysis of sound-processing circuits, such noise is
	assumed to be white and de-correlated with the signal, but in reality
	it is perceptually tied to the signal itself, in such an
	extent that quantization can be perceived as an effect.
      </para>
      <para id="applet_quant">
	To have a visual and intuitive exploration of the phenomenon
	of quantization, consider the <link src="quantagondole.html">
	applet </link> that allows to vary between <m:math>
	<m:cn>1</m:cn> </m:math> and <m:math> <m:cn>8</m:cn> </m:math>
	the number of bits dedicated to the representation of each of
	the RGB channels representing color. The same number of bits
	is dedicated to the representation of an audio signal coupled
	to the image. The visual effect that is obtained by reducing
	the number of bits is similar to a <term>solarization</term>.
	</para>
    </section>
    <exercise id="estensioni_aliasing">
      <problem>
	<para id="estensionip">
	  Extend the code of the applet <cnxn target="aliasingTab"/> to add some interaction features:
	  <list id="estensionil">
	    <item>Make the fundamental frequency of the
	    automatically-generated sound changing randomly at each
	    frame (see <link src="http://www.processing.org/reference/random_.html"><code>random()</code></link>).</item>

	    <item>Make the framerate (and the metronome for generating
	    notes) dependent on the horizontal position of the mouse
	    (<code>mouseX</code>). </item>

	    <item>Make the number of harmonics of the sound (i.e., its
	    brightness) dependent on the vertical position of the
	    mouse (<code>mouseY</code>).</item>

	    <item>Paint the window background in such a way that
	    moving from left to right the tint changes from blue to
	    red. In this way, a blue color will correspond to a slow
	    tempo, and a red color to a fast tempo.</item>

	    <item>Make the color saturation of the background
	    dependent on the vertical position of the mouse. In this
	    way a sound with a few harmonics (low brightness) will
	    correspond to a grayish color, while a sound rich of
	    harmonics (high brightness) will correspond to a saturated
	    color. </item>

	    <item>Add a control to stop the computation and display of
	    the spectrum and create an effect of image freezing, while
	    sound continues to be generated (for instance by keeping
	    the mouse button pressed).</item>

	    <item>Add a control to cancel the dependence of tempo,
	    brightness, and color saturation on mouse position (for
	    instance by pressing a key). </item>

	    <item>Add a control that, in case of image freezing (mouse
	    button pressed), will stop the generation of new notes
	    while "freezing" the current note. </item>
 
	    <item>Finally, add a control that, in case of image
	    freezing, will stop the sound generation and make the
	    application silent.</item>
	  </list>
	</para>
      </problem>
      <solution>
	<para id="estensionis">
	  The proposed extensions are implemented in the  <link src="./aliasingFermoSemplice.pde">Processing code</link>.
	</para>
      </solution>
    </exercise>
  </content>
  
<bib:file>
<bib:entry id="rocISP">
<bib:book>
<bib:author>Davide Rocchesso</bib:author> <bib:title>Introduction to
Sound Processing</bib:title> <bib:publisher>Mondo
Estremo</bib:publisher> <bib:year>2003</bib:year>
<bib:note>http://www.mondo-estremo.com</bib:note>
</bib:book>
</bib:entry>
</bib:file>

</document>
