function  [perc_dif_pre,perc_dif_post,dif_post_pre] = ...
    analyze_data(seq_pre,resp_pre,seq_post,resp_post) 
% function for analysis of data pre/post training

% remove last number in sequence (artifact of program)
seq_p2_pre = seq_pre(1:end-1);
resp_p2_pre = resp_pre; % same=1, different=0
seq_p2_post = seq_post(1:end-1);
resp_p2_post = resp_post;

% reformat/parse and remove practice trials (10% of total num)
num_practice_pre = 9;
data_pre = zeros(length(seq_p2_pre) - num_practice_pre,2);
data_pre(:,1) = seq_p2_pre(num_practice_pre+1:end);
data_pre(:,2) = resp_p2_pre(num_practice_pre+1:end);

num_practice_post = 9;
data_post = zeros(length(seq_p2_post)-num_practice_post,2);
data_post(:,1) = seq_p2_post(num_practice_post+1:end);
data_post(:,2) = resp_p2_post(num_practice_post+1:end);

% sort
%data_pre_sort = sortrows(data_pre,1)
%data_post_sort = sortrows(data_post,1)
% check sort
% temp_pre = 50*ones(7,2);
% j=1;
% for i = 1:119
%     if data_pre(i,1) == 14
%         temp_pre(j,:) = data_pre(i,:);
%         j = j+1;
%      end
% end


% compute "% different"

% pre-train data
count_pre = zeros(15,2);
for i = 1:length(seq_p2_pre) - num_practice_pre
    for j = 0:14
        if data_pre(i,1) == j
            count_pre(j+1,2) = count_pre(j+1,2) + 1;
            if data_pre(i,2) == 0 % == different
                count_pre(j+1,1) = count_pre(j+1,1) + 1;
            end
        end
    end
end
perc_dif_pre = 100*count_pre(:,1) ./ count_pre(:,2);

% post-train data 
count_post = zeros(15,2);
for i = 1:length(seq_p2_post) - num_practice_post
    for j = 0:14
        if data_post(i,1) == j
            count_post(j+1,2) = count_post(j+1,2) + 1;
            if data_post(i,2) == 0 % == different
                count_post(j+1,1) = count_post(j+1,1) + 1;
            end
        end
    end
end
perc_dif_post = 100*count_post(:,1) ./ count_post(:,2);
 
% difference functions = post - pre
difference = perc_dif_post - perc_dif_pre;
% sorted correctly - ascending freq
dif_post_pre = [difference(4:8); difference(2); difference(9:end)];

