Simultaneously Acquire and Generate Periodic Waveforms using Digilent® Analog Discovery™

This example shows how to use the function generator channel to generate an 1 KHz sinusoidal waveform function and record data at the same time, using analog inputs. The output voltage-range of the outgoing signal is -5.0 to +5.0 volts. This example uses the session-based interface with Digilent Analog Discovery hardware.

Create a session with a Digilent device

Discover Digilent devices connected to your system using daq.getDevices and create a session using the listed Digilent device.

s = daq.createSession('digilent')
s = 

Data acquisition session using Digilent Inc. hardware:
   Will run for 1 second (10000 scans) at 10000 scans/second.
   No channels have been added.

Add a function generator channel

Add a function generator channel with device ID AD1 and channel ID 1. Set the waveform type to Sine.

fgenCh = addFunctionGeneratorChannel(s, 'AD1', 1, 'Sine')
fgenCh = 

Data acquisition sine waveform generator '1' on device 'AD1':

          Phase: 0
          Range: -5.0 to +5.0 Volts
 TerminalConfig: SingleEnded
           Gain: 1
         Offset: 0
      Frequency: 4096
   WaveformType: Sine
 FrequencyLimit: [0.0001 25000000.0]
           Name: ''
             ID: '1'
         Device: [1x1 daq.di.DeviceInfo]
MeasurementType: 'Sine'

Set channel properties

Set channel gain to 5 (sets the amplitude of the sinusoid to 5 V). Save the gain setting to a variable, in order to set axes.

gain = 5;
fgenCh.Gain = gain;

Set the frequency to 1 KHz

fgenCh.Frequency = 1000;

Add an analog input channel

Add an analog input channel with device ID AD1 and channel ID 1. Set the measurement type to Voltage.

aiCh = addAnalogInputChannel(s, 'AD1', 1, 'Voltage')
aiCh = 

Data acquisition analog input voltage channel '1' on device 'AD1':

       Coupling: DC
 TerminalConfig: Differential
          Range: -25 to +25 Volts
           Name: ''
             ID: '1'
         Device: [1x1 daq.di.DeviceInfo]
MeasurementType: 'Voltage'

Set Session properties

Acquire data at a higher rate than the highest frequency in the generated waveform. Set the duration of the generation to be 1 second.

s.Rate = 100 * fgenCh.Frequency;
s.DurationInSeconds = 1;

Generate continuous data and record input simultaneously

[data, timestamps] = startForeground(s);

Display the results

period = 1/fgenCh.Frequency;
plot(timestamps, data);
xlabel('Time in seconds'); ylabel('Voltage in volts');
title(['Period = ', num2str(period), ' seconds'])
axis([0 5*period -gain gain])