Math
584, Mathematics of Medical Imaging
Matlab
Lesson 3: Inline functions and the Fourier transform
Unlike Maple and other
symbolic
mathematics programs which manipulate variable names, until recently Matlab
mainly manipulates numeric arrays. This
made it more difficult to work with function objects such as f(x)=cos(x) or g(x)=x^2.
In Matlab, one mostly worked with functions
by
sampling on a uniform
grid. To create a
uniform grid (in 1-d) use a command such as:
>> X=-5:0.1:5;
This creates a vector of
numbers
between -5 and 5 with step size delta=0.1.
Now create an inline function:
>> F=inline('cos(x)','x');
We can now evaluate this
function on
our uniform grid and then plot the result:
>> Y=F(X);
figure; plot(X,Y);
Inline functions can also be
evaluated at
numbers:
>> F(pi)
ans
=
-1
or at vectors (as above).
However, you must be careful to write the "vectorized"
version of an inline function if you want to evaluate it on vectors. For example, the following command produces
an error:
>> G=inline('x^2','x');
Y=G(X);
??? Error using ==> inlineeval
Error in inline expression ==> x^2
??? Error using ==> ^
Matrix must be square.
This is because '^2'
denotes the
operation of squaring a matrix. Instead
we want to use
>> G=inline('x.^2','x');
Y=G(X);
which is the "vectorized"
version of the
function. Try defining other vectorized functions.
For example, '1/(1+x^2)' becomes
'1./(1+x.^2)'
and 'xcos(x)+x^2sin(x)' becomes 'x.*cos(x)+x.^2.*sin(x)'.
Recently MATLAB introduced improved symbolic manipulations capabilities, which make it easier todirectly define simple functions of several variables. These are called 'anonymous functions', which have the following syntax:
>> sqr= @(x) x.^2;
As before, we use vectorized notation to be sure that our functions can take arrays as arguments, but now we can explicitly functions of symbolic variables. 'sqr' is called the "function handle." It is used very much like function names familiar in mathematical notation. To evaluate this function, we would type
ans =
4
Using
a similar syntax we can define functions of several variables as well.
For example a function of two variables is defined by
>> expk = @(x,k) exp(i*x.*k)
Download the files ft_demo.fig and ft_demo.m
to your
Matlab working directory.
(To do this use your browser's capability to "save a link target" as a
local file.)
This is a simple program which allows you to
experiment with the Fourier transform. To
run the program, type
>> ft_demo
This should open the
following
window:
To define a function,
select the
menu item Define: Define using the inline function syntax introduced
above.
Enter the function information and click "OK".
This will plot the function in the
window. The function is sampled on a
vector, so be sure to enter a "vectorized"
function
string (as described above). To apply
the Fourier transform or the inverse Fourier transform, simply use the
Transform menu.
Experiment by computing
the Fourier
transform of various functions.
To get good results, the
functions
should decay to zero. One way to
guarantee that a function decays to zero is to multiply it by the
characteristic function on a finite interval.
The function F=inline('abs(x)<1')
is equal
to 1 in the interval [-1,1] and is 0 outside this interval. So inline('cos(x).*(abs(x)<pi/2)') is supported on the
interval [-pi/2,pi/2].
Try the exercises below.
For more information on
these
topics, see the following help topics: fft,
ifft. Finite Fourier transforms for
arrays depending upon more than one variable can be defined using
fftn, ifftn.
1. Try sampling a function on a uniform grid using
a 'for
>> X=-5:0.01:5;
for j=1:length(X) Y(j)=F(X(j));
end; figure; plot(X,Y);
Try changing the step size to 0.001. Is the execution of this command faster or slower than the vector command >>Y=F(X)?
2. The smoothness of a function is related to the decay of its Fourier transform. Demonstrate this using the ft_demo program. For example, define the functions [cos(x).*(abs(x)<pi/2)].^n for n=1,2,3,4,5,etc. What happens to the function as n increases? What happens to its Fourier transform? Try experimenting with functions that are discontinuous.
3. A translation of a function corresponds to a linear phase
factor in the
Fourier transform. Demonstrate this
using the ft_demo program.
For example, start with the function exp(-5*x.^2),
then translate it 3 units to the left: exp(-5*(x+3).^2).
What happens to the Fourier transform? Now
figure out how to modify the original
function to induce a translation in the Fourier domain.