function [phys,grid] = TS_itransform (spec, nphys, basis)
% Computes 'spec', the first 'ndeal' dealiased coefficients
% of the expansion of 'phys' on the 'basis' functions

% phys is a vector of values of a function on a physical grid.
% ndeal is an even integer at most equal to the length of 'phys'.
% basis is a string 'a' or 'b' (see equation [18]).

nspec=length(spec);
h_max_cos = floor((nspec-1)/2);
h_max_sin = floor( nspec   /2);

temp = zeros(nphys,1);
grid = zeros(nphys,1);

if strcmp(basis,'a')
   temp(1) = spec(1);
   temp(3:2:(h_max_cos*2+1)) = temp(3:2:(h_max_cos*2+1)) + 1/ 2    *spec(3:2:(h_max_cos*2+1));
   temp(3:2:(h_max_sin*2+1)) = temp(3:2:(h_max_sin*2+1)) + 1/(2*1i)*spec(2:2:(h_max_sin*2));
   temp(nphys+2-(3:2:(h_max_cos*2+1))) = temp(nphys+2-(3:2:(h_max_cos*2+1))) + 1/ 2    *spec(3:2:(h_max_cos*2+1));
   temp(nphys+2-(3:2:(h_max_sin*2+1))) = temp(nphys+2-(3:2:(h_max_sin*2+1))) - 1/(2*1i)*spec(2:2:(h_max_sin*2));

elseif strcmp(basis,'b')
    disp('Not implemented yet. Will be uploaded soon.');
    return;

else
    disp('wrong ''basis'' flag. Must be ''a'' or ''b''');
    return;

end;

phys = ifft(temp)*nphys;
grid = 2*pi/nphys*((1:(nphys/2+1))-1);
err = phys((nphys/2+2):end) - phys(2:nphys/2);
a = std(err)
phys((nphys/2+2):end)=[];


   
