Project 03

Due Tuesday, October 24, 2017

Problem 1 (10 points)

In Notebook 032 you have seen how to convert a color image to its YCbCr components. Recreate the original color image from its Y-, Cb- and Cr- components. (Check your result by computing the Peak Signal to Noise Ratio to the original image. Note that when computing the luminance/chrominance components, we round to the nearest integer. )

SetDirectory[NotebookDirectory[]];<<DW.m;

photo=;
pic=ImageData[photo,"Byte"];
ImagePlot[pic]

fct[v_]:={16+1/256(65.738v[[1]]+129.057v[[2]]+25.064v[[3]]),128+1/256(-37.945v[[1]]-74.494v[[2]]+112.439v[[3]]),128+1/256(112.439v[[1]]-94.154v[[2]]-18.285v[[3]])};

An example: If the RGB values of a pixel are 10, 100, and 200, respectively, the next command produces the YCbCR values (not rounded).

fct[{10,100,200}]

YCbCr=Round[Map[fct,pic,{2}]];
Y=YCbCr[[All,All,1]];
Cb=YCbCr[[All,All,2]];
Cr=YCbCr[[All,All,3]];
ImagePlot[Y]
ImagePlot[Cb]
ImagePlot[Cr]

Problem 2 (10 points)

Write two commands (or modules if necessary) for Huffman encoding and decoding. Inputs to the first module should be a character string and a coding table; the output should be a binary sequence encoding the character string.

cstr="ABRACADABRA"

huff = {{"A", "0"}, {"R", "111"}, {"B", "10"}, {"D", "1101"}, {"C", "1100"}}

You have to write the code for the next two commands!

bstring=StreamEncoding[cstr,huff]

The second command should have as inputs a binary string and a coding table and output the corresponding character string:

StreamDecoding[bstring,huff]

You will need to consult the help section to find out about strings and replacement operators; the next command may be useful to rewrite the coding table as a list of replacement rules:

Apply[Rule,huff,2]

Problem 3 (10 points)

The problem will use the command 'FourierSCPlot':

?FourierSCPlot

By the way, you can create π by either writing 'Pi' or by pressing 'EscpEsc'.

Pi
N[Pi]

Consider the following function :

f[x_]=Piecewise[{{-1/2,x<0},{1/2,x>0}}];
Plot[f[x],{x,-π,π},PlotStyle->{Blue,Thick}]

Below is the graph of f(x) again (dashed) and the graph of its 8th "degree" Fourier approximation (green) :

FourierSC[f[x],x,8]

FourierSCPlot[f[x],x,8]

We can use ' PlotRange' to zoom in, and notice that - in the worst case scenario -  the value of the function f(x) differs from its Fourier approximation by about  Δy=0.092; the "first" peak of the Fourier approximation happens at a distance of about Δx=0.39 from the point of discontinuity of the function:

Here is the same plot without the blue lines :

FourierSCPlot[f[x],x,8,GridLines->Automatic,PlotRange->{{.0,.5},{.49,.6}}]

1. How do these values of Δx and Δy change when you increase the number of terms of the Fourier approximation? 
2. Repeat 1. for the function g(x):

g[x_]=Piecewise[{{-1,x<0},{1,x>0}}];
Plot[g[x],{x,-π,π},PlotStyle->{Blue,Thick}]

3. Repeat 1. for the "sawtooth" function h(x) :

h[x_]=Piecewise[{{(x+π)/π,x<0},{x/π,x>0}}];
Plot[h[x],{x,-π,π},PlotStyle->{Blue,Thick}]

4. Summarize your findings : How do the values Δx and Δy depend on the degree of the approximation? How do they depend on the size of the "jump" at x = 0? Do they depend on the shape of the function? What other observations can you make?