<<

IWKS 2300 Data Representation and Boolean Arithmetic Fall 2019 John K. Bennett Data Representation

 Almost anything can be represented digitally:

 Numbers, and anything that can be expressed numerically

 Sounds, Audio, etc.

 Pictures, Video, etc. Digitizing an Analog Value

An Analog Signal ... Frequency and Amplitude

Different Analog Signals Digital Representation of Analog Signals • We sample the amplitude at time(i). • Record x,y pairs: time(i), amplitude. • The smaller the time increment, the more faithfully we represent the analog wave form. • We can also increase A/D converter precision. • It’s just numbers ... How Fast Must We Sample?

Nyquist Sampling Theorem: We must sample at least twice the highest frequency of interest. Example: An Audio Compact Disc

 44,100 samples per second  Each sample is a 16-bit number  2 channels (L+R)

=>176,400 bytes per second

72 minutes capacity, => 762MB data A Brief Sidequest Into Human Perception of Color • Receptors (cones) for med and high- ** • Sensitivity peaks in Short (blue), Middle (green), Long (red) wavelengths. • Thus, three parameters, corresponding to levels of stimulus to the three types of , can in principle describe any color sensation.

**Also low-brightness, monochromatic "night-vision" rods (peak sensitivity around 490–495 nm). Human Perception of Emitted Light • We do not usually refer to using the LMS space defined by the cone spectral sensitivities. Instead, we use these tristimulus values conceptualized as various amounts of three primary colors in an additive • Most wavelengths of light will not stimulate only one type of cone cell, because the sensitivity curves of the three cone cells overlap. To have a single component that describes the perceived brightness, “imaginary” primary colors and corresponding color-matching functions were formulated. The resulting tristimulus values are defined by the CIE 1931 , in which they are denoted X, Y, and Z. • The CIE XYZ color space is intended to encompass all color sensations that an “average” person can experience. The Visible Color : CIE 1931

The CIE 1931 XYZ color space was created by the International Commission on Illumination (CIE) in 1931. It was derived from a series of experiments done in the late 1920s by William David Wright and John Guild. Their experimental results were combined into the specification of the CIE RGB color space, from which the CIE XYZ color space was derived. How To Interpret a Gamut The Meaning of X, Y, and Z • When judging the relative (brightness) of different colors in well-lit situations, humans tend to perceive light within the green parts of the spectrum as brighter than red or blue light of equal power. The luminosity function that describes the perceived brightness of different wavelengths is thus roughly analogous to the frequency response of M cones. • The CIE model capitalizes on this fact by defining Y as luminance (or “brightness”). Z is roughly equal to blue stimulation, or the S cone response, and X is a mix (a linear combination) of cone response curves chosen to be nonnegative. The XYZ tristimulus values are thus analogous to, but not equal to, the LMS cone responses of the . Defining Y as luminance has the useful result in that for any given Y value, the XZ plane will contain all possible at that luminance. We Can Only Approximate the Human Gamut What’s Wrong With CIE 1931 • An equal mixture of two equally bright colors will not generally lie on the midpoint of that line segment. Thus, a given distance on the xy diagram does not correspond to the degree of difference between two colors. • In the early 1940s, David MacAdam studied the nature of visual sensitivity to color differences, and summarized his results in the concept of a MacAdam ellipse. • Based on the work of MacAdam, the CIE 1960, CIE 1964, and CIE 1976 color spaces were developed, with the goal of achieving perceptual uniformity (have an equal distance in the color space correspond to equal differences in perceived color). MacAdam Ellipses MacAdam compared perception of color differences at 25 points on the chromaticity diagram. He found that the size and orientation of the ellipses of equivalent perception on the diagram varied widely depending on the test color. These 25 ellipses measured by MacAdam, for a particular observer are shown here. How to Avoid These Distortions? • A number of attempts have been made to define a color space that is not as distorted as the CIE XYZ space. The most notable of these are the CIELUV and CIELAB color spaces. Although both of these spaces are less distorted than the CIE XYZ space, they are not completely free of distortion. This means that the MacAdam ellipses become nearly (but not exactly) circular in these spaces. • The CIE 1976 (L*, u*, v*) color space, commonly known by its abbreviation CIELUV, is a simple-to- compute transformation of the 1931 CIE XYZ color space that attempts perceptual uniformity. The CIE 1936 and 1976 LUV Emitted (Additive) Color Mixing Combine Perceived As Red + Green Red + Blue Magenta Green + Blue Cyan Red + Green + Blue White Reflected (Substractive) Color We do not perceive only emitted light. Most light that we perceive is reflected off of objects. When light is reflected, what we actually perceive is the light that is not absorbed, i.e., if all colors of light except those of the red wavelengths are absorbed, we perceive a red object. Subtractive Color Mixing When the source of color is or dye, the result of combining colors is different from when the source of color is light. Subtractive color mixing occurs when light is reflected off a surface or is filtered through a translucent object. For example, a red pigment or filter only appears red because it absorbs (subtracts) all of the light that is not red and only reflects or allows the red light. Similarly, a green pigment only reflects green light and a green filter only passes green light.

