Tuesday, December 13, 2011

A simple FFT

So, following my PDE's class, I decided to try my hand at writing an FFT algorithm. I ended up doing it in both c and Fortran. Surprisingly, I found it easier to write in Fortran (accounting for the fact that I am more familiar with c, and had to look up the syntax for just about everything in fortran). So, without further ado, here is my code:

FORTRAN:


 module FFT  
   implicit none  
   save  
   public  
   REAL, parameter :: PI = 4*atan(1.0)  
   contains  
   recursive function fft1(f) result(r)  
     !assumes that f is of length n where n is a power of 2  
     !output is zero indexed  
     COMPLEX, intent(in) :: f(:)   
     integer :: N, first, last, k   
     COMPLEX :: r(0:size(f)-1)  
     COMPLEX, dimension(0:size(f)/2-1) :: even, odd  
     N = size(f)  
     first = lbound(f,1)  
     last=ubound(f,1)  
     if( N == 1) then  
       r = f  
       return  
     end if  
     even = fft1(f(first:last-1:2))  
     odd = fft1(f(first+1:last:2))  
     do k=0,N/2-1  
      odd(k) = exp((0,-2)*PI*k/N)*odd(k)  
     end do  
     r(0:N/2-1) = even+odd  
     r(N/2:N-1) = even-odd  
   end function  
   recursive function ifft1(f) result(r)  
     !assumes that f is of length n where n is a power of 2  
     !output is zero indexed  
     COMPLEX, intent(in) :: f(:)   
     integer :: N, first, last, k   
     COMPLEX :: r(0:size(f)-1)  
     COMPLEX, dimension(0:size(f)/2-1) :: even, odd  
     N = size(f)  
     first = lbound(f,1)  
     last=ubound(f,1)  
     if( N == 1) then  
       r = f  
       return  
     end if  
     even = ifft1(f(first:last-1:2))  
     odd = ifft1(f(first+1:last:2))  
     do k=0,N/2-1  
       odd(k) = exp((0,2)*PI*k/N)*odd(k)  
     end do  
     r(0:N/2-1) = even+odd  
     r(N/2:N-1) = even-odd  
     r=r/2  
   end function  
 end module  

C:

 #include <complex.h>  
 #include <math.h>  
 #include <stdlib.h>  
 #include "fft.h"  
 double complex* _fft1( double complex*, size_t, unsigned int);  
 double complex* _ifft1(double complex*, size_t, unsigned int);  
 /** use the Cooley-Tukey method to take the fast fourier transform of an  
  * Array of lenth 2^n   
  *  
  * input and output are both complex arrays of length N, which must be a  
  * power of 2. Returns 0 if succusfull, nonzero if failed.  
  **/  
 double complex* fft1( double complex *input, size_t N) {  
   return _fft1(input,N,1);  
 }  
 double complex* _fft1( double complex *data, size_t N, unsigned int step) {  
   double complex *result;  
   double complex *even;  
   double complex *odd;  
   size_t k;  
   if ( (N > 2) && ( N % 2 != 0 )) return NULL;  
   result = (double complex*) malloc( sizeof(double complex) * N);  
   if ( N ==1 ) {  
     result[0]=data[0];  
     return result;  
   }  
   even = _fft1(data, N/2, 2*step);  
   odd = _fft1(data+step, N/2, 2*step);  
   //multiply the odds by the twiddle factor e^(-i*2*pi*k/N)  
   for( k=0; k < N/2; k++) {  
     odd[k] = cexp(-2.0*I*M_PI*k/N)*odd[k];  
   }  
   //generate result  
   for( k =0; k < N/2; k++) {  
     result[k] = even[k] + odd[k]; //first N/2 results  
     result[k+N/2] = even[k]-odd[k]; // last N/2 results  
   }  
   free(even);  
   free(odd);  
   return result;  
 }  
 double complex* ifft1( double complex* data, size_t N) {  
   return _ifft1(data, N, 1);  
 }  
 double complex* _ifft1( double complex *data, size_t N, unsigned int step) {  
   double complex *result;  
   double complex *even;  
   double complex *odd;  
   size_t k;  
   if ( N % 2 != 0 ) return NULL;  
   result = (double complex*) malloc( sizeof(double complex) * N);  
   if ( N ==1 ) {  
     result[0]==data[0];  
     return result;  
   }  
   even = _ifft1(data, N/2, 2*step);  
   odd = _ifft1(data+1, N/2, 2*step);  
   //multiply the odds by the twiddle factor e^(i*2*pi*k/N)  
   for( k=0; k < N/2; k++) {  
     odd[k] = cexp(I*2*M_PI*k/N)*odd[k];  
   }  
   //generate result  
   for( k =0; k < N/2; k++) {  
     result[k] = (even[k] + odd[k])/2; //first N/2 results  
     result[k+N/2] = (even[k]-odd[k])/2; // last N/2 results  
   }  
   free(even);  
   free(odd);  
   return result;  
 }  

