main.cpp 3.65 KB
Newer Older
Laurent Belcour's avatar
Laurent Belcour committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#include <core/args.h>
#include <core/data.h>
#include <core/function.h>
#include <core/fitter.h>
#include <core/plugins_manager.h>

#include <QApplication>

#include <iostream>
#include <vector>
#include <iostream>
#include <fstream>
#include <limits>
#include <cstdlib>
#include <cmath>
#include <engine.h>

#define BUFFER_SIZE 10000

int main(int argc, char** argv)
{
	QApplication app(argc, argv, false);
	arguments args(argc, argv) ;

	plugins_manager manager(args) ;

	if(args.is_defined("help")) {
28
		std::cout << "<<HELP>> fouriertransform --input data.file --output gnuplot.file --loader loader.so --in [input vector] --raw gnuplot.file" << std::endl ;
Laurent Belcour's avatar
Laurent Belcour committed
29 30 31 32 33 34 35 36 37 38 39 40 41
		std::cout << " - input and output are mandatory parameters" << std::endl ;
		return 0 ;
	}

	if(! args.is_defined("input")) {
		std::cerr << "<<ERROR>> the input filename is not defined" << std::endl ;
		return 1 ;
	}
	if(! args.is_defined("output")) {
		std::cerr << "<<ERROR>> the output filename is not defined" << std::endl ;
		return 1 ;
	}

42
	ptr<data> d = NULL ;
Laurent Belcour's avatar
Laurent Belcour committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
	d = manager.get_data(args["loader"]) ;
	d->load(args["input"]);

	// Create output file
	std::ofstream file(args["output"].c_str(), std::ios_base::trunc);

	if(d != NULL)
	{
		// Create matlab engine
		Engine *ep;
		if (!(ep = engOpen(""))) 
		{
			std::cerr << "<ERROR>> can't start MATLAB engine" << std::endl ;
			return false ;
		}


		std::cout << "<<INFO>> will export " << d->size() << " elements" << std::endl ;
61
		
Laurent Belcour's avatar
Laurent Belcour committed
62
		vec in(3), out(3) ;
63 64 65
		vec arg_in = args.get_vec("in", 2);
		double theta_in = (double)arg_in[0];
		double phi_in   = (double)arg_in[1];
Laurent Belcour's avatar
Laurent Belcour committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
		in[0] = cos(phi_in)*sin(theta_in);
		in[1] = sin(phi_in)*sin(theta_in);
		in[2] = cos(theta_in);

		const int N = 100;
		double* data  = new double[N];
		double* xdata = new double[N];
//		for(int i=0; i<N; ++i)
		int i=0;
		for(int j=0; j<N; ++j)
			{
				double phi   = (i-N/2) * M_PI / (N-1) * 2;
				double theta = (j-N/2) * M_PI / (N-1) ;
				out[0] = cos(phi)*sin(theta);
				out[1] = sin(phi)*sin(theta);
				out[2] = cos(theta);
				vec v = d->value(in, out) ;
83 84
				xdata[j] = theta;

Laurent Belcour's avatar
Laurent Belcour committed
85 86 87 88 89 90 91 92 93 94 95 96
				data[j]  = v[0] ;

				/*
				file << phi << "\t" << theta << "\t" ;
				for(int u=0; u<d->dimY(); ++u)
					file << v[u] << "\t" ;
			
				file << std::endl ;
				*/
			}

		// Create the MATLAB defintion of objects
97
		mxArray *d, *x, *f;
Laurent Belcour's avatar
Laurent Belcour committed
98 99 100 101 102 103 104 105 106 107 108 109 110
		d = mxCreateDoubleMatrix(  N, 1, mxREAL);
		x = mxCreateDoubleMatrix(  N, 1, mxREAL);
		memcpy((void *)mxGetPr(d), (void *) data,  N*sizeof(double));
		memcpy((void *)mxGetPr(x), (void *) xdata,  N*sizeof(double));

		engPutVariable(ep, "x", x);
		engPutVariable(ep, "d", d);

		char* output = new char[BUFFER_SIZE+1];
		output[BUFFER_SIZE] = '\0';
		engOutputBuffer(ep, output, BUFFER_SIZE) ;
		engEvalString(ep, "normd = sum(d) / max(size(d));");
		engEvalString(ep, "d = d ./ normd;");
111 112
		engEvalString(ep, "ff = fftshift(fft(d));");
		engEvalString(ep, "f = sqrt(real(ff).^2 + imag(ff).^2);");
Laurent Belcour's avatar
Laurent Belcour committed
113 114
		engEvalString(ep, "normf = sum(f) / max(size(f));");
		engEvalString(ep, "f = f ./ normf;");
115 116 117 118 119 120 121 122 123 124 125
//		engEvalString(ep, "plot(x, f, x, d)");
//		std::cout << output << std::endl ;

		f = engGetVariable(ep, "f");
		std::ofstream out_fourier(args["output"].c_str(), std::ios_base::trunc);
		double* val = (double*)mxGetData(f) ;
		for(int i=0; i<N; ++i)
		{
			out_fourier << i - N/2 << "\t" << val[i] << std::endl ;
		}
		out_fourier.close();
Laurent Belcour's avatar
Laurent Belcour committed
126

127 128 129 130 131 132 133 134 135
		if(args.is_defined("raw"))
		{
			std::ofstream out_raw(args["raw"].c_str(), std::ios_base::trunc);
			for(int i=0; i<N; ++i)
			{
				out_raw << xdata[i] << "\t" << data[i] << std::endl ;
			}
			out_raw.close();
		}
Laurent Belcour's avatar
Laurent Belcour committed
136

137 138
		mxDestroyArray(x);
		mxDestroyArray(d);
Laurent Belcour's avatar
Laurent Belcour committed
139 140 141 142 143 144 145 146 147
		engClose(ep);
	}	
	else
	{
		std::cerr << "<<ERROR>> cannot export data" << std::endl ;
	}

	return 0 ;
}