function iodaq(samplerate,duration,amplitude,frequency,filename) % iodaq(samplerate,duration,amplitude,frequency,filename) % Data Acquisition with simultaneous Analog Input and Analog Ouput % Samples per second for both analog input and analog ouput % Duration of test (seconds) % Amplitude of sinusoidal signal for analog output (0 to 10 volts) % Frequency of sinusoidal signal for analog output % Data file name for analog input data % % example: % iodaq(100,10,3,2.5,'mydata1.dat') % Chris Dillenbeck, Duke University, 7/04, cjd9@duke.edu % Matthew Sin, Duke University, 11/05, mcs21@duke.edu % Henri Gavin, Duke University, 11/05, hpgavin@duke.edu delete(daqfind); % delete all previous data acquisition settings points = samplerate*duration; % number of data points %Initialize analog input ai = analoginput('nidaq'); % specify analog input addchannel(ai, 1:2); % channels for data acquisition set(ai,'SampleRate',samplerate) % samples per second set(ai,'SamplesPerTrigger',points) % number of data points set(ai,'InputType','Differential') % differential inputs set(ai,'TransferMode','Interrupts') % transfer data by interrupts ai.Channel.InputRange = [-10 10]; % input voltage range ... all channels ai.Channel.ChannelName(1) = 'sensor 1'; % name of sensor 1 ai.Channel.Units(1) = 'Volts'; % the physical units of sensor 1 ai.Channel.SensorRange(1) = [ -5 5 ]; % typical voltage output from sensor ai.Channel.UnitsRange(1) = [ -5 5 ]; % the physical units corresponding % to the sensor range levels ai.Channel.ChannelName(2) = 'sensor 2'; % name of sensor 2 ai.Channel.Units(2) = 'Volts'; % the physical units of sensor 2 ai.Channel.SensorRange(2) = [ -5 5 ]; % typical voltage output from sensor ai.Channel.UnitsRange(2) = [ -5 5 ]; % the physical units corresponding % to the sensor range levels %Initialize analog output ao = analogoutput('nidaq',1); % specify analog output addchannel(ao,0); % channels for analog output set(ao,'SampleRate',samplerate) % sample rate for analog output set(ao, 'TransferMode', 'None') % cludge! %ActualRate = get(ao,'SampleRate'); set([ai ao],'TriggerType','Manual') % start when program starts ai % display properties of analog input ao % display properties of analog output %Test data time = [1:points]/samplerate; analog_out_data = amplitude*sin(2*pi*frequency*time)'; %Output data putdata( ao , analog_out_data ) % load the analog output data %Run start([ao ai]) % start the analog input/output trigger([ao ai]) % trigger analog input/output %Input data analog_in_data = getdata(ai); % get data from the analog input waittilstop(ao,duration+1) % wait for analog output to finish figure(1) % plot the data plot(time,analog_in_data) xlabel('time, sec') ylabel('volts') chan = get(ai,'Channel'); idx = chan.Index; fp_ai = fopen(filename,'w'); % open analog input data file name % save the analog input data for p=1:points fprintf(fp_ai,' %8.3f', time(p)); for i = 1:length(idx) c = idx{i}; % curly brackets! fprintf(fp_ai,' %14.5e', analog_in_data(p,c)); end fprintf(fp_ai,'\n'); end fclose(fp_ai); % Discard Variables delete ([ao ai]) clear [ao ai] % ----------------------------------------------------- IODAQ