Generalfox.com

Advanced Science Topics and Thought

Generalfox.com

% 1 Adjust the filename as required below
filename = ‘SDconverted-2018-01-20_16-21-20.csv’;

% remove last Y lines – when last reviewed there was a bunch of information
% written at the end of the file that is not required
remove_log_data = 17;

% Count the number of lines
fid = fopen(filename, ‘r’) ;
fseek(fid, 0, ‘eof’);
chunksize = ftell(fid);
fseek(fid, 0, ‘bof’);
ch = fread(fid, chunksize, ‘*uchar’);
nol = sum(ch == sprintf(‘\n’)); % number of lines
fclose(fid);

% Remove the first X lines of the sample (always)
% remove the last Y lines of the sample (review to see if required)
Number_Of_Line_To_Remove = 4;
fid = fopen(filename, ‘r’) ;
fid2 = fopen(‘Processed_EEG_Data.csv’,’w’);
x = 1;
for x = 1:Number_Of_Line_To_Remove
fgetl(fid) ;
end
x = 1;
for x = 1:(nol – remove_log_data)
tline = fgets(fid);
fwrite(fid2, tline);
end
fclose(fid2);
fclose(fid);

% Clear Temporary Variables
clear filename;
clear remove_log_data;
clear x;
clear ch;
clear nol;
clear tline;
clear chunksize;
clear fid;
clear fid2;
clear ans;
clear Number_Of_Line_To_Remove;

% Import Raw Data
eeg_raw_data = csvread(‘Processed_EEG_Data.csv’, 0, 0);

% 2 Remove accellerometer and gyroscope data
% Adjust the “end-X” calculation as per the number of entries
eeg_raw_data = eeg_raw_data(:,1:end-4);

% 3 Copy the raw data, transposing it into rows
eeg_data = eeg_raw_data’;

% 4 Separate the data into separate bands
Timer_Raw = eeg_data(1,:);
Fp1_Raw = eeg_data(2,:);
Fp2_Raw = eeg_data(3,:);
C3_Raw = eeg_data(4,:);
C4_Raw = eeg_data(5,:);
P7_Raw = eeg_data(6,:);
P8_Raw = eeg_data(7,:);
O1_Raw = eeg_data(8,:);
O2_Raw = eeg_data(9,:);
F7_Raw = eeg_data(10,:);
F8_Raw = eeg_data(11,:);
F3_Raw = eeg_data(12,:);
F4_Raw = eeg_data(13,:);
T7_Raw = eeg_data(14,:);
T8_Raw = eeg_data(15,:);
P3_Raw = eeg_data(16,:);
P4_Raw = eeg_data(17,:);

delete Processed_EEG_Data.csv;

% 5 HP Filter for 5 Hz and above
fs = 250;
d = designfilt(‘highpassfir’,’StopbandFrequency’,2,’PassbandFrequency’,4,’StopbandAttenuation’,40,’PassbandRipple’,.5,’SampleRate’,fs,’DesignMethod’,’equiripple’);
Fp1_Temp = filtfilt(d,Fp1_Raw);
Fp2_Temp = filtfilt(d,Fp2_Raw);
C3_Temp = filtfilt(d,C3_Raw);
C4_Temp = filtfilt(d,C4_Raw);
P7_Temp = filtfilt(d,P7_Raw);
P8_Temp = filtfilt(d,P8_Raw);
O1_Temp = filtfilt(d,O1_Raw);
O2_Temp = filtfilt(d,O2_Raw);
F7_Temp = filtfilt(d,F7_Raw);
F8_Temp = filtfilt(d,F8_Raw);
F3_Temp = filtfilt(d,F3_Raw);
F4_Temp = filtfilt(d,F4_Raw);
T7_Temp = filtfilt(d,T7_Raw);
T8_Temp = filtfilt(d,T8_Raw);
P3_Temp = filtfilt(d,P3_Raw);
P4_Temp = filtfilt(d,P4_Raw);

% 6 Notch Filter for 60Hz
d = designfilt(‘bandstopiir’,’FilterOrder’,2, ‘HalfPowerFrequency1′,59,’HalfPowerFrequency2’,61, ‘DesignMethod’,’butter’,’SampleRate’,fs);
Fp1_Temp = filtfilt(d,Fp1_Temp);
Fp2_Temp = filtfilt(d,Fp2_Temp);
C3_Temp = filtfilt(d,C3_Temp);
C4_Temp = filtfilt(d,C4_Temp);
P7_Temp = filtfilt(d,P7_Temp);
P8_Temp = filtfilt(d,P8_Temp);
O1_Temp = filtfilt(d,O1_Temp);
O2_Temp = filtfilt(d,O2_Temp);
F7_Temp = filtfilt(d,F7_Temp);
F8_Temp = filtfilt(d,F8_Temp);
F3_Temp = filtfilt(d,F3_Temp);
F4_Temp = filtfilt(d,F4_Temp);
T7_Temp = filtfilt(d,T7_Temp);
T8_Temp = filtfilt(d,T8_Temp);
P3_Temp = filtfilt(d,P3_Temp);
P4_Temp = filtfilt(d,P4_Temp);

% 7 LP Filter for 125 Hz and below
[N, Fo, Ao, W] = firpmord([122, 125]/(fs/2), [1 0], [0.057501127785, 0.0001]);
b = firpm(N, Fo, Ao, W, {20});
Hd = dfilt.dffir(b);
Fp1_Filtered = filter(Hd,Fp1_Temp);
Fp2_Filtered = filter(Hd,Fp2_Temp);
C3_Filtered = filter(Hd,C3_Temp);
C4_Filtered = filter(Hd,C4_Temp);
P7_Filtered = filter(Hd,P7_Temp);
P8_Filtered = filter(Hd,P8_Temp);
O1_Filtered = filter(Hd,O1_Temp);
O2_Filtered = filter(Hd,O2_Temp);
F7_Filtered = filter(Hd,F7_Temp);
F8_Filtered = filter(Hd,F8_Temp);
F3_Filtered = filter(Hd,F3_Temp);
F4_Filtered = filter(Hd,F4_Temp);
T7_Filtered = filter(Hd,T7_Temp);
T8_Filtered = filter(Hd,T8_Temp);
P3_Filtered = filter(Hd,P3_Temp);
P4_Filtered = filter(Hd,P4_Temp);

% 8 Number the samples
[col_count,row_count] = size(Timer_Raw);
Sample_Number = (1:1:row_count);
Sample_Time = (0.004:0.004:(row_count/fs));

% 9 Clear Temporary Variables
clear Fp1_Temp;
clear Fp2_Temp;
clear C3_Temp;
clear C4_Temp;
clear P7_Temp;
clear P8_Temp;
clear O1_Temp;
clear O2_Temp;
clear F7_Temp;
clear F8_Temp;
clear F3_Temp;
clear F4_Temp;
clear T7_Temp;
clear T8_Temp;
clear P3_Temp;
clear P4_Temp;
clear Ao;
clear b;
clear d;
clear Fo;
clear Hd;
clear N;
clear W;
clear col_count;