Combine Absorbs Leaves Cyan + Magenta Red + Green Blue Cyan + Yellow Red + Blue Green Magenta + Yellow Green + Blue Red Cyan + Magenta + Yellow Red + Green + Blue Black CMYK Colors With reflected (subtractive) color we are basically mixing to absorb different colors of light rather than mixing light colors. The black created by mixing Cyan, Magenta and Yellow is not generally satisfactory, so we usually add an actual black. It’s the Same ; We are just mixing it differently

… End of color sidequest RGB Color Model Color is expressed as an RGB value – three numbers that indicate the relative contribution of each of these primary colors. Digital Color Representation We need to make choices about: • Range of values (minimum, maximum) • Number of steps between min and max

This equates to choosing the number of pixels in x and y (e.g., 1024x768), and the number of bits for each color (typically in the range 2-8 bits per color, plus the same number for alpha).

Example: 8 bits each for R,G, & B, + 8 bits for alpha (transparency) = 32 bits/pixel Raster/Bitmapped Graphics • Storage of data is on a pixel-by-pixel basis Common formats include: Bitmap (BMP), GIF, JPEG, and PNG • Each pixel in a BMP image is just its RGB values • GIF, JPG, and PNG images use compression • How much data is for a BMP image? Typical image size might be 1024 by 768 pixels (= 786,432 pixels) 786,432 pixels * 4 bytes per pixel = 3,145,728 bytes (for one picture) • A 4096 x 2304 24 bpp picture would be 28,311,552 bytes. • A 60 minute 37 fps movie this size would be 3,771,098,726,400 bytes (before compression) Digital Images What Are the Advantages of Digital Representation?

• Noise immunity in storage and transmission

• Ease of storage/manipulation/transformation

• Uniformity -- It's all just bits! So How Do We Represent Numbers in Binary?

quantity decimal binary 3-bit register

0 0 000

 1 1 001

 2 10 010

 3 11 011

 4 100 100

 5 101 101

 6 110 110

 7 111 111

 8 1000 overflow

 9 1001 overflow

 10 1010 overflow Number Representation

Base 10:

3 2 1 0 (9038)ten  910  010  310  810  9038

Base 2: 4 3 2 1 0 (10011)two 1 2  0 2  0 2 1 2 1 2 19

General Case: n i (xn xn1...x0 )b   xi  b i0 Binary Representation of Numeric Values

 Sign Magnitude Sign Magnitude (e.g., -2 = 1010) 1 0  One’s Complement 0 0 1 1 = 3 (invert: e.g., -2 = 1101) + 1 0 1 0 = -2

 Two’s Complement 1 1 0 1 = -5  (invert and add 1: e.g., -2 = 1101) Two zeros: 0 != -0  

Two’s Complement One’s Complement 1 1 1 1 1 1 1 0 0 1 1 = 3 0 0 1 1 = 3 + 1 1 1 0 = -2 + 1 1 0 1 = -2 0 0 0 1 = 1  = 0  1 0 0 0 0 Only one zero: 0 = -0   Two zeros: 0 != -0   Invertible: --n = n    Representing 2’s Complement Negative Numbers (4-bit system)

