Effacer les filtres
Effacer les filtres

Using StartBackground with NIDAQ with while loop

9 vues (au cours des 30 derniers jours)
Nicholas Jackson
Nicholas Jackson le 23 Fév 2018
Commenté : Sadegh Rahimi le 3 Juil 2019
I am trying to display continuous EMG recordings live and trigger TMS pulses with accurate timing in the same script. I want to use a while loop to do this, but the while loop is inhibiting the presentation of data, so that data is only shown after the while loop executed. i.e. The figure is blank until after 1 second, and then displays data accurately.
I have gotten this to work with both the timer and pause functions but would like to do this with a while loop to get greater timing accuracy. Is there any other way of approaching this? Why does the while loop cause the StartBackground function to not display data when other functions don't seem to interrupt it?
Thanks for your help!
delete(instrfindall);
serialPortObj = serial('COM1', 'BaudRate', 9600, 'DataBits', 8, 'StopBits', 1, 'Parity', 'none', 'FlowControl', 'none', 'Terminator', '?');
fopen(serialPortObj);
serialPortObj.TimerFcn = {'Rapid2_MaintainCommunication'};
Rapid2_Delay(150, serialPortObj);
f1 = figure(1);
set(f1,'Position',[300 300 1200 600]);
for i=1:200
TMS_pulse = 0;
%timer to delay pulse
% t=timer;
% t.StartDelay = 1;
% t.TimerFcn = @(myTimerObj, thisEvent) Rapid2_TriggerPulse(serialPortObj, 0);
s = daq.createSession('ni');
s.DurationInSeconds = 4;
%pair channel 1
addAnalogInputChannel(s,'Dev1','ai0','Voltage');
addAnalogInputChannel(s,'Dev1','ai8','Voltage');
%pair channel 2
addAnalogInputChannel(s,'Dev1','ai1','Voltage');
addAnalogInputChannel(s,'Dev1','ai9','Voltage');
lh = addlistener(s,'DataAvailable',@plotData);
% start(t)
start_time = GetSecs;
s.startBackground();
%
while GetSecs-start_time < 1
end
%pause(1);
success = Rapid2_TriggerPulse(serialPortObj, 0);
s.wait();
clf
end
stop(s);
function plotData(src,event)
%channel specific data
pairchan1=event.Data(:,1:2);
pairchan2=event.Data(:,3:4);
plot1= (event.Data(:,1)-event.Data(:,2));
plot2= (event.Data(:,3)-event.Data(:,4));
%channel 1 plot
subplot(2,1,1);
plot(event.TimeStamps,plot1,'k');
hold on
xlabel('Time');
ylabel('Amplitude (mV)');
peakline = refline([0 0.25]);
troughline = refline([0 -0.25]);
peakline.Color = 'r';
troughline.Color = 'r';
ylim([-1.5 1.5]);
xlim([0 2]);
%channel 2 plot
subplot(2,1,2);
plot(event.TimeStamps,plot2,'k')
hold on
xlabel('Time')
ylabel('Amplitude (mV)')
peakline = refline([0 0.25]);
troughline = refline([0 -0.25]);
peakline.Color = 'r';
troughline.Color = 'r';
ylim([-1.5 1.5]);
xlim([0 2]);
end

Réponses (1)

Marion Grosperrin
Marion Grosperrin le 3 Avr 2019
Hello Nicholas Jackson,
Did you find a solution ?
I would like to do something similar. I want to trigger stimulations from EMG and having a good timing resolution of course. I would be very interesting in knowing how you resolved the problem.
Thank you very much !
Marion
  2 commentaires
Nicholas Jackson
Nicholas Jackson le 3 Avr 2019
Hi Marion,
Since asking this question, our lab has developed a suite of tools for acquiring/analyzing EMG data using the data acquisition toolbox in MATLAB:
https://github.com/greenhouselab/Veta
For the boilerplate code on how to record EMG refer to the 'recordEMG.m' function.
For example code on how we integrate EMG collection with stimulus presentation refer to the 'delayedGo.m' function.
Feel free to reach out if you need any more help!
Best,
Nick
Sadegh Rahimi
Sadegh Rahimi le 3 Juil 2019
Thanks Nick for sharing them, I would like to use Data Acquisition Toolbox to display continuous audio input and trigger audio output (according to the amplitude and frequency of audio input) with accurate timing and your code might be helpful for me, but I could not find 'delayedGo.m'. It is not on the github link.
Could you check it again?
Bests,
Sadegh

Connectez-vous pour commenter.

Catégories

En savoir plus sur Multichannel Audio Input and Output dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by