Anda di halaman 1dari 14

EEG-FFT Analysis

Final Project
Neenu Baby, David Diaz, Saran Phatharodom
Emails: nb74@drexel.edu, dd564@drexel.edu, sp694@drexel.edu

12/7/2011

BMES 546

Final Project

Baby, Diaz, Phatharodom

Table of Contents
Abstract: ........................................................................................................................................................................3 Definition of term: .........................................................................................................................................................3 Background: ..................................................................................................................................................................3 Problem Description: .....................................................................................................................................................3 Program Description: .....................................................................................................................................................4 Input of the system: .......................................................................................................................................................5 Output of the system: .....................................................................................................................................................5 Data Representation .......................................................................................................................................................6 Process Flow ..................................................................................................................................................................6 Preprocessing ............................................................................................................................................................7 Run FFT analysis on each epoch ...............................................................................................................................7 GUI: Step by Step .....................................................................................................................................................7 GUI Prototype .......................................................................................................................................................8 Function Assignments ...................................................................................................................................................8 Challenges .....................................................................................................................................................................9 Results ...........................................................................................................................................................................9 Conclusions and Future Work .....................................................................................................................................10 Additions/Modifications (since Presentation) ..............................................................................................................10 Function Definitions ....................................................................................................................................................11 [1] savetomat(rawdatapath,matpath) .......................................................................................................................11 [2] runanalysis(input parameters listed in Section3 GUI at 3))ASASASASA ........................................................11 [3] result = checkconsistency(rawdatapath) ............................................................................................................11 [4] Fs = readvhdr(vhdrfilepath) ...............................................................................................................................11 [5] fieldparse(input parameters listed in Section3 Gui at 4)) ..................................................................................12 [6] signaldata = readeeg.m(txtfilepath,matpath,channel) .......................................................................................12 [7] [mkNum,evType,time] = readvmrk.m(vmrkfilepath) ........................................................................................12 [8] pnum = getperiodnum(periodendtime,currentperiod,time) ................................................................................13 [9] meanFFT = meanperiod(FFTepoch,periodNum,periodTotal) ...........................................................................13 [10] writefft(savefilename, outputbody, outputbodymean) .....................................................................................13 [11] plotfft(savefilename,expNum) .........................................................................................................................13 [12] [chName, chNum, totalChNum] = getchinfo(rawdatapath,chInput) ...............................................................13

BMES 546

Final Project

Baby, Diaz, Phatharodom

Abstract:
The project goal is to apply Fast Fourier Transform (FFT) analysis on specific time intervals of signal data from experiments that use electroencephalograpy (EEG) recordings. Each time interval consists of time periods before (pre) and after (post) the event of stimulus. Only the user-specified time intervals will be extracted for FFT analysis of each interval. Event marker data is given along with the EEG signal to mark the time of the stimulus event. There are 64 channels recording for each experiment result. The program should be able to perform FFT analysis on specified channels, at specified time intervals with a given maximum frequency. The program also divides a specific experiment (or EEG reading session) into a certain number of periods, chosen by the user. Then, the average of the FFT output within each period will be calculated, printed and plotted to compare the change in frequency spectrum throughout the progression of the experiment.

Definition of term:
Epoch: A time interval consisting of time intervals prior to and after an stimulus Period: One experiment is evenly divided into N-period based on time, where N is specified by the user input.

Background:
EEG is a test that measures and records the electrical activity of brain. EEG generally has complex characteristics and has a great significance in brain research and clinical. This motivated for the early introduction of signal analysis methods to EEG studies. EEG processing using spectral methods started with the first attempt of Fourier analysis application to EEG in 1932.

Figure 1: Example of EEG signal data of a channel and its transformation from time domain to frequency domain after applying FFT

