-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
292 additions
and
292 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,88 @@ | ||
function [signal, hr] = plotLive(Bhapi, MpSys, isEcg, t, unit, slideWinPct) | ||
arguments | ||
Bhapi(1,1) struct; | ||
MpSys(1,1) struct; | ||
isEcg {mustBeNumericOrLogical} = true; | ||
t(1,1) {mustBeInteger, mustBePositive} = 12; | ||
unit char {mustBeMember(unit, ['minutes', 'seconds'])} = 'seconds'; | ||
slideWinPct(1,1) {mustBeInRange(slideWinPct, 0, 1)} = 0.05; | ||
end | ||
%% DESCRIPTION | ||
|
||
%% SET PARAMETERS | ||
if strcmp(unit, 'seconds') | ||
k = 1; | ||
else | ||
k = 60; | ||
end | ||
|
||
recSecs = t*k; | ||
dpStored = 0; | ||
dpToStore = MpSys.fs*recSecs; | ||
sws = MpSys.fs*slideWinPct; % sliding window size | ||
dpBuffer = zeros(1, sws); % temporary buffer | ||
dpOffset = 1; | ||
|
||
%% START DAEMON | ||
openApi(Bhapi, MpSys); | ||
fprintf('Start acquisition daemon...\n'); | ||
MpSys.status = calllib(Bhapi.lib, 'startMPAcqDaemon'); | ||
if ~strcmp(MpSys.status,'MPSUCCESS') | ||
fprintf('FAILED to start acquisition daemon!\n'); | ||
calllib(Bhapi.lib, 'disconnectMPDev'); | ||
return | ||
end | ||
|
||
%% START RECORDING | ||
fprintf('Start data acquisition...\n\n'); | ||
MpSys.status = calllib(Bhapi.lib, 'startAcquisition'); | ||
if ~strcmp(MpSys.status,'MPSUCCESS') | ||
fprintf('FAILED to start data acquisition!\n'); | ||
calllib(Bhapi.lib, 'disconnectMPDev'); | ||
return | ||
end | ||
|
||
signal = nan(1, dpToStore); | ||
while(dpToStore > 0) | ||
[MpSys.status, dpBuffer, dpStored] = calllib(Bhapi.lib, 'receiveMPData', dpBuffer, sws, dpStored); | ||
if ~strcmp(MpSys.status,'MPSUCCESS') | ||
fprintf('FAILED to receive MP data!\n'); | ||
calllib(Bhapi.lib, 'disconnectMPDev'); | ||
return | ||
else | ||
signal(dpOffset:dpOffset+dpStored-1) = dpBuffer(1:dpStored); | ||
|
||
len = length(~isnan(signal)); | ||
y = signal(1:len); | ||
x(1:len) = (1:len); | ||
pause(1e-3); | ||
figure(201); | ||
plot(x(1:length(y)), y); | ||
|
||
dpOffset = dpOffset + sws; | ||
dpToStore = dpToStore - sws; | ||
% fprintf('Progress: %.2f%%\n', 100-((dpToStore/(MpSys.fs*recSecs))*100)); | ||
end | ||
end | ||
|
||
%% STOP RECORDING | ||
fprintf('\nStop acquisition...\n'); | ||
MpSys.status = calllib(Bhapi.lib, 'stopAcquisition'); | ||
if ~strcmp(MpSys.status,'MPSUCCESS') | ||
fprintf('FAILED to stop acquisition!\n'); | ||
calllib(Bhapi.lib, 'disconnectMPDev'); | ||
return | ||
end | ||
closeApi(Bhapi, MpSys); | ||
|
||
%% ESTIMATE HR | ||
if isEcg && t == 12 | ||
[~, pks, ~] = getEcgThresh(signal, MpSys, 0.6, 97, 0); | ||
hr = length(pks)*5; | ||
elseif isEcg && t~= 12 | ||
hr = NaN; | ||
msg = sprintf("\nThe HR was not estimated.\nTo estimate it, set the arguments t = 12 and unit = 'seconds'."'); | ||
warning(msg); | ||
end | ||
function [signal, hr] = plotLiveSignal(Bhapi, MpSys, isEcg, t, unit, slideWinPct) | ||
arguments | ||
Bhapi(1,1) struct; | ||
MpSys(1,1) struct; | ||
isEcg {mustBeNumericOrLogical} = true; | ||
t(1,1) {mustBeInteger, mustBePositive} = 12; | ||
unit char {mustBeMember(unit, ['minutes', 'seconds'])} = 'seconds'; | ||
slideWinPct(1,1) {mustBeInRange(slideWinPct, 0, 1)} = 0.05; | ||
end | ||
%% DESCRIPTION | ||
|
||
%% SET PARAMETERS | ||
if strcmp(unit, 'seconds') | ||
k = 1; | ||
else | ||
k = 60; | ||
end | ||
|
||
recSecs = t*k; | ||
dpStored = 0; | ||
dpToStore = MpSys.fs*recSecs; | ||
sws = MpSys.fs*slideWinPct; % sliding window size | ||
dpBuffer = zeros(1, sws); % temporary buffer | ||
dpOffset = 1; | ||
|
||
%% START DAEMON | ||
openApi(Bhapi, MpSys); | ||
fprintf('Start acquisition daemon...\n'); | ||
MpSys.status = calllib(Bhapi.lib, 'startMPAcqDaemon'); | ||
if ~strcmp(MpSys.status,'MPSUCCESS') | ||
fprintf('FAILED to start acquisition daemon!\n'); | ||
calllib(Bhapi.lib, 'disconnectMPDev'); | ||
return | ||
end | ||
|
||
%% START RECORDING | ||
fprintf('Start data acquisition...\n\n'); | ||
MpSys.status = calllib(Bhapi.lib, 'startAcquisition'); | ||
if ~strcmp(MpSys.status,'MPSUCCESS') | ||
fprintf('FAILED to start data acquisition!\n'); | ||
calllib(Bhapi.lib, 'disconnectMPDev'); | ||
return | ||
end | ||
|
||
signal = nan(1, dpToStore); | ||
while(dpToStore > 0) | ||
[MpSys.status, dpBuffer, dpStored] = calllib(Bhapi.lib, 'receiveMPData', dpBuffer, sws, dpStored); | ||
if ~strcmp(MpSys.status,'MPSUCCESS') | ||
fprintf('FAILED to receive MP data!\n'); | ||
calllib(Bhapi.lib, 'disconnectMPDev'); | ||
return | ||
else | ||
signal(dpOffset:dpOffset+dpStored-1) = dpBuffer(1:dpStored); | ||
|
||
len = length(~isnan(signal)); | ||
y = signal(1:len); | ||
x(1:len) = (1:len); | ||
pause(1e-3); | ||
figure(201); | ||
plot(x(1:length(y)), y); | ||
|
||
dpOffset = dpOffset + sws; | ||
dpToStore = dpToStore - sws; | ||
% fprintf('Progress: %.2f%%\n', 100-((dpToStore/(MpSys.fs*recSecs))*100)); | ||
end | ||
end | ||
|
||
%% STOP RECORDING | ||
fprintf('\nStop acquisition...\n'); | ||
MpSys.status = calllib(Bhapi.lib, 'stopAcquisition'); | ||
if ~strcmp(MpSys.status,'MPSUCCESS') | ||
fprintf('FAILED to stop acquisition!\n'); | ||
calllib(Bhapi.lib, 'disconnectMPDev'); | ||
return | ||
end | ||
closeApi(Bhapi, MpSys); | ||
|
||
%% ESTIMATE HR | ||
if isEcg && t == 12 | ||
[~, pks, ~] = getEcgThresh(signal, MpSys, 0.6, 97, 0); | ||
hr = length(pks)*5; | ||
elseif isEcg && t~= 12 | ||
hr = NaN; | ||
msg = sprintf("\nThe HR was not estimated.\nTo estimate it, set the arguments t = 12 and unit = 'seconds'."'); | ||
warning(msg); | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,19 @@ | ||
function Stim = prepStimStruct(stimPath, windowPtr, durSecs, soaSecs, nPres) | ||
arguments | ||
stimPath(1,:) {mustBeText, mustBeFile}; | ||
windowPtr(1,1) {mustBeNumeric, mustBeGreaterThanOrEqual(windowPtr, 0)}; | ||
durSecs(1,1) {mustBeNumeric, mustBeGreaterThanOrEqual(durSecs, 1e-3)} = 0.200; | ||
soaSecs(1,1) {mustBeNumeric, mustBeGreaterThanOrEqual(soaSecs, 0)} = 0.0; | ||
nPres(1,1) {mustBeInteger, mustBePositive} = 1; | ||
end | ||
|
||
%% DESCRIPTION | ||
|
||
%% SET PARAMETERS | ||
Stim.loc = stimPath; | ||
Stim.windowPtr = windowPtr; | ||
Stim.dur = durSecs; | ||
Stim.soa = soaSecs; | ||
Stim.nPres = nPres; | ||
|
||
function Stim = prepStim(stimPath, windowPtr, durSecs, soaSecs, nPres) | ||
arguments | ||
stimPath(1,:) {mustBeText, mustBeFile}; | ||
windowPtr(1,1) {mustBeNumeric, mustBeGreaterThanOrEqual(windowPtr, 0)}; | ||
durSecs(1,1) {mustBeNumeric, mustBeGreaterThanOrEqual(durSecs, 1e-3)} = 0.200; | ||
soaSecs(1,1) {mustBeNumeric, mustBeGreaterThanOrEqual(soaSecs, 0)} = 0.0; | ||
nPres(1,1) {mustBeInteger, mustBePositive} = 1; | ||
end | ||
|
||
%% DESCRIPTION | ||
|
||
%% SET PARAMETERS | ||
Stim.loc = stimPath; | ||
Stim.windowPtr = windowPtr; | ||
Stim.dur = durSecs; | ||
Stim.soa = soaSecs; | ||
Stim.nPres = nPres; | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,70 @@ | ||
function signal = recBaseline(Bhapi, MpSys, t, unit, slideWinPct) | ||
arguments | ||
Bhapi(1,1) struct; | ||
MpSys(1,1) struct; | ||
t(1,1) {mustBeInteger, mustBePositive} = 5; | ||
unit char {mustBeMember(unit, ['minutes', 'seconds'])} = 'minutes'; | ||
slideWinPct(1,1) {mustBeInRange(slideWinPct, 0, 1)} = 0.05; | ||
end | ||
%% DESCRIPTION | ||
|
||
%% SET PARAMETERS | ||
if strcmp(unit, 'seconds') | ||
k = 1; | ||
else | ||
k = 60; | ||
end | ||
|
||
recSecs = t*k; | ||
dpStored = 0; | ||
dpToStore = MpSys.fs*recSecs; | ||
sws = MpSys.fs*slideWinPct; % sliding window size | ||
dpBuffer = zeros(1, sws); % temporary buffer | ||
dpOffset = 1; | ||
|
||
%% START DAEMON | ||
openApi(Bhapi, MpSys); | ||
fprintf('Start acquisition daemon...\n'); | ||
MpSys.status = calllib(Bhapi.lib, 'startMPAcqDaemon'); | ||
if ~strcmp(MpSys.status,'MPSUCCESS') | ||
fprintf('FAILED to start acquisition daemon!\n'); | ||
calllib(Bhapi.lib, 'disconnectMPDev'); | ||
return | ||
end | ||
|
||
%% START RECORDING | ||
fprintf('Start data acquisition...\n\n'); | ||
MpSys.status = calllib(Bhapi.lib, 'startAcquisition'); | ||
if ~strcmp(MpSys.status,'MPSUCCESS') | ||
fprintf('FAILED to start data acquisition!\n'); | ||
calllib(Bhapi.lib, 'disconnectMPDev'); | ||
return | ||
end | ||
|
||
signal = nan(1, dpToStore); | ||
while(dpToStore > 0) | ||
[MpSys.status, dpBuffer, dpStored] = calllib(Bhapi.lib, 'receiveMPData', dpBuffer, sws, dpStored); | ||
if ~strcmp(MpSys.status,'MPSUCCESS') | ||
fprintf('FAILED to receive MP data!\n'); | ||
calllib(Bhapi.lib, 'disconnectMPDev'); | ||
return | ||
else | ||
signal(dpOffset:dpOffset+dpStored-1) = dpBuffer(1:dpStored); | ||
|
||
dpOffset = dpOffset + sws; | ||
dpToStore = dpToStore - sws; | ||
fprintf('Progress: %.2f%%\n', 100-((dpToStore/(MpSys.fs*recSecs))*100)); | ||
end | ||
end | ||
|
||
%% STOP RECORDING | ||
fprintf('\nStop acquisition...\n'); | ||
MpSys.status = calllib(Bhapi.lib, 'stopAcquisition'); | ||
if ~strcmp(MpSys.status,'MPSUCCESS') | ||
fprintf('FAILED to stop acquisition!\n'); | ||
calllib(Bhapi.lib, 'disconnectMPDev'); | ||
return | ||
end | ||
closeApi(Bhapi, MpSys); | ||
|
||
function signal = recSignal(Bhapi, MpSys, t, unit, slideWinPct) | ||
arguments | ||
Bhapi(1,1) struct; | ||
MpSys(1,1) struct; | ||
t(1,1) {mustBeInteger, mustBePositive} = 5; | ||
unit char {mustBeMember(unit, ['minutes', 'seconds'])} = 'minutes'; | ||
slideWinPct(1,1) {mustBeInRange(slideWinPct, 0, 1)} = 0.05; | ||
end | ||
%% DESCRIPTION | ||
|
||
%% SET PARAMETERS | ||
if strcmp(unit, 'seconds') | ||
k = 1; | ||
else | ||
k = 60; | ||
end | ||
|
||
recSecs = t*k; | ||
dpStored = 0; | ||
dpToStore = MpSys.fs*recSecs; | ||
sws = MpSys.fs*slideWinPct; % sliding window size | ||
dpBuffer = zeros(1, sws); % temporary buffer | ||
dpOffset = 1; | ||
|
||
%% START DAEMON | ||
openApi(Bhapi, MpSys); | ||
fprintf('Start acquisition daemon...\n'); | ||
MpSys.status = calllib(Bhapi.lib, 'startMPAcqDaemon'); | ||
if ~strcmp(MpSys.status,'MPSUCCESS') | ||
fprintf('FAILED to start acquisition daemon!\n'); | ||
calllib(Bhapi.lib, 'disconnectMPDev'); | ||
return | ||
end | ||
|
||
%% START RECORDING | ||
fprintf('Start data acquisition...\n\n'); | ||
MpSys.status = calllib(Bhapi.lib, 'startAcquisition'); | ||
if ~strcmp(MpSys.status,'MPSUCCESS') | ||
fprintf('FAILED to start data acquisition!\n'); | ||
calllib(Bhapi.lib, 'disconnectMPDev'); | ||
return | ||
end | ||
|
||
signal = nan(1, dpToStore); | ||
while(dpToStore > 0) | ||
[MpSys.status, dpBuffer, dpStored] = calllib(Bhapi.lib, 'receiveMPData', dpBuffer, sws, dpStored); | ||
if ~strcmp(MpSys.status,'MPSUCCESS') | ||
fprintf('FAILED to receive MP data!\n'); | ||
calllib(Bhapi.lib, 'disconnectMPDev'); | ||
return | ||
else | ||
signal(dpOffset:dpOffset+dpStored-1) = dpBuffer(1:dpStored); | ||
|
||
dpOffset = dpOffset + sws; | ||
dpToStore = dpToStore - sws; | ||
fprintf('Progress: %.2f%%\n', 100-((dpToStore/(MpSys.fs*recSecs))*100)); | ||
end | ||
end | ||
|
||
%% STOP RECORDING | ||
fprintf('\nStop acquisition...\n'); | ||
MpSys.status = calllib(Bhapi.lib, 'stopAcquisition'); | ||
if ~strcmp(MpSys.status,'MPSUCCESS') | ||
fprintf('FAILED to stop acquisition!\n'); | ||
calllib(Bhapi.lib, 'disconnectMPDev'); | ||
return | ||
end | ||
closeApi(Bhapi, MpSys); | ||
|
||
end |
Oops, something went wrong.