Sunday, November 13, 2011

What is a miracle

The first definition of miracle on Dictionary.com is "an effect or extraordinary event in the physical world that surpasses all known human or natural powers and is ascribed to a supernatural cause." I don't really like this definition, and this post will explain why.

First of all, what does it mean for something to be caused by a supernatural cause? It would be something that does not obey the laws of the universe, correct? But the universe, and its laws were created by God. And why would God ever have need to break his own laws? God certainly influences things in the universe, and performs miracles on scales from affecting the entire universe to a single individual. But I have no reason to believe that he does so outside of the laws of the universe that he established at his creation. As an Example look at Moses parting the Red Sea. It is entirely conceivable that some natural phenomenon caused the water to stop flowing at some point, and then start again. But why should that preclude the involvement of God. It would be a remarkable coincidence for the phenomenon to occur at precisely the right time for the Israelites to cross on dry land. If it is the case, as I speculate that it is, that God performed the miracle by utilizing natural forces, then Moses' miracle was the result of a natural cause, but it was also a miracle of God.

Now let me talk about my own life. There have been a vast many occurances in my life that are entirerly explainable by human and/or natural forces, which nonetheless I would consider miracles. Often they take the form of something like me, or someone else, being in the right place at the right time, or someone saying something I really needed to hear, or a pathway opening to me that had previously been shut. I believe that these sorts of things happen to everyone. Some may say that they are just coincidences or random occurances. But the frequency with which they have occured in my life is too statistically significant to reasonably accept that as an explanation. I see the work of God in my life, and the lives of others, even when they do not see it. That is what I call a miracle.

You can do incredible things with science and technology. Today's technology would look like magic to the ancients. And our current understanding of the laws of the universe is limited at best. Just look at quantum mechanics. We can make remarkable predictions in that realm, but we have only half-baked philosophical musings about what is really going on. There are many things in this world that cannot be explained by the science we currently have, but they are not considered miracles by most, because eventually when our knowledge progresses we can explain them as natural phenomina. And there is nothing wrong with that. But just because something is natural doesn't mean it doesn't come from God.

One of God's greatest miracles is the miracle of creation. It is a miracle that we can see just by looking around us, and on deeper study the miracle only becomes more profound. If the physical constants that show up in our laws of physics differed by even a small amount, the universe would be the way it is, there might have been no stars, no matter, and certaiinly no life as we know it. And there is a beuaty and symmetry in the way the world works, down from the most complex organisms on Earth (i.e. humans) to the most basic laws of physics.

In order to miss the works of God in the Universe you must actively be trying not to see them.

Wednesday, August 17, 2011

Thoughts on Unity

This is just a short post. I have finally had a chance to try out Ubuntu's Unity Desktop. In short, it appears to be an attempt to mimac macs. There is a dock along the side which functions pretty well with basic drag and drop functionality. Although I would like a little bit more customizability, such as controlling when it is visible and maybe put special applets on it. Also, the if a program is maximized its menu bar combines with the task-bar. Which is kind of cool, but is inconstant with non-maximized windows, and isn't obvious since the menus are hidden unless the mouse is hovering over them.

As for the Ubuntu symbol in the left top corner, clickin on it gives you something akin to Kickoff in KDE4, where you have an entry box where you can type the name of an application or folder and it will show results which you can select to run. It also lists favorites, and shortcuts to menus for other apps. The downside is that if browsing through you applications is a bit tedious with the mouse, however it gives you hints about uninstalled programs you might like, which I think is pretty cool.

Overall, I am pretty impresssed by Unity, but I still like KDE 4 better. And Enlightenment for when I want something a little simpler.

Apple, iTunes, and DRM

First of all, as a Linux user, I am rather annoyed by the fact that you need to use iTunes in order to purchase anything from the iTunes store. Then again, it doesn't matter all that much, since DRM makes it impossible to play anyways.

DRM for those of you that don't know is "Digital Rights Management," or as some more correctly call it "Digitla Restrictions Management." It is the use of technology to restrict the ways that users can use copyrighted material, and is supposedly intended to combat Piracy, though in reality I think the restrictions it places on users, only makes piracy more popular and illegally downloaded materials more attractive than legal downloads. http://www.slate.com/id/2298871/pagenum/all/gives a very good discussion of the issue.