The above graphs show the representation of the important fields that need to be extracted from the three data files (TXT, VMRK, and VHDR) of each experiment. The maximum (cutoff) frequency, the time intervals and event markers for both the event stimulus and event response, as well as the channel number and the Epoch are all important parameters that will be specified by the users as inputs to our program and will be used for specific data parsing and analysis (as indicated above).

Problem Description:
Fast Fourier transform (FFT) was used in EEG signal processing soon after its introduction. Even today, this method remains the most widespread method in EEG signal processing. The data used in this project

BMES 546

Final Project

Baby, Diaz, Phatharodom

contain three types of files generated by commercial software while recoding EEG signals. The recordings are used to test the responses of individuals with specific impairments (i.e dyslexia) and compare them to responses from individuals not suffering from the ailment/disorder in an attempt to better understand the expression of the disorder(s) and the similarities/differences between the two groups. Each experiment has data recorded from 64 channels. The problem is that, as of now, there is no current standard platform on which to analyze the specific frequency data from experiments using EEG recordings. The data can be analyzed more effectively if there is a way for analyzing the data from user specified channels. Therefore the goal of this project is to provide a way for EEG data collected in the time domain to be visualized in the frequency domain using the information in the three types of data files. The program will also allow researchers to specify channel and time interval information in order to obtain a more specific analysis of the specific stimulus.

Program Description:
The program will be taking inputs from the user and showing plot(s) in response to the user request for investigation and further adjust input parameters (e.g. requested channel number, time interval length, number of periods, etc.)

Figure 2: EEG signal with five periods (Left) and their respective average FFT plot (Right)

Implementation: (The process could be divided into three phases.) (1) Phase I Pre-processing (Use: I/O, string parsing, save Matlab variable, GUI) Since the raw data is in text file format and each column contains signal data for each EEG channel. Usually only signals of a few channels will be inquired. To improve speed of reading time, reading and saving the data as Matlab variables (binary format) would speed up columns reading. (2) Phase II FFT analysis (Use: I/O, string parsing, vector indexing, GUI) Information will be extracted from filenames and file contents. FFT analysis and averaging will be performed on signals for requested channels. Selecting data for selected period using vector indexing. Output will be written to files. (3) Phase III Post-processing (Use: I/O, plot) Plot will be shown. User will be able to call up the plot for each experiment in order to compare multiple experimental tests.

BMES 546

Final Project

Baby, Diaz, Phatharodom

Input of the system:

Figure 3: Input file system of the program

All the files of EEG recording for all experiments of a study are in one folder to be analyzed. For each of the experiments, there are 3 files with the same filename but different file extensions. 1. .txt contains EEG signal data for all 64 channels. (The largest file of all three types) 2. .vmrk contains event marker information (event type and time of occurrence) 3. .vhdr contains system parameters (constant for each experiment), which are sampling frequency, channel positions, channel numbers and channel names.

Output of the system:

Figure 4: The output system of the program

Two .csv files (will be imported to SPSS program for further statistical analysis) 1. Output file containing each selected epoch, for all experiments within a study 2. Output file containing average FFT values within each period, for all experiments within a study Plot of average FFT values within each period, for each experiment. However, the user would probably select to show some but not all experiment, e.g. one every five experiment, show the plot.

BMES 546

Final Project

Baby, Diaz, Phatharodom

Data Representation

Process Flow

Figure 5: Flow chat describing the entire program: input and output system

BMES 546

Final Project

Baby, Diaz, Phatharodom

Preprocessing
(Optional) read each .txt file and save the data as MATLAB variable in binary format. Since the raw data is in text format and data of interest are in column-wise, data parsing time could be long. To minimize the paring time, we could store the data in MATLAB variable. It should be faster due to binary file reading and column-wise data parsing within MATLAB protocol. The drawback is the extra space taken, since the file size of .txt EEG signal data file is large. Note that the program will run analysis on each channel, thus it will read a column each time the analysis is run. Called functions: (1) savetomat.m

Run FFT analysis on each epoch


