Code Samples
Snippets of Code Used in EMG Tools and a Description of What They Do
Filter parameters
Both bandpass and lowpass filters are set up using the butter
function in Matlab. Following the setup of filter coefficients, they are used to filter the data using Matlab's filtfilt
function.
% Bandpass Filter
[B, A] = butter(order, [cutoff_low,cutoff_high]/(sample_frequency/2), 'bandpass');
% Butterworth Filter
[B, A] = butter(order, cutoff_high/(sample_frequency/2), 'low');
% Filter raw data
data_filtered = filtfilt(B, A, data);
Remove DC shift.
DC bias is removed by taking the average of each channel of filtered data for the quiet lying trials and subtracting them from the relevant channels in the data files.
% Taking the average of each channel of the filtered quiet lying data
QL_data_avg = mean(QL_data_filt);
% Subtract the average from the relevant channel using the specified quiet lying file
data_bias_removed = data_filt - QL_data_avg;
Padding data.
Matlab's filter
function, which is used for the linear envelope does not pad the data. The data is therefore padded by EMG Tools using the invert method. This reverses the filtered data, divides it into two halves, inverts it, and puts each end on either end of the data array.
% find length of half the data array, use 'round' to ensure that
% value obtained is an integer
padding = round(length(data) / 2);
% reverse the data array (front to end, end to front)
reversed_data = flip(data, 1);
% divide reversed array into halves
first_half_data = reversed_data(padding:end,:);
last_half_data = reversed_data(1:padding,:);
% subtract value that will border start and end of data array from
% all of the front and end padding points respectively. Inverts
% each padding array and ensures no artificial noise is generated
% when padding points are added to the front and end of the data
for r=1:number_of_channels
front_padding_points(:,r) = first_half_data(end,r) - first_half_data(:,r);
end_padding_points(:,r) = last_half_data(1,r) - last_half_data(:,r);
end
% add padding points to the front and end of the data array
data_padded = [front_padding_points; data; end_padding_points];
Full wave rectify.
Full wave rectification is simply done by taking the absolute value of each data point.
% Taking the absolute value of each data point
rectified_data= abs(data_bias_removed);
Linear Envelope.
Filter coefficients for the linear envelope are set up using Matlab's butter
function. These are then used to linear envelope the data using Matlab's filter
function.
% Set up linear envelope coefficients
[D, E] = butter(filter_order_LE, cutoff_LE/(sample_frequency/2), 'low');
% Use coefficients in single-pass filter
data_LE = filter(D, E, data_full_wave_rectified);
Normalization.
The maximum value is found for each channel of linear enveloped MVC data. From here, the correct value from the correct channel from the correct MVC file is used on the relevant data file. Each point in the data file is converted to a percentage of this maximal value.
% Find the max value in the linear enveloped MVC data to be used for data
% normalization
MVC_max = max(MVC_LE);
% Normalize data
for i=1:number_of_MVC_files
for r=1:number_of_channels
for p=1:number_of_files
% compare strings in MVC dropdown menu to those in channel
% feedback window to find a match
if strcmp(char(filenames_MVC_trials (i,:)), get(handles.channel_list_MVC(1,r), 'String'))
% normalize the channel in each data file to the channel from the corresponding MVC file that was selected
data_normalized{p}(:,r) = ((data_LE{p}(:,r)) / (MVC_max{i}(:,r))) * 100;
end
end
end
end