function [fh,omega] = ftrans( effe, N, p, q, omega0)
%FTRANS	Fourier transform
%   [F,w]=ftrans(effe,N,p,q,omega0) computes the Fourier transform of function
%   effe (function handle) sampling it on N equispaced points in [p,q].
%   The tranform sampling points are returned in w, omega0 is the central point
%   of the interval, that is, (w(1)+w(end))/2.
%   Only the first parameter is mandatory, the others have default values.
%   Example: [F,w]=ftrans(@(x)exp(-abs(x)),256,-30,30,5).
%
%   This function uses the standard definition:
%      F(k) = int_{-inf}^{inf} f(x) exp(-ikx) dx

%	Giuseppe Rodriguez,  23-04-10
%	Email: rodriguez@unica.it

if (nargin<5),  omega0 = 0;  end
if (nargin<4),  p = -20;  q = 20;  end
if (nargin<2)||isempty(N),  N = 256;  end
if mod(N,2),  error('N must be even.'),  end

A = q-p;
x0 = (p+q)/2;
Omega = 2*pi*N/A;
rho = exp(-1i*x0*omega0);
if x0
	d1 = exp(-1i*x0*2*pi/A*[-N/2:N/2-1]');
else
	d1 = 1;
end
if omega0
	d2 = exp(-1i*omega0*A/N*[-N/2:N/2-1]');
else
	d2 = 1;
end

x = linspace( -A/2, A/2, N+1)';
x = x(1:N) + x0;
f = effe(x);
fh = fft(fftshift(d2 .* f));
fh = rho * A/N * (d1 .* fftshift(fh));
omega = linspace( -Omega/2, Omega/2, N+1)';
omega = omega(1:N) + omega0;

