필터 지우기
필터 지우기

Store eeg data to matrix and save it to csv file

조회 수: 20 (최근 30일)
Leonardo
Leonardo 2013년 3월 30일
댓글: Jan 2016년 12월 26일
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 );
  댓글 수: 3
Leonardo
Leonardo 2013년 4월 1일
So, I found the problem and it is not about saving the matrix to a csv file. There are an issue in each TG_GetValueStatus function, so it is not a matlab problem.
Thanks.
Leonardo
Leonardo 2013년 4월 2일
It is the right way to assign a value to an element of matrix?
data = ones(61440,11);
if(j == 2)
if(calllib('thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_ATTENTION) ~= 0)
fprintf( 'attention: %f\n',calllib('thinkgear','TG_GetValue',connectionId1,TG_DATA_ATTENTION));
data(i,j) = calllib('thinkgear','TG_GetValue',connectionId1,TG_DATA_ATTENTION);
end
end
fprintf print the value, but the element of matrix have no value.

댓글을 달려면 로그인하십시오.

답변 (4개)

Jan
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');
  댓글 수: 1
Leonardo
Leonardo 2013년 4월 3일
>> If you are not sure, if data contains any value, test this:
>> disp(data)
After all query, data does not contains the values assign by calllib.
>> Using the debugger by setting a breakpoint would be a good idea also.
If I use a fprintf of TG_DATA_ATTENTION (for example) first the assignment I see the correct value. So, there is some problem in the assignment I think. I mean this.
>> 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.
I know.
>> 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:
Ok.
>> 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?
Do you have alternatives in order to get the timestamp of when it happened the sampling? However, the data are acquired with
if (calllib('thinkgear','TG_ReadPackets',connectionId1,1) == 1)
so, do not spend a lot of time between the acquisition and the timestamp.
>> It is confusing also, that you create an CSV file and overwrite it >> immediately one line later:
dlmwrite is commented out, but the comment was lost to copy the code.
New code. Same issue.

댓글을 달려면 로그인하십시오.


charmi
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?

EMILIO
EMILIO 2014년 8월 13일
Hello Leonardo, what was the problem ?
I'm having the same kind of problem. How did you solve it ?
Thanks.

Himanshu Srivastava
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
Jan 2016년 12월 26일
Please open a new thread for a new question. Posting questions in the section for answers is confusing.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 EEG/MEG/ECoG에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by