% homework_2014_09_11.m  
%
% Matlab Club
% Homework assignment for September 11, 2014
% 

%% 1. read through raster_demo.m and make sure you understand what's
% happening on each line. Note: the "pause" commands are purely for
% dramatic effect and can be removed or commented out for faster execution.
raster_demo

%% 2. modify raster_demo.m to plot rasters for 3585 Hz and for 20655 Hz
% (hint:)
stim_index = find(tone_frequency==3585)

%% 3. plot a tuning curve based on the mean firing rate between tone onset
% and offset. Use the "plot" command.
% Bonus points: plot with error bars representing standard error of the
% mean (SEM). Use the "errorbar" command
%
% hint:
stim_index=6
spikes_per_presentation=zeros(trial_count,1);
for trial_index=1:trial_count,
    % count number of spikes between StimOnsetTime and StimOffsetTime on
    % trial trial_index
    spikes_per_presentation(trial_index)=...
        sum(spike_times{trial_index,stim_index} > StimOnsetTime & ...
            spike_times{trial_index,stim_index} <= StimOffsetTime);
    
end
% divide by tone length to convert to spikes/sec. each member of the
% spikes_per_presentation vector is divided by the same scalar value
spikes_per_presentation=spikes_per_presentation./(StimOffsetTime-StimOnsetTime);

% calculate mean rate across all trials
mean_rate_4149=mean(spikes_per_presentation);
sem_rate_4149=std(spikes_per_presentation)./sqrt(trial_count);

fprintf('mean response to %d Hz: %.3f +/- %.3f spikes/sec\n',...
    tone_frequency(stim_index),mean_rate_4149,sem_rate_4149);

