HW 2, EECS 207A. Fall 2004. UCI. 
First problem.

by Nasser Abbasi

Problem

The following  table displays the pixel intensity values of a macro block of a 1-dimensional image
   60, 75, 86, 200, 235, 255, 46, 34
a) compute the DCT coefficients of this function.
b) compute the original function from the DCT using the IDCT algorithm
c)Ignore the last 4 bytes and recompute the original function, how much error is introduced?

extra work: I also did an edditional analysis on this problem. I showed how the error in data changes as a function of number of terms dropped from the DCT table

Part(a)

Define the DCT and IDCT functions

Clear["Global`*"];

buildDCT[i_, data_, c_] := 
      Module[{L = Length[data]}, 
c[[
            i + 1]] (∑_(n = 0)^(L - 1))(data[[n + 
                    1]] Cos[(((2  n + 1) i π)/(2 L))]) 
] 

buildIDCT[i_, dct_, c_, endIndex_] := 
      Module[{}, 
(∑_(n = 0)^endIndex) 
          c[[n + 
                1]] dct[[
              n + 1]] Cos[(((2  i + 1) n π)/(2 L))]
]

Define the data and create  the C table

originalData = {60, 75, 86, 
          200, 235, 255, 46, 34};


    L = Length[originalData];

    c = Table[(Sqrt[(2/L)]), {L}];

    c[[1]] = (Sqrt[(1/L)]);

Create the DCT values, and print them

dct = 
        Table[buildDCT[i, originalData, c], {i, 0, 
            L - 1}];
Print["DCT values = ", 
      TableForm[Chop[N[dct]], 
        TableDirections -> {Row, 
            Column}]]

Print["Original data = ",  
      TableForm[originalData, 
        TableDirections -> {Row, 
            Column}]]

\(Print["C constants table= ", 
TableForm[c, 
        TableDirections -> {Row, Column}, 
        TableSpacing -> {1, 
            6}], 
"
C constants table= ", 
TableForm[N[c], TableDirections -> {Row, Column}, 
        TableSpacing -> {1, 6}]]\)

Part(b)

Use IDCT to recompute the original data, i.e. use all DCT points.

endIndex = Length[dct] - 1;
dataNew = 
        Table[buildIDCT[i, dct, c, endIndex], {i, 0, 
            L - 1}];
Print["data back from IDCT = ", 
      TableForm[Chop[N[dataNew]], TableDirections -> {Row, Column}, 
        TableSpacing -> {1, 6}]]

Part(c)

bytesToIgnore = 4;
endIndex = 
        Length[dct] - bytesToIgnore - 1;
dataNewShort = 
        Table[buildIDCT[i, dct, c, endIndex], {i, 0, 
            L - 1 }];
Print["data back from IDCT = ", 
      TableForm[Chop[N[dataNewShort]], TableDirections -> {Row, Column}, 
        TableSpacing -> {1, 6}]]
error = dataNew - dataNewShort;
Print["Absolut error       = ", 
      TableForm[ N[error] , TableDirections -> {Row, Column}, 
        TableSpacing -> {1, 6}]]
Print["% error             = ", 
      TableForm[ N[Abs[error]/dataNew 100] , 
        TableDirections -> {Row, Column}, 
        TableSpacing -> {1, 6}]]

Extra work. Generate table showing how much error (in percentage) in the data recomputed as we drop more terms in the DCT. Try from 1 to 7 terms dropped.

L = 
        Length[dct];

    data = Table[i, {i, 1, L - 1}, {j, 1, L}];

    For[n = 1, n ≤    L - 1,  
      n = n + 1, 
{
endIndex = 
          L - n; 
data[[n, 
            All]] = 
          Table[buildIDCT[i, dct, c, endIndex], {i, 0, 
              Length[dct] - 
                1 }];
}
]

Data recomputed, each row shows the data based on dropping as many terms from DCT as the row number-1.
For example, the first row shows the data recomputed if we dropped ZERO terms from DCT.
The second row shows the data recomputed if dropped ONE term from DCT, etc...

Print["Original input data\n", 
      TableForm[originalData, 
        TableDirections -> {Row, 
            Column}], "\nTable showing the recomputed data values as more \
DCT terms are dropped\n"
MatrixForm[
          Chop[N[data[[All, 
                All]]]]]]

\(\(\( (*\ 
      now\ compute\ the\ error\ in\ %\ *) \)\(
\)\(\(error = Table[i, {i, 1, L - 1}, {j, 1, L}];\)

    \(maxerror = Table[i, {i, 1, L - 1}];\)

    \(averageError = 
        Table[i, {i, 1, L - 1}];\)


    For[n = 1, n ≤ \ L - 1, \ 
      n = n + 1, 
{
\ 
ϵ = 
          Chop[N[originalData - 
                data[[n, 
                  All]]]]; 
error[[n, All]] = 
          N[Abs[\ \ ϵ\ ]\/originalData\ 100]; 
maxerror[[n]] = 
          Max[error[[n, 
              All]]]; 
averageError[[
            n]] = \(∑\+\(k = 1\)\%L error[[n, k]]\)\/L;
}
]

    \(Print["Table showing the % erorr in each data term as more DCT terms are dropped", 
MatrixForm[
          error]];\)


    Print["Average error in data as more DCT terms are dropped       ]
", TableForm[\ N[averageError]\ , 
        TableSpacing -> {1, 6}]]


    \(g1 = 
        ListPlot[maxerror, PlotStyle -> PointSize[0.02], \ 
          PlotLabel -> "Max % error in data ", 
          AxesLabel -> {"Number of DCT terms droped", ""}, 
          PlotJoined -> True, DisplayFunction -> Identity, 
          PlotRange -> All];\)

    \(g2 = 
        ListPlot[maxerror, PlotStyle -> PointSize[0.02], 
          PlotRange -> All, \ PlotLabel -> "Max % error in data ", 
          AxesLabel -> {"Number of DCT terms droped", ""}\ , 
          DisplayFunction -> Identity];\)

    Show[{g1, g2}, 
      DisplayFunction -> $DisplayFunction]



    \(g1 = 
        ListPlot[averageError, PlotStyle -> PointSize[0.02], \ 
          PlotLabel -> "Average % error in data ", 
          AxesLabel -> {"Number of DCT terms droped", ""}, 
          PlotJoined -> True, PlotRange -> All, 
          DisplayFunction -> Identity];\)

    \(g2 = 
        ListPlot[averageError, PlotStyle -> PointSize[0.02], \ 
          PlotLabel -> "Average % error in data ", 
          AxesLabel -> {"Number of DCT terms droped", ""}, 
          PlotJoined -> False, PlotRange -> All, 
          DisplayFunction -> Identity];\)

    Show[{g1, g2}, 
      DisplayFunction -> $DisplayFunction]

    \)\)\)

Conclusion

The Max error in the recomputed data array increases as more DCT terms are dropped. But that is not always true each time. In this example, as we dropped the 6th term in the DCT table, the max error was actually smaller, but we see that the average error in data is always increasing as expected. I am not sure now why the max error in data did not go down every time we dropped an additional term from the DCT. This needs more investigation. I have tried this on another data input and saw the same result.