Store eeg data to matrix and save it to csv file
20 次查看(过去 30 天)
显示 更早的评论
Hi guys, I'm trying to store EEG signals in a matrix and then save they into a csv file. If I open the csv file, no EEG signal is being saved. If I try to plot the signals extracted, I get a graph, so the signals are extracted from the device and the connection works. I think there is some problem in storing the signals within the matrix. I'm using matlab r20122 on windows xp sp3 and the device I'm using is the NeuroSky Mindset. This is the code that I am using.
function SaveComponentToCsv
clear all
close all
data = ones(61440,14);
portnum1 = 1; %COM Port #
comPortName1 = sprintf('\\\\.\\COM%d', portnum1);
% Baud rate for use with TG_Connect() and TG_SetBaudrate().
TG_BAUD_57600 = 57600;
% Data format for use with TG_Connect() and TG_SetDataFormat().
TG_STREAM_PACKETS = 0;
% Data type that can be requested from TG_GetValue().
TG_DATA_POOR_SIGNAL = 1;
TG_DATA_ATTENTION = 2;
TG_DATA_MEDITATION = 3;
TG_DATA_RAW = 4;
TG_DATA_DELTA = 5;
TG_DATA_THETA = 6;
TG_DATA_ALPHA1 = 7;
TG_DATA_ALPHA2 = 8;
TG_DATA_BETA1 = 9;
TG_DATA_BETA2 = 10;
TG_DATA_GAMMA1 = 11;
TG_DATA_GAMMA2 = 12;
TG_DATA_BLINK_STRENGTH = 37;
%load thinkgear dll
loadlibrary('Thinkgear.dll','thinkgear.h');
fprintf('Thinkgear.dll loaded\n');
%get dll version
dllVersion = calllib('Thinkgear', 'TG_GetDriverVersion');
fprintf('ThinkGear DLL version: %d\n', dllVersion );
Get a connection ID handle to ThinkGear
connectionId1 = calllib('Thinkgear', 'TG_GetNewConnectionId');
if ( connectionId1 < 0 )
error( sprintf( 'ERROR: TG_GetNewConnectionId() returned %d.\n', connectionId1 ) );
calllib('Thinkgear', 'TG_FreeConnection', connectionId1 );
end;
% Set/open stream (raw bytes) log file for connection
errCode = calllib('Thinkgear', 'TG_SetStreamLog', connectionId1, 'streamLog.txt' );
if( errCode < 0 )
error( sprintf( 'ERROR: TG_SetStreamLog() returned %d.\n', errCode ) );
calllib('Thinkgear', 'TG_FreeConnection', connectionId1 );
end;
% Set/open data (ThinkGear values) log file for connection
errCode = calllib('Thinkgear', 'TG_SetDataLog', connectionId1, 'dataLog.txt' );
if( errCode < 0 )
error( sprintf( 'ERROR: TG_SetDataLog() returned %d.\n', errCode ) );
calllib('Thinkgear', 'TG_FreeConnection', connectionId1 );
end;
% Attempt to connect the connection ID handle to serial port "COM3"
errCode = calllib('Thinkgear', 'TG_Connect', connectionId1,comPortName1,TG_BAUD_57600,TG_STREAM_PACKETS );
if ( errCode < 0 )
error( sprintf( 'ERROR: TG_Connect() returned %d.\n', errCode ) );
calllib('Thinkgear', 'TG_FreeConnection', connectionId1 );
end
fprintf( 'Connected. Reading Packets...\n' );
record data and save to csv file
%i = 1;
%j = 1;
%recording data
for i=1:61440 %loop for 120 seconds
if (calllib('Thinkgear','TG_ReadPackets',connectionId1,1) == 1) %if a packet was read...
for j=1:14
if(j == 1)
data(i,j) = now;
end
if(j == 2)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_POOR_SIGNAL) ~= 0) %if RAW has been updated
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_POOR_SIGNAL);
end
end
if(j == 3)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_ATTENTION) ~= 0)
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_ATTENTION);
end
end
if(j == 4)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_MEDITATION) ~= 0)
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_MEDITATION);
end
end
if(j == 5)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_RAW) ~= 0)
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_RAW);
end
end
if(j == 6)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_DELTA) ~= 0)
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_DELTA);
end
end
if(j == 7)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_THETA) ~= 0)
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_THETA);
end
end
if(j == 8)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_ALPHA1) ~= 0)
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_ALPHA1);
end
end
if(j == 9)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_ALPHA2) ~= 0)
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_ALPHA2);
end
end
if(j == 10)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_BETA1) ~= 0)
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_BETA1);
end
end
if(j == 11)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_BETA2) ~= 0)
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_BETA2);
end
end
if(j == 12)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_GAMMA1) ~= 0)
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_GAMMA1);
end
end
if(j == 13)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_GAMMA2) ~= 0)
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_GAMMA2);
end
end
if(j == 14)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_BLINK_STRENGTH) ~= 0)
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_BLINK_STRENGTH);
end
end
end
end
end
%save data to csv file
csvwrite('G:\matlab_function\data.csv',data);
dlmwrite('G:\matlab_function\data.csv',data,'precision', '%.6f');
disconnect
calllib('Thinkgear', 'TG_FreeConnection', connectionId1 );
回答(4 个)
Jan
2013-4-2
编辑:Jan
2013-4-2
You have been able to import the data successfully, as you see by the possibility to plot them, or how should I understand "I get a graph". Therefore everything in the code which concerns the import and thinkgear is confusing only. A simple RAND() command would be sufficient instead of the pile of commands to obtain the data.
If you are not sure, if data contains any value, test this:
disp(data)
Using the debugger by setting a breakpoint would be a good idea also. Please note that the readers of this forum cannot debug your program, because they do not have your device. But for yourself it is one mous click only.
The for j loop and the bunch on if j==x is not useful in your code: You can simply omit both and replace the j manually for all steps:
% Instead of:
% for j=1:14
% if(j == 1)
% ...
% prefer:
data(i, 1) = now;
if calllib('Thinkgear', 'TG_GetValueStatus', connectionId1, TG_DATA_POOR_SIGNAL) ~= 0
data(i, 2) = calllib('Thinkgear', 'TG_GetValue', connectionId1, TG_DATA_POOR_SIGNAL);
end
etc.
It is strange that you create a time-stamp by now and import the single values step by step afterwards. How meaningful is the time-stamp, if testing the availability and requesting takes some time?
It is confusing also, that you create an CSV file and overwrite it immediately one line later:
csvwrite('G:\matlab_function\data.csv', data);
dlmwrite('G:\matlab_function\data.csv', data, 'precision', '%.6f');
charmi
2014-2-1
I have the eeg data in .gnt format. How should I deal with it in MATLAB and even in eeglab toolbox? Can you please suggest some format which will be useful?
0 个评论
EMILIO
2014-8-13
Hello Leonardo, what was the problem ?
I'm having the same kind of problem. How did you solve it ?
Thanks.
0 个评论
Himanshu Srivastava
2016-12-21
hello, i just want to know the raw data comes from this matlab code what its signify and plot of this data is unit less so, please provide me code for extracting raw data from headset with meaningful plots
1 个评论
Jan
2016-12-26
Please open a new thread for a new question. Posting questions in the section for answers is confusing.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 EEG/MEG/ECoG 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!