Anyway, my family recently purchased the Third Season of Merlin (from BBC) in my absense via iTunes without fully understanding the limitations of DRM. Then we discovered just how crippling DRM is. First of all, we can't burn the videos onto a DVD that is playable by our DVD player, which would make it impossible for most people to watch the movies on their TVs. Fortunately we have a (older) computer hooked up to our TV, but that only partly solved the problem. The videos are in the .m4v format, and are encrypted which means that the only program that can play it is iTunes. This would be a little annoying in and of itself (I would prefer to play it with VLC), but it is even worse becuas playing movies (at least these TV episodes) is very glitchy on iTunes, with frames constantly freezing, the audio misaligning with the video, and worst of all, about once every 10 minutes iTunes crashes, and it takes about 5 minutes to stop the not responding iTunes program and start it again. To be fair, I was playing off of a data DVD which might possibly be damagged, and iTunes was unable to copy the files to the hard drive, (another failing for iTunes).

Ok, so I looked around and tried to figure out if there was a way to remove the DRM from the files so I could actually watch the content we purchased. And there was, unfortunately it involved playing the content, and recording it. Well, if iTunes crashes whenever I try to play it I can't really record it again, can I?

So, Apple is not going to get any business from me anytime soon, and never if they don't start offering products that are more interoperable with products provided by companies other than Apple. And I am going to avoid DRM as much as possible, which might mean that I will be sticking to DVDs and hard copies until the Movie Industry realizes that DRM is restricting their profits and making consumers unhappy. The music industry figured that out eventually, I just hope that the movie industyr figures it out faster. And I would really like Congress to repeal the Digital Millenium Copyright Act, which is in contradiction with the Fair Use Act anyway.

By the way, if anyone knows of a way to legally obtain movies that are playable on Linux besides ripping DVDs, or to remove the DRM of iTunes movies, please let me know.

Wednesday, July 13, 2011

Installing Peppermint OS without a CD

In this post, I will describe how I installed Peppermint Linux onto my harddrive without burning a CD and the issues I ran into.  First I should warn you that this method is not for the light-hearted. It involves manually editing GRUB configurations, chrooting, loop mounting, etc.

The first step is to prepare the partition.  In my case I formated an existing partition as ext4 using the mkfs command. I won't go into the details of how to do this, because I assume that you either already know how to do it, or are intelligent enough to find a howto somewhere else. 

The next step is to get the installation ISO of the desired distro and make the filesystem accessible. In my case I just downloaded the 64-bit ISO from the Peppermint OS website.  Then I mounted the ISO image using a command of the form "mount -o loop path/to/isofile.iso /mnt/mountpoint" which needs to be run as root. While you are at it you also need to make sure that the partition you want to install to is mounted (from now on I will refer to that mount point as /mnt/new).  Finally, you will also need to find the squashfs filesystem on the ISO, I believe for PeppermintOS it was in the Casper folder, but it could vary from distro to distro. You will either need to extract it or mount it.  I mounted it because I didn't have the squashfs tools installed, but you could probably extract it directly into the desired partition. In my case I mounted the squashfs with the command "mount -o loop -t squashfs filesystem.squashfs /mnt/squash." Then once I had it mounted I copied the entire contents into the new partitions with "cp -R ./* /mnt/new". 

I had a slight problem in that the squashfs only appeared to have a broken symlink instead of a kernel, so make sure that you have a kernel (vmlinuz26) and an initram disk (prabably initrd.img or something like that). It may be in the root directory or in the boot directory depending on the distro.

Now you need to modify grub so you can actually log into your new OS.  In my case (I am using grub 2) I added a new menu item with the following lines:

set root="(hd0,5)"
linux /vmlinuz root=/dev/sda5 ro quiet splash
initrd /initrd.img
The last part is the trickiest, and most likely to change from OS to OS.  I was unable to log in because there wasn't any account created, so I went back to my original OS and chrooted into the partitions (note you will need to mount with the exec option). Then I created a new user account, and granted it sudo priveledges. While in the chroot I also ran dpkg-reconfigure on several packages, such as the locale packages, and edited /etc/fstab and a couple other /etc files to behave properly, and for some reason I also needed to give the /tmp folder world writeable access (don't know why it didn't have it before). Unfortunately I am still having some kind of trouble with dbus, and I am not sure what is causing it.  I've tried reinstalling it, recnfiguring it, etc.  But since my Arch Linux works fine, and this was more of an experiment than anything else, I haven't worried too much about it. 

So, installing Linux this way is a lot of effort, but if, like me, you like to do things the hard way and are too cheap to buy a CD :), or if you don't have a CD drive on the computer you want to put it on, this is definetely an option.

