ThinkGear, rawdata and Matlab coding

6 ビュー (過去 30 日間)
Marijana Teljega
Marijana Teljega 2018 年 8 月 24 日
編集済み: madhan ravi 2018 年 8 月 27 日
Hi, I am new in Matlab programming, but have experience i Java code, wich is helping but sometimes really confused, questioning if I am coding in correct way. I appriciate any help with my questions. I use similar code https://www.pantechsolutions.net/brain-computer-interface/matlab-code-to-read-raw-eeg-signals-using-mindwave-mobile to read raw data from the headset, and I have several question:
1. How can I found out what data type is inside the Data= zeros(1,1000);
2. In the end of the code there are two rows that free the connection and disconnect. I would like to free the connection and disconnect when I need to do that, like for example when I press letter A on the keyboard.
3. What is the difference between plot(data); and disp(data)? I mean, I understand what they do, but if I place them one after the other, the value in the signal is givin only zeros but plot function will plot the signal with values other than zeros?
4. I need to use this equation on the Data values: (rawdata*(1,8/4096)/2000). Can I write the equation in one row or I need to split it in several steps to get the correct answer? In other words, I guess that Matlab is handling the equation in the correct way, calculating the most important part first when using parantesis?
5. I am using really nice function [s, f, t, ps] = spectrogram(data,window,noverlap,NFFT,Fs,'yaxis' ); wich help me a lot solving many things in one row. I want to plot ps, but ps contains complex numbers. How can I plot ps?
I really appriciate any help in my questions.
Marijana
  1 件のコメント
madhan ravi
madhan ravi 2018 年 8 月 24 日
Upload your code.

サインインしてコメントする。

採用された回答

madhan ravi
madhan ravi 2018 年 8 月 24 日
編集済み: madhan ravi 2018 年 8 月 24 日
1) To find what data type just type whos in command window.
2)Didn't understand ,but I guess you can do this by defining it as an input function for example:
DISCONNECT = input('disconnect?')
if size(DISCONNECT) == 1
break
end
3)Yes plot function can plot other than zeros.
4)Yes it calculates the most important part inside parantheses.
5)Read the link below:
  2 件のコメント
