Salta al contenuto
Note per Studenti Digital Representation of Audio and Image Signals Report

Digital Representation of Audio and Image Signals

In questa pagina 7

A microphone produces a voltage that varies continuously in time; a photographic sensor measures light at every point of the plane. To store or send these signals through a network they must be turned into sequences of bits. This note explains how it is done, what is lost and what is not, and how to go back. It is the basis of the following notes: how many bits per second a signal produces decides how much compression is needed (Source Coding and Compression - Entropy, Huffman and the Quality-Bitrate Trade-offThe information of an event of probability p is log2(1/p) bits and the entropy H = sum p log2(1/p) is the average information of a source; lossless source coding maps source words to variable-length prefix codewords (shorter for likelier words), the Kraft-McMillan inequality constrains the lengths, Shannon's theorem gives H/log2(My) <= L <= H/log2(My) + 1, Huffman coding is optimal among prefix codes; lossy compression gives up exactness to reach a lower rate, and rate, quality, complexity, delay and robustness are conflicting parameters.Source Coding and Compression - Entropy, Huffman and the Quality-Bitrate Trade-off →, Image Coding - DCT and JPEGQuantizing pixels directly is inefficient because they are not sparse, so JPEG applies an orthogonal linear transform on 8x8 blocks (the 2-D DCT, Y = A X A^T) that concentrates energy in few coefficients, quantizes them with a table of steps scaled by a quality factor Q, scans them in zig-zag order, codes the DC coefficient differentially and the AC coefficients as (run, category) pairs with Huffman codes; one worked block shrinks from 512 bits to 49 bits (0.766 bit/pixel) and higher Q gives more bits and higher PSNR.Image Coding - DCT and JPEG →), and the effect of quantization is quantified in Quantization Noise and SNRA uniform mid-riser quantizer with L = 2^b levels over [-Vsat, Vsat] has step Delta = 2 Vsat / L; the error e = aq - a is granular (at most Delta/2, roughly uniform, power Delta^2/12) or saturation (outside the range), and with negligible saturation the SNR is [SNR]dB = 6.02 b + 4.77 + 20 log10(sigma / Vsat), so every extra bit gives 6 dB; Vsat follows from the saturation probability and b from the required SNR rounded up; with bit errors on the channel the PCM SNR becomes SNR_q / (1 + 4 P_bit (4^b - 1)).Quantization Noise and SNR →.

1. Signal types and the transmission chain

Three kinds of signal are distinguished, according to whether the domain (the "when") and the codomain (the "how much") are continuous or discrete.

Type Domain Codomain Model
analog continuous time t∈Rt\in\mathbb R continuous amplitude s:R→Rs:\mathbb R\to\mathbb R
sampled discrete time n∈Zn\in\mathbb Z continuous amplitude sc:Z→Rs_c:\mathbb Z\to\mathbb R
digital discrete time discrete amplitude (finite alphabet) sd:Z→Zs_d:\mathbb Z\to\mathbb Z

The definitions extend to several variables. A grey-scale analog image is I:(x,y)∈R2→RI:(x,y)\in\mathbb R^2\to\mathbb R; a digital one is I:(n,m)∈{1,…,N}×{1,…,M}→{0,1,…,2b−1}I:(n,m)\in\{1,\dots,N\}\times\{1,\dots,M\}\to\{0,1,\dots,2^b-1\}; in colour every pixel has three components, {0,…,2b−1}3\{0,\dots,2^b-1\}^3. A video adds the frame index. A computer represents and processes exactly only digital signals.

The analog-to-digital conversion (ADC) has three steps:

  1. sampling: the independent variable becomes discrete;
  2. quantization: the amplitude becomes discrete;
  3. coding (binarization): the quantized amplitude is written with bits.

The complete chain. s(t) →ADC sd(n) →COD bits →NET bits →DEC sd′(n) →DAC s′(t)s(t)\ \xrightarrow{\text{ADC}}\ s_d(n)\ \xrightarrow{\text{COD}}\ \text{bits}\ \xrightarrow{\text{NET}}\ \text{bits}\ \xrightarrow{\text{DEC}}\ s'_d(n)\ \xrightarrow{\text{DAC}}\ s'(t).

  • ADC: sampling and quantization.
  • COD (source encoder): turns the sequence into bits and can compress, even introducing distortion.
  • NET: the network carries the bits to the destination (it may introduce errors and losses).
  • DEC: decodes the bit stream into the quantized samples.
  • DAC (digital-to-analog conversion): rebuilds a signal usable by a person.

Quality can degrade in every block: filter and sampler (if badly chosen), quantization, lossy coding, network, non-ideal DAC. The converter itself consists of four blocks: an anti-aliasing filter (a low-pass filter), the sampler, the quantizer and the inverse bit mapper followed by a parallel-to-serial converter that puts the words in a single binary stream. At the receiver the opposite path is followed: serial-to-parallel conversion, bit mapping back to levels, and an interpolator that rebuilds the continuous signal.

2. Sampling

Definition (sampling). The signal is read at regular intervals of duration TcT_c (sampling period): sc(n)=s(nTc)s_c(n)=s(nT_c), n∈Zn\in\mathbb Z. The sampling frequency is Fc=1TcF_c=\frac1{T_c}.

Does sampling lose information? Between one sample and the next the signal could do anything. The answer of Shannon's theorem is surprising: no, if the signal does not vary too fast with respect to FcF_c. "Too fast" is made precise with the spectrum (see Sampling and the Sampling TheoremSampling multiplies a continuous-time signal by a pulse train, which replicates its spectrum every omega_s; if the signal is band-limited with maximum angular frequency omega_M and omega_s exceeds 2 omega_M, an ideal low-pass filter recovers it exactly (Shannon theorem and sinc interpolation formula).Sampling and the Sampling Theorem → for the proof).

A signal is band-limited if its spectrum is zero beyond a maximum frequency fMf_M. Band-limited signals are "all and only" those that can be perfectly rebuilt from their samples.

Sampling theorem (Shannon). A band-limited signal s(t)s(t) with maximum frequency fMf_M can be rebuilt with zero error from the samples sc(n)=s(nTc)s_c(n)=s(nT_c) if and only if the sampling frequency is at least twice the maximum frequency: Fc≥2fM(Nyquist criterion).\boxed{F_c\ge2f_M}\qquad\text{(Nyquist criterion).} When it holds, the ideal interpolation formula returns the signal: s(t)=∑n∈Zs(nTc) sinc ⁣(t−nTcTc).s(t)=\sum_{n\in\mathbb Z}s(nT_c)\,\mathrm{sinc}\!\left(\frac{t-nT_c}{T_c}\right).

Reading: a sinc\mathrm{sinc} is placed at each sampling instant nTcnT_c, stretched horizontally so that its zeros fall at distance TcT_c, and scaled in height by the sample value. At the instant kTckT_c all the sinc functions are zero except the one centred at kk, which equals s(kTc)s(kT_c).

Why Fc≥2fMF_c\ge2f_M: spectrum replicas. Sampling in time is equivalent to periodically replicating the spectrum with period FcF_c: the spectrum of the sampled signal is the sum of the copies S(f−kFc)S(f-kF_c), k∈Zk\in\mathbb Z. The original spectrum occupies [−fM,fM][-f_M,f_M], the copy k=1k=1 occupies [Fc−fM,Fc+fM][F_c-f_M,F_c+f_M]. They do not overlap if Fc−fM≥fMF_c-f_M\ge f_M, i.e. exactly Fc≥2fMF_c\ge2f_M. In that case a low-pass filter selecting the copy centred at zero gives back the original spectrum (ideal interpolation is exactly this filter). The quantity 2fM2f_M is the Nyquist frequency. If Fc<2fMF_c<2f_M the copies overlap and cannot be separated.

2.1 Aliasing

Definition (aliasing). Aliasing is the frequency shift of a signal rebuilt from samples taken with an FcF_c that does not respect the Nyquist criterion: high-frequency components appear, after reconstruction, as lower-frequency components.

A pure tone at frequency ff sampled at FcF_c is rebuilt as a tone at the frequency

fapparent=∣f−Fc⋅round(f/Fc)∣,f_{\text{apparent}}=\bigl|f-F_c\cdot\mathrm{round}(f/F_c)\bigr|,

the distance of ff from the nearest multiple of FcF_c. Intuition: to see a signal oscillate at least two samples per period are needed; if there are fewer, the samples are also compatible with a slower oscillation.

Example. Fc=8F_c=8 kHz. A 3 kHz tone (<Fc/2=4<F_c/2=4 kHz) is rebuilt correctly. A 5 kHz tone becomes ∣5−8∣=3|5-8|=3 kHz: it is confused with the 3 kHz one. A 7 kHz tone becomes 11 kHz; a 9 kHz tone becomes 11 kHz. Another way to see it: a 6 kHz tone sampled at 8 kHz has samples cos⁡(2π68n)=cos⁡(2π28n)\cos\left(2\pi\frac68n\right)=\cos\left(2\pi\frac28n\right), identical to those of a 2 kHz tone. For a signal of bandwidth B=3B=3 kHz, among Fs=1F_s=1, 22 and 88 kHz the only valid choice is 88 kHz (≥2B=6\ge2B=6 kHz).

2.2 In practice: the anti-aliasing filter

No real signal is strictly band-limited: a signal of finite duration has, strictly, unlimited bandwidth. But all interesting signals concentrate most of their information in a limited band, so high frequencies can be removed with a low-pass filter without significant distortion. For this reason every A/D converter has, before the sampler, a low-pass filter with cut-off Fc/2F_c/2 (see Aliasing and Anti-Aliasing FiltersAliasing is the overlap of spectral replicas that occurs when the band-limited or Nyquist assumptions of the sampling theorem fail; an anti-aliasing low-pass filter with cut-off omega_s/2 applied before sampling removes it at the price of losing the high frequencies; two worked exercises.Aliasing and Anti-Aliasing Filters →): it enforces the Nyquist criterion by removing frequencies above Fc/2F_c/2. Without the filter, high components would create aliasing inside the useful band and could no longer be removed: once sampled, an alias cannot be told apart from a true signal. The error committed by the filter is the energy of the discarded part of the spectrum, Me=2∫B∞Ex(f) dfM_e=2\int_B^\infty\mathcal E_x(f)\,df. The same rule holds for subsampling an already digital signal: filter, then decimate.

If the anti-aliasing filter has cut-off f0f_0 and the sampler works at FcF_c, aliasing is avoided if Fc≥2f0F_c\ge2f_0. With Fc=10F_c=10 kHz: f0≤5f_0\le5 kHz.

The practical choice of FcF_c starts from the band of the signal and takes a value slightly above 2fmax⁡2f_{\max}: 8 kHz for speech (band 200-3400 Hz, filtered at 4 kHz), 44.1 kHz for music (useful band up to 20 kHz). The margin is needed because a real filter does not have a vertical cut.

2.3 Interpolation at the receiver

The ideal interpolation uses sinc functions, which have infinite support and cannot be built (the ideal filter Tsrect⁡f2BT_s\operatorname{rect}\frac f{2B} has a sinc impulse response, of infinite duration and non-causal). In practice other kernels hh are used with the formula

s~(t)=∑n∈Zs(nTc) h ⁣(t−nTcTc).\tilde s(t)=\sum_{n\in\mathbb Z}s(nT_c)\,h\!\left(\frac{t-nT_c}{T_c}\right).

The kernel is scaled (dividing the variable by TcT_c) and shifted (t→t−nTct\to t-nT_c). Constraints: finite support, to be realisable; and h(0)=1h(0)=1, h(n)=0h(n)=0 for the other integers nn, so that interpolation preserves the values at the samples.

Kernel Expression Quality Cost Typical use
order 0, sample and hold h0(t)=rect(t−12)h_0(t)=\mathrm{rect}(t-\tfrac12) lowest; stepped signal, spurious high frequencies very low acceptable if FcF_c is very high
order 1, linear h1(t)=max⁡(0,1−∣t∣)h_1(t)=\max(0,1-\lvert t\rvert) (triangle) intermediate low when something better than S&H is needed
order 3, cubic (spline) degree-3 polynomials excellent for images medium-high image resampling
truncated sinc\mathrm{sinc} h(t)=sinc(t) rect(t/T)h(t)=\mathrm{sinc}(t)\,\mathrm{rect}(t/T) highest highest sound cards, near the Nyquist limit

The zero-order holder has frequency response G(f)=Tssinc⁡(fFs)e−jπfTsG(f)=T_s\operatorname{sinc}\left(\frac f{F_s}\right)e^{-j\pi fT_s}: the sinc is rounded (not flat) and distorts the frequencies near Fs/2F_s/2. It is compensated by oversampling: sampling at FsF_s well above 2B2B, so that in the band [0,B][0,B] the filter is almost flat (with Fs=2.5BF_s=2.5B one loses 24% at f=Bf=B, with Fs=8BF_s=8B only 2.6%). If FcF_c is high enough the simple interpolators (S&H, linear) suffice; if FcF_c is near the Nyquist frequency the truncated sinc is needed.

3. Quantization and binarization

After sampling the values are still real numbers. Quantization maps them onto a discrete set, the set of quantization levels. If there are LL levels, every sample is represented with b=log⁡2Lb=\log_2L bits: more levels mean more precision but more bits.

The most common type is the uniform quantizer: the interval (−A,A)(-A,A) of possible values is divided into LL equal intervals of width

Δ=2AL(quantization step).\boxed{\Delta=\frac{2A}{L}}\qquad(\text{quantization step}).

Two variants exist: mid-tread, Q(x)=Δ round(x/Δ)Q(x)=\Delta\,\mathrm{round}(x/\Delta), in which zero is a level (useful when the signal has many values near zero), and mid-rise (mid-riser), Q(x)=Δ(⌊x/Δ⌋+12)Q(x)=\Delta\left(\lfloor x/\Delta\rfloor+\frac12\right), in which zero is a threshold between two levels and the levels are the centres of the intervals. The maximum error inside the covered interval is Δ2\frac\Delta2. The detailed error statistics are in Quantization Noise and SNRA uniform mid-riser quantizer with L = 2^b levels over [-Vsat, Vsat] has step Delta = 2 Vsat / L; the error e = aq - a is granular (at most Delta/2, roughly uniform, power Delta^2/12) or saturation (outside the range), and with negligible saturation the SNR is [SNR]dB = 6.02 b + 4.77 + 20 log10(sigma / Vsat), so every extra bit gives 6 dB; Vsat follows from the saturation probability and b from the required SNR rounded up; with bit errors on the channel the PCM SNR becomes SNR_q / (1 + 4 P_bit (4^b - 1)).Quantization Noise and SNR →.

Quantization is irreversible: unlike sampling (which under Nyquist loses nothing), it always loses information, since a whole interval of values ends up in the same level. For this reason the quantization of the ADC is very fine, so that the loss is not perceptible. Typical experimentally found values:

  • music: 16 bits per sample (216=65 5362^{16}=65\,536 levels) for perfect perception; for speech 8 bits are enough;
  • grey-scale images: 8 bits (256 grey levels);
  • colour images: 8 bits for each of the three RGB channels, i.e. 24 bits per pixel (224≈162^{24}\approx16 million colours).

Two quantizations in a real system. (1) The one inside the ADC, very fine with negligible distortion. (2) The one inside the source encoder, i.e. of the compression algorithm: this one introduces the evident distortion. It should be called requantization, because it acts on already quantized data.

Binarization. The inverse bit mapper transforms each level into the corresponding string of mm bits (usually the binary representation of the index of the interval); a parallel-to-serial converter serialises the bits into a single stream. Example (5 levels, 3 bits): the levels −1,−0.5,0,0.5,1-1,-0.5,0,0.5,1 have codes 000,001,010,011,100000,001,010,011,100; the quantized signal 0,−0.5,−1,−0.5,0,…0,-0.5,-1,-0.5,0,\dots gives the stream 010 001 000 001 010…010\ 001\ 000\ 001\ 010\dots The index is transmitted, not the value; the bit mapper at the receiver gives back the values.

Bit-rate. One sample every TcT_c seconds and bb bits per sample give one bit every Tb=TcbT_b=\frac{T_c}{b} seconds, i.e.

Rb=Fc⋅log⁡2L=b Fc bit/s.\boxed{R_b=F_c\cdot\log_2L=b\,F_c\ \text{bit/s}.}

(TcT_c is bb times the bit time, not equal to it.) This is the nominal bit-rate, without source coding. For any uncompressed signal:

bit-rate=(samples per second)×(bits per sample)×(number of channels or components).\text{bit-rate}=(\text{samples per second})\times(\text{bits per sample})\times(\text{number of channels or components}).

Do not compute the bit-rate with LL in place of b=log⁡2Lb=\log_2L.

4. Audio

4.1 How we perceive sound

The human auditory system performs a frequency analysis of the incoming sounds: it can be modelled as a bank of filters H1(f),…,HN(f)H_1(f),\dots,H_N(f), each analysing a band. Two phenomena matter for compression.

  • Hearing threshold. There is a minimum power below which a pure tone of frequency ff is not heard; it varies from person to person and with age. Taking the 1 kHz tone as reference, the threshold as a function of frequency is very high at the extremes of the audible spectrum and lower in the 1-5 kHz region (bowl-shaped). Sounds under the curve are useless to encode.
  • Frequency masking. A strong tone raises the threshold for nearby frequencies: a weak tone next to a strong one is not heard. The threshold-raising function decreases with frequency distance but is not symmetric: it falls more slowly for higher frequencies. A tone masks higher frequencies more than lower ones.
  • Temporal masking. A strong tone also masks in time: a little before (pre-masking, 2-5 ms) and much after (post-masking, 100-200 ms).

Perceptual coding. The signal can be quantized so that the generated quantization noise falls below the masking threshold and is therefore not heard. This is done by shaping the noise spectrum, or with a time-frequency analysis that coarsely quantizes the masked components. It is the principle of MP3 and AAC.

4.2 Speech: PCM at 64 kbit/s

The power of the voice signal is concentrated in the band [200,3400][200,3400] Hz. Speech is acquired with a low-pass filter at fcut=4f_{\text{cut}}=4 kHz and sampling at Fc≥8F_c\ge8 kHz. The number of levels was found experimentally. Two common choices: 128 levels, i.e. 7 bits per sample (Asia and America): R=8000×7=56R=8000\times7=56 kbit/s; 256 levels, i.e. 8 bits (rest of the world): R=8000×8=64 kbit/sR=8000\times8=\mathbf{64\ kbit/s}. This scheme is called PCM (pulse code modulation), although strictly it is a digitization scheme and not a modulation.

Packetisation. A packet typically corresponds to 20 ms of voice: 20⋅10−3×8000=16020\cdot10^{-3}\times8000=160 samples, i.e. 160160 bytes with 8 bits (140140 bytes with 7 bits). The rate is therefore 50 packets per second.

4.3 Music: the audio CD

For music the parameters that make distortion imperceptible are: useful band ≈[15,20 000]\approx[15,20\,000] Hz; two channels (stereo); Fc=44.1F_c=44.1 kHz with anti-aliasing filter (slightly more than 2×20=402\times20=40 kHz); 16 bits per sample, i.e. 65 53665\,536 levels.

R=44 100×16×2=1 411 200 bit/s≈1.411 Mbit/s.R=44\,100\times16\times2=1\,411\,200\ \text{bit/s}\approx\boxed{1.411\ \text{Mbit/s}}.

A single channel (mono) needs 705.6705.6 kbit/s.

Example (capacity of a CD). The stream written on the disc contains 87% useful data and 13% is error protection code. The disc holds 730730 MB of useful data. Total bit-rate: RT=1.4110.87≈1.622R_T=\dfrac{1.411}{0.87}\approx1.622 Mbit/s (useful data are 87% of the total, so total = useful / 0.87). Maximum duration: T=730×8 Mbit1.4112 Mbit/s≈4138T=\dfrac{730\times8\ \text{Mbit}}{1.4112\ \text{Mbit/s}}\approx4138 s ≈69\approx69 minutes (the data rate of the useful data only is used, since the 730 MB are useful).

Signal FcF_c levels bits/sample channels data rate
Speech 8 kHz 256 8 1 64 kbit/s
Music 44.1 kHz 65 536 16 2 1.411 Mbit/s

File size: bytes=bit-rate×duration8\text{bytes}=\dfrac{\text{bit-rate}\times\text{duration}}{8}. Example: 128 kbit/s for 10 minutes: 128×10×60=76 800128\times10\times60=76\,800 kbit =9.6=9.6 MB. Bits per sample with LL levels: ⌈log⁡2L⌉\lceil\log_2L\rceil (2048 levels →\to 11 bits). A signal of band 20 kHz sampled at the minimum frequency (4040 kHz) and quantized on 2048 levels produces 40 000×11=44040\,000\times11=440 kbit/s.

5. Images

5.1 Acquisition and quantization

Light from the scene crosses the optics and hits the sensor on the image plane. Each pixel of the sensor measures the light hitting its surface: a filtering and a two-dimensional spatial sampling are performed. Quality grows with the number of pixels (the first digital cameras had about 1 Mpixel; today over 100 Mpixel).

Grey-scale images use 1 byte per pixel: 256 grey levels, practically indistinguishable from a non-quantized version. But 256 brightness levels may not be enough if the image contains areas much darker (underexposed) or brighter (overexposed) than the rest: the "true" values of those areas end at 0 or 255 and detail is lost. The solution is more bits per pixel: HDR (high dynamic range) formats use 10 or 12 bits per sample.

5.2 Colour: RGB

The eye perceives colour thanks to the cones of the retina: there are three types, each with a different response as a function of wavelength. A colour can be obtained by combining three primary colours: red, green, blue. The RGB space represents the colour of a pixel with three intensities. Each channel has bb bits; with b=8b=8 there are (28)3≈16⋅106(2^8)^3\approx16\cdot10^6 colours (24 bits/pixel); with HDR at 10 or 12 bits, 36 bits/pixel.

Uncompressed sizes. An H×WH\times W RGB image at 8 bits/channel takes 3 HW3\,HW bytes. HD: 1080×1920×3×8≈49.81080\times1920\times3\times8\approx49.8 Mbit ≈6.22\approx6.22 MB. 1000×10001000\times1000 pixels RGB: 2424 Mbit.

5.3 YCbCr and chroma subsampling

The three RGB channels are strongly correlated: the three images R, G, B are almost equal. The YCbCr space is obtained by rotating the axes so that the diagonal of the colour cube (the grey line R=G=BR=G=B) becomes one axis, followed by an offset keeping values in [0,255][0,255]: YY is the luminance, CbC_b and CrC_r are the chrominances. In one of the versions in use (the one of JPEG files):

Y=0.299R+0.587G+0.114B,Cb=128−0.1687R−0.3313G+0.5B,Cr=128+0.5R−0.4187G−0.0813B.Y=0.299R+0.587G+0.114B,\quad C_b=128-0.1687R-0.3313G+0.5B,\quad C_r=128+0.5R-0.4187G-0.0813B.

A grey (R=G=BR=G=B) has Cb=Cr=128C_b=C_r=128. Example. The pixel (R,G,B)=(200,100,50)(R,G,B)=(200,100,50) gives Y=124.2Y=124.2, Cb=86.1C_b=86.1, Cr=182.1C_r=182.1.

The eye is much less sensitive to chrominance than to luminance, so three chrominance samples out of four can be discarded without visible degradation. Sampling schemes: 4:4:4 (all components at full resolution), 4:2:2 (CbC_b and CrC_r halved horizontally), 4:2:0 (CbC_b and CrC_r halved both horizontally and vertically: one chroma sample every 2×22\times2 pixels). In 4:2:0 a frame H×WH\times W has HW+14HW+14HW=1.5 HWHW+\frac14HW+\frac14HW=\boxed{1.5\,HW} samples: half of the RGB (3 HW3\,HW). Example: a 10 Mpixel image takes 3030 MB in RGB and 10+2.5+2.5=1510+2.5+2.5=15 MB in YCbCr 4:2:0. At the receiver the missing chroma samples are recreated by zero-order interpolation (replicating the value) before converting back to RGB.

5.4 Video

A video is a sequence of images, the frames, typically at 24, 30, 50, 60 fps. For uncompressed 4:2:0 video at bb bits per sample:

R=H⋅W⋅1.5⋅b⋅f bit/s.R=H\cdot W\cdot1.5\cdot b\cdot f\ \text{bit/s}.

Format Resolution fps Rate (8 bit, 4:2:0)
CIF 352×288352\times288 30 36 Mbit/s
HDTV 720p 1280×7201280\times720 50 553 Mbit/s
HDTV 1080p 1920×10801920\times1080 50 1244 Mbit/s
4K 4096×21604096\times2160 50 5308 Mbit/s

Check of one row: 1080p at 50 fps: 2 073 600×1.5×8×50=1 244 160 0002\,073\,600\times1.5\times8\times50=1\,244\,160\,000 bit/s. These rates are far too high for network and memory: compression is needed. For example a 4K video (3840×21603840\times2160, 30 fps, 4:2:0, 8 bit) is ≈2.99\approx2.99 Gbit/s; to send it on a 10 Mbit/s link a compression ratio of at least 2.99⋅109107≈300\frac{2.99\cdot10^9}{10^7}\approx300 is needed. This shows how traffic depends on the coding parameters: resolution, frame rate, bits per sample and chroma format all multiply in the bit-rate.

6. Measuring quality: MSE and PSNR

Measuring the quality of a signal gg with respect to the original ff is measuring the distortion between the two. With the error signal e(n)=f(n)−g(n)e(n)=f(n)-g(n), the mean squared error is

MSE(f,g)=1N∑n=0N−1(f(n)−g(n))2=1N∥f−g∥2,\mathrm{MSE}(f,g)=\frac1N\sum_{n=0}^{N-1}\bigl(f(n)-g(n)\bigr)^2=\frac1N\|\mathbf f-\mathbf g\|^2,

which, up to 1N\frac1N, is the squared Euclidean distance between the sample vectors. The PSNR (peak signal-to-noise ratio) expresses the same number in decibels:

PSNR=10log⁡10(2b−1)2MSE dB.\mathrm{PSNR}=10\log_{10}\frac{(2^b-1)^2}{\mathrm{MSE}}\ \text{dB}.

The numerator is the peak power of the signal; the denominator is the noise power. MSE is a measure of distortion (smaller is better), PSNR of quality (larger is better). For 8-bit grey-scale images PSNR≈48.13 dB−10log⁡10MSE\mathrm{PSNR}\approx48.13\ \text{dB}-10\log_{10}\mathrm{MSE}.

PSNR quality
≥45\ge45 dB excellent, indistinguishable from the original
40…4540\ldots45 dB very good, barely visible differences
35…4035\ldots40 dB good, visible differences
30…3530\ldots35 dB sufficient, clear differences
<30<30 dB poor

Example (requantizing an image from 8 to 4 bits). From 256 to 16 levels: the step becomes Δ=256/16=16\Delta=256/16=16 grey levels. The quantization error has power Δ212=25612≈21.3\frac{\Delta^2}{12}=\frac{256}{12}\approx21.3. Then PSNR≈48.13−10log⁡1021.3≈34.8\mathrm{PSNR}\approx48.13-10\log_{10}21.3\approx34.8 dB: "good-sufficient" quality, with visible differences. At 2 bits (Δ=64\Delta=64, MSE ≈341\approx341) PSNR drops to about 22.8 dB.

Limit of PSNR: it does not always reflect the quality perceived by a human observer (too simple a model: it ignores contrast sensitivity, colour perception, masking). It remains widely used for simplicity and ease of interpretation. The perceptual listening and viewing experiments that the program mentions (effect of sampling frequency, number of quantization bits and filtering on perceived quality) are not described by any certified source, so they are not written here; the sources only say that taking bits away, even a few, worsens quality quickly (audio at 16, 8, 4, 2 bits per sample, images at 256, 16, 4 levels), so enough bits are kept at acquisition.

Typical mistakes

  • Writing Fc≥fMF_c\ge f_M (it is 2fM2f_M); believing that Fc=2BF_c=2B exactly always suffices (it requires the ideal filter: in practice one samples a little above).
  • Saying that quantization is invertible.
  • Using Δ=A/L\Delta=A/L for the interval (−A,A)(-A,A) (it is 2A/L2A/L).
  • Forgetting the filter before subsampling.
  • Confusing index and quantized value; confusing TbT_b and TsT_s (Ts=b TbT_s=b\,T_b).
  • Reading PSNR as a "percentage" (it is in dB).

Versione ripasso

Chain. s(t)→s(t)\to ADC →sd(n)→\to s_d(n)\to COD →\to bits →\to NET →\to DEC →\to DAC →s′(t)\to s'(t). ADC = sampling + quantization (+ binarization). COD may compress (even with loss), NET may add errors and losses, DAC is not ideal: quality can degrade at every block. Analog = continuous time and amplitude; sampled = discrete time, continuous amplitude; digital = both discrete. Digital image: I:{1..N}×{1..M}→{0..2b−1}I:\{1..N\}\times\{1..M\}\to\{0..2^b-1\} (colour: three components).

Sampling. sc(n)=s(nTc)s_c(n)=s(nT_c), Fc=1/TcF_c=1/T_c.

Quantization. L=2bL=2^b levels. Δ=2AL\Delta=\frac{2A}{L} on (−A,A)(-A,A). Mid-tread Δ round(x/Δ)\Delta\,\mathrm{round}(x/\Delta) (zero is a level) or mid-rise Δ(⌊x/Δ⌋+12)\Delta(\lfloor x/\Delta\rfloor+\frac12). Maximum error Δ/2\Delta/2. Irreversible: ADC quantization is fine (16 bits audio, 8 bits speech, 8 bits per RGB channel = 24 bits/pixel); the encoder adds a second one (requantization), the evident distortion. Binarization: index →\to bb bits; Rb=FcbR_b=F_cb. Speech 8000×8=648000\times8=64 kbit/s; Tc=bTbT_c=bT_b.

Audio. Speech: band 200-3400 Hz, Fc=8F_c=8 kHz, 8 bits, 64 kbit/s (7 bits: 56 kbit/s); packet of 20 ms = 160 samples = 160 bytes. CD: 44.1 kHz, 16 bits, 2 channels: 1.411 Mbit/s; 730 MB gives 69 minutes (useful rate). File size =rate×time8=\frac{\text{rate}\times\text{time}}8. Band 20 kHz on 2048 levels: 440 kbit/s. Perception: hearing threshold, frequency masking (a tone masks higher frequencies more), temporal masking (pre 2-5 ms, post 100-200 ms); perceptual coding puts the noise under the masking threshold (MP3, AAC).

Images and video. Grey 8 bits; RGB 24 bits/pixel. YCbCr: Y=0.299R+0.587G+0.114BY=0.299R+0.587G+0.114B, CbC_b and CrC_r centred on 128; grey ⇒Cb=Cr=128\Rightarrow C_b=C_r=128; (200,100,50)→(124.2,86.1,182.1)(200,100,50)\to(124.2,86.1,182.1). The eye is less sensitive to chroma: 4:2:0 keeps 1 chroma sample per 2×22\times2 pixels, 1.5 HW1.5\,HW samples (half of RGB); chroma restored by zero-order interpolation. Video rate =HW⋅1.5⋅b⋅f=HW\cdot1.5\cdot b\cdot f: 1080p at 50 fps =1244=1244 Mbit/s; 4K 30 fps 8 bit =2.99=2.99 Gbit/s, compression at least 300 for a 10 Mbit/s link.

Quality. MSE=1N∑(f−g)2\mathrm{MSE}=\frac1N\sum(f-g)^2; PSNR=10log⁡10(2b−1)2MSE\mathrm{PSNR}=10\log_{10}\frac{(2^b-1)^2}{\mathrm{MSE}}; 8 bits: ≈48.13−10log⁡10MSE\approx48.13-10\log_{10}\mathrm{MSE}. Table: ≥45\ge45 excellent, 40-45 very good, 35-40 good, 30-35 sufficient, below 30 poor. Example: 8 to 4 bits: Δ=16\Delta=16, MSE =21.3=21.3, PSNR =34.8=34.8 dB; 2 bits: 22.8 dB. PSNR ignores contrast, colour and masking. Perceptual experiments have no certified source and are not written.

Typical mistakes: Fc≥fMF_c\ge f_M; Fc=2BF_c=2B assumed enough; invertible quantization; Δ=A/L\Delta=A/L; no filter before subsampling; PSNR as a percentage; LL instead of bb in the bit-rate.

More reminders.

  • Converter blocks: anti-aliasing filter, sampler, quantizer, inverse bit mapper, P/S; receiver: S/P, bit mapper, interpolator. The ideal interpolator Tsrect⁡f2BT_s\operatorname{rect}\frac f{2B} has a sinc response: infinite and non-causal, hence not realisable.
  • Replicas: the spectrum of the sampled signal is ∑kS(f−kFc)\sum_kS(f-kF_c); the filter error is the energy of the discarded part, 2∫B∞Ex(f) df2\int_B^\infty\mathcal E_x(f)\,df.
  • Holder: G(f)=Tssinc⁡(f/Fs)e−jπfTsG(f)=T_s\operatorname{sinc}(f/F_s)e^{-j\pi fT_s}; at f=Bf=B it loses 24% with Fs=2.5BF_s=2.5B and 2.6% with Fs=8BF_s=8B: oversample.
  • Binarization example: levels −1,−0.5,0,0.5,1-1,-0.5,0,0.5,1 with codes 000,001,010,011,100000,001,010,011,100; the signal 0,−0.5,−1,−0.5,00,-0.5,-1,-0.5,0 becomes 010 001 000 001 010010\,001\,000\,001\,010. The index is sent, not the value.
  • Speech packet: 20 ms =160=160 samples =160=160 bytes (140 bytes at 7 bits), 50 packets/s.
  • CD capacity: useful data 87%, 730 MB: total rate 1.4110.87=1.622\frac{1.411}{0.87}=1.622 Mbit/s, duration 730×81.4112≈4138\frac{730\times8}{1.4112}\approx4138 s ≈69\approx69 min.
  • Sizes: HD RGB 1080×1920×3=6.221080\times1920\times3=6.22 MB; 1000×10001000\times1000 RGB =24=24 Mbit; 10 Mpixel: RGB 30 MB, 4:2:0 15 MB. 4:4:4 = all components, 4:2:2 = chroma halved horizontally, 4:2:0 = halved both ways.
  • Video rows: CIF 30 fps 36 Mbit/s; 720p 50 fps 553 Mbit/s; 1080p 50 fps 1244 Mbit/s; 4K 50 fps 5308 Mbit/s; the rate is the product H⋅W⋅1.5⋅b⋅fH\cdot W\cdot1.5\cdot b\cdot f.
  • Quantization levels in practice: music 16 bits, speech 8 bits, grey image 8 bits, RGB 24 bits/pixel; two quantizations (ADC fine, encoder requantization).
  • Mid-rise vs mid-tread: mid-rise has zero as a threshold, mid-tread has zero as a level.
  • Interpolation choice: high FcF_c means S&H or linear is enough; near Nyquist the truncated sinc is needed; with a larger support TT the interpolation is better but complexity and delay grow.
  • Subsampling a digital signal: filter first, then keep one sample out of MM (Fnew=Fc/MF_{\text{new}}=F_c/M).
  • Speech parameters: filtered at 4 kHz, sampled at 8 kHz; 128 levels (7 bits) in Asia and America, 256 levels (8 bits) elsewhere.
  • HDR: 10 or 12 bits per sample when 256 levels lose detail in very dark or very bright areas; 36 bits/pixel in RGB.

Teoria collegata