function [AP_count, Rheobase, lin_Rin, AP_height, half_width, dep_speed,...
    rep_speed, thresh, adaptation_ratio,APtime_out] = FI_analysis(FI_master, Freq, userdvdt,...
    MiniFI, MaxFI, DeltaFI, Rin_check, step_dur, bl_dur, sweepnumbers, APdetect)

if max(max(FI_master(:,:,sweepnumbers))) < APdetect 
        uiwait(errordlg('No APs detected in sweeps - check that all required sweeps are visible'));
        return
end

correction = (0.0156 * (size(FI_master,1) / Freq))/(1/Freq);

FI_master(1:correction,:,:) = [];

timebase = (1:length(FI_master))*(1/Freq);
currents = [MiniFI:DeltaFI:MaxFI];

% input resistance

deleted_sweeps = [];
lin_Rin = [];
if Rin_check == 1
    rin_current = [MiniFI:DeltaFI:0];
    
    if size(rin_current,2) <= 1
        lin_Rin = [];
        uiwait(errordlg('No negative currents in given file - change Rin measurement to ''Single step'' in settings'))
    else
    volt_resp = [];
    for ii = 1:size(rin_current,2)
    volt = mean(FI_master((step_dur + (bl_dur - 100))*Freq:(step_dur + bl_dur) *Freq,:,ii)) - mean(FI_master(1:(bl_dur*Freq),:,ii));    % calculates steady state voltage from last 100ms of current step
    volt_resp(ii) = volt;
    end
    
    undeleted_sweeps = sweepnumbers(1:numel(rin_current));
    deleted_sweeps = setdiff(1:numel(rin_current),undeleted_sweeps);
    
    for ii = 1:numel(deleted_sweeps)
        rin_current(deleted_sweeps(ii)) = [];
        volt_resp(deleted_sweeps(ii)) = [];
    end

    lin_coef = polyfit(rin_current(1:end), volt_resp, 1);     % takes linear coef of voltages and current steps
    lin_Rin = (lin_coef(1))*1000;
    end
end

% FI count
AP_count_temp = [];
 
Co_ord_zero = find(currents == 0);
for ii = Co_ord_zero:size(FI_master,3)
    count = 0;
    for jj = 1:(bl_dur + step_dur)*Freq
    if FI_master(jj,:,ii) < APdetect && FI_master(jj+1,:,ii) > APdetect
        count = count + 1;
        AP_count_temp(ii) = count;
    end
    end
end

adapt_AP_count = AP_count_temp;

if numel(AP_count_temp) ~= numel(sweepnumbers)
   
    deleted_sweeps = (setdiff(1:size(FI_master,3),sweepnumbers)); 
   
   for ii = 1:numel(deleted_sweeps)
       AP_count_temp(deleted_sweeps(ii)) = NaN;
   end
end

AP_count = AP_count_temp(Co_ord_zero:end)'; % outputs number of AP from 0pA onwards

if numel(AP_count) ~= numel(currents(Co_ord_zero:end))
    NaN_values = NaN(1,(numel(currents(Co_ord_zero:end)) - numel(AP_count)))';
    AP_count = vertcat(AP_count, NaN_values);
else NaN_values = [];
end



% Rheobase

Rheo_sweep = find(AP_count_temp > 0, 1, 'first'); % finds first sweep that contained an AP
Rheobase = currents(Rheo_sweep);

% Threshold + dvdt


dvdt = ((FI_master(2:end,:,Rheo_sweep) - FI_master(1:end-1,:,Rheo_sweep))/0.05);

co_ord = [(find(dvdt>=userdvdt, 1, 'first')-1) (find(dvdt>=userdvdt, 1, 'first'))]; % finds first point that dvdt exceeds 20mV/ms (threshold)

if max(FI_master((co_ord(1)+1):(co_ord(1)+(5/(1/Freq))),:,Rheo_sweep)) > APdetect

x = [(FI_master((co_ord(1)+1),:,Rheo_sweep)), (FI_master((co_ord(2)+1),:,Rheo_sweep))];
y = [dvdt(co_ord(1)) dvdt(co_ord(2))]; 
slope_int = polyfit(x,y,1);
thresh = (userdvdt-slope_int(2))/slope_int(1);
thresh_coord = (find(dvdt>userdvdt, 1, 'first'));
time_first_AP = timebase(thresh_coord);

dep_speed = max(dvdt);
rep_speed = min(dvdt);


% AP height

[abs_peak_amp, peak_time] = max(FI_master(thresh_coord:thresh_coord+100,:,Rheo_sweep)); 
peak_amp = mean(FI_master((thresh_coord + peak_time)-2:(thresh_coord + peak_time),:,Rheo_sweep));
AP_height = peak_amp-thresh;

% AP half-width

half_volt = abs_peak_amp-(AP_height/2);
pre_half_volt_upper = (find(FI_master(:,:,Rheo_sweep) > half_volt, 1, 'first')); pre_half_volt_lower = pre_half_volt_upper - 1;
post_half_volt_upper = (find(FI_master(thresh_coord+peak_time:end,:,Rheo_sweep) < half_volt, 1, 'first')); post_half_volt_lower = post_half_volt_upper - 1;
pre_peak_time = interp1(FI_master(pre_half_volt_lower:pre_half_volt_upper,:,Rheo_sweep), timebase(pre_half_volt_lower:pre_half_volt_upper), half_volt);
post_peak_time = interp1(FI_master(thresh_coord + peak_time + post_half_volt_lower -1 : thresh_coord + peak_time + post_half_volt_upper - 1,:,Rheo_sweep), timebase(thresh_coord + peak_time + post_half_volt_lower -1 : thresh_coord + peak_time + post_half_volt_upper - 1), half_volt);
half_width = post_peak_time - pre_peak_time;


else
    uiwait(errordlg('Baseline noise level appears too high for action potential waveform analysis - consider changing threshold dvdt in settings'))
    AP_height = nan; half_width = nan; dep_speed = nan; rep_speed = nan; thresh = nan;
end

%% Spike accomodation

 sweep = find(adapt_AP_count >= 4);
 adaptation_ratio = [];
 APtime_out.sweeps = sweep;
 APtime_out.ISIs = nan(length(sweep),max(AP_count));
 
 if isempty(sweep)
     errordlg('There is no sweep that contains the selected number of APs or greater')
 else
 
    for ss = 1:length(sweep)
        AP_times = [];    
    for ii = 1:(length(FI_master)-1)
        if FI_master(ii,:,sweep(ss)) < 0 && FI_master((ii+1),:,sweep(ss)) > 0
            [~, peak_coord] = max(FI_master(ii:ii+10,:,sweep(ss)));
            peak_time = timebase(ii+peak_coord-2);
             AP_times = [AP_times peak_time];
        end  
    end
    APtime_out.ISIs(ss,1:length(AP_times)) = [1,diff(AP_times)];
    
    isi2 = AP_times(3) - AP_times(2);
    isilast = AP_times(end) - AP_times(end-1);
    sweep_ad_ratio = isilast / isi2;
    adaptation_ratio = [adaptation_ratio sweep_ad_ratio]; 
    
    end


NaNvalues = NaN(1,sum(numel(AP_count))-numel(adaptation_ratio))';
adaptation_ratio = vertcat(NaNvalues, adaptation_ratio');

if ~isempty(deleted_sweeps)
    deleted_sweeps = deleted_sweeps - (Co_ord_zero - 1);
    deleted_sweeps = deleted_sweeps(deleted_sweeps > 0);
   for ii = 1:numel(deleted_sweeps)
       adaptation_ratio(deleted_sweeps(ii)) = NaN;
   end
end
 end
    
