Homework 1
- Due Apr 10, 2020 by 11:59pm
- Points 10
- Submitting a file upload
IMPORTANT
See these notes on how to turn in assignments and assign credit.
INTRODUCTION
In this homework you will do two things:
- Install python/scipy on a computer
- Write a program to invert a 2d Fourier transform and get a recognizable image
We'll talk about these things in detail below. Using computers for interesting scientific research extremely useful, especially in biophysics. Though languages like C++ can be daunting, python and scipy have become popular because they're a lot easier to use. Here are instruction on how to install the software you'll need, and links to tutorials on python and SciPy. Everyone in this class must get python and SciPy working for them on some computer that they have access to. Otherwise it will be impossible to collaborate effectively on the class projects
Test your installation
As described in the installation page above, you should test your installation on the two examples given. Also notes that different platforms sometimes require different code. If the code here isn't working (e.g. the window is black) let me know, there are some graphics issues that sometimes are fixable. It also contains a complete set of all the code with slight modifications.
- Every student in the class should take screenshots of the simulations as they're running and upload them along with the rest of their homework.
INVERT AN FFT OF A 2D IMAGE
Now for fun stuff. Go to the hw1 directory of either the python 3 or python 2 branch (most people will probably be using py3). You'll see a bunch of images in png format in the image directory. Pick one of these and save it somewhere, (it's pretty small.)
This is the real part of the 2d fourier transform of some image which should be fairly easily recognizable biological organism, if you've decoded it correctly. Decoding involves mostly inverting a 2d fourier transform. To start with, I created make_ffts.py to encode the original images. If you don't know scipy already, you must read all the comments in this file very carefully in order to be able to understand enough to write the decoding part. The syntax of python is quite neat. White space matters and indentation must be used. It plays the same role as "{}" in C or java. Comments start out with #'s.
Now you're almost ready to write display_image.py. Fill in display_image_hints.py
with the extra code that is alluded to in the comments (# means a comment).
Get an ipython shell in windows or if you're in linux or a max, open a terminal and type
ipython -pylab
(Note for mac users: There seems to be a bug in one of the older installations of ipython and typing the command will use all of your CPU. Instead type
ipython --pylab=tk
You can also just type directly into a terminal:
python program_name.py if you're having trouble with python.)
Then wait and you'll get a prompt. After the prompt enter:
run display_image.py
it'll run python for you. You can use ipython in windows too. Change directory (cd) to where your image is:
cd directory_with_image
and then
run display_image.py
There will probably be bugs. The ones involving syntax normally involve googling. The real fun begins when the program runs but gives you the wrong results. Under ipython, typing in the name of a variable gives it's contents. You can fiddle around in ipython by executing python commands. If you don't like this, you can also use eclipse.
When you have it all working, what do you see?
Now that hopefully you see an image, there is an extra step at the bottom of the hints file that will make it look a little better. Comment the imshow line with a # and uncomment the lines below it. This will make the image look a little better. What is that extra bit of code doing?
INTERPRETATION OF RESULT
- 1. Images involve real data, not complex numbers, so in this algorithm,
the real part was taken. To understand this, solve the following problem. Suppose the fourier transform (FT) g(x) = G(k), that is FT{g(x)} = G(k). In general G(k) is complex. If we invert this by applying and inverse fourier transform (IFT), IFT{G(k)} = g(x).
What is IFT{real(G(k)}? Express you answer in terms of the function g.
Hint: There are many slightly different definition of Fourier transforms you can use (Boas's one is fine), or you can google and readily find ones like this Links to an external site. (see Eqs 10 and 11). So if you write G(k) in terms of FT{g(x)}, this will involve complex numbers. Now take that expression and take its real part. To do that, notice that the only part that involves complex numbers is an exp(ikx) (depending on your definition). Now you can take the real part of G(k), and this will turn your exp(ikx) into something real. Re-express what you've found in terms of the sum of two exp(ikx)-like terms. You'll see that one of these looks like the Fourier transform. The other is a Fourier transform as well, something quite close to the first term in fact. (Substitute x for -x.) Now you should be able to see how you can take the IFT of both terms. In this analysis, Euler's formula Links to an external site., relating exp(iy) to cos(y) an sin(y) comes in handy. And so does how cos(y) is related to things like exp(iy).
- 2. What is the (1D) fourier transform of cos(ax)?
Here "a" is a parameter.
plot(real(fft(cos(arange(128)*10*2*pi/128))))
Try changing this to:
plot(real(fft(cos(arange(128)*25*2*pi/128))))
Compare this is with the analytical result for cos(ax) You're really calculating a discrete fourier tranform (DFT)
For a discussion of DFT's see http://texas.math.ttu.edu/~gilliam/ttu/dft.pdf Links to an external site. and http://en.wikipedia.org/wiki/Discrete_Fourier_transform Links to an external site.
Reading on Fourier Transforms
Great intuitive introduction with many links to interesting material:
http://www.ysbl.york.ac.uk/~cowtan/fourier/fourier.html Links to an external site.
This is explained without a lot of math but gives you a visual idea of how Fourier Transforms work. You are expected to understand Fourier Transforms and the Convolution Theorem (also described there) at this level.