% The purpose of this script is to generate image frames to use for optical
% stimulation of ChR2-transfected cultures.

lambda = 10; %average stimulation rate, in Hz
T = 1; %total length of movie, in seconds
dt = 0.02; %length of single stim pulse, in seconds (1/dt = frame rate)

t = [0:dt:T];
stim_times = [];

% for i = 1:length(t)
%     if lambda*dt >= rand(1)
%         stim_times(1,end+1) = t(i);
%     end
% end

figure(1);
blank_image = zeros([360 480]);
imshow(blank_image);
ti = get(gca,'TightInset');
set(gca,'Position',[ti(1)-.1 ti(2)-.12 1-ti(3)-ti(1)+.25 1-ti(4)-ti(2)+.25]);
        
for i = 1:length(t)
    if lambda*dt >= rand(1)
        stim_times(1,end+1) = t(i);
        figure(2);
        image = rand([360 480]);
        imshow(image);
        ti = get(gca,'TightInset');
        set(gca,'Position',[ti(1)-.1 ti(2)-.12 1-ti(3)-ti(1)+.25 1-ti(4)-ti(2)+.25]);
    else
        figure(1);
    end
    print(['stim' num2str(i) '.jpg'],'-djpeg');
end