0 0000 0000 -0  The representation of all positive numbers begins with a “0” 1 0001 1111 -1  The representation of all negative numbers 2 0010 1110 -2 begins with a “1“ 3 0011 1101 -3  To convert a two’s comp. number: 4 0100 1100 -4  Inside CPU: complement number and 5 0101 1011 -5 add 1 (easy to do with full adders)  On Paper: leave all trailing 0’s and first 6 0110 1010 -6 1 from right intact, then flip all the 7 0111 1001 -7 remaining bits moving left

Example: 2 - 5 = 2 + (-5) = 0 0 1 0 + 1 0 1 1 1 1 0 1 = -3 Overflow in 2’s Complement Binary Addition

Assuming 4-bit numbers (3 bits plus sign):

0 0 0 1 1 1 1 1 1 0 0 1 = -7 1 0 1 1 = -5 + 0 1 0 1 = 5 + 0 1 1 1 = 7

1 1 1 0 = -2 0 0 1 0 = 2

No overflow Overflow (but we don’t care) How it works:  Like decimal addition 0 1 1 1  When signs are opposite, 0 1 0 1 carry into or out of sign bit = 5 can be ignored + 0 1 1 1 = 7

 When signs are the same, 1 1 0 0 = -4  carry into sign bit (that changes sign) indicates Overflow (we do care) overflow (that must be handled) Building a Binary Adder

16 a 16-bit 16 out 16 adder b

 Adder: a chip designed to add two (two’s comp.) integers

 Implementation:  Half adder: designed to add 2 bits  Full adder: designed to add 3 bits  Adder: designed to add two n-bit numbers. Half adder (designed to add 2 bits)

a b sum carry

0 0 0 0 a half sum 0 1 1 0 adder b carry 1 0 1 0 1 1 0 1

Implementation: based on two gates that you’ve seen before. How To Build A Half Adder

 Truth table for Half Adder ∑ A B A XOR B (A + B) Cout 0 0 0 0 0 0 1 1 1 0 1 0 1 1 0 1 1 0 0 1

A B Half Adder ?? Cout ∑ How To Build A Half Adder

 Truth table for XOR and Half Adder ∑ A B A XOR B (A + B) Cout A AND B 0 0 0 0 0 0 0 1 1 1 0 0 1 0 1 1 0 0 1 1 0 0 1 1

A B ∑ = A XOR B Half Adder Cout = A AND B Cout ∑ Full adder (designed to add with carry in)

a b c sum carry

0 0 0 0 0

0 0 1 1 0 a sum 0 1 0 1 0 b full 0 1 1 0 1 adder carry c 1 0 0 1 0 1 0 1 0 1 1 1 0 0 1 1 1 1 1 1

Implementation: based on half-adder gates. Implementation of Full Adder

 Truth table for Full Adder

Cin A B ∑ Cout 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1 1 0 1 A B Cin 1 0 0 1 0 Full Adder 1 0 1 0 1 Cout ∑ 1 1 0 0 1 1 1 1 1 1 How To Build A Full Adder A B Cin  Truth table for Full Adder

A1 B1 Cin A B ∑ 1 Cout Cout ∑ Cout 1 2 Half Adder 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 0 Cout1 ∑1 0 1 0 1 0 0 1 0 0 1 1 0 1 0 0 1 1 0 0 0 0 0 1 0 A2 B2 1 0 1 1 0 1 0 1 Half Adder 1 1 0 1 0 1 0 1 ∑ 1 1 1 0 1 0 1 1 Cout2 2

?? ∑ Cout How To Build A Full Adder A B Cin  Truth table for Full Adder

A1 B1 Cin A B ∑ 1 Cout Cout ∑ Cout 1 2 Half Adder 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 0 Cout1 ∑1 0 1 0 1 0 0 1 0 0 1 1 0 1 0 0 1 1 0 0 0 0 0 1 0 A2 B2 1 0 1 1 0 1 0 1 Half Adder 1 1 0 1 0 1 0 1 ∑ 1 1 1 0 1 0 1 1 Cout2 2

∑ Cout n-bit Adder (designed to add two 16-bit numbers)

16 a 16-bit 16 out 16 adder b

