Sunteți pe pagina 1din 9

ELEN E3084: Signals and Systems Lab

Lab III: Signal Representation using Fourier Series

1 Introduction
In this lab we will further develop our understanding of Fourier series, and see how we can
represent discrete-time signals using complex exponentials. We will start by illustrating some
of the concepts we have seen in class for the Fourier series representation of a continuous-
time signal. Afterwards we will consider discrete-time signals, which are easier to deal with
in MATLAB. We will consider a two-dimensional version of Fourier series, and will use it to
compress and image quite effectively.
Before we begin we will start again the diary function which will capture all our commands.
Go the lab3 directory using the cd command the same way as in UNIX. Then type:

>> diary on;

Note that the goal of these labs is for you to tinker and play with the concepts that are
introduced, in addition to the completion of the required tasks. Show the diary file at the
end of the session to a TA or LA, so that she/he can assess the extent of your work.

2 Fourier Series
As we have seen in class the vast majority of periodic signals can be represented in terms
of the exponential Fourier series. In fact we can use Fourier series to represent almost any
signal over a finite length interval. Consider the real signal

f (t) = tu(t) ,

Clearly this is not a periodic signal, and therefore it cannot be represented by a Fourier
series for all t. Nevertheless we can represent it by a Fourier series over a finite interval.
Suppose we want to represent f (t) over the interval (0, 1). We can accomplish this by first
constructing another signal x(t). This signal must be periodic and be identical to f (t) for
any t ∈ (0, 1). There are various ways of doing this. In Figure 1 we depict two possibilities.
Notice that both of these signals have period 2. Clearly we can write their Fourier basis
representation. After some simple algebra (check this at home) we are able to derive that

X 2
x1 (t) = − (−1)k sin(πkt) ,
k=1
πk

1
f (t) x1 (t)

−1 0 1
··· −1 0 1
···
t t

(a) (b)

x2 (t)

··· −1 0 1
···
t

(c)

Figure 1: Two different periodic signals x1 (t) and x2 (t) that are identical to f (t).

and ∞
1 X 2
x2 (t) = + ((−1)k − 1) cos(πkt) .
2 k=1 π 2 k 2

To Do 1
Let’s see what happens when we approximate x1 and x2 by N -harmonic truncated versions
N
(N )
X 2
x1 (t) =− (−1)k sin(πkt) ,
k=1
πk

and
N
(N ) 1 X 2
x2 (t) = + ((−1)k − 1) cos(πkt) .
2 k=1 π 2 k 2
Write the following MATLAB function, that takes as inputs a time index vector t and number
(N ) (N )
of harmonics N and outputs signals f , x1 and x2 .

function [f,x1,x2]=example(t,N);
%takes as input a time index vector t and number of harmonics N
%and outputs signals f, x_1 and x_2, where f(t)=tu(t) and x_1(t) and x_2(t)
%are the FS approximations using N harmonics (see To Do 1 for more
%information)
f=t.*(t>=0);

2
x1=zeros(size(t));
x2=zeros(size(t))+1/2;

for k=1:N;
x1=x1-2/pi/k*(-1)^k*sin(pi*k*t);
x2=x2+2/pi^2/k^2*((-1)^k-1)*cos(pi*k*t);
end;

(N ) (N )
Use this to plot, in the same axes, f (t), x1 (t) and x2 (t) for various values of N . Comment
on the quality of the approximation. Which one is better for the same number of harmonics?
Can you relate this to the behavior of the coefficients as k gets larger? Show these results
to your TA.

3 Discrete-time Periodic Signals


The Fourier series representation is better understood as a representation of the signal is a
basis of complex exponentials. In linear algebra you have seen that vectors can be represented
in different basis, and some basis are more convenient than others. Let’s learn more about
this.
In class we have been focusing mostly on continuous-time signals. Today we are going to
consider discrete-time signals, and see how these can be represented in different domains.
Let x[n] be a DT periodic signal, with period N0 . Clearly to describe this signal it suffices
to describe what happens in a period. That is if you are told the signal has period N and
given the values of x[i] for i = 0, 1, . . . , N − 1 you are able to compute x[n] for any value
n ∈ N. You can think of the collection x[0], x[1], . . . , x[N − 1] as the entries of a vector
v = (v0 , v2 , . . . , vN −1 ) ∈ RN . Therefore if we want to describe x[·] we need simply to describe
v. Note that, since MATLAB processes vectors it can be quite useful to think about our
signal as a vector (at least over a period).
We are quite familiar with the canonical basis representation, in which we write v as

v = v0 e(0) + v1 e(1) + · · · + vN −1 e(N −1) ,


(i)
where e(i) = (0, 0, . . . , 0, 1, 0, . . . , 0) where i is the only non-zero entry (that is ei = 1 and all
the other entries are zero). The set of vectors e(i) , i = 0, . . . , N − 1 is known as the canonical
basis of RN .
Interestingly enough one can consider other basis for RN , in particular a basis using complex
exponentials, akin to the Fourier series. Let {b(0) , b(1) , . . . , b(N −1) } be a basis for RN . A very
desirable feature of any basis is orthogonality, meaning that all elements in the basis are

