Using StartBackground with NIDAQ with while loop
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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
0 comentarios
Respuestas (1)
Marion Grosperrin
el 3 de Abr. de 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 comentarios
Sadegh Rahimi
el 3 de Jul. de 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
Ver también
Categorías
Más información sobre Multichannel Audio Input and Output en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!