Skip to main content

Exercises 26.4 Homework

1.

Instead of deciding in advance how many coefficients to discard, it is usually preferable to maintain some degree of quality of the image. This is often measured in terms of “energy”, defined as the sum of squares of absolute values of Fourier coefficients (except the constant one). That, is to have 95% quality, we want to keep enough Fourier coefficients so that the sum of their squares is at least 95% of the total sum of squares. Equivalently, we discard the coefficients that contribute 5% of the energy.

The goal of this exercise is to modify Example 26.2.2 so that the input value is quality q such as 90 or 95, and the image in Figure 26.2.1 is compressed based on the energy consideration.

Suggestions: sort the Fourier coefficients from largest to smallest using

sorted = sort(abs(fc(:)), 'descend');  

and remove the largest of them (the constant term):

sorted = sorted(2:end);  

The total energy is

total = sum(sorted.^2); 

The key step is writing a loop that sums the squares of the elements of sorted array until the sum reaches the desired proportion of the total energy. When the loop exits, you will know what threshold value thr should be used for truncating Fourier coefficients.

Compare the quality of compressed image with q = 90, q = 80, and q = 70. In addition to showing the compressed image, the script should display the percentage of Fourier coefficients that were kept as non-zero values: for example, 100*numel(nonzeros(fc))/numel(fc).

Hint

Consider a small example: the sorted array of nonconstant coefficients is [10 8 7 5 4 2 1] which makes its total energy \(259\text{.}\) Suppose we want to determine the threshold thr that preserves 90% of energy, which is \(0.9\cdot 259 = 233.1\text{.}\) Start adding the squares of coefficients until the required amount is reached:

  • \(10^2 = 100\text{,}\) not enough
  • add \(8^2\text{,}\) get \(164\text{,}\) still not enough
  • add \(7^2\text{,}\) get \(213\text{,}\) still not enough
  • add \(5^2\text{,}\) get \(238\text{,}\) which is enough

At the conclusion of this process (which can be either a while loop or a for loop with a break statement) we find that \(5\) is the smallest coefficient that we should keep; any smaller ones should be discarded. Thus, in this example thr = 5 would be the correct choice of threshold. Once the threshold is determined, the rest proceeds as in Example 26.2.2.

Note that the command prctile is not needed in this homework.