3
mutually orthogonal. In other words for any i 6= j

< b(i) , b(j) >= 0 ,

where < ·, · > denotes the usual inner product, that is


N −1
(i) (j)
X ∗
< b ,b >= b(i)
n b(j)
n .
n=0

It is easy to see that the canonical basis is orthogonal. In what follows we will consider the
(k) (k)
basis with the following elements b(k) = (b0 , . . . , bN −1 ), where
 
(k) 2πk
bn = exp j (n − 1) .
N

To Do 2
Verify experimentally that < b(i) , b(j) > is zero if i 6= j and N if i = j. Choose of value for
N (arbitrarily). Create the two vectors, corresponding to two basis elements. For example
a=exp(j*2*pi/N*45*(0:N-1)); corresponds to b(45) and b=exp(j*2*pi/N*23*(0:N-1));
corresponds to b(23) . Now check that < a, b >= 0. We can compute the inner product
effectively by transposing and conjugating one of the vectors (recall that the the operator
{’} transposes and conjugates a vector). Type a*b’. What is the result you get? Now type
a*a’, what is the result you get? Try this for various values of N, and various basis vectors.

The above procedure is not a proof, but rather evidence that the proposed basis is orthogonal.
This fact is not hard to prove analytically. The orthogonality property allows us to very easily
compute the representation of an arbitrary vector in this new basis. In fact, for a vector
v ∈ RN and any orthogonal basis {b(1) , b(2) , . . . , b(N ) }
N
X
v= Dk b(k) ,
k=1

where Dk =< v, b(k) > / < b(k) , b(k) >. This means that, for the particular choice of basis
above we have
N −1  
X 2πk
x[n] = D[k] exp j n , (1)
k=0
N
where
N −1  
1 X 2πk
D[k] = x[n] exp −j n .
N n=0 N

4
This is what is known as the (D)iscrete (T)ime (F)ourier (S)eries (DTFS). Note that D[k] is
also a vector of length N (but this time in CN ), and contains exactly the same information
as x[n].
In MATLAB we can compute the DTFS very easily using the command fft. We will study
this in the context of images.
To Do 3
Take N = 100 and create the signals n=0:(N-1) and x=n.*exp(-n/10/N). Plot this signal.
Now compute the DTFS using D=fft(x). Plot the magnitude of the coefficients D[k]. What
do you notice about these coefficients? Given (1) it is clear that the terms corresponding
to D[k]’s with small amplitude can possibly be removed without too much loss. Let’s try it
and see what we get

threshold=5;

%take only the Fourier coefficients that have magnitude greater than
%threshold

E=D.*(abs(D)>threshold);

%compute the inverse DTFS (taking the real part prevents some numerical
%round-up problems)

x2=real(ifft(E));

%plot x vs x2
plot(n,x,n,x2);legend(’original’,’approximation’);

Play with different threshold values, and see what happens. How many coefficients are you
keeping in each case (i.e., how many of the elements of E are non-zero)? Comment your
results with the TA.
Now repeat all the above, but using the signal x=(n>N/2) (that is, a step function). Comment
on the results

The ideas above can easily be used to efficiently encode signals. Having a “good” represen-
tation of a signal, that is, a representation where the signal can approximately be written as
the sum of a small number of components, is key to good data compression. In the following
play around with this concept a little bit, in the context of image processing

5
4 Images
The concept of basis representation can be easily extended to two-dimensional signals, that
is signals with two independent variables. An important such set of signals are images.
Consider the signal
x[n, m], where n, m = 0, . . . , N − 1 .
This signal can represent an digital image, where x[n, m] is the intensity of the pixel with
coordinate n and m. There is an analogous to the DTFS we have seen above for this class
of signals, in particular
N −1    
X 2πk 2πl
x[n, m] = D[k, l] exp j n exp j m , (2)
k,l=0
N N

where
N −1    
1 X 2πk 2πl
D[k, l] = 2 x[n, m] exp −j n exp −j m .
N n,m=0 N N
Despite looking complicated the transformation x[n, m] into D[k, l] can be done in a com-
putational simple way. We will use the optimized MATLAB implementations to do it.
To Do 4
Load the cameraman.mat file into MATLAB (use the command load). Use who to see what
in the workspace (it is easier if you clear the workspace before loading the file, so you can start
with a clean slate). When you load the .mat file you notice a matrix X in the workspace.
This has a gray-scale image. In order to display it type

>> imagesc(X)
>> colormap(gray(256));
>> axis(’image’);

Now we will proceed to compute the DTFS of this signal by typing D=fft2(X). Notice that
D is a matrix of the same size as X, but with complex values. Let’s look at their magnitudes.
Run the following code

>> D=fft2(X);
>> imagesc(abs(D));colormap(gray(256));axis(’image’);

As you see there is not much visible. This is because most values of D have very small
magnitude and only a few are large. We will see later in the lab that this is a very nice
feature that allows us to compress images effectively.
In order to see things better lets image the magnitudes in a log scale (that is, let’s plot
log |D[k, l]| instead). This can be done as listed below, where we also plot the phase of
D[k, l].