(2.1) Check consistency check if all the experiments have the same system parameters. The most important one is sampling frequency. Usually this is consistent. Called functions: (1) runanalysis.m, (2) checkconsistency.m, (3) readvhdr.m (2.2) Field parsing from filename extract some information from filenames. For example, groupID for each experiment (control vs. experimental), experimentVariation (visual cue set 1, 2 or 3). Called function: (1) fieldparse.m (2.3) Run FFT on each experiment, for a chosen channel, until all experiments in the folder being analyzed are processed. Called functions: (1) readeeg.m, (2) readvmrk.m, (3) getperiodnum.m,(4) meanperiod.m (2.4) For each experiment, write to the two output files, appending the latter experiment to the previous. Called function: (1) writefft.m (2.5) For each experiment, display plot of average FFT values within each period (if user request). Called function: (1)plotfft.m

GUI: Step by Step


1. Click on the browse button to assces the folder saved in the computer and choose the folder where rawdata from EEG experiements are saved (fakedata) 2. Click on the preprocessing button underneath the file browsing section to make .mat versions of the txt file in the raw data folder. This is part is optional for the user. 3. Select a channel number from 1 to 64 and click enter to automatically fill the channel name box. 4. Choose an integre greater than zero as the numeber of periods. 5. Choose an interger greater than zero as the max frequncy. 6. In the event marker section, chooe two of the stimuli labels (11, 12, 13). 7. Choose response label as one and two. 8. In the epoch time interval, choose 0.2 and 0.8 as the pre-stimuli and post-stimuli time respectively. 9. Sample field parsing parameters are already given in the GUI, however, the user can change them. 10. Click on the Get Filename button and then click on Test FileParsing to see the Sample Result. 11. Specify an output file name and click on Run FFT Analysis button to generate the output files and the plot. The output files will be saved to the result folder. 12. Input Experiment Number box can be used to view plots of specified experiemnts. 13. Clear button at the bottom can be used clear the plots.

BMES 546 GUI Prototype

Final Project

Baby, Diaz, Phatharodom

Function Assignments

Figure 6: Main sections of the program. Each section has one to several functions. The color code in the function shows the division of work

BMES 546

Final Project

Baby, Diaz, Phatharodom

Challenges
1. The most challenging parts of the code were the lengthy debugging process and making sure all the variables matched the assignments by all three different authors. 2. Another challenge was working with big files with 60+ mb each and reading only one of the many columns. We solved it by using .mat version of the txt files and saving data for each column to one variable so that we could load each variable for respective columns. 3. Naming the variables and functions with small and easily understanding names. Also having consistency in the variable and function names used in different parts of the program was also an issue. 4. Coming up with the possible input errors the users can make in order to raise the error warning dialogues. For solving this problem, an inbuilt function called try&catch was used which is able to lump all the error dialogues. However, this solution has a disadvantage that the function generalizes the error. This will lead to display inappropriate error messages in some situations. 5. Another challenge in error handling was to decide whether the GUI or the individual functions should raise the error dialogue boxes. Finally, we end up dividing these tasks between the GUI and individual functions.

Results
The result of the program will output two excel file, output and mean of the output, and the plot of the result obtained. Following figures shows the examples of the output and mean output files.

Figure 7:First three lines of the output csv file

In the figure shown above, the first four columns, A to D, are the result obtained from parsing option in the GUI. This function is optional and the user has the freedom to choose what information he/she wants to parse. For this reason, information in those columns is subjected to changes according to the user preferences. The information in columns E to L shows the channel name, period number, marker number, stimulus types, response type, response time and FFT data obtained, where the frequency goes from 1 to upper bound frequency chosen by the user. The following figure shows the output file for mean FFT obtained. Columns A to D will not be different from from the output csv file. The rest of the columns show the channel number, period number (1 to the period total), sample size (number of samples in the period) and the mean FFT data.

BMES 546

Final Project

Baby, Diaz, Phatharodom

