function plotDecodePerformance(type,decode,subjectData)

header = decode.header;
timeline = header.timeline;
frameshift = header.frameShift;

psthStart         = timeline.psthStart;
psthEnd           = timeline.psthEnd;
frameStart        = frameshift.frameShiftStart;
frameEnd          = frameshift.frameShiftEnd;

nClasses          = numel(header.classDef.labelCells);
decodePerformance = decode.decodePerformance;
psth              = decode.rawTimeCourse;
SubjectID         = subjectData;

nSubjects         = size(SubjectID,2);


    f = figure;
  
    hold on;
    chanceLevel = 100/nClasses;
    goodPredictionLevel = chanceLevel*1.5;
    plot([psthStart psthEnd],[chanceLevel chanceLevel],'k:');
    plot([psthStart psthEnd],[goodPredictionLevel goodPredictionLevel],'k:');
    axis([psthStart psthEnd 0 100])
    xlabel('time [sec]');
    ylabel('decode performance [%]');

    switch type
        case 'psth'
            plotPSTH(psth,psthStart,psthEnd);  
        case 'simple'
            plotDecodePerformanceWithSE(frameStart,frameEnd,decodePerformance)
        case 'class performance'
            plotClassPerformance(frameStart,frameEnd,decodePerformance,nClasses)
        case 'x-subject-val'
            for c = 1:nSubjects
                plot(frameStart:frameEnd, decodePerformance(:,c) ,[colorChooser(mod(c,nSubjects)+3) '-']);
            end
            plotDecodePerformanceWithSE(frameStart,frameEnd,decodePerformance)
    end
    hold off;
    
%     setTitle(f,header,decode,subjectData);
    
end

function plotClassPerformance(frameStart,frameEnd,decodePerformance,nClasses)
for c = 1:size(decodePerformance,2)
    plot(frameStart:frameEnd, decodePerformance(:,c) ,[colorChooser(mod(c,nClasses)+3) '-']);
end
plot(frameStart:frameEnd, mean(decodePerformance,2) ,'b','LineWidth',2);
end

function plotDecodePerformanceWithSE(frameStart,frameEnd,decodePerformance)
plot(frameStart:frameEnd, mean(decodePerformance,2) ,'b','LineWidth',2);

se = myStdErr(decodePerformance,2);
plot(frameStart:frameEnd, mean(decodePerformance,2)+se ,'b:');
plot(frameStart:frameEnd, mean(decodePerformance,2)-se ,'b:');
end


function plotPSTH(psth,psthStart,psthEnd)

PSTH_AXIS_MIN = -2;
PSTH_AXIS_MAX = 2;

    hold on;
    if (size(psth) > 0)
      for voxel = 1:size(psth,2)
          for label = 1:size(psth{voxel},2)
              psthData = [];
              for timepoint = 1:size(psth{voxel}{label},2)
                  psthData = nanmean(psth{voxel}{label});
              end
              plot(psthStart:psthEnd,psthData,[colorChooser(voxel), lineStyleChooser(label)]);
          end
      end
    end
    axis([psthStart psthEnd PSTH_AXIS_MIN PSTH_AXIS_MAX])
    xlabel('time [sec]');
    ylabel('fMRI-signal change [%]');
    hold off
end


function color = colorChooser(n)
    switch (mod(n,8))
    case 0
        color = 'r';
    case 1
        color = 'g';
    case 2
        color = 'b';
    case 3
        color = 'c';
    case 4
        color = 'm';
    case 5
        color = 'y';
    otherwise
        color = 'k';
    end
end

function style = lineStyleChooser(n)
switch(mod(n,4))
    case 0
      style = '--';
    case 1
        style = '-';
    case 2 
        style = ':';
    case 3
        style = '-.';
end
end