希望了解更多的道友点这里
0. 分享【脑机接口 + 人工智能】的学习之路
1.1 . 脑电EEG代码开源分享 【1.前置准备-静息态篇】
1.2 . 脑电EEG代码开源分享 【1.前置准备-任务态篇】
2.1 . 脑电EEG代码开源分享 【2.预处理-静息态篇】
2.2 . 脑电EEG代码开源分享 【2.预处理-任务态篇】
3.1 . 脑电EEG代码开源分享 【3.可视化分析-静息态篇】
3.2 . 脑电EEG代码开源分享 【3.可视化分析-任务态篇】
4.1 . 脑电EEG代码开源分享 【4.特征提取-时域篇】
4.2 . 脑电EEG代码开源分享 【4.特征提取-频域篇】
4.3 . 脑电EEG代码开源分享 【4.特征提取-时频域篇】
4.4 . 脑电EEG代码开源分享 【4.特征提取-空域篇】
5 . 脑电EEG代码开源分享 【5.特征选择】
6.1 . 脑电EEG代码开源分享 【6.分类模型-机器学习篇】
6.2 . 脑电EEG代码开源分享 【6.分类模型-深度学习篇】
汇总. 专栏:脑电EEG代码开源分享【文档+代码+经验】
0 . 【深度学习】常用网络总结
本文档旨在归纳BCI-EEG-matlab的数据处理代码,作为EEG数据处理的总结,方便快速搭建处理框架的Baseline,实现自动化、模块插拔化、快速化。本文以任务态(锁时刺激,如快速序列视觉呈现)为例,分享脑电EEG的分析处理方法。
脑电数据分析系列。分为以下6个模块:
本文内容:【3. 数据可视化】
提示:以下为各功能代码详细介绍,若节约阅读时间,请下滑至文末的整合代码
可视化的主要功能,分为以下3部分:
静息态可视化的代码框图、流程如下所示:
脑电信号可视化有助于对处理数据综合信息的掌握,是最直观了解数据状态的方法
同时,可视化应注重多维度、多角度分析,从不同特征域对数据进行全面了解
本文将脑电数据可视化紧邻预处理之后,放置于特征提取之前,就是为了最快速了解脑电数据情况,
同时为下一阶段的特征提取 及 分类器设计提供参考。
时域可视化:
脑电信号采集后的最原始格式就是 通道数 x 时间点数 的数组,时域的时序信号是对脑电数据最直观的分析。由于脑电信号的低信噪比,通常采用叠加多试次的脑电信号提升数据质量。但是静息态脑电信号的时域波形观测效果一般,主要源于脑电信号的随机性,脑电信号近似粉红噪声,其叠加后时域波形趋于0值。因此通常对静息数据的单试次进行简要分析。
静息态时域可视化:
频域可视化:
频域是静息态(长时任务)数据的主要分析的特征域,个体在静息状态下的频域能量包含重要的节律信息,并且在一定时间内频谱状态是稳定的。目前研究学者也将脑电频域主要分为5个频带, 这5个频带能量(找不到希腊字母格式,大家看下方表格吧),并且各频段有特殊的大脑模式含义。
5频带分布及脑模式含义:
静息态频域可视化:
空域可视化:
大脑运行的重要特点是 多脑区协同,意思是大脑运转时时刻刻需要多个脑区的 协作和交流。脑电通过在头皮上放置大量的电极以高密度采集脑响应,通过整合电极采集的脑信号,可以绘制出大脑能量的空间分布图。当然,前面讲到静息态下脑电随机性导致时域可视化效果不佳,因此静息态常常是 频域+空间,绘制大脑不同频段能量的空间分布。
静息态空域可视化(空频地形图):
本文非锁时任务态(下文以静息态代替)范例为:ADHD患者、正常人群在静息状态下的脑模式分类
提示:代码环境为 matlab 2018
可视化内容可以选择,把希望可视化特征域写在analysis_content中
%% 0.标准数据参数设置
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data_path = 'C:\Users\Amax\Desktop\basetest_flod\save_fold\';
svae_path = 'C:\Users\Amax\Desktop\basetest_flod\save_fold\';
channel_name_path = 'F:\机制分析-128导\channel_name_128';
channel_loc_path = 'C:\Users\Amax\Desktop\ZRK_BCI_code_Summary\channel_122_loc_2d.txt';
analysis_content = ['time\','freq_mean\','map\'];
plot_para = ['time\','freq\','space\'];
subject_num = [1 ; 10];
line_size = 1;
text_size = 5;
channel_num = [1:16];
freq_axis = [1;80];
freq_draw_log = 1;
space_axis = [1:5];
space_label = 'off';
disp(['||任务态数据分析中...||']);
disp(['被试量: ' , num2str(subject_num(1,1)),'-',num2str(subject_num(2,1))]);
disp(['是否绘图: ' , num2str(plot_para)]);
disp(['频谱绘制范围: ' , num2str(freq_axis(1,1)),'-',num2str(freq_axis(2,1))]);
disp(['地形图绘制范围: ' , num2str(space_axis)]);
disp(['线条宽度:' , num2str(line_size),' ||字体大小:' , num2str(text_size)]);
disp(['绘图通道:' , num2str(channel_num)]);
导入上一步预处理-静息态阶段处理后的数据:
%% 1.标准输入赋值
disp(['||预处理后数据加载中...||']);
Proprocess_target_file = load([data_path ,'Proprocess_target_',num2str(subject_num(1,1)),'_',num2str(subject_num(2,1))]);
Proprocess_nontarget_file = load([data_path ,'Proprocess_nontarget_',num2str(subject_num(1,1)),'_',num2str(subject_num(2,1))]);
stuct_target_name = 'Proprocess_target';
stuct_nontarget_name = 'Proprocess_nontarget';
Proprocess_target_data = Proprocess_target_file.(stuct_target_name).data;
Proprocess_nontarget_data = Proprocess_nontarget_file.(stuct_nontarget_name).data;
subject_num = Proprocess_target_file.(stuct_target_name).subject_num;
fs_down = Proprocess_target_file.(stuct_target_name).fs_down;
remain_trial_target = Proprocess_target_file.(stuct_target_name).remain_trial;
remain_trial_nontarget = Proprocess_nontarget_file.(stuct_nontarget_name).remain_trial;
Baseline_reference = Proprocess_target_file.(stuct_target_name).Baseline_reference ;
disp(['目标试次剩余: ' , num2str(remain_trial_target),'||平均: ', num2str(mean(remain_trial_target)),'||剩余比例: ',num2str(mean(remain_trial_target)/size(Proprocess_target_data,1))]);
disp(['非目标试次剩余: ' , num2str(remain_trial_nontarget),'||平均: ', num2str(mean(remain_trial_nontarget)),'||剩余比例: ',num2str(mean(remain_trial_nontarget)/size(Proprocess_nontarget_data,1))]);
主体调用函数Analysis_rest_time
%% 2.时域分析
disp(['||时域分析加载中...||']);
[time_target]= Analysis_rest_time(Proprocess_target_data,fs_down,Baseline_reference,remain_trial_target,mean_para);
[time_nontarget]= Analysis_rest_time(Proprocess_nontarget_data,fs_down,Baseline_reference,remain_trial_nontarget,mean_para);
%绘图
if contains(plot_para,'time')
load (channel_name_path);
Analysis_time_plot(time_target,time_nontarget,fs_down,Baseline_reference,channel_name,line_size,text_size)
end
主功能函数 Analysis_rest_time:
function [time_analysis_out]= Analysis_rest_time(Standard_input_data,fs_down,Baseline_reference,remain_trial,mean_para)
%% 1.时域均值计算
time_analysis_out = cell(1,1);
channel_mean_raw = zeros(size(Standard_input_data{1,1},1),size(Standard_input_data{1,1},2));
for channel_loop = 1:size(Standard_input_data{1,1},1)
temp_data = [];
for subject_loop = 1:size(Standard_input_data,2)
for trial_loop = 1:remain_trial(1,subject_loop)
temp_data = [temp_data ; Standard_input_data{trial_loop,subject_loop}(channel_loop,:)];
end
end
channel_mean_raw(channel_loop,:) = mean(temp_data);
end
time_analysis_out{1,1} = channel_mean_raw; %必有普通均值的选项,是最基础的分析
if contains(mean_para,'zscore_baseline')
channel_std = std(channel_mean_raw(:,1:round(fs_down * Baseline_reference(2)))');
time_analysis_out{2,1} = {time_analysis_out ;channel_mean_raw./channel_std'};
end
end
时域可视化绘图函数 Analysis_task_time:
function Analysis_time_plot(time_target,time_nontarget,fs_down,Baseline_reference,channel_name,line_size,text_size)
figure('name','time-simple_mean');
x = (0:1/fs_down:size(time_target{1,1},2)/fs_down-1/fs_down) - Baseline_reference(2);
sub_axis_x = ceil(sqrt(size(time_target{1,1},1)));
sub_axis_y = ceil(size(time_target{1,1},1)/sub_axis_x);
for plot_loop = 1:size(time_target{1,1},1)
subplot(sub_axis_x,sub_axis_y,plot_loop);
plot(x,time_target{1,1}(plot_loop,:),'r','LineWidth',line_size);
hold on;
plot(x,time_nontarget{1,1}(plot_loop,:),'b','LineWidth',line_size);
hold on;
title([channel_name{1,plot_loop}, ' 第' num2str(plot_loop), '导联'], 'Fontsize',text_size);
hold on;
max_y = max([time_nontarget{1,1}(plot_loop,:) time_target{1,1}(plot_loop,:)]);
min_y = min([time_nontarget{1,1}(plot_loop,:) time_target{1,1}(plot_loop,:)]);
Dotted_line=plot([0,0],[min_y,max_y],'--k','LineWidth',1);
set(Dotted_line,'handlevisibility','off');
xlabel('Time/(s)', 'Fontsize',text_size);
ylabel('Amplitude/(uv)','Fontsize',text_size);
end
if size(time_target,1)>1
figure('name','time-zcore');
x = (0:1/fs_down:size(time_target{2,1},2)/fs_down-1/fs_down) - Baseline_reference(2);
sub_axis_x = ceil(sqrt(size(time_target{2,1},1)));
sub_axis_y = ceil(size(time_target{2,1},1)/sub_axis_x);
for plot_loop = 1:size(time_target{2,1},1)
subplot(sub_axis_x,sub_axis_y,plot_loop);
plot(x,time_target{2,1}(plot_loop,:),'r','LineWidth',line_size);
hold on;
plot(x,time_nontarget{2,1}(plot_loop,:),'b','LineWidth',line_size);
hold on;
title([channel_name{1,plot_loop}, ' 第' num2str(plot_loop), '导联'], 'Fontsize',text_size);
hold on;
max_y = max([time_nontarget{2,1}(plot_loop,:) time_target{2,1}(plot_loop,:)]);
min_y = min([time_nontarget{2,1}(plot_loop,:) time_target{2,1}(plot_loop,:)]);
Dotted_line=plot([0,0],[min_y,max_y],'--k','LineWidth',1);
set(Dotted_line,'handlevisibility','off');
xlabel('Time/(s)', 'Fontsize',text_size);
ylabel('Amplitude/(uv)','Fontsize',text_size);
end
end
end
主体调用函数Analysis_rest_freq
%% 3.频域分析
disp(['||频域分析加载中...||']);
% 注:fft使用采样率作为参数,因此频谱分辨率为1hz
[freq_target]= Analysis_rest_freq(Proprocess_target_data,fs_down,remain_trial_target);
[freq_nontarget]= Analysis_rest_freq(Proprocess_nontarget_data,fs_down,remain_trial_nontarget);
%绘图
if contains(plot_para,'freq')
load (channel_name_path);
Analysis_freq_plot(freq_target,freq_nontarget,fs_down,channel_name,freq_axis,line_size,text_size,freq_draw_log)
end
主功能函数 Analysis_rest_freq:
function [freq_analysis_out]= Analysis_rest_freq(Standard_input_data,fs_down,remain_trial)
%% 1.频域均值计算
freq_analysis_out = cell(1,1);
channel_mean_raw = [];
for channel_loop = 1:size(Standard_input_data{1,1},1)
temp_data = [];
for subject_loop = 1:size(Standard_input_data,2)
for trial_loop = 1:remain_trial(1,subject_loop)
temp_data = [temp_data ; abs(fft(Standard_input_data{trial_loop,subject_loop}(channel_loop,:),fs_down))];
end
end
channel_mean_raw(channel_loop,:) = mean(temp_data);
end
freq_analysis_out{1,1} = channel_mean_raw; %必有普通均值的选项,是最基础的分析
end
频域绘图函数Analysis_freq_plot:
function Analysis_freq_plot(freq_target,freq_nontarget,fs_down,channel_name,freq_axis,line_size,text_size,freq_draw_log)
figure('name','freq-simple_mean');
x = freq_axis(1):1:freq_axis(2);
sub_axis_x = ceil(sqrt(size(freq_target{1,1},1)));
sub_axis_y = ceil(size(freq_target{1,1},1)/sub_axis_x);
for plot_loop = 1:size(freq_target{1,1},1)
subplot(sub_axis_x,sub_axis_y,plot_loop);
if freq_draw_log == 0
plot(x,freq_target{1,1}(plot_loop,freq_axis(1):freq_axis(2)),'r','LineWidth',line_size);
hold on;
plot(x,freq_nontarget{1,1}(plot_loop,freq_axis(1):freq_axis(2)),'b','LineWidth',line_size);
hold on;
end
if freq_draw_log == 1
semilogy(x,freq_target{1,1}(plot_loop,freq_axis(1):freq_axis(2)),'r','LineWidth',line_size)
hold on;
semilogy(x,freq_nontarget{1,1}(plot_loop,freq_axis(1):freq_axis(2)),'b','LineWidth',line_size)
hold on;
end
title([channel_name{1,plot_loop}, ' 第' num2str(plot_loop), '导联'], 'Fontsize',text_size);
hold on;
xlabel('freq/(s)', 'Fontsize',text_size);
ylabel('Amplitude/(uv)','Fontsize',text_size);
end
if size(freq_target,1)>1
figure('name','freq-zcore');
x = freq_axis(1):1:freq_axis(2);
sub_axis_x = ceil(sqrt(size(freq_target{1,1},1)));
sub_axis_y = ceil(size(freq_target{1,1},1)/sub_axis_x);
for plot_loop = 1:size(freq_target{2,1},1)
subplot(sub_axis_x,sub_axis_y,plot_loop);
plot(x,freq_target{2,1}(plot_loop,freq_axis(1):freq_axis(2)),'r','LineWidth',line_size);
hold on;
plot(x,freq_nontarget{2,1}(plot_loop,freq_axis(1):freq_axis(2)),'b','LineWidth',line_size);
hold on;
title([channel_name{1,plot_loop}, ' 第' num2str(plot_loop), '导联'], 'Fontsize',text_size);
hold on;
xlabel('freq/(s)', 'Fontsize',text_size);
ylabel('Amplitude/(uv)','Fontsize',text_size);
end
end
end
主体调用函数Analysis_freq_space_plot
%% 4.空域分析 脑地形图
disp(['||空域分析加载中...||']);
if contains(plot_para,'space')
Analysis_freq_space_plot(time_target,time_nontarget,channel_loc_path,fs_down,space_axis,space_label)
end
disp(['||数据分析完毕||']);
空域绘图函数Analysis_freq_space_plot:
function Analysis_freq_space_plot(freq_target,freq_nontarget,channel_loc_path,fs_down,space_axis,space_label)
five_band_target = zeros(size(freq_target{1,1},1),5);
five_band_nontarget = zeros(size(freq_nontarget{1,1},1),5);
for channel_loop = 1:size(freq_target{1,1},1)
five_band_target(channel_loop,:) = sum_five_band(freq_target{1,1}(channel_loop,:),fs_down);
five_band_nontarget(channel_loop,:) = sum_five_band(freq_nontarget{1,1}(channel_loop,:),fs_down);
end
figure();
sub_axis = size(space_axis,2);
clim = [min(min(five_band_target)),max(max(five_band_target))];
for time_loop=1:size(space_axis,2)
subplot(2,size(space_axis,2),time_loop);
topoplotEEG(five_band_target(:,time_loop),channel_loc_path,'electrodes',space_label,'maplimits',clim);
title([ 'target-', num2str(space_axis(time_loop)),'s']);
end
for time_loop=1:size(space_axis,2)
subplot(2,size(space_axis,2),size(space_axis,2) + time_loop);
topoplotEEG(five_band_nontarget(:,time_loop),channel_loc_path,'electrodes',space_label,'maplimits',clim);
title([ 'nontarget-',num2str(space_axis(time_loop)),'s']);
end
end
这里的地形图绘制代码topoplotEEG,源自EEGLAB软件 [https://sccn.ucsd.edu/eeglab/index.php]:
% topoplot() - plot a topographic map of an EEG field as a 2-D
% circular view (looking down at the top of the head)
% using cointerpolation on a fine cartesian grid.
% Usage:
% >> topoplot(datavector,'eloc_file');
% >> topoplot(datavector,'eloc_file', 'Param1','Value1', ...)
% Inputs:
% datavector = vector of values at the corresponding locations.
% 'eloc_file' = name of an EEG electrode position file {0 -> 'chan_file'}
%
% Optional Parameters & Values (in any order):
% Param Value
% 'colormap' - any sized colormap
% 'interplimits' - 'electrodes' to furthest electrode
% 'head' to edge of head
% {default 'head'}
% 'gridscale' - scaling grid size {default 67}
% 'maplimits' - 'absmax' +/- the absolute-max
% 'maxmin' scale to data range
% [clim1,clim2] user-definined lo/hi
% {default = 'absmax'}
% 'style' - 'straight' colormap only
% 'contour' contour lines only
% 'both' both colormap and contour lines
% 'fill' constant color between lines
% 'blank' just head and electrodes
% {default = 'both'}
% 'numcontour' - number of contour lines
% {default = 6}
% 'shading' - 'flat','interp' {default = 'flat'}
% 'headcolor' - Color of head cartoon {default black}
% 'electrodes' - 'on','off','labels','numbers'
% 'efontsize','electcolor','emarker','emarkersize' - details
%
% Note: topoplot() only works when map limits are >= the max and min
% interpolated data values.
% Eloc_file format:
% chan_number degrees radius reject_level amp_gain channel_name
% (Angle-0 = Cz-to-Fz; C3-angle =-90; Radius at edge of image = 0.5)
%
% For a sample eloc file: >> topoplot('example')
%
% Note: topoplot() will ignore any electrode with a position outside
% the head (radius > 0.5)
% Topoplot Version 2.1
% Begun by Andy Spydell, NHRC, 7-23-96
% 8-96 Revised by Colin Humphries, CNL / Salk Institute, La Jolla CA
% -changed surf command to imagesc (faster)
% -can now handle arbitrary scaling of electrode distances
% -can now handle non integer angles in eloc_file
% 4-4-97 Revised again by Colin Humphries, reformat by SM
% -added parameters
% -changed eloc_file format
% 2-26-98 Revised by Colin
% -changed image back to surface command
% -added fill and blank styles
% -removed extra background colormap entry (now use any colormap)
% -added parameters for electrode colors and labels
% -now each topoplot axes use the caxis command again.
% -removed OUTPUT parameter
% 3-11-98 changed default emarkersize, improve help msg -sm
function handle = topoplot(Vl,loc_file,p1,v1,p2,v2,p3,v3,p4,v4,p5,v5,p6,v6,p7,v7,p8,v8,p9,v9)
% User Defined Defaults:
MAXCHANS = 250;
DEFAULT_ELOC = '16channel.txt';
INTERPLIMITS = 'head'; % head, electrodes
MAPLIMITS = 'absmax'; % absmax, maxmin, [values]
GRID_SCALE = 67;
CONTOURNUM = 6;
STYLE = 'both'; % both,straight,fill,contour,blank
HCOLOR = [0 0 0];
ECOLOR = [0 0 0];
CONTCOLOR = [0 0 0];
ELECTROD = 'on'; % ON OFF LABEL
EMARKERSIZE = 6;
EFSIZE = get(0,'DefaultAxesFontSize');
HLINEWIDTH = 2;
EMARKER = '.';
SHADING = 'flat'; % flat or interp
%%%%%%%%%%%%%%%%%%%%%%%
nargs = nargin;
if nargs < 2
loc_file = DEFAULT_ELOC;
end
if nargs == 1
if isstr(Vl)
if any(strcmp(lower(Vl),{'example','demo'}))
fprintf(['This is an example of an electrode location file,\n',...
'an ascii file consisting of the following four columns:\n',...
' channel_number degrees arc_length channel_name\n\n',...
'Example:\n',...
' 1 -18 .352 Fp1.\n',...
' 2 18 .352 Fp2.\n',...
' 5 -90 .181 C3..\n',...
' 6 90 .181 C4..\n',...
' 7 -90 .500 A1..\n',...
' 8 90 .500 A2..\n',...
' 9 -142 .231 P3..\n',...
'10 142 .231 P4..\n',...
'11 0 .181 Fz..\n',...
'12 0 0 Cz..\n',...
'13 180 .181 Pz..\n\n',...
'The model head sphere has a diameter of 1.\n',...
'The vertex (Cz) has arc length 0. Channels with arc \n',...
'lengths > 0.5 are not plotted nor used for interpolation.\n'...
'Zero degrees is towards the nasion. Positive angles\n',...
'point to the right hemisphere; negative to the left.\n',...
'Channel names should each be four chars, padded with\n',...
'periods (in place of spaces).\n'])
return
end
end
end
if isempty(loc_file)
loc_file = 0;
end
if loc_file == 0
loc_file = DEFAULT_ELOC;
end
if nargs > 2
if ~(round(nargs/2) == nargs/2)
error('topoplot(): Odd number of inputs?')
end
for i = 3:2:nargs
Param = eval(['p',int2str((i-3)/2 +1)]);
Value = eval(['v',int2str((i-3)/2 +1)]);
if ~isstr(Param)
error('topoplot(): Parameter must be a string')
end
Param = lower(Param);
switch lower(Param)
case 'colormap'
if size(Value,2)~=3
error('topoplot(): Colormap must be a n x 3 matrix')
end
colormap(Value)
case {'interplimits','headlimits'}
if ~isstr(Value)
error('topoplot(): interplimits value must be a string')
end
Value = lower(Value);
if ~strcmp(Value,'electrodes') & ~strcmp(Value,'head')
error('topoplot(): Incorrect value for interplimits')
end
INTERPLIMITS = Value;
case 'maplimits'
MAPLIMITS = Value;
case 'gridscale'
GRID_SCALE = Value;
case 'style'
STYLE = lower(Value);
case 'numcontour'
CONTOURNUM = Value;
case 'electrodes'
ELECTROD = lower(Value);
case 'emarker'
EMARKER = Value;
case {'headcolor','hcolor'}
HCOLOR = Value;
case {'electcolor','ecolor'}
ECOLOR = Value;
case {'emarkersize','emsize'}
EMARKERSIZE = Value;
case {'efontsize','efsize'}
EFSIZE = Value;
case 'shading'
SHADING = lower(Value);
if ~any(strcmp(SHADING,{'flat','interp'}))
error('Invalid Shading Parameter')
end
otherwise
error('Unknown parameter.')
end
end
end
[r,c] = size(Vl);
if r>1 & c>1,
error('topoplot(): data should be a single vector\n');
end
fid = fopen(loc_file);
if fid<1,
fprintf('topoplot(): cannot open eloc_file (%s).\n',loc_file);
return
end
A = fscanf(fid,'%d %f %f %s',[7 MAXCHANS]);
fclose(fid);
A = A';
if length(Vl) ~= size(A,1),
fprintf(...
'topoplot(): data vector must have the same rows (%d) as eloc_file (%d)\n',...
length(Vl),size(A,1));
A
error('');
end
labels = setstr(A(:,4:7));
idx = find(labels == '.'); % some labels have dots
labels(idx) = setstr(abs(' ')*ones(size(idx))); % replace them with spaces
Th = pi/180*A(:,2); % convert degrees to radians
Rd = A(:,3);
ii = find(Rd <= 0.5); % interpolate on-head channels only
Th = Th(ii);
Rd = Rd(ii);
Vl = Vl(ii);
labels = labels(ii,:);
[x,y] = pol2cart(Th,Rd); % transform from polar to cartesian coordinates
rmax = 0.5;
ha = gca;
cla
hold on
if ~strcmp(STYLE,'blank')
% find limits for interpolation
if strcmp(INTERPLIMITS,'head')
xmin = min(-.5,min(x)); xmax = max(0.5,max(x));
ymin = min(-.5,min(y)); ymax = max(0.5,max(y));
else
xmin = max(-.5,min(x)); xmax = min(0.5,max(x));
ymin = max(-.5,min(y)); ymax = min(0.5,max(y));
end
xi = linspace(xmin,xmax,GRID_SCALE); % x-axis description (row vector)
yi = linspace(ymin,ymax,GRID_SCALE); % y-axis description (row vector)
[Xi,Yi,Zi] = griddata(y,x,Vl,yi',xi,'v4'); % Interpolate data
% Take data within head
mask = (sqrt(Xi.^2+Yi.^2) <= rmax);
ii = find(mask == 0);
Zi(ii) = NaN;
% calculate colormap limits
m = size(colormap,1);
if isstr(MAPLIMITS)
if strcmp(MAPLIMITS,'absmax')
amin = -max(max(abs(Zi)));
amax = max(max(abs(Zi)));
elseif strcmp(MAPLIMITS,'maxmin')
amin = min(min(Zi));
amax = max(max(Zi));
end
else
amin = MAPLIMITS(1);
amax = MAPLIMITS(2);
end
delta = xi(2)-xi(1); % length of grid entry
% Draw topoplot on head
if strcmp(STYLE,'contour')
contour(Xi,Yi,Zi,CONTOURNUM,'k');
elseif strcmp(STYLE,'both')
surface(Xi-delta/2,Yi-delta/2,zeros(size(Zi)),Zi,'EdgeColor','none',...
'FaceColor',SHADING);
% colorbar;
contour(Xi,Yi,Zi,CONTOURNUM,'k');
elseif strcmp(STYLE,'straight')
surface(Xi-delta/2,Yi-delta/2,zeros(size(Zi)),Zi,'EdgeColor','none',...
'FaceColor',SHADING);
% colorbar
elseif strcmp(STYLE,'fill')
contourf(Xi,Yi,Zi,CONTOURNUM,'k');
else
error('Invalid style')
end
caxis([amin amax]) % set coloraxis
end
set(ha,'Xlim',[-rmax*1.3 rmax*1.3],'Ylim',[-rmax*1.3 rmax*1.3])
% %%% Draw Head %%%%
l = 0:2*pi/100:2*pi;
basex = .18*rmax;
tip = rmax*1.15; base = rmax-.004;
EarX = [.497 .510 .518 .5299 .5419 .54 .547 .532 .510 .489];
EarY = [.0555 .0775 .0783 .0746 .0555 -.0055 -.0932 -.1313 -.1384 -.1199];
% Plot Electrodes
if strcmp(ELECTROD,'on')
hp2 = plot(y,x,EMARKER,'Color',ECOLOR,'markersize',EMARKERSIZE);
elseif strcmp(ELECTROD,'labels')
for i = 1:size(labels,1)
text(y(i),x(i),labels(i,:),'HorizontalAlignment','center',...
'VerticalAlignment','middle','Color',ECOLOR,...
'FontSize',EFSIZE)
end
elseif strcmp(ELECTROD,'numbers')
whos y x
for i = 1:size(labels,1)
text(y(i),x(i),int2str(i),'HorizontalAlignment','center',...
'VerticalAlignment','middle','Color',ECOLOR,...
'FontSize',EFSIZE)
end
end
% Plot Head, Ears, Nose
plot(cos(l).*rmax,sin(l).*rmax,...
'color',HCOLOR,'Linestyle','-','LineWidth',HLINEWIDTH);
plot([.18*rmax;0;-.18*rmax],[base;tip;base],...
'Color',HCOLOR,'LineWidth',HLINEWIDTH);
plot(EarX,EarY,'color',HCOLOR,'LineWidth',HLINEWIDTH)
plot(-EarX,EarY,'color',HCOLOR,'LineWidth',HLINEWIDTH)
hold off
axis off
静息态信号-可视化 整体代码:
%% 0.标准数据参数设置
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data_path = 'C:\Users\Amax\Desktop\basetest_flod\save_fold\';
svae_path = 'C:\Users\Amax\Desktop\basetest_flod\save_fold\';
channel_name_path = 'F:\机制分析-128导\channel_name_128';
channel_loc_path = 'C:\Users\Amax\Desktop\ZRK_BCI_code_Summary\channel_122_loc_2d.txt';
analysis_content = ['time\','freq_mean\','map\'];
plot_para = ['time\','freq\','space\'];
subject_num = [1 ; 10];
line_size = 1;
text_size = 5;
channel_num = [1:16];
freq_axis = [1;80];
freq_draw_log = 1;
space_axis = [1:5];
space_label = 'off';
disp(['||任务态数据分析中...||']);
disp(['被试量: ' , num2str(subject_num(1,1)),'-',num2str(subject_num(2,1))]);
disp(['是否绘图: ' , num2str(plot_para)]);
disp(['频谱绘制范围: ' , num2str(freq_axis(1,1)),'-',num2str(freq_axis(2,1))]);
disp(['地形图绘制范围: ' , num2str(space_axis)]);
disp(['线条宽度:' , num2str(line_size),' ||字体大小:' , num2str(text_size)]);
disp(['绘图通道:' , num2str(channel_num)]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 1.标准输入赋值
disp(['||预处理后数据加载中...||']);
Proprocess_target_file = load([data_path ,'Proprocess_target_',num2str(subject_num(1,1)),'_',num2str(subject_num(2,1))]);
Proprocess_nontarget_file = load([data_path ,'Proprocess_nontarget_',num2str(subject_num(1,1)),'_',num2str(subject_num(2,1))]);
stuct_target_name = 'Proprocess_target';
stuct_nontarget_name = 'Proprocess_nontarget';
Proprocess_target_data = Proprocess_target_file.(stuct_target_name).data;
Proprocess_nontarget_data = Proprocess_nontarget_file.(stuct_nontarget_name).data;
subject_num = Proprocess_target_file.(stuct_target_name).subject_num;
fs_down = Proprocess_target_file.(stuct_target_name).fs_down;
remain_trial_target = Proprocess_target_file.(stuct_target_name).remain_trial;
remain_trial_nontarget = Proprocess_nontarget_file.(stuct_nontarget_name).remain_trial;
Baseline_reference = Proprocess_target_file.(stuct_target_name).Baseline_reference ;
disp(['目标试次剩余: ' , num2str(remain_trial_target),'||平均: ', num2str(mean(remain_trial_target)),'||剩余比例: ',num2str(mean(remain_trial_target)/size(Proprocess_target_data,1))]);
disp(['非目标试次剩余: ' , num2str(remain_trial_nontarget),'||平均: ', num2str(mean(remain_trial_nontarget)),'||剩余比例: ',num2str(mean(remain_trial_nontarget)/size(Proprocess_nontarget_data,1))]);
%% 2.时域分析
disp(['||时域分析加载中...||']);
[time_target]= Analysis_rest_time(Proprocess_target_data,fs_down,Baseline_reference,remain_trial_target,mean_para);
[time_nontarget]= Analysis_rest_time(Proprocess_nontarget_data,fs_down,Baseline_reference,remain_trial_nontarget,mean_para);
%绘图
if contains(plot_para,'time')
load (channel_name_path);
Analysis_time_plot(time_target,time_nontarget,fs_down,Baseline_reference,channel_name,line_size,text_size)
end
%% 3.频域分析
disp(['||频域分析加载中...||']);
% 注:fft使用采样率作为参数,因此频谱分辨率为1hz
[freq_target]= Analysis_rest_freq(Proprocess_target_data,fs_down,remain_trial_target);
[freq_nontarget]= Analysis_rest_freq(Proprocess_nontarget_data,fs_down,remain_trial_nontarget);
%绘图
if contains(plot_para,'freq')
load (channel_name_path);
Analysis_freq_plot(freq_target,freq_nontarget,fs_down,channel_name,freq_axis,line_size,text_size,freq_draw_log)
end
%% 4.空域分析 脑地形图
disp(['||空域分析加载中...||']);
if contains(plot_para,'space')
Analysis_freq_space_plot(freq_target,freq_nontarget,channel_loc_path,fs_down,space_axis,space_label)
end
disp(['||数据分析完毕||']);
可视化是对脑电数据状态的直观呈现,既是对已处理信号的检查回顾,也是为下一步处理提供借鉴
通过时域抖动观察时序成分,频域能谱分析节律特征,空域分布了解脑区激活
避免从数据输入 到结果输出全程 端到端黑盒,中间缺少对数据的把控
当然,本文只介绍了最基础的时域、频域、空域可视化
还有多种可视化方法如PCA降维分布可视化、直方图统计可视化、脑网络连接可视化等
如有需要可以联系我,写新文章介绍进阶的脑电数据可视化方式
囿于能力,挂一漏万,如有笔误请大家指正~
感谢您耐心的观看,本系列更新了约30000字,约3000行开源代码,体量相当于一篇硕士工作。
往期内容放在了文章开头,麻烦帮忙点点赞,分享给有需要的朋友~
坚定初心,本博客永远:
免费拿走,全部开源,全部无偿分享~
自己:脑机接口+人工智领域,主攻大脑模式解码、身份认证、仿脑模型…
在读博士第3年,在最后1年,希望将代码、文档、经验、掉坑的经历分享给大家~
做的不好请大佬们多批评、多指导~ 虚心向大伙请教!
想一起做些事情 or 奇奇怪怪点子 or 单纯批评我的,请至[email protected]