Marijana Teljega
Marijana Teljega 2018 年 8 月 27 日
Hi Madhan Ravi, and thank you for your answers!
1) Thanks!
2) Thanks! I would appriciate if you can give me some suggestion where is the best place inside my code to add your part of the code.I would like to manually disconnect from the thread during the runing operation.
3)Can you give me more explaination about why plot can plot other than zero? Does it not use the same values? Or is it about matrix data type?
4) So if i write volt_data = (data * (1.8 / 4096)/2000); all elements, one by one inside the data matrix will be used in the equation, and they all will be saved in the same place(index) inside volt_data? This part is confusing because while using plot(), the function will go trough all elements in the data matrix, but will it do that for the volt_data = (data * (1.8 / 4096)/2000); as well? Or I need to loop all elements in volt_data to add to each one of them new value based on the equation.
In other words, I want to use the equation for each element inside the data and after that I wolud like to plot it so that I present it visually as well. Please give me some suggestion how to achieve that.
5) I wolud like to use data from the ps, to create integer values from complex values and save it in an array for futher usage.
Please correct me if I am using wrong words while explaining this issue, like matrix, arrays, different data types...
Br Marijana
function readRAW
%run this function to connect and plot raw EEG data
%make sure to change portnum1 to the appropriate COM port
clear all
close all
data = zeros(1,10240)';
portnum1 = 40; %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;
%TG_STREAM_5VRAW= 1;
%TG_STREAM_FILE_PACKETS = 2;
% Data type that can be requested from TG_GetValue().
TG_DATA_RAW = 4;
%TG_DATA_BLINK_STRENGTH=37;
%load thinkgear dll
loadlibrary('thinkgear64.dll','thinkgear64.h');
fprintf('thinkgear64.dll loaded\n');
%get dll version
dllVersion = calllib('thinkgear64', 'TG_GetDriverVersion');
fprintf('ThinkGear DLL version: %d\n', dllVersion );
Get a connection ID handle to ThinkGear
connectionId1 = calllib('thinkgear64', 'TG_GetNewConnectionId');
if ( connectionId1 < 0 )
error( sprintf( 'ERROR: TG_GetNewConnectionId() returned %d.\n', connectionId1 ) );
end;
% Set/open stream (raw bytes) log file for connection
errCode = calllib('thinkgear64', 'TG_SetStreamLog', connectionId1, 'streamLog.txt' );
if( errCode < 0 )
error( sprintf( 'ERROR: TG_SetStreamLog() returned %d.\n', errCode ) );
end;
% Set/open data (ThinkGear values) log file for connection
errCode = calllib('thinkgear64', 'TG_SetDataLog', connectionId1, 'dataLog.txt' );
if( errCode < 0 )
error( sprintf( 'ERROR: TG_SetDataLog() returned %d.\n', errCode ) );
end;
% Attempt to connect the connection ID handle to serial port "COM3"
errCode = calllib('thinkgear64', 'TG_Connect', connectionId1,comPortName1,TG_BAUD_57600,TG_STREAM_PACKETS );
if ( errCode < 0 )
error( sprintf( 'ERROR: TG_Connect() returned %d.\n', errCode ) );
end
fprintf( 'Connected. Reading Packets...\n' );
%record data
j = 0;
i = 0;
volt_data = 0% zeros(1, 10240);
while (i < 10240) %loop for 20 seconds 10240 and loop for 120 seconds 10240x60=61440
if (calllib('thinkgear64','TG_ReadPackets',connectionId1,1) == 1) %if a packet was read...
j = j + 1;
i = i + 1;
%read raw data
data(j) = calllib('thinkgear64','TG_GetValue',connectionId1,TG_DATA_RAW );
%fprintf('data(j)-inside: %d\n', data(j));
%volt_data = (data * (1.8 / 4096)/2000);
end
if (j == 256)
%***********************************************************************************************
% plot the raw data signal before any future change
%***********************************************************************************************
subplot(2,2,1);
%fprintf('data(j): %d\n', data(j));
plot(data, 'b');
axis([0 280 -2500 2500])
drawnow;
title('Raw data signal in time domain');
xlabel('Time seconds');
ylabel('Raw data');
%
%***********************************************************************************************
% plot the raw data signal after applying the voltage equation
%***********************************************************************************************
volt_data = (data * (1.8 / 4096)/2000);
%disp(data);
fprintf('data(j): %d\n', data(j));
fprintf('volt_data(j): %d\n', volt_data(j));
subplot(2,2,2);
plot(data, 'b');
axis([0 280 -3000 3000])
drawnow;
title('Volt data signal in time domain');
xlabel('Time seconds');
ylabel('Volt data');
%***********************************************************************************************
% use bandpass filter to filter out signals that will not be used. Here I use only 2-12 Hz
%***********************************************************************************************
%***********************************************************************************************
% plot the signal after fft and the power calculated from the spectrogram function
%***********************************************************************************************
%[s, f, t, ps] = spectrogram(data,window,noverlap,NFFT,Fs,'yaxis' );
% subplot(2,2,2);
% plot(s,'r');
% title('The fft of the signal in time domain');
% xlabel('Time(s)');
% ylabel('Frequency(Hz)');
% %***********************
% subplot(2,2,3);
% plot(ps,'g');
% title('The power of the signal in time and frequency domain');
% xlabel('Time(s)');
% ylabel('Power(dB/Hz)');
j = 0;
end
end
%***********************************************************************************************
% %disconnect
%***********************************************************************************************
calllib('thinkgear64', 'TG_FreeConnection', connectionId1 );
fprintf('\n');
fprintf( 'Disconnected....\n' );
madhan ravi
madhan ravi 2018 年 8 月 27 日
編集済み: madhan ravi 2018 年 8 月 27 日
Difference
Generally in matlab we use the term vector for dimensional data (1 by any number or vice versa). Matrix is with dimension (any number by any number) other than one.
2) Disconnection Just use it where you have commented disconnect.
3) plot link:
https://www.mathworks.com/help/matlab/ref/plot.html
4) Element wise operation:
https://www.mathworks.com/help/matlab/ref/times.html
5) Complex as integers
I don’t know what you mean but what I know is you can save the real and imaginary parts of a complex number separately.
Plotting ps example
plot(real(ps),imag(ps),'-r').

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSpectral Measurements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by