... 1 0 1 1 a + … 0 0 1 0 b

… 1 1 0 1 out

Simple Implementation: series of full-adder gates. Chained Carry Adder

 Chained carry makes addition time approximately equal to number of bits times the propagation delay of a Full Adder

A B Cin A B Cin A B Cin Full Adder Full Adder Full Adder Cout ∑ Cout ∑ Cout ∑

Full adder prop. delay = 3 gpd (carry output) So a 16 bit adder would take 48 gpd to complete add Carry Look Ahead Basics  If we understand how carry works we can compute carry in advance. This is called “Carry Look-Ahead.”  For any bit position, if A = 1 and B = 1; Cout = 1, i.e., a carry will be generated to the next bit position, regardless of value of Cin. This is called “Carry Generate”  For any bit position, if one input is 1 and the other input is 0; Cout will equal Cin (i.e., the value of Cin will be propagated to the next bit position. This is called “Carry Propagate”  For any bit position, if A = 0 and B = 0; Cout will equal 0, regardless of value of Cin. This is called “Carry Stop.”

A B Cin A B Cin A B Cin Full Adder Full Adder Full Adder Cout ∑ Cout ∑ Cout ∑ Carry Generate, Propagate and Stop

 Truth table for Full Adder

Cin A B ∑ Cout f fgps 0 0 0 x CSi gps fgps 0 1 1 x CPi fgps 1 0 1 x CPi A B Cin fgps 1 1 0 x CGi Full Adder Cout ∑ X

No need for carry chain  Carry Look Ahead Basics

 The equations to compute Cin at Bit Position i are as follows:

Cini = Cgi-1

+ Cpi-1 ● Cgi-2

+ Cpi-1 ● Cpi-2 ● Cgi-3 …

+ Cpi-1 ● Cpi-2 … Cp1 ● Cg0 Practical Considerations

Very wide (more than 8 input) gates are impractical, so we

would likely use a logn depth tree of gates to implement the wide ANDs and ORs. This is still faster than chained carry, even for 16 bits (and is much faster for 32 or 64 bit adders).

Cini = Cgi-1

+ Cpi-1 ● Cgi-2

+ Cpi-1 ● Cpi-2 ● Cgi-3 …

+ Cpi-1 ● Cpi-2 … Cp1 ● Cg0 Practical Implementation Note

 Use Cin0 to add one (for example, when inverting the sign of a two’s complement number).

 Cin0 then becomes a control signal that can be turned on by the control part of the microprocessor.

A B Cin Full Adder Cout ∑ What About Multiplication?

3 2 1 0

3 2 1 0

03 02 01 00

13 12 11 10

23 22 21 20

33 32 31 30 Carry Save Addition

The idea is to perform several additions in sequence, keeping the carries and the sum separate. This means that all of the columns can be added in parallel without relying on the result of the previous column, creating a two output "adder" with a time delay that is independent of the size of its inputs. The sum and carry can then be recombined using one normal carry-aware addition (ripple or CLA) to form the correct result. CSA Uses Full Adders

Depth = 4; 7 adders “Wallace Tree” Addition Plus add with carry CSA Adder Tree Depth = 7; 7 adders used Plus add with carry A 4-bit Example (carry propagating to the right)

(or carry look-ahead) Example: An 8-bit Carry Save Array Multiplier

A parallel multiplier for unsigned operands. It is composed of 2-input AND gates for producing the partial products, a series of carry save adders for adding them and a ripple-carry adder for producing the final product.

FA with 3 inputs, 2 outputs

Generating Partial Products Historical End-note: Leibnitz (1646-1716)

 “The binary system may be used in place of the decimal system; express all numbers by unity and by nothing”  1679: built a mechanical calculator (+, -, *, /)

 CHALLENGE: “All who are occupied with the reading or writing of scientific literature have assuredly very often felt the want of a common scientific language, and regretted the great loss of time and trouble caused by the multiplicity of languages employed in scientific literature:  SOLUTION: “Characteristica Universalis”: a universal, formal, and decidable language of reasoning Leibniz’s medallion for the Duke of Brunswick  The dream’s end: Turing and Gödel in 1930’s (there are undecidable problems).