Figure 8: : First four lines of the csv file with mean of the output obtained

Finally, the program also will output the plot of the average FFT of epochs in each periods specified by the use. Following figure is an example of such a plot.

Figure 9: Example of average FFT plot

Conclusions and Future Work


The end result shows that we were successful in meeting the goals of the project. The program is able to read the input file, parse the data from the files and to do the FFT analysis on the EEG data on user specified channels. It successfully generates .csv output files and average FFT plot on the GUI. The plots help the user to view the peak of the graphs (8 to 12 Hz) and other major trends in the amplitudes of the data. The main advantage of this program is that it is flexible enough to be used for any other of experimental data that requires performing FFT analysis. It does not necessarily have to be EEG data. In future, we will perform more debugging on the GUI to find additional possible invalid inputs for creating more meaningful warning messages to help the user to use the software effectively. Currently, the program can run only the data from one channel at a time. In future, we will also need to make the program capable enough to run for more than one user specified channels. Also, we will also consider implementing front/back arrow button for viewing plots in a better way. More options for save/load configurations such that the user can specify where to save the output files will be also added in future.

Additions/Modifications (since Presentation)


After the presentation, most of our time was spent doing debugging for the GUI. We used try and catch as well as multiple error, warning, and message box dialogs to provide the user with warnings about invalid inputs and the overall functionality of the program.

BMES 546

Final Project

Baby, Diaz, Phatharodom

Function Definitions
[1] savetomat(rawdatapath,matpath)
The only input argument for this function is the rawdatapath or the folder where all the raw data for the experiments is located. The program will run through all the .txt files in the folder and check if .mat for each file exists in the matpath folder. If the .mat file exist, the function will skip to the next file. If not, the function will import the EEG signal data from the .txt file and read a matrix of Nx64 (N = number of data points, 64 channels). Finally, the function will save the Nx64 matrix to a binary format (.mat) into the matpath folder, using the save function. This function is invoked by the GUI preprocessing button.

[2] runanalysis(input parameters listed in Section3 GUI at 3))ASASASASA


This function is the main functionality of the program. It calls many other functions, as the function progresses in the below steps. It is the overarching function that links all the other functions to execute all the string parsing, files reading, output file creation, and plotting. Take a look at the matlab function code for more details.

[3] result = checkconsistency(rawdatapath)


This function checks if all the experiments have the same system parameters on each VHDR file. The most important parameter we check for is the sampling frequency (FS). The function has the raw data path as the only input argument and it performs the consistency check for the sampling frequencies on all the VDHR files found on that folder path. Once all files are run, if there is more than one Fs found, the function returns 0, and prints to the command window the distribution of the Fs found. If there is only one Fs found, result = 1. The sampling frequency is usually consistent across all experimental tests.

[4] Fs = readvhdr(vhdrfilepath)
The input argument of the function is the vdhr file path and the output argument is the sampling frequency. The program will find the sampling frequency given in the vdhr file. Then it will run through vhdr given to see if data type is in time domain or frequency domain using regular expression. If data type is in time domain, sampling frequency will be (10^6)/sampling Interval, else if data type is frequency domain, sampling frequency will be sampling Interval captured.

BMES 546

Final Project

Baby, Diaz, Phatharodom

[5] fieldparse(input parameters listed in Section3 Gui at 4))


Certain information about the experiment can be found from the filename only. In order to obtain this information, we need to extract these strings from the filename. This function parses field information from the strinf of the (filename) using user specified description (fieldlocation), and returns the parsed information (fieldvalue) as a cell of strings. (fieldlocation) is a structure array with fields: (1) fieldname: String of the name of the field (2) refphrase: String of the reference phrase (3) nthphrase: Nth number of the reference phrase (4) offset: Number of Char shifted from the index of the first char of the refphrase found in the filename string (5) charlength: Number of Char of field information to be captured If (fieldlocation(i)) leads to non-existing field information, (fieldvalue(i)) for that field will be set to '*Wrong Entry*'.

