Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Data read from AI buffer missing samples at start

Solved!
Go to solution

I'm using a USB-6251. The program sets up two AI channels (reading I and Q) on a single task and a DO channel on another task. The DO channel uses the ai\SampleClock as it's clock, so the two are synchronized. The DO creates a periodic rising edge digital pulse (a clock, basically) that is used as a trigger on an external function generator. The signal from the function generator, after passing through some external signal processing hardware, is eventually what is read in by the AI channel.

 

We know from scoping the relevant signals, that they appear to be synchronized correctly. That is to say, the analog signal to be read arrives at the AI channel of the DAQ more or less instananeously with the DO trigger being activated. If there is a delay, it is on the order of a few microseconds.

 

However, when I read in the AI buffer (FiniteSamples in a repeated fashion), the waveform I get back always has a section of samples at the start that appear to be backfilled from the first actually read data-point (see the attached image). This delay is on the order of milliseconds (it varies with each run).

 

I want to eliminate this delay entirely. The signal should be a sinusoid that starts at sample 0 and is continuous through until the last sample read.

 

I have put the code below.

 

Setup:

 

// Create the analog read task
analogReadTask = new Task("analogReadTask");

// Create the virtual channel for the I component
analogReadTask.AIChannels.CreateVoltageChannel(initParams.AddrI.ChannelAddress, "I", AITerminalConfiguration.Differential, -4, 4, AIVoltageUnits.Volts);  

// Create the virtual channel for the Q component
analogReadTask.AIChannels.CreateVoltageChannel(initParams.AddrQ.ChannelAddress, "Q", AITerminalConfiguration.Differential, -4, 4, AIVoltageUnits.Volts);

// Configure the clock for the analog reads
analogReadTask.Timing.ConfigureSampleClock(string.Empty, initParams.SamplingRateHz, SampleClockActiveEdge.Rising, SampleQuantityMode.FiniteSamples, totalSamples);

// Create the mult-channel reader
analogReader = new AnalogMultiChannelReader(analogReadTask.Stream);
                analogReader.SynchronizeCallbacks = false;

pulseWriterTask = new Task("pulseWriterTask");

// Create a digital output channel that provides the trigger to the U/S system
pulseWriterTask.DOChannels.CreateChannel(initParams.AddrUsTrigger.PortLineAddress, "US trigger", ChannelLineGrouping.OneChannelForEachLine);
pulseWriterTask.Timing.ConfigureSampleClock("/Dev1/ai/SampleClock", initParams.SamplingRateHz, SampleClockActiveEdge.Rising, SampleQuantityMode.ContinuousSamples, samplesPerPulse);
pulseWriterTask.Stream.Buffer.OutputBufferSize = samplesPerPulse;
pulseWriterTask.Stream.WriteRegenerationMode = WriteRegenerationMode.AllowRegeneration;

pulseWriter = new DigitalSingleChannelWriter(pulseWriterTask.Stream);

pulseWaveform = new DigitalWaveform(samplesPerPulse, 1, DigitalState.ForceDown);
pulseWaveform.Signals[0].States[0] = DigitalState.ForceUp;

analogReadTask.Control(TaskAction.Verify);
pulseWriterTask.Control(TaskAction.Verify);

 Starting the read:

analogReadTask.Start();

// Start writing the digital pulse, however it will not begin
// until the ai/SampleClock starts, thus implicitly synchronizing the two tasks
pulseWriter.WriteWaveform(true, pulseWaveform);

analogReader.BeginReadWaveform(totalSamples, readerCallback, analogReadTask);

 

 Result (should be a sinusoid from start to finish)

 delay.PNG

 

 

 

 

 

0 Kudos
Message 1 of 4
(4,447 Views)

I should add: the X axis in the image is in samples. That's 4000 samples at 40kHz. The trigger pulses at 160Hz and each trigger pulse generates a single-cycle 160Hz sine wave on the AI input channel. 

 

On the scope, we see the very first trigger and the analog input signal lining up, but when reading from the DAQ, you can see there are multiple cycles of no signal before we start reading actual data.

0 Kudos
Message 2 of 4
(4,437 Views)

More information. The very first read is different from subsequent reads.

 

On the first read, I only see 11 trigger signals, even though we expect 16 (250-sample period for 4000 samples at 40Khz).

 

delay.PNG


On subsequent reads, we see 16 trigger signals as expected, but still a small delay at the beginning of the read.

delay2.PNG

 

 

0 Kudos
Message 3 of 4
(4,433 Views)
Solution
Accepted by topic author zkhan.cimtec

I always seem to solve these problems shortly after posting them.

 

The problem was starting the digital task AFTER the analog task. In the small delay between the two lines of code being executed, the analog read had already started and thus some of the pulses from the ai/SampleClock were missed by the DO task. Switching the start order between the two tasks resolved the issue.

0 Kudos
Message 4 of 4
(4,431 Views)