Measurement Computing   Easy to Use | Easy to Integrate | Easy to Support catalog banner

Measurement Computing Data Acquisition Knowledgebase

Welcome Guest ( )

MatLab Analog Input Video Script

Expand / Collapse
 

MatLab Analog Input Video Script


MatLab Video:MatLab_Analog_Input

Welcome to using Measurement Computing devices with Matlab. Today I will be using the USB-1608GX-2A0 to read an input signal and compute its amplitude spectrum.

Let’s begin…Start a new script file to hold the commands

I am going to begin by defining a few variables. These are used in my display routine and help make the code clearer.

>>count = 256;
This is the number of samples I want to read; 

>>rate = 256;
This is the frequency at which samples are read.

>>range = 5.0;
My device supports voltage ranges of 1, 2, 5 & 10. For this example I want 5 volts

Now I need to get the analog input object which contains the acquisition settings.
>>ai = analoginput( ‘mcc’, 0 );
The string ‘mcc’ is the driver identifier and the 0 is the board number assigned by the InstaCal program. 

now add a channel to the analog input
>>ch = addchannel( ai, 0 );
Where zero is the channel number

Set the input range
>>ch.InputRange = [-range range];

Set the sample rate
>>ai.SampleRate = rate;

set the number of samples to read
>>ai.SamplesPerTrigger = count;

And the trigger method
>>ai.TriggerType = ‘Immediate’;

The start command will start the acquisition running
>>start(ai);

And to read the voltage data use the getdata function
>>y = getdata(ai);

Use the FFT function to change the data to frequencies
>>yy = fft( y ) / count;  

And the amplitude spectrum is twice the absolute value of the FFT
>>yyy = 2* abs( yy );

Finally, I will name my script FetchData.
Now I need an input signal and for that I have a Generator routine that outputs a complex waveform on the analog output line. To complete the electrical connection I have connected a wire between the output and the input. To display the data I have another routine called Plot that will graph the voltage and frequency data.
Now let’s test 
>>gen; fetchdata; PlotData;
That's all there is to it! Thanks for watching! Visit our website for more information about Data Acquisition devices and the software that controls them. 

Refer to the transcript for a listing on all three scripts used in this video.

Analog Input Script

%Number of samples to acquire in powers of 2 (ie 64,128,256,512 etc)
count = 256; 

%Rate at which the samples are to be acquired
rate = 256; 

%Input voltage range
range = 5.0;

%get analog input object
ai = analoginput('mcc',0);

%add channel to ai object
ch = addchannel(ai,0);

%set the input range for the channel
ch.InputRange = [-range range];

%set the sample rate
ai.SampleRate = rate;

%set number of samples to acquire
ai.SamplesPerTrigger = count;

%set acquisition start type
ai.TriggerType = 'Immediate';

%Start the acquisition
start(ai);

%Read the data
y = getdata(ai);

%calculate complex spectrum
yy = fft(y)/count;  

%calculate amplitude spectrum
yyy = 2* abs(yy);


Analog Output Script

%Waveform length
cnt = 1024;

%Waveform update rate
fs = 4096;

%number of repeated cycles
cycles = 50;

%calculate complex waveform
angle = 2*pi/cnt;
x = (0:cnt-1) * angle;


y = sin(x * 2) + .5 * sin(x * 4) + 2 * sin(x * 8);

%get analog output object
ao = analogoutput('mcc',0);
chan = addchannel(ao,0);

%set analog output parameters
set(ao,'SampleRate',fs);
set(ao,'RepeatOutput',cycles);

ai = analoginput('mcc',0);
ch = addchannel( ai, 0 );

%use the transpose function and send the data to ao
putdata(ao,transpose(y));

%start the waveform
start(ao);

Plot Script

%create window for displaying plots
h = figure;

%create an x-axis for time data
x = transpose((0:count-1) * 1 / rate);

%create x-axis frequency data
xx = transpose((0:count-1) * rate / count); 

%plot the time data
subplot(2,1,1), plot(x(1:count),y(1:count));

%Scale and label plot display
axis([0.0 (count * 1/rate) -range range ]);
title('Voltage Waveform');
xlabel('Time');
ylabel('Volts');

%plot the frequency data
n = count / 2;
subplot(2,1,2),plot(xx(1:n),yyy(1:n));

%Scale and label plot display
axis([0.0 (rate/2) 0 range/2 ]);
title('Amplitude Spectrum');
xlabel('Frequency (Hz)');
ylabel('Volts');



Rate this Article:

Add Your Comments


For comments email TechSupport@mccdaq.com.

Details
Article ID: 50495

Last Modified:11/19/2013 10:51:40 AM

Article has been viewed 6,088 times.

Options