[6] signaldata = readeeg.m(txtfilepath,matpath,channel)


The input arguments for this function are the txt file path and the channel number specified by the user. If the .mat or .txt files that are running exist, the program will load the column for the requested channel. Otherwise, the program will fopen the txt file and textscan on the column for the requested channel, fclose the file and return the loaded column or textscanned column.

[7] [mkNum,evType,time] = readvmrk.m(vmrkfilepath)


This function is used to read the VMRK extension files and gather pertinent information about the event and stimulus marker. The function takes the full VMRK file path as its only input argument and extracts three output arguments from them in the form of matrices: "mknum", "evtype", and "time". "mknum" is a matrix containing the marker number for a specific stimulus linking it to the original raw data file. The "evtype" is a matrix containing all the event types for each marker number. Finally "time" is a matrix containing all the time the time data of when the specific marker number and event type combination occurred. This information is used in order to select and display specific information requested by the user.

BMES 546

Final Project

Baby, Diaz, Phatharodom

[8] pnum = getperiodnum(periodendtime,currentperiod,time)


The input argument of the function is the end time of the period (vector with end time of all the periods), current period number and the time. And the output is the period number of the current epoch. While time is greater than current period, current period is current period plus one. When time is less than current period, period number is current period. By doing this, we could avoid having to use an if-else tree. Since the progress of event being analyzed is along the time progression as well. We could always compare only to the starting time of the next period.

[9] meanFFT = meanperiod(FFTepoch,periodNum,periodTotal)


This function takes the average on all the FFT values within each period, and returns these values in the form of a matrix (meanFFT). The input argument periodNum is an Mx1 matrix of the period numbers for each epoch, where M is the total number of selected epochs in each experiment. The input argument FFTepoch is an MxN matrix obtained by reading the FFT values (frequencies) from the output file created by the runanlysis.m function on the selected frequency spectrum. N is the number of data points (number of frequencies) along the user specified frequency spectrum (which ranges from 0 to fmax). The last input argument periodTotal is a single number which indicates the total number of periods specified by the user. There may be multiple epochs on one particular period (in which case the mean would be the average of all the epoch frequencies found on that period) or there may be no epochs on a particular period (in which case the mean frequency would be equal to NaN for that period).

[10] writefft(savefilename, outputbody, outputbodymean)


This function first computes FFT output, and then writes experimental test info, epoch info, and FFT output of the input epoch into a csv file. The function writes the outputs to the two files, savefilename.csv and savefilename_mean.csv, by appending lines (outputbody and outputbodymean) to the previous results, respectively. The function will write once after each experiment is run through.

[11] plotfft(savefilename,expNum)
This function takes an output filename as a string (savefilename), and the ith experiment number as an integer (expNum), and plots the mean of the FFT data. The function reads the mean FFT data from "(savefilename)mean.csv", which is located in the "Result" folder. The expNum is used to index the desired experimental test to be plotted.

[12] [chName, chNum, totalChNum] = getchinfo(rawdatapath,chInput)


This function is used to automatically populate the channel number once the channel name is given on the GUI interface or vice-versa. Function getchinfo takes the directory of the raw data for the experiment as the first input and the channel input as the second. The channel input can

BMES 546

Final Project

Baby, Diaz, Phatharodom

be either channel name (string) or channel number (positive integer). The function returns the specified channel name and number as well as the total number of channels.

[13] isposnum(num,isInt,inputname)
If (isInt) is true, the function checks if the input string (num) is a string of a positive integer. If (isInt) is false, the function checks if the input string is a string of a non-negative number. The function will raise an error dialog box if the condition above is false. It will show an error dialog box with the (inputname) string passed on in the title. If (inputname) is not given, error dialog box title would not add additional information of (inputname). If (isInt) is not given, (isInt) will be false by default.

Anda mungkin juga menyukai