USGS

Isis 3.0 Application Source Code Reference

Home

ifft.cpp

Go to the documentation of this file.
00001 #include "Isis.h"
00002 #include <complex>
00003 #include "FourierTransform.h"
00004 #include "ProcessByTile.h"
00005 #include "AlphaCube.h"
00006 
00007 using namespace std;
00008 using namespace Isis;
00009 
00010 void IFFT1(vector<Buffer *> &in, vector<Buffer *> &out);
00011 void IFFT2(vector<Buffer *> &in, vector<Buffer *> &out);
00012 
00013 FourierTransform fft;
00014 QString tmpMagFileName = "Temporary_IFFT_Magnitude.cub";
00015 QString tmpPhaseFileName = "Temporary_IFFT_Phase.cub";
00016 
00017 void IsisMain() {
00018   // We will be processing by line first
00019   ProcessByTile lProc;
00020   lProc.Progress()->SetText("First pass");
00021 
00022   // Get the original cubes dimensions
00023   UserInterface &ui = Application::GetUserInterface();
00024   Pvl lab;
00025   lab.Read(ui.GetFileName("MAGNITUDE"));
00026   AlphaCube acube(lab);
00027   int initSamples = acube.BetaSamples();
00028   int initLines = acube.BetaLines();
00029 
00030   // Setup the input and output cubes
00031   Cube *magCube = lProc.SetInputCube("MAGNITUDE");
00032   Cube *phaseCube = lProc.SetInputCube("PHASE");
00033 
00034   int numSamples = magCube->sampleCount();
00035   int numLines = magCube->lineCount();
00036   int numBands = magCube->bandCount();
00037 
00038   // error checking for valid input cubes
00039   // i.e. the dimensions of the magnitude and phase cubes
00040   // are the same and are powers of two
00041   if(!fft.IsPowerOfTwo(numSamples) || !fft.IsPowerOfTwo(numLines)
00042       || magCube->sampleCount() != phaseCube->sampleCount()
00043       || magCube->lineCount() != phaseCube->lineCount()) {
00044     cerr << "Invalid Cubes: the dimensions of both cubes must be equal"
00045             " powers of 2." << endl;
00046     return;
00047   }
00048 
00049   lProc.SetTileSize(numSamples, 1);
00050 
00051   Isis::CubeAttributeOutput cao;
00052 
00053   lProc.SetOutputCube(tmpMagFileName, cao, numSamples, numLines, numBands);
00054   lProc.SetOutputCube(tmpPhaseFileName, cao, numSamples, numLines, numBands);
00055 
00056   // Start the line processing
00057   lProc.ProcessCubes(&IFFT2);
00058   lProc.Finalize();
00059 
00060   // Then process by sample
00061   ProcessByTile sProc;
00062   sProc.Progress()->SetText("Second pass");
00063   sProc.SetTileSize(1, numLines);
00064 
00065   // Setup the input and output cubes
00066   Isis::CubeAttributeInput cai;
00067 
00068   sProc.SetInputCube(tmpMagFileName, cai);
00069   sProc.SetInputCube(tmpPhaseFileName, cai);
00070 
00071   // the final output cube is cropped back to the original size
00072   sProc.SetOutputCube("TO", initSamples, initLines, numBands);
00073 
00074   //Start the sample proccessing
00075   sProc.ProcessCubes(&IFFT1);
00076   sProc.Finalize();
00077 
00078   remove(tmpMagFileName.toAscii().data());
00079   remove(tmpPhaseFileName.toAscii().data());
00080 }
00081 
00082 // Processing routine for the inverse fft
00083 void IFFT1(vector<Buffer *> &in, vector<Buffer *> &out) {
00084   Buffer &inReal = *in[0];
00085   Buffer &inImag = *in[1];
00086 
00087   int n = inReal.size();
00088   vector< complex<double> > input(n);
00089 
00090   // copy and rearrange the data to fit the algorithm
00091   // the image is centered at zero, the array begins at zero
00092   for(int i = 0; i < n / 2; i++) {
00093     input[i] = complex<double>(inReal[i+n/2], inImag[i+n/2]);
00094     input[i+n/2] = complex<double>(inReal[i], inImag[i]);
00095   }
00096 
00097   // compute the inverse fft
00098   vector< complex<double> > output = fft.Inverse(input);
00099 
00100   Buffer &image = *out[0];
00101   // and copy the result to the output cube
00102   for(int i = 0; i < n; i++) {
00103     image[i] = real(output[i]);
00104   }
00105 }
00106 
00107 // Processing routine for the inverse fft with two output cubes
00108 void IFFT2(vector<Buffer *> &in, vector<Buffer *> &out) {
00109   Buffer &mag = *in[0];
00110   Buffer &phase = *in[1];
00111 
00112   int n = mag.size();
00113   vector< complex<double> > input(n);
00114 
00115   // copy and rearrange the data to fit the algorithm
00116   // the image is centered at zero, the array begins at zero
00117   for(int i = 0; i < n / 2; i++) {
00118     input[i] = complex<double>(polar(mag[i+n/2], phase[i+n/2]));
00119     input[i+n/2] = complex<double>(polar(mag[i], phase[i]));
00120   }
00121 
00122   // compute the inverse fft
00123   vector< complex<double> > output = fft.Inverse(input);
00124 
00125   Buffer &realCube = *out[0];
00126   Buffer &imagCube = *out[1];
00127   // and copy the result to the output cubes
00128   for(int i = 0; i < n; i++) {
00129     realCube[i] = real(output[i]);
00130     imagCube[i] = imag(output[i]);
00131   }
00132 }