Wednesday, April 20, 2011

On the Art of Science

Today, I will write about a somewhat common misconception about science. And it is centered around the phrase "It's an art, not a science." When someone says that, what they generally mean is something more along the lines of "It is a non-algorithmical process." Or equivelently, "It is something that can't be done by a computer." But I think that that phrase is damaging to the public perception of science. In fact we could use that phrase to describe every field of scienc that I am aware of, becuase they all require a significant amount of creative, imaginative, and distinctly non-algorithmical thinking. And these days, many of the parts of science that are simply precisely following a set procedure are done by computers, or are otherwise largely mechanized.

That isn't to say that collecting and cataloging data using precise repeatable procedures isn't an important of science, but saying that that is all of science is like saying that the mechanics of applying paint onto canvas is all of art. But there is some much more to science. For one thing interpretting the results require a great deal of creativity. If the universe worked the way we expected it to there would be no place for science. Science progresses only because of people that are able to think outside the box and come up with plausable explanations for data that doesn't fit the currently accepted models. For another, scientists most come up with increasingly more inventive ways of collecting data. The experimental methods of the 1600's are entirely inadequate for exploring the internal structure of atoms, much less finding the Higgs boson, or analyzing gamma ray bursts (forgive me if my examples are a little, well a lot, biased toward physics and astronomy, that is what I know best). Science is not, simply applying the rules of the universe to certain problems, although that is the way it is often taught in primary and secandary school. Rather it is trying to discover what those laws are, and there aren't any preset rules on how to do that.

Sunday, January 23, 2011

On Operating System

As an avid Linux user, I have seen a lot of debate about the pros and cons of various operating systems (henceforth referred to as OSs). And my current opinion is that there is no best OS for everyone. For me Linux is the best operating system, specifically, Arch Linux, with its extreme customizability, is the best OS for me. But not everyone loves taking things apart and putting them back together the way I do. And probably the majority of computer users don't want to spend multiple hours at a command prompt just so that they can have a system that is customized to their specific needs and wants. And so I will not religiously recommend GNU/Linux to anyone and everyone I meet. And although I personally hate using Mac OS X, I can understand why so many people love it. To put it simply: Macs make easy things easier and hard things harder, while Linux makes easy things harder and hard things easier.

Now, technology is pushed by competition, and so, much as I love Linux, I do not want it to dominate the operating system market. I want there to be competition and I don't care whether it is proprietary or open-source. But for this same reason I have a very big problem with Microsoft Windows. The problem is that they hold way too much of the market, and thereby stifle a good deal of competition. Furthermore, Windows isn't that great of an OS. I have used Windows for many years (including Windows 7), and I have used Linux for a couple of years, and I have used Macs from time to time. Of the three I like Windows the least. Here is a list of the advantages that Windows has, if anyone has something to add to it please post a comment:

  1. It comes preinstalled on cheap hardware (admit it Apple hardware is more expensive).
  2. It's what most people are used to. If you use it at work or at school, why not use it at home as well?
  3. There are a lot of good programs that are only available on Windows. Especially games, and professional software.
  4. Hardware drivers are sometimes only available for Windows
  5. Compatibility with the rest of the Windows using world

The thing about this list, is that everything on it is a result of Microsoft's near monopoly, not the quality of the product itself. I'm not saying that Windows is a terrible operating system, because it isn't. But it isn't great either. In my opinion it is mediocre, and it relies far too heavily on market power. If computer distributors distributed desktops and laptops preloaded with Ubuntu, and schools and businesses used Ubuntu on all of their computers, then the above list would apply to Ubuntu.

For the betterment of technology through greater competition, I want a more even playing field. The OEM deals that put Windows on just about every consumer computer should be outlawed as monopolistic behavior. Consumers should have the opportunity to choose the Operating System of their choice when purchasing a computer, without having to uninstall an unwanted operating system and installing a new one. The government, and ideally businesses and organizations as well, should support open standards such as ODF, which are OS independent. And perhaps most importantly, schools should have computers running every major OS available to students, so that they can try each one, and find which one works best for them. I understand that some schools may not be able to afford this luxury, but then why are they paying for Windows licenses when Linux is free?

I would like to end this post by saying that although I support Open Source Software, I am not strictly opposed to proprietary software. I am, however, opposed to monopoly. I am perfectly okay with other people using Windows, and OS X. What bothers me is that I have to pay for an operating system I do not want in order to get the hardware I do want, and that when I go to a library or any public (or even private) computer lab, I have to use an operating system I don't really care to use.