Jpp  18.0.0-rc.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Functions
JFFT.cc File Reference

Auxiliary program to run fast Fourier transformation (FFT). More...

#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <fftw3.h>
#include <cmath>
#include "JLang/gzstream.h"
#include "Jeep/JeepToolkit.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Detailed Description

Auxiliary program to run fast Fourier transformation (FFT).


For more information, see FFTW.

Author
mdejong

Definition in file JFFT.cc.

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 23 of file JFFT.cc.

24 {
25  using namespace std;
26  using namespace JPP;
27 
28  string inputFile;
29  string outputFile;
30  double binWidth;
31  int precision;
32  int debug;
33 
34  try {
35 
36  JParser<> zap("Auxiliary program to run fast Fourier transformation (FFT).");
37 
38  zap['f'] = make_field(inputFile, "input file (containing 1D array of values)");
39  zap['o'] = make_field(outputFile, "output file (containing 2D array of values)") = "fft.txt";
40  zap['B'] = make_field(binWidth, "bin width of input array (if zero, use index)") = 0.0;
41  zap['F'] = make_field(precision, "number of decimals if output format") = 9;
42  zap['d'] = make_field(debug) = 2;
43 
44  zap(argc, argv);
45  }
46  catch(const exception& error) {
47  FATAL(error.what() << endl);
48  }
49 
50  // input
51 
52  vector<double> data;
53 
54  istream* in = open<istream>(inputFile);
55 
56  if (in == NULL) {
57  FATAL("Invalid file " << inputFile << endl);
58  }
59 
60  for (double x; *in >> x; ) {
61  data.push_back(x);
62  }
63 
64  close(in);
65 
66  const size_t N = data.size();
67 
68  if (N == 0) {
69  FATAL("Number of points: " << N << endl);
70  }
71 
72  // FFT
73 
74  const size_t NS = N/2 + 1;
75  double* input = (double*) malloc(sizeof(double) * N);
76  fftw_complex* output = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * NS);
77  fftw_plan plan = fftw_plan_dft_r2c_1d(N, input, output, FFTW_MEASURE);
78 
79  for (size_t i = 0; i != N; ++i) {
80  input[i] = data[i];
81  }
82 
83  // execute plan
84 
85  fftw_execute(plan);
86 
87  // destroy plan
88 
89  fftw_destroy_plan(plan);
90 
91  // output
92 
93  const double xmin = (binWidth != 0.0 ? 1.0 / (N * binWidth) : 0.0);
94  const double xmax = (binWidth != 0.0 ? 1.0 / (binWidth) : 0.0);
95 
96  const int width = precision + 7;
97 
98  ostream* out = open<ostream>(outputFile.c_str());
99 
100  for (size_t i = 0; i != NS; ++i) {
101 
102  const double real = output[i][0];
103  const double imag = output[i][1];
104  const double power = sqrt(real*real + imag*imag);
105 
106  if (binWidth != 0.0)
107  *out << SCIENTIFIC(width,precision) << xmin + (double) i * (xmax - xmin) / (double) (N - 1);
108  else
109  *out << i;
110 
111  *out << ' ';
112  *out << SCIENTIFIC(width,precision) << power << endl;
113  }
114 
115  fftw_free(output);
116  free(input);
117 
118  close(out);
119 }
const double xmax
Definition: JQuadrature.cc:24
Utility class to parse command line options.
Definition: JParser.hh:1514
string outputFile
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1989
void close(std::istream *pf)
Close file.
Definition: JeepToolkit.hh:386
#define FATAL(A)
Definition: JMessage.hh:67
const double xmin
Definition: JQuadrature.cc:23
then usage $script< input file >[option[primary[working directory]]] nWhere option can be N
Definition: JMuonPostfit.sh:36
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:46
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:484
int debug
debug level