MATLAB Answers

how to generate a pulse train of known frequency and visualize/count the pulses (NI USB 6211 DAQ)

조회 수: 19(최근 30일)
LO 2020년 3월 16일
편집: LO 2020년 3월 16일
Hi everyone
I am having trouble with some code to start a pulse generation from a device (NI USB-6211 DAQ) while counting it.
The pulse is generated by Dev1/ctr0 and should be detected by Dev1/ctr1. If I use the NI MAX software the process is flawless.
this is my code
d = daq.getDevices;
s = daq.createSession('ni');
s.Rate = 20000; % sample rate
ch3=addAnalogInputChannel(s,'Dev1','ai7','Voltage'); % this channels records the camera output (a pulse for each frame acquired)
ch3.TerminalConfig = 'SingleEnded';
ch1 = addCounterOutputChannel(s,'Dev1','ctr0','PulseGeneration'); % camera trigger
ch1.Frequency =40;
ch1.DutyCycle = 0.2;
ch2=addCounterInputChannel(s,'Dev1','ctr1','EdgeCount'); %
dataIn = startForeground(s);
I am trying now to implement this in MATLAB but I have the following issues:
1) the pulse train has a pulse more than what it should (45 instead of 40)
2) the pulse train output is actually 45 instead of 40 (the pulse triggers a camera which acquires 45 frames instead of 40)
3) when I plot the data I can see the analog input (see attached picture) but the digital input is a flat line (red line on the bottom of the plot) which, even when magnified, does not show any pulse (perhaps I am wrong expecting to see pulses here but then I do not know how to visualize the readout of the digital-input "edge counter"
  댓글 수: 1
LO 2020년 3월 16일
an updated version of the code:
d = daq.getDevices;
s = daq.createSession('ni');
s.Rate = 20000; % sample rate
s.DurationInSeconds = 1;
ch3=addAnalogInputChannel(s,'Dev1','ai7','Voltage'); % record camera output through analog input channel
ch3.TerminalConfig = 'Differential';
ch1 = addCounterOutputChannel(s,'Dev1','ctr0','PulseGeneration'); % camera trigger / digital output
ch1.Frequency =1;
ch1.DutyCycle = 0.5;
ch2=addCounterInputChannel(s,'Dev1','ctr1','EdgeCount'); % counter input channel (counts camera shots)
[data, time] = startForeground(s);
hold on
plot(time,data(:,1)) % analog version of the camera output signal
scatter(time(find(diff(data(:,2))==1)),zeros(numel(time(find(diff(data(:,2))==1))),1),'filled','r') % shots timestamps
I've managed to get the timestamps for these pulses. See image below:
However, the device receiving these TTL pulses (camera) seems to respond more times than the actual pulse train (23 times -> 20 Hz train, 12 times -> 10 Hz, 6 times -> 5Hz train, 2 times -> 1Hz train). this issue seems to depend on the MATLAB code, if I trigger the camera using the NI-MAX software no additional shots (camera responses to the trigger) are present.
Also it seems that the issue does not depend on the pulse duty cycle. Neither it depends on the session duration. For instance if I have a 5Hz pulse train, the camera shoots always one addition shot (6, if the session lasts 1 sec, 11 for 2 sec, 26 for 5 sec, etc.)

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


Community Treasure Hunt

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

Start Hunting!

Translated by