function [Cfit,sensitivity_functions]=C_fitted_delay(x,t,aif,model,rescale_parameters,x_fixed,x_start)
% ziska model dle zadanych parametru
% provede konvoluci s aif
% (vypocte sensitivity functions)

Ts=t(2)-t(1);
if nargin>5
    x2=x;
    x=zeros(1,length(x_fixed));
    x(~x_fixed)=x2;
    x(x_fixed)=x_start(x_fixed);
end
x=x./rescale_parameters;  % pozor !!!!!!!!!!!!!!!!!

delay=x(end);
x=x(1:end-1);
h=model(x,t);

aif=posun(aif,delay);
Cfit=konvf(h,aif);
% Cfit((end-1)/2:end)=0;
% NaNy2=sum(isnan(h))
% Infy2=sum(isinf(h))
if nargout>1
    [h,dh]=model(x,t);
    sensitivity_functions=zeros(length(x)+1,length(t));
    for n=1:length(x)
        sensitivity_functions(n,:)=konvf(dh(:,n),aif);
%         sensitivity_functions(:,(end/2):end)=0;
    end
    A=pi*t/Ts-delay*(N-1)/2;
    ddelay=-((N-1)/2)/A.*(cos(A)-sinc(A/pi));
    sensitivity_functions(end,:)=konvf(Cfit,ddelay);
end
% Cfit=Cfit.';
end