Why Am I Not Getting High Sampling Rates When I Use the Python with DAQ API

Updated Sep 29, 2024

Reported In

Driver

  • NI-DAQ™mx

Programming Language

  • Python

Issue Details

When I use Python with DAQ API the rate of reading is really slow. I'm using the analog input example and there are no other examples that show doing a continuous input. 

Solution

The Python analog input example doesn't create a buffer to stream analog data so the rates are slowed down as they convert each individual sample. To implement a buffer to stream data, you'll need to initialize  nidaqmx.stream_readers. 

Here is an example for implementing a stream reader in Python with the DAQ API: 

import nidaqmx
import numpy
from nidaqmx.stream_readers import AnalogSingleChannelReader

with nidaqmx.Task() as read_task:
    number_of_samples = 10000  # change this to number of samples to acquire

    # create task and channel
    read_task.ai_channels.add_ai_voltage_chan("Dev1/ai0",
                                              max_val=10, min_val=-10)

    # create stream reader object
    reader = AnalogSingleChannelReader(read_task.in_stream)

    # initialize data array
    read_array = numpy.zeros(number_of_samples, dtype=numpy.float64)

    # acquire and store in read_array
    reader.read_many_sample(
        read_array, number_of_samples_per_channel=number_of_samples,

        timeout=10.0)

    # print to console the result
    print(read_array)