%% Initialisation
% first make sure no figures are open or no data is stored in working space
close all;
clearvars;
clc;

% Check whether my custom functions are available
hasCbrewer  = ~isempty(which('cbrewer'));
hasNicegraph = ~isempty(which('nicegraph'));
hasSavegraph = ~isempty(which('savegraph'));

if hasCbrewer
    % Use custom cbrewer colormap if available
	col = cbrewer('qual','Pastel1',3);
else
    % Fallback to built-in colormap
    colors = lines(3);
end


%% Poisson Parameters
lambda	= [2 5 10]; % mean spike counts per interval
x		= 0:20;		% range for x-axis (max count to display)

%% Plotting
figure(1);
clf;
hold on;
for ii = 1:3
	% Poisson probability mass function
	y		= poisspdf(x,lambda(ii));

	% plot
	plot(x,y,'ko-','LineWidth',1.5,'MarkerSize',15,'MarkerFaceColor',col(ii,:));
end

%% Create labels and make nice
if hasNicegraph
    nicegraph;
	axis normal;
    set(gca, 'FontSize', 24);
	
else
    % Minimal built-in equivalent
    box off;
    set(gca, 'TickDir', 'out', 'FontSize', 11);
end
xlabel({'number of events in fixed time interval';'e.g. number of spikes in 1 s'});
ylabel({'probability';'frequency'});
legend('\lambda = 2', '\lambda = 5', '\lambda = 10');
set(gca,'XTick',x,'YTick',[]);

%% Save figure
fname = fullfile('..','images','spike_poisson_pdf.png');
if hasSavegraph
    % Use custom helper
    savegraph(fname,'png');
else
    % Built-in export (R2020a+; otherwise use print/saveas)
    exportgraphics(gcf, fname, 'Resolution', 300);
end

