\chapter{Documentation of the Code}
\label{app:docu}
The documentation of the code will be split into parts according to the use. In this parts the order will be alphabetically in the name of the function.

\section{\texttt{callAll.m}}
    \texttt{callAll.m} and \texttt{callAllPos.m} are the central point in the corresponding calculations. From this script every other function is called and the parameters are defined here.\\
    The default values for the parameters are given in table \ref{tab:default}.

    There are two independent scripts since this makes it possible to do calculations at the same time on the same machine and change called scripts without influencing the other run. In addition when calculating positions instead of velocities, some calculations do not need to be redone. Those are left out in \texttt{callAllPos.m}.
\section{Data Preprocessing}
    \subsection{\texttt{balanceClasses.m}}
        Balances classes e.g. for a SVM by dropping data from bigger classes.

        This function also takes an additional parameter \texttt{maxPerClass} which defines how many elements per class are taken in at most. This parameter can be used to enhance computations.

        The number of classes (\texttt{noClasses}) has to be passed to make sure no class is completely omitted because there was no such value in the training set.

        This function throws an error if the number of samples per class is lower than 1.
    \subsection{\texttt{classifyAccordingToEMG.m}}
    \label{code:classify}
        Here the reclassification as described in section~\ref{mm:newClass} is done.

        For each data point it is decided whether it belongs to movement or not according to the given threshold in EMG activity. If not there is no movement so the class is set to 0 (rest).\\
        If there is movement, it is decided whether there should be according to the given classification. If not, the old class is applied also for this point. If yes, it is checked whether the movement has just started (up to now class was 0). If the movement has just started, 1 second before is taken out (pause \true) or half a second before is classified as the same class, 0.5s to 1s is dropped (pause \false).
    \subsection{\texttt{generateTrainingData.m}}
    \label{code:generate}
        In this function the transformation of EEG and EMG signals is done as PSD with Burg's method for EEG and waveform length for EMG. Additionally velocities are computed from kinematic data.
    \subsection{\texttt{generateTrainingDataPos.m}}
        Same as \ref{code:generate} but kinematic data is used as positions.
    \subsection{\texttt{myDownsample.m}}
    \label{code:myDown}
        \texttt{myDownsample} takes the given number of equidistant samples to represent the input.
    \subsection{\texttt{namesAndNumbers.m}}
        \texttt{namesAndNumbers} returns names and numbers of subjects and runs according to the file given (created by \ref{code:run.bash})
    \subsection{\texttt{readAll.m}}
    \label{code:readAll}
        This is the central function for the preprocessing of data.

        First, the name of the generated file is composed out of the given parameters. In this way the preprocessing only has to be done once.\\
        If the file does not exist yet, it is created in the following steps:\\
        Data from BCI2000 is read along the corresponding kinematic information. Then this data is transformed in the form we want to use it (cf. \texttt{generateTrainingData} \ref{code:generate}). The data from each of the five runs (cf. section~\ref{mm:design}) is aggregated in one variable per modality.

        Next, the classification is done using \texttt{classifyAccordingToEMG.m} (\ref{code:classify}). The result is then smoothed and adjusted to the length of EEG data.

        Finally the kinematics and the synergies are generated matching the size of EMG data. All is then saved under the given path as \texttt{.mat} file.
    \subsection{\texttt{readAllPos.m}}
        Same as \ref{code:readAll} but using position instead of movement from kinematics data.
    \subsection{\texttt{readEMGsig.m}}
        Reads only the EMG signal (used for \ref{code:noSyn}).
    \subsection{\texttt{shiftingDownsample}}
        Shifts \texttt{myDownsample} \ref{code:myDown} over a signal.
    \subsection{\texttt{shiftingKin.m}}
        Calculates velocity in a window
    \subsection{\texttt{shiftingMean.m}}
        Downsamples by using the mean for a window.
    \subsection{\texttt{shiftingPburg.m}}
        Applies Pburg to a window.
    \subsection{\texttt{shiftingPos.m}}
        Calculates position for a window. If there is no data the next existent data will be used.
    \subsection{\texttt{shiftingSum.m}}
        Downsamples by using the sum in a window.
    \subsection{\texttt{waveformLength.m}}
        Calculates the waveform length for the given signal.
    \subsection{\texttt{waveformLengthAll.m}}
        Calculates the waveform length for the whole EMG signal calling \texttt{waveformLength.m} on defined windows
