You are on page 1of 3

Matlab code to sample Analog channels (A/D Conversion)

Purpose
To sample analog data from one or more A/D channels at a specified frequency and specified duration

Setup
Select channels
Before making a call to perform the conversion, you have to decide on which channels you want to sample. You should set up a vector indicating the desired channels. For example, if you want to sample channels AD1 and AD3, you should create the vector [1,3]. It may be advantageous to create a variable and set its value to the vector. A typical matlab command would be:

Channels = [1, 3]; Select sampling frequence


Before performing the conversion, you should decide the sampling frequency in samples per second. Currently, the fastest sampling rate is 100 samples/second. A typical matlab command would be

Freq = 50 Select sampling duration


Next you have to decide on the duration of conversion in seconds. For example, if you want 3 seconds worth of data, you would use

Duration = 3
The duration and frequency must satisfy the constraint Freq * Duration <=1000. In other words, you can not obtain more than 1000 samples per channel in any A/D conversion.

Perform the conversion


You perform the A/D conversion by calling the matlab function a2d. The function returns two arrays, [y, t]. The first array y will have as many columns as the number of channels you want to convert. The columns are ordered in accordance with the channels you specify. For example if the channels you selected are [1,3], the first column of y is the data from AD1 and the second column is the data from AD3. The vector t, is the time vector, starting with time t=0. The code for performing the conversion is

[y,t] = a2d(Channels, Freq, Duration)


You can plot your data using plot(t,y).

Source Code
function [y, t] = a2d(chnl, freq, duration) %A2D % % [y, t] = a2d(chnl, freq, duration) % % Perform A/D conversion at a specified frequency for a specified duration. % The frequency is in samples per second and duration in seconds % % Default values: % chnl: 0 % freq: 100 % duration: 1000/freq % % This function returns % y: Analog signal that is measured % t: Time vector % % Examples % % [y, t] = a2d; % scans A/D channel #0 at 100 samples per second for 3 seconds % % [y, t] = a2d(3, 75, 2); % scans A/D channel #3 at 75 samples per second for 2 seconds % % [y, t] = a2d([1,3], 75, 2); % scans A/D channels #1 and #3 at 75 samples per second for 2 seconds % Note: y will have two columns corresponding to channels 1 and 3 % % scan_data: data in channels 0, 1, ... chnl % Number of columns in scan_data is 1 more than chnl % The first column is the data in channel #0 % The second column is the data in channel #1 % etc. % %

% Author: N. Narasimhamurthi % if nargin < 1 chnl = 0; end if nargin < 2 freq=100; end if nargin < 3 duration = 1000/freq; end chrange = 0:max(chnl); craw=vscan(chrange, ones(1, max(chrange)+1), freq, duration); y=craw(:,chnl+1); t = 1:length(y); t=t(:)-1; t=t / freq;

You might also like