6
>> subplot(121);
>> imagesc(log(abs(D)+eps));colormap(gray(256));axis(’image’);
>> title(’log-magnitude’);
>> subplot(122);
>> imagesc(angle(D));colormap(gray(256));axis(’image’);
>> title(’phase’)

You see it looks a little funky, but it contains all the original information about x. Note
that both the information about the magnitude and phase are important. To see this let’s
remove either one and see what we get.

>> D_nophase=abs(D);
>> X_nophase=real(ifft2(D_nophase));
>> D_nomag=D./abs(D);
>> X_nomag=real(ifft2(D_nomag));
>> subplot(121);imagesc(X_nophase);title(’Discarding phase information’);
>> colormap(gray(256));axis(’image’);
>> subplot(122);imagesc(X_nomag);title(’Discarding magnitude information’);
>> colormap(gray(256));axis(’image’);

Comment on the results.


As we noted most values of D[k, l] have very small magnitude. Since X can be written
according to (2) it is plausible to assume those terms can be eliminated from the summation
without incurring a significant error. Let’s do this. Let’s organize all the coefficient in a
vector and plot their magnitudes.

>> D=fft2(X);
>> d=D(:);
>> stem(abs(d))

Clearly many of the coefficients have small magnitude, but some are rather large. To better
see this lets sort the values of abs(d))

>> sorted_coeffs=sort(abs(d));
>> stem(sorted_coeffs)

As we see here most coefficients have rather small magnitudes, and only a few are large.
Let’s use this fact and see if we can represent X using only a few elements in the summation
of (2).

>> %Construct a coefficient matrix D2 where


>> %we zero-out the smallest 90 percent coefficients
>> D2=D.*(abs(D)>=sorted_coeffs(round(0.90*length(sorted_coeffs))));

7
Try to understand what the above code is doing. Check that D2 has roughly only 10%
non-zero entries.
Now let’s compute the summation in equation (2), by doing the inverse DTFS.

>> X2=real(ifft2(D2));
>> imagesc(X2);
>> colormap(gray(256));
>> axis(’image’);

Comment on the results. Is the reconstructed image relatively good? Note that the above
procedure is indeed compressing the image quite dramatically as instead of N 2 real numbers
you only have to store N 2 /10 complex numbers.
Incorporate the above code in a function DTFS approx that takes an image and a percentage,
and computes the above approximation. Play around with different values of the percentage
of coefficients. What do you see when you keep only 5% of the coefficients?

5 The basis elements


As you seen above if you keep only a very reduced number of harmonics in the Fourier
series representation your approximation degrades quite a lot, and furthermore you start
seeing weird artifacts. The reason for is that the basis elements of the DTFS are essen-
tially complex sinusoids, therefore spatially uniform, but images have edges (discontinu-
ities), and therefore you need many sinusoids to accurately represent these. Let’s plot a few
of these basis vectors. Plot the
 real part of the basis element corresponding to D[k, l], that
is Re{exp j 2πk 2πl

N
n exp j N
m }

>> k=5,l=10

k =
5

l =
10

>> D3=zeros(size(X));
>> D3(k,l)=1;
>> imagesc(real(ifft2(D3)));
>> colormap(gray(256));axis(’image’);

8
Use the above code to plot a few more basis elements. Notice what they look like. It is
remarkable that we are able to reconstruct the original image using only a sum of a few of
these basis elements.
Despite this apparent inadequacy of the Fourier basis to represent discontinuities it is still
a very powerful idea, and it was quite successfully applied in popular image and video
compression standards, such as JPEG and MPEG.
To Do 5

Instead of Fourier series basis we can consider other types of basis. A particular type of
basis that is quite useful are Wavelet. These have the advantage that many of the basis
elements are spatially concentrated, and therefore can be used the represent discontinuities
(that is edges) in images quite effectively. Although we are not going to study these in this
course we can still play a bit with them. The functions wavelet.m and iwavelet.m compute
a wavelet and inverse wavelet transform respectively1 , and work exactly like the functions
fft2.m and ifft2.m. Repeat the To Do 4 part using the wavelet transform, instead of the
DTFS (simply replace fft2 by wavelet and ifft2 by iwavelet. See how many coefficients
you can throw away without messing up the reconstruction too much. Comment on the
results
Wavelet transforms are using in the JPEG2000 standard, and allow for significant better
encoding performance (unfortunately due to bureaucratic and non-scientific reasons this
standard is not used in a widespread way).

At this point you should turn off the diary function we started in the beginning of this lab
session. You can do that by typing:

>> diary off;

This will create the diary file that contains all the commands you typed in the command
prompt as well as all the output they generated. You should show the LA or TA the diary
file.

In this case the wavelet transform implemented is known as a Daubechies-4 multi-level wavelet transform.
1

The implementation I give is not the most efficient, and an efficient implementation is even faster than a
Fourier transform.

S-ar putea să vă placă și