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

Ts=t(2)-t(1);
N=length(t);

if nargin>6
    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_shifted=posun(aif,delay);
Cfit=konvf(h,aif_shifted);
Cfit=Cfit-y;

% Cfit(1:5:end)=0;Cfit(2:5:end)=0;Cfit(3:5:end)=0;Cfit(4:5:end)=0;                 % orezani konce funkce !!!!!!
% Cfit=y-Cfit;

% 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_shifted);
        %         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)); % puvodni vzorec - jak ho ozavorkovat?
    %     ddelay=(-((N-1)/2)/(A.*(cos(A)-sinc(A/pi)))).';
    %     sensitivity_functions(end,:)=konvf(Cfit,ddelay); % puvodni
    ddelay(isnan(ddelay))=0;
    sensitivity_functions(end,:)=konvf(konvf(h,aif),ddelay);
    sensitivity_functions=sensitivity_functions.';
end
% Cfit=Cfit.';

end