## FFTW and MelGibson

Printing an image before applying FFTW

Using the FFTW library, we can improve the visualization of the edges of an image. In this case we are going to use Fortran to compile the program fftw1.c using a make file: Now lets run it to have the image indicated in the source code Working with Plans to make it better after applying FFTW

Lets create the plan forward and backward

```plan_forward = fftw_create_plan(NX,FFTW_FORWARD,FFTW_MEASURE);
plan_backward = fftw_create_plan(NX,FFTW_BACKWARD,FFTW_MEASURE);```

Now we are going to perform the plan forward FFT in one dimension

```  for(i=0;i<NX;i++)
{
for(j=0;j<NY;j++)
{
store[i][j].re = data[i][j];
store[i][j].im = 0.0;
}
}
for (i=0;i<NY;i++)
{
for (j=0;j<NX;j++)
{
in[j].re = store[i][j].re;
in[j].im = store[i][j].im;
}
fftw_one(plan_forward, in, out);
for (j=0;j<NX;j++)
{
store[i][j].re=out[j].re;
store[i][j].im=out[j].im;
}
}```

Let’s perform it in the 2nd dimension

```for (j=0;j<NY;j++)
{
for (i=0;i<NX;i++)
{
in[i].re = store[i][j].re;
in[i].im = store[i][j].im;
}
fftw_one(plan_forward, in, out);
for (i=0;i<NX;i++)
{
store[i][j].re=out[i].re;
store[i][j].im=out[i].im;
}
}```

Writing the real and imaginary parts of the image in Fourier space to separate files.

``` for (i=0;i<NX;i++)
{
for (j=0;j<NX;j++)
{
data[i][j] = store[i][j].re;
}
}
pgmwrite("data_real.pgm", data, NX, NY, 1, 2);
for (i=0;i<NX;i++)
{
for (j=0;j<NX;j++)
{
data[i][j] = store[i][j].im;
}
}
pgmwrite("data_imag.pgm", data, NX, NY, 1, 2);```

Operate on the image in Fourier space; either:

*  1) cut off high frequencies, or

*  2) cut off low frequencies

`  filter_high_frequencies(store);`

Performing backward FFT in the 1st dimension.

```  for (i=0;i<NY;i++)
{
for (j=0;j<NX;j++)
{
in[j].re = store[i][j].re;
in[j].im = store[i][j].im;
}
fftw_one(plan_backward, in, out);
for (j=0;j<NX;j++)
{
store[i][j].re=out[j].re;
store[i][j].im=out[j].im;
}
}```

Performing backward FFT in the 2nd dimension.

```  for (j=0;j<NY;j++)
{
for (i=0;i<NX;i++)
{
in[i].re = store[i][j].re;
in[i].im = store[i][j].im;
}
fftw_one(plan_backward, in, out);
for (i=0;i<NX;i++)
{
store[i][j].re=out[i].re;
store[i][j].im=out[i].im;
}
}```

Storing the final image.

```    for (i=0;i<NX;i++)
{
for (j=0;j<NX;j++)
{
data[i][j]=(store[i][j].re)/(NX*NX);
}
}
pgmwrite("finalFFT.pgm", data, NX, NY, 0, 255);```

Cleaning up the plans:

```   fftw_destroy_plan(plan_forward);

fftw_destroy_plan(plan_backward);```

Finally compiling and running and the Mel Gibson image will improve with the functions.

```gcc -O2 -L \$LIB  -l \$PATH-o  fftw1 fftw1.o io.o -lsfftw -lm

./fftw1``` 