% Adjust the filename as required below
filename = ‘ThisOne.txt’;

% How many lines to remove at the beginning?
% Enter the number of text lines – MUST BE AN EVEN NUMBER
% Remember that the first two samples are not valid in this file type
Number_Of_Lines_To_Remove = 10;

% How many lines to remove from the end? – MUST BE AN EVEN NUMBER
% Remember that the last line may not be complete
remove_log_data = 2;

% 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);

% If number of lines in file is odd then adjust the number of lines
% to remove at the end or some of the samples will have one extra
if mod(nol,2) == 1
nol = nol – 1;
end

% Remove the end data
nol = nol – Number_Of_Lines_To_Remove – remove_log_data;

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

% Import Raw Data
% Remove accellerometer and gyroscope data
fileID = fopen(‘Processed_EEG_Data.csv’);
C = textscan(fileID,’%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %*f %*f %*f %*s’, ‘Delimiter’,’,’);
fclose(fileID);

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

% Raw files are only actually 125Hz – the samples are doubled
% so you must remove every second one
Fp1_Raw = Fp1_Raw(2:2:end);
Fp2_Raw = Fp2_Raw(2:2:end);
C3_Raw = C3_Raw(2:2:end);
C4_Raw = C4_Raw(2:2:end);
P7_Raw = P7_Raw(2:2:end);
P8_Raw = P8_Raw(2:2:end);
O1_Raw = O1_Raw(2:2:end);
O2_Raw = O2_Raw(2:2:end);
F7_Raw = F7_Raw(1:2:end);
F8_Raw = F8_Raw(1:2:end);
F3_Raw = F3_Raw(1:2:end);
F4_Raw = F4_Raw(1:2:end);
T7_Raw = T7_Raw(1:2:end);
T8_Raw = T8_Raw(1:2:end);
P3_Raw = P3_Raw(1:2:end);
P4_Raw = P4_Raw(1:2:end);
Timer_Raw = Timer_Raw(1:floor(end/2));

% HP Filter for 5 Hz and above
fs = 125;
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);

% LP Filter for 60Hz and below
[N, Fo, Ao, W] = firpmord([58, 60]/(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);

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

% 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;
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_Lines_To_Remove;
clear eeg_data;
clear fileID;
clear C;
delete Processed_EEG_Data.csv;