EEG Analysis is the basic way to do next step for machine leanring  other high dimension works , anyway before you kick off the this , it’s better to understand the whole job of the madeups

as we all know , EEG is recored while the eletrode are placed on the sclap, the signals ares shown as the waves with all kind of paprameters as frenquency phase amplitude  location and so on

next figure show :

we have two group patients and every subjects has three dataset as baseline  treatment and follow up,  considering the visual problems ,so we only need  two channels which is decided by the patient for different part stroke of brain, luckly  the O1 and O2 channel have been marked , a script need to be done to load the right one .

 

微信截图_20160306212657

 

EEGData Screeshot

微信截图_20160306213007

 

as above figure shows

the EEG data are stored as a 19X34048 matrixs which mean 19 channels and every channel has 34048 data points, the value of each cell is the amplitude .

meanwhile, when EEG is recording, other improtant infromation has been enclosed with the data, for example the resample rate,

微信截图_20160306213615

 

ncchan:the channels of EEG

trials :the subjects

pnts: the number of data point

srate: resample rate, every second  256 datapoint has been recored

xmin: the min value of data

xmax:the max value of data

times : the length of the data

data: the EEG data

chanloc:channel localtion

微信截图_20160306214152

 

ref: reference common

epoch: how many window has been selected

代码用中文作解释:

%% new matrixs
clf;%清除程序执行前的所有
file=dir(‘*.set’);%获取当前结尾.set的所有文件的文件名,并且存储在file 中,file是一个48×1的矩阵或者struck 每一行存储文件的信息,比如文件名 创建日期
filename=cell(length(file),1);%创建一个空的cell 储存文件名 长度为file的列数

group1=[];
group2=[];
baseline1=[];
baseline2=[];
treatment1=[];
treatment2=[];
followup1=[];
followup2=[];%%创建临时数组 储存数据
%% file catagloraze
for i=1:length(file) %for 循环讲文件名取出

filename(i,1)={file(i,1).name};%注意两个知识点:matlab中很多矩阵,取值和赋值的时候都是矩阵方式,另外取cell值必须永大括号{} 把file第i行第一列的数据储存在filename的第i列 第一行

if filename{i,1}(1)==’1’%此处filename(i,1)(1)取值是文件名的第一个字符 判断时是否为1即第一组

group1=[group1;filename(i,1)];%matlab赋值函数,将filename中的第i行第一列的值循环储存到group1,这种写法 data=[data;file] 中间是冒号
if filename{i,1}(6)==’1’&&filename{i,1}(1)==’1’%双重判定

baseline1=[baseline1;filename{i,1}];%将文件名第六个字符是1的储存在baseline1
elseif filename{i,1}(1)==’1’&&filename{i,1}(6)==’2′

treatment1=[treatment1;filename{i,1}];%将文件名第六个字符是2的储存在treatment1
elseif filename{i,1}(1)==’1’&&filename{i,1}(6)==’3′

followup1=[followup1;filename{i,1}];%将文件名第六个字符是3的储存在followup1

end
end%for循环结束

if filename{i,1}(1)==’2’%如果是第二组 同上

group2=[group2;filename(i,1)];

if filename{i,1}(1)==’2’&&filename{i,1}(6)==’1′

baseline2=[baseline2;filename{i,1}];

elseif filename{i,1}(1)==’2’&&filename{i,1}(6)==’2′

treatment2=[treatment2;filename{i,1}];

elseif filename{i,1}(1)==’2’&&filename{i,1}(6)==’3′

followup2=[followup2;filename{i,1}];

end

end

end%for 循环结束
%% signal clustering

baseEEG1=[];
treatEEG1=[];
followEEG1=[];%定义新的数据用来储存EEG信号数据

for i=1:length(baseline1(:,1))%找文件名baseline1

EEG1=pop_loadset(baseline1(i,:));%批量load数据进来
if baseline1(i,8)==’1’%如果文件名第8个字符是1,则
baseEEG1=[baseEEG1;EEG1.data(18,1:11008)];%把数据中的18行 每列的1到11008储存到baseEEG1
else
baseEEG1=[baseEEG1;EEG1.data(19,1:11008)]%把数据中的19行 每列的1到11008储存到baseEEG1
end

end

for i=1:length(treatment1(:,1))

EEG2=pop_loadset(treatment1(i,:));
if treatment1(i,8)==’1′
treatEEG1=[treatEEG1;EEG2.data(18,1:11008)];
else
treatEEG1=[treatEEG1;EEG2.data(19,1:11008)]
end

end

for i=1:length(followup1(:,1))

EEG3=pop_loadset(followup1(i,:));
if followup1(i,8)==’1′
followEEG1=[followEEG1;EEG3.data(18,1:11008)];
else
followEEG1=[followEEG1;EEG3.data(19,1:11008)]
end

end

%% mean of the signals
meanbaseEEG1=mean(baseEEG1);%求平均值,记得不加参数是行平均,加个2是列平均
meantreatEEG1=mean(treatEEG1);
meanfollowEEG1=mean(followEEG1);

EEG_pnts=11008; %频率谱函数需要的数据长度 就是 datapoints
EEG_srate=256;%抽样频率
%% plot
figure%画图
hold on

spectopos(meanbaseEEG1,EEG_pnts,EEG_srate);%spectopo画频率谱的函数。极好
hold on

spectopos(meantreatEEG1,EEG_pnts,EEG_srate);

hold on

spectopos(meanfollowEEG1,EEG_pnts,EEG_srate);
%% remarks and geting parameters
for j=1:size(baseEEG1) % the size and lenght

[eegdb,fres]=spectopos(baseEEG1(j,:),11008,256); %spectopo functions
baseEEGDB1=[baseEEGDB1;eegdb];
baseFRESS1=[baseFRESS1;fres’];% putthe values into new matrixs

end

save(‘EEGDB1.mat’,’baseEEGDB1′);
save(‘fres1.mat’,’baseFRESS1′);/save files in local

微信截图_20160306224243