6545631

在这里插入代码片
```TargetChar=[];
StimulusType=[];

fprintf(1, 'Collecting Responses and Performing classification... \n\n' );
load 'Subject_A_Train.mat' % load data file
window=240; % window after stimulus (1s)
channel=11; % only using Cz for analysis and plots

% convert to double precision
Signal=double(Signal);
Flashing=double(Flashing);
StimulusCode=double(StimulusCode);
StimulusType=double(StimulusType);

% 6 X 6 onscreen matrix
screen=char('A','B','C','D','E','F',...
            'G','H','I','J','K','L',...
            'M','N','O','P','Q','R',...
            'S','T','U','V','W','X',...
            'Y','Z','1','2','3','4',...
            '5','6','7','8','9','_');

% for each character epoch
for epoch=1:size(Signal,1)%size返回signal的行数,循环signal的每一行
    
    % get reponse samples at start of each Flash
    rowcolcnt=ones(1,12);
    for n=2:size(Signal,2)%size返回signal的列数,循环每个字符纪元期间的信号
        if Flashing(epoch,n)==0 & Flashing(epoch,n-1)==1%当flashing左列为1右列为0时(无增强时),筛选增强时的数据
            rowcol=StimulusCode(epoch,n-1);%将增强的行或列赋值给rowcolrow,rowcol代表增强的行或列
            responses(rowcol,rowcolcnt(rowcol),:,:)=Signal(epoch,n-24:n+window-25,:);%responses前两维固定,将signal的该字符纪元后两维相应脑电信号赋值给responses后两维
            rowcolcnt(rowcol)=rowcolcnt(rowcol)+1;%最终rowcolcnt的值为16,代表了该行/列增强了15次
        end
    end

    % average and group responses by letter  计算每个字符的平均响应
    m=1;%m每循环一个字符就+1
    avgresp=mean(responses,2);%对列求均值,并形成新的矩阵
    avgresp=reshape(avgresp,12,window,64);%将avgrep重构成12行240列和64的三维矩阵。
    for row=7:12
        for col=1:6%通过行和列定位字符
            % row-column intersection
            letter(m,:,:)=(avgresp(row,:,:)+avgresp(col,:,:))/2;%letter代表每个字符在采集周期内的均值
            % the crude avg peak classifier score (**tuned for Subject_A**)          
            score(m)=mean(letter(m,54:124,channel))-mean(letter(m,134:174,channel));%为什么取54-124与134-174的均值差? 
            m=m+1;
        end
    end
    
    [val,index]=max(score);%获取最大值的索引,最大值为L
    charvect(epoch)=screen(index);%charvect是预测的字符集
    
    % if labeled, get target label and response
    if isempty(StimulusType)==0%stimulustype为1,即出现了目标字符时
        label=unique(StimulusCode(epoch,:).*StimulusType(epoch,:));%两个矩阵点乘,然后取出不重复的元素
        targetlabel=(6*(label(3)-7))+label(2);
        Target(epoch,:,:)=.5*(avgresp(label(2),:,:)+avgresp(label(3),:,:));%将avgresp第三行和第五行相加后与5点乘
        NonTarget(epoch,:,:)=mean(avgresp,1)-(1/6)*Target(epoch,:,:);
    end
end

% display results             

if isempty(TargetChar)==0

    k=0;%k统计正确的字符个数
    for p=1:size(Signal,1)
        if charvect(p)==TargetChar(p)
            k=k+1;
        end
    end

    correct=(k/size(Signal,1))*100;

    fprintf(1, 'Classification Results: \n\n' );
    for kk=1:size(Signal,1)
        fprintf(1, 'Epoch: %d  Predicted: %c Target: %c\n',kk,charvect(kk),TargetChar(kk));
    end
    fprintf(1, '\n %% Correct from Labeled Data: %2.2f%% \n',correct);

    % plot averaged responses and topography
    Tavg=reshape(mean(Target(:,:,:),1),window,64);
    NTavg=reshape(mean(NonTarget(:,:,:),1),window,64);
    figure
    plot([1:window]/window,Tavg(:,channel),'linewidth',2)
    hold on
    plot([1:window]/window,NTavg(:,channel),'r','linewidth',2)
    title('Averaged P300 Responses over Cz')
    legend('Targets','NonTargets');
    xlabel('time (s) after stimulus')
    ylabel('amplitude (uV)')
    
    % Target/NonTarget voltage topography plot at 300ms (sample 72)
    vdiff=abs(Tavg(72,:)-NTavg(72,:));
    figure
    topoplotEEG(vdiff,'eloc64.txt','gridscale',150)
    title('Target/NonTarget Voltage Difference Topography at 300ms')
    caxis([min(vdiff) max(vdiff)])
    colorbar
    
else

    for kk=1:size(Signal,1)
        fprintf(1, 'Epoch: %d  Predicted: %c\n',kk,charvect(kk));
    end

end

fprintf(1, '\nThe resulting classified character vector is the variable named "charvect". \n');
fprintf(1, 'This is an example of how the results *must* be formatted for submission. \n');
fprintf(1, 'The character vectors from each case and subject are to be labeled, grouped, and submitted according to the accompanied documentation. \n');
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值