Dynamic Signal Acquisition

cancel
Showing results for 
Search instead for 
Did you mean: 

Sync AI and AO using nidaqmx python

Hello everyone I am trying to write a code for a analog out from speaker and collecting the speaker stimuli and response via microphone. When the codes written individually for speaker output and microphone input they work but when I try to sync them the code is not generating output.

 

!! The code for generating wave!!

!!!!

def generate_wave(freqA, freqB, N_freq, Amplitude, duration, samplingRate, dutycycle) :

    N_freq = N_freq+1    
    #Time vector
    t = np.linspace(0, duration, int(samplingRate*duration),endpoint=False)
   
    #sample Points
    N_tot = int(samplingRate*duration)
   
    N_max = int(0.8*N_tot*dutycycle)
    N_rise = int(0.1*N_tot*dutycycle)
    N_zero = N_tot-N_max-N_rise*2

    #initialize wave
    #wave_amplitude = np.zeros(N_tot)

    wave_amplitude1 = np.linspace(0,Amplitude,N_rise)
    wave_amplitude2 = Amplitude*np.ones(N_max)
    wave_amplitude3 = np.linspace(Amplitude,0,N_rise)
    wave_amplitude4 = np.zeros(N_zero)

    wave_amplitude = np.concatenate((wave_amplitude1,wave_amplitude2,wave_amplitude3,wave_amplitude4),axis=None)

    wave = wave_amplitude*np.sin(2*np.pi*freqA*t)
    freq_step = (freqB-freqA)/(N_freq-1)
   
    for i in range(N_freq-2) :
        frequency = freqA + freq_step*(i+1)
        ti = np.linspace(duration*(i+1),duration*(i+2),int(samplingRate*duration),endpoint=False)
        wave_cur = wave_amplitude*np.sin(2*np.pi*frequency*ti)
        wave = np.concatenate((wave,wave_cur),axis=None)

    time  = np.linspace(0, duration*(N_freq-1), int(samplingRate*duration)*(N_freq-1),endpoint=False)
    return time, wave
!!!! 
freqA = 10 # Initial Frequency
freqB = 100 # Final frequency
N_freq = 10 # Number of frequencies
Amplitude = 0.5 # Amplitude of wave
duration = 1 # duration for one frquency
samplingRate = 1000 #number of time steps
dutycycle = 0.9 # duty cycle in decimals

time, output = generate_wave(freqA, freqB, N_freq, Amplitude, duration, samplingRate, dutycycle)
!!!!
The above code generates the output to speaker and when run individually I can differentiate the 10 frequencies given
 
The error occurs in the next part of the code given below
!!!
try:
    # Create a task for analog output
    with nidaqmx.Task() as ao_task,nidaqmx.Task() as ai_task:
        ao_task.ao_channels.add_ao_voltage_chan("Dev1/ao0")
        ao_task.timing.cfg_samp_clk_timing(sampling_rate, samps_per_chan=num_samples, sample_mode=nidaqmx.constants.AcquisitionType.CONTINUOUS)
       
       

        # Write the output signal to the analog output channel
       
   
    # Create a task for analog input
        ai_task.ai_channels.add_ai_voltage_chan("Dev1/ai0")
        ai_task.ai_channels.add_ai_voltage_chan("Dev1/ai1")  # Second analog input channel
        ai_task.timing.cfg_samp_clk_timing(sampling_rate, samps_per_chan=num_samples, sample_mode=nidaqmx.constants.AcquisitionType.CONTINUOUS)

        # Read data
        output_signal = output

        ao_task.write(output_signal, auto_start=False)      
        data = ai_task.read(number_of_samples_per_channel=num_samples)

        ao_task.start()
        ai_task.start()
       
        ao_task.wait_until_done()
        ai_task.wait_until_done()
        ao_task.stop()
        ai_task.stop()

        # Separate the data from the two analog input channels
        data_ai1 = [d[0] for d in data]  # Data from the first analog input channel
        data_ai2 = [d[1] for d in data]  # Data from the second analog input channel

        # Store data to excel
        df = pd.DataFrame({"Voltage_AI0": data_ai1, "Voltage_AI1": data_ai2})

        # Save DataFrame to Excel file
        df.to_csv("acquired_data_AI_AO.csv", index=False)

except nidaqmx.DaqError as e:
    print(f"DAQmx Error: {e}")
!!!
DAQmx Error: Specified operation cannot be performed while the task is running.

Can anyone help regarding this issue
0 Kudos
Message 1 of 1
(71 Views)