使用多svm识别睡眠障碍

时间:2019-05-20 19:37:58

标签: matlab machine-learning

我对睡眠障碍的识别感兴趣,但是我尝试了睡眠阶段分类的代码,但是我进入了睡眠障碍的编码部分,并找到了诸如能量,准确性,MSE,标准差,方差之类的参数。 所以,请帮我编码

function varargout = EEG_Application_Sleep(varargin)
% EEG_APPLICATION_SLEEP MATLAB code for EEG_Application_Sleep.fig
%      EEG_APPLICATION_SLEEP, by itself, creates a new EEG_APPLICATION_SLEEP or raises the existing
%      singleton*.
%
%      H = EEG_APPLICATION_SLEEP returns the handle to a new EEG_APPLICATION_SLEEP or the handle to
%      the existing singleton*.
%
%      EEG_APPLICATION_SLEEP('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in EEG_APPLICATION_SLEEP.M with the given input arguments.
%
%      EEG_APPLICATION_SLEEP('Property','Value',...) creates a new EEG_APPLICATION_SLEEP or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before EEG_Application_Sleep_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to EEG_Application_Sleep_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help EEG_Application_Sleep

% Last Modified by GUIDE v2.5 22-Apr-2019 00:38:09

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @EEG_Application_Sleep_OpeningFcn, ...
                   'gui_OutputFcn',  @EEG_Application_Sleep_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before EEG_Application_Sleep is made visible.
function EEG_Application_Sleep_OpeningFcn(hObject, ~, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to EEG_Application_Sleep (see VARARGIN)
handles.output = hObject;
ah=axes('unit','normalized','position',[0 0 1 1]);
bg=imread('EEG-Brain1.jpg'); imagesc(bg);
set(ah,'handlevisibility','off','visible','off');
uistack(ah,'bottom');
handles.output = hObject;
% Choose default command line output for EEG_Signal_Processing_Analysis
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes EEG_Application_Sleep wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = EEG_Application_Sleep_OutputFcn(~, ~, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, ~, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

load('Features_8.mat')
load('Features_7.mat')
load('Features_6.mat')
load('Features_5.mat')
load('Features_4.mat')
load('Features_3.mat')
load('Features_2.mat')
load('Features_1.mat')

trainData=[feaures_S1; ...
           feaures_S2; ...
           feaures_S3; ...
           feaures_S4; ...
           feaures_S5; ...
           feaures_S6; ...
           feaures_S7; ...
           feaures_S8];

   class=[1 2 2 1 2 1 2 2];


SVMStruct = svmtrain (trainData, class);



originalFolder = pwd; 
        [baseFileName, folder] = uigetfile('*.*', 'Specify an image file'); 
        fullImageFileName = fullfile(folder, baseFileName);

            % Read in image into an array.
%   [rgbImage storedColorMap] = imread(fullImageFileName); 

%% Import the data
[~, ~, raw] = xlsread(fullImageFileName,'Sheet1');
% [~, ~, raw] = xlsread('C:\Users\SEM\OneDrive\Projects\2018\MATLAB\Oxford_excel\project_cancer\HCC cancerous 1551nm.ods','Sheet1','B1:C1551');


%% Create output variable
data = reshape([raw{:}],size(raw));
% figure,plot(data)

%% Allocate imported array to column variable names
% VarName2 = data(:,1);
% VarName3 = data(:,2);

%% Clear temporary variables
% clearvars data raw;
% subplot(2,1,1)
% plot(data(:,2))
% subplot(2,1,2)
% plot(data(:,1))



% load eegdata.mat;
% s=eegdata;
% s=LeftBackward3(:,1);
s=data;
s=s(1:3000);
 figure,plot(s);title('Input signal');
        %band pass filtering
         y = bpfilt(s,0.2,60,500);


        eeg=y;
f_s=500;
N=length(eeg);
t=[0:N-1]/f_s; %time period(total sample/Fs )
% figure
% plot(t,eeg); title('Raw ECG Data plotting ')             
% xlabel('time')
% ylabel('amplitude')
w=50/(500/2);
bw=w;
[num,den]=iirnotch(w,bw); % notch filter implementation 
eeg_notch=filter(num,den,eeg);
% figure,
N1=length(eeg_notch);
t1=[0:N1-1]/f_s;


figure; subplot(2,1,1); plot(eeg);
title('Notch filter input ') 
subplot(2,1,2);plot(eeg_notch)
title('Notch filter output ') 

N=length(eeg_notch);
%  wavlet_dec_fun_a( ecg_notch,N )


waveletFunction = 'db8';
                [C,L] = wavedec(eeg_notch,8,waveletFunction);

                cD1 = detcoef(C,L,1);
                cD2 = detcoef(C,L,2);
                cD3 = detcoef(C,L,3);
                cD4 = detcoef(C,L,4);
                cD5 = detcoef(C,L,5); 
                cD6 = detcoef(C,L,6); 
                cD7 = detcoef(C,L,7); 
                cD8 = detcoef(C,L,8); 
                cA8 = appcoef(C,L,waveletFunction,8); 
                D1 = wrcoef('d',C,L,waveletFunction,1);
                D2 = wrcoef('d',C,L,waveletFunction,2);
                D3 = wrcoef('d',C,L,waveletFunction,3);
                D4 = wrcoef('d',C,L,waveletFunction,4);
                D5 = wrcoef('d',C,L,waveletFunction,5); 
                D6 = wrcoef('d',C,L,waveletFunction,6); 
                D7 = wrcoef('d',C,L,waveletFunction,7); 
                D8 = wrcoef('d',C,L,waveletFunction,8); 
                A8 = wrcoef('a',C,L,waveletFunction,8);


%                 axes(handles.axes3);plot(D5);title('GAMMA');
                figure; subplot(5,1,3); plot(D5);title('GAMMA');

%                 axes(handles.axes4);plot(D6);title('BETA');
                subplot(5,1,2); plot(D6);title('BETA');

%                 axes(handles.axes5);plot(D7);title('ALPHA');
                subplot(5,1,1); plot(D7);title('ALPHA');

                D8 = detrend(D8,0);
%                 axes(handles.axes6);plot(D8);title('THETA');
                subplot(5,1,4); plot(D8);title('THETA');

                Gamma = D5;
                Beta = D6;
                Alpha = D7;
                Theta = D8;
                Delta = A8;

%                 c=xlsread('data ep3');
% plot(c)
% ts=2;
% fs=500;
% [N,nu]=size(c);
% t=(1:N)*ts;
% h=figure
% plot(t,c,'r');
% title('eeg data');
%  
% y=fft(c);
% ps1=abs(y).^2;
% freq=(1:N)*fs/N;
% h2=figure
% plot (freq,20*log(ps1),'b')
% title ('power spectrum using fft method')
%  
% waveletFunction = 'db8';
%                 [C,L] = wavedec(c,8,waveletFunction);
%        
%                 cD1 = detcoef(C,L,1); 
%                 cD2 = detcoef(C,L,2); 
%                 cD3 = detcoef(C,L,3); 
%                 cD4 = detcoef(C,L,4); 
%                 cD5 = detcoef(C,L,5); %GAMA
%                 cD6 = detcoef(C,L,6); %BETA
%                 cD7 = detcoef(C,L,7); %ALPHA
%                 cD8 = detcoef(C,L,8); %THETA
%                 cA8 = appcoef(C,L,waveletFunction,8); %DELTA 
%                 D1 = wrcoef('d',C,L,waveletFunction,1); 
%                 D2 = wrcoef('d',C,L,waveletFunction,2); 
%                 D3 = wrcoef('d',C,L,waveletFunction,3); 
%                 D4 = wrcoef('d',C,L,waveletFunction,4); 
%                 D5 = wrcoef('d',C,L,waveletFunction,5); %GAMMA
%                 D6 = wrcoef('d',C,L,waveletFunction,6); %BETA
%                 D7 = wrcoef('d',C,L,waveletFunction,7); %ALPHA
%                 D8 = wrcoef('d',C,L,waveletFunction,8); %THETA
%                 A8 = wrcoef('a',C,L,waveletFunction,8); %DELTA
%                
%                 Gamma = cD5; 
%          figure; subplot(5,1,3); plot(1:1:length(Gamma),Gamma);title('GAMMA');
%                
%                 Beta = cD6; 
%                 subplot(5,1,2); plot(1:1:length(Beta), Beta); title('BETA');
%                 
%                 
%                 Alpha = cD7; 
%                 subplot(5,1,1); plot(1:1:length(Alpha),Alpha); title('ALPHA'); 
%                 
%                 
%                 Theta = cD8; 
%                 subplot(5,1,4); plot(1:1:length(Theta),Theta);title('THETA');
%                 
%                 
%                 Delta = cA8; 
%                 subplot(5,1,5);plot(1:1:length(Delta),Delta);title('DELTA');
% fprintf('energy of gamma')
% SIGNALENERGY1=sum(abs(D5).^2)
% fprintf('energy of beta')
% SIGNALENERGY2=sum(abs(D6).^2)
% fprintf('energy of alpha')
% SIGNALENERGY3=sum(abs(D7).^2);
% fprintf('energy of theta')
% SIGNALENERGY4=sum(abs(D8).^2)
% fprintf('energy of delta')
% SIGNALENERGY5=sum(abs(A8).^2)
%  
% el3=[SIGNALENERGY1;SIGNALENERGY2;SIGNALENERGY3;SIGNALENERGY4;SIGNALENERGY5]



%             axes(handles.axes7);plot(A8);title('Delta');
             subplot(5,1,5); plot(A8);title('Delta');


D5 = detrend(D5,0);
xdft = fft(D5);
freq = 0:N/length(D5):N/2;
xdft = xdft(1:length(D5)/2+1);

%   axes(handles.axes8);plot(freq,abs(xdft));title('GAMMA-FREQUENCY');
  figure;subplot(513);plot(freq,abs(xdft));title('GAMMA-FREQUENCY');
[~,I] = max(abs(xdft));
fprintf('Gamma:Maximum occurs at %3.2f Hz.\n',freq(I));
set(handles.text7, 'String', freq(I));

D6 = detrend(D6,0);
xdft2 = fft(D6);
freq2 = 0:N/length(D6):N/2;
xdft2 = xdft2(1:length(D6)/2+1);
%  axes(handles.axes9);plot(freq2,abs(xdft2));title('BETA');
subplot(512);plot(freq,abs(xdft2));title('BETA');
[~,I] = max(abs(xdft2));
fprintf('Beta:Maximum occurs at %3.2f Hz.\n',freq2(I));

set(handles.text8, 'String', freq2(I));

D7 = detrend(D7,0);
xdft3 = fft(D7);
freq3 = 0:N/length(D7):N/2;
xdft3 = xdft3(1:length(D7)/2+1);

[~,I] = max(abs(xdft3));
fprintf('Alpha:Maximum occurs at %f Hz.\n',freq3(I));

set(handles.text9, 'String', freq3(I));     
%  axes(handles.axes10);plot(freq3,abs(xdft3));title('ALPHA');
 subplot(511);plot(freq,abs(xdft3));title('ALPHA');


 xdft4 = fft(D8);
freq4 = 0:N/length(D8):N/2;
xdft4 = xdft4(1:length(D8)/2+1);

%  axes(handles.axes11);plot(freq4,abs(xdft4));title('THETA'); 
subplot(514);plot(freq,abs(xdft4));title('THETA');
[~,I] = max(abs(xdft4));
fprintf('Theta:Maximum occurs at %f Hz.\n',freq4(I));

set(handles.text10, 'String', freq4(I));
A8 = detrend(A8,0);
xdft5 = fft(A8);
freq5 = 0:N/length(A8):N/2;
xdft5 = xdft5(1:length(A8)/2+1);

% axes(handles.axes12);plot(freq4,abs(xdft5));title('DELTA'); 
subplot(515);plot(freq,abs(xdft5));title('DELTA');
[~,I] = max(abs(xdft5));
fprintf('Delta:Maximum occurs at %f Hz.\n',freq5(I));


set(handles.text11, 'String', freq5(I));
Alpha = D7;
                Theta = D8;


                 Delta = A8;
 pw_A = pwelch(Alpha); SA = std(pw_A);  vA=var(pw_A);
 pw_B = pwelch(Beta);  SB = std(pw_B);  vB=var(pw_B);
 pw_G = pwelch(Gamma); SG = std(pw_G);  vG=var(pw_G);
 pw_T = pwelch(Theta); ST = std(pw_T);  vT=var(pw_T);
 pw_D = pwelch(Delta); SD = std(pw_D);  vD=var(pw_D);


%  Right forward train 1-channel5
%  features_mat1_RF1_5=[SG SB SA ST SD];
%  features_mat2_RF1_5=[vG vB vA vT vD];

  features_mat1_RB1_5=[SG SB SA ST SD];
%  features_mat2_RB1_5=[vG vB vA vT vD];

 feaures_S= [SG SB SA ST SD];
 feaures_V = [vG vB vA vT vD];

 feaures_S_param=feaures_S*10^8;
 feaures_V_param=feaures_V*10^12;
%  max(data)

%  Type_S = predict(mdS,feaures_S)
%  Type_V = predict(mdV,feaures_V)
xx=25;

process_dll
input_data= feaures_S;

result = svmclassify(SVMStruct, feaures_S);

temp_H=ones(100,100,'uint8')*255;
temp_L=ones(100,100,'uint8')*0;

 gimg(:,:,1)=temp_L;
 gimg(:,:,2)=temp_H;
 gimg(:,:,3)=temp_L;

 rimg(:,:,1)=temp_H;
 rimg(:,:,2)=temp_L;
 rimg(:,:,3)=temp_L;

 bimg(:,:,1)=temp_L;
 bimg(:,:,2)=temp_L;
 bimg(:,:,3)=temp_H;
% disp('feature v')
 feaures_V_param(1,1);
 if(feaures_V_param(1,1)>1.204 && feaures_V_param(1,1)<1.3046)
    axes(handles.axes1)
        imshow(gimg)
        pause(0.25)
       axes(handles.axes1)
        imshow(ones)
         pause(0.25)
      axes(handles.axes1)
        imshow(gimg)
         pause(0.25)
          axes(handles.axes1)
        imshow(ones)
           pause(0.25)
       axes(handles.axes1)
        imshow(gimg)
         pause(0.25)
        axes(handles.axes1)
        imshow(ones)
      axes(handles.axes1)
        imshow(gimg)
            disp('normal ');
    set(handles.text1, 'String', 'normal ');
    voice_play('normal '); 
    else

if(result==1)

        axes(handles.axes1)
        imshow(rimg)
        pause(0.25)
       axes(handles.axes1)
        imshow(ones)
         pause(0.25)
      axes(handles.axes1)
        imshow(rimg)
         pause(0.25)
          axes(handles.axes1)
        imshow(ones)
           pause(0.25)
       axes(handles.axes1)
        imshow(rimg)
         pause(0.25)
        axes(handles.axes1)
        imshow(ones)
      axes(handles.axes1)
        imshow(rimg)
                 disp('stage1');
        set(handles.text1, 'String', 'stage1');
        voice_play('stage1');  
else

axes(handles.axes1)
        imshow(bimg)
        pause(0.25)
       axes(handles.axes1)
        imshow(ones)
         pause(0.25)
      axes(handles.axes1)
        imshow(bimg)
         pause(0.25)
          axes(handles.axes1)
        imshow(bimg)
           pause(0.25)
       axes(handles.axes1)
        imshow(bimg)
         pause(0.25)
        axes(handles.axes1)
        imshow(ones)
      axes(handles.axes1)
        imshow(bimg)
            disp('stage2');
    set(handles.text1, 'String', 'stage2');
    voice_play('stage2'); 
end
 end
% Choose default command line output for EEG_Application_Sleep
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);


% --- Executes on mouse press over axes background.


% --- Executes when figure1 is resized.
% function figure1_ResizeFcn(hObject, eventdata, handles)
% % hObject    handle to figure1 (see GCBO)
% % eventdata  reserved - to be defined in a future version of MATLAB
% % handles    structure with handles and user data (see GUIDATA)
% 
% 
% % --------------------------------------------------------------------
% function Untitled_1_Callback(hObject, eventdata, handles)
% % hObject    handle to Untitled_1 (see GCBO)
% % eventdata  reserved - to be defined in a future version of MATLAB
% % handles    structure with handles and user data (see GUIDATA)


% --- Executes during object creation, after setting all properties.
% hObject    handle to axes3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: place code in OpeningFcn to populate axes3


% --- Executes during object deletion, before destroying properties.
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes during object deletion, before destroying properties.

% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    struc

具有句柄和用户数据的实体(请参阅GUIDATA)

0 个答案:

没有答案