Frequently with EEG data you need to filter the data as a part of the analysis. Here you will see how to implement a Butterworth filter on EEG data and also a plot to see the impact of the filter.
Download the data HERE first and put it in your MATLAB path.
The code to run is HERE and below.
Download the data HERE first and put it in your MATLAB path.
The code to run is HERE and below.
clear all;
close all;
clc;
load('eegData');
samplingRate = 500;
filterOrder = 2;
lowCutoff = 0.1;
highCutoff = 30;
amountOfDataToPlot = 500;
% get some data to plot from before the filter
preFilterData = eegData(1,1:amountOfDataToPlot);
% demean the data for comparison with the filtered data
preFilterData = preFilterData - mean(preFilterData);
% define Butterworth filter
[b,a] = butter(filterOrder,[lowCutoff highCutoff]/(samplingRate/2));
% filter data one channel at a time
for channelCounter = 1:size(eegData,1)
eegData(channelCounter,:) = filtfilt(b,a,double(eegData(channelCounter,:)));
end
% get some data to plot post filter
postFilterData = eegData(1,1:amountOfDataToPlot);
% plot some stuff
plot([1:1:amountOfDataToPlot],preFilterData,'linewidth',3);
hold on;
plot([1:1:amountOfDataToPlot],postFilterData,'linewidth',3);
hold off;
title('Pre versus Post Filter Data');
close all;
clc;
load('eegData');
samplingRate = 500;
filterOrder = 2;
lowCutoff = 0.1;
highCutoff = 30;
amountOfDataToPlot = 500;
% get some data to plot from before the filter
preFilterData = eegData(1,1:amountOfDataToPlot);
% demean the data for comparison with the filtered data
preFilterData = preFilterData - mean(preFilterData);
% define Butterworth filter
[b,a] = butter(filterOrder,[lowCutoff highCutoff]/(samplingRate/2));
% filter data one channel at a time
for channelCounter = 1:size(eegData,1)
eegData(channelCounter,:) = filtfilt(b,a,double(eegData(channelCounter,:)));
end
% get some data to plot post filter
postFilterData = eegData(1,1:amountOfDataToPlot);
% plot some stuff
plot([1:1:amountOfDataToPlot],preFilterData,'linewidth',3);
hold on;
plot([1:1:amountOfDataToPlot],postFilterData,'linewidth',3);
hold off;
title('Pre versus Post Filter Data');