\section{Data Analysis}
    \subsection{\texttt{correlation2.m}}
        Calculates the squared correlation between the corresponding columns of two matrices.\\
        This is used as a measure for fit when comparing the number of synergies.
    \subsection{\texttt{kFoldCV.m}}
    \label{code:kfoldCV}
        \texttt{kfoldCV} does a k-fold Cross-validation of parameter c for SVM.
    \subsection{\texttt{kFoldRidge.m}}
    \label{code:kFoldRidge}
        \texttt{kFoldRidge} does a k-fold Cross-validation of parameter lambda for Ridge.
    \subsection{\texttt{noSynergies.m}}
    \label{code:noSyn}
        Plots performance (squared elementwise correlation) for 1 to 6 synergies for Autoencoder, PCA and NNMF (is called by \ref{code:noSyn.bash}).\\
        Whether the figure is shown or the data is saved can be adjusted by altering the corresponding flag.
    \subsection{\texttt{ridgeCorrelation.m}}
        Calculates the correlation with original data, according coefficients and the testing data for the prediction.
    \subsection{\texttt{ridgeCV.m}}
    \label{code:ridgeCV}
        Runs a ridge regression to predict 'goal' from 'data'. Therefore calls kFoldRidge.m \ref{code:kFoldRidge}.
    \subsection{\texttt{ridgeCVvia.m}}
        Same as \ref{code:ridgeCV} but has an intermediate step (usually synergies).
    \subsection{\texttt{svmEciton.m}}
        Runs a SVM to classify the data. Cross validation is done by calling \texttt{kfoldCV} \ref{code:kfoldCV}.
\section{Evaluation}
    \subsection{\texttt{evaluation.m}}
        Script for composing the file containing the results (\texttt{evaluation.mat}). This file is used for the evaluations after some manual post-processing (mainly renaming).
    \subsection{\texttt{evaluationAccuracys.m}}
        Evaluations run on results from SVMs.
    \subsection{\texttt{evaluationCorrelations.m}}
        Evaluations run on results from ridge regressions.
    \subsection{\texttt{plotCM.m}}
        Plots a confusion matrix. Axes are labeled according to the classes of EEG and EMG.
\section{Plots}
    \subsection{\texttt{mySaveFigure.m}}
        \texttt{mySaveFigure.m} save a given figure with default size at given filename.
    \subsection{\texttt{topoplot/plotOneSubjectOneDay.m}}
        Plots the topographical plot for on session. With a slider and a textbox any second can be inspected.
    \subsection{\texttt{topoplot/topoplot\_martin.m}}
        Function to generate topographical plots provided by Martin Spüler.
    \subsection{\texttt{topoplot/topoplot\_Wrapper.m}}
        Provides a slider and a textbox to navigate data.
\section{Bash Scripts}
    \subsection{\texttt{noOfSyn.bash}}
    \label{code:noSyn.bash}
        Starts \texttt{noSynergies.m} and pipes output to logfile.
    \subsection{\texttt{runOnEciton.bash}}
    \label{code:run.bash}
        Creates \texttt{dirs.txt} containing a list of sessions, then starts \texttt{callAll.m} and pipes output to logfile.
    \subsection{\texttt{runOnEcitonPos.bash}}
    \label{code:runPos.bash}
        Same as \ref{code:run.bash} but starts \texttt{callAllPos.m}.
\section{miscellaneous}
    \subsection{\texttt{psdPlot.m}}
        Plots PSD with pburg and pwelch.
    \subsection{\texttt{pickFromStruct.m}}
        Allows to pick $i$th entry of a \matlab-struct (without knowing the name).
