Skip to content

Instantly share code, notes, and snippets.

@jsnyder
Created November 7, 2011 18:09
Show Gist options
  • Save jsnyder/1345697 to your computer and use it in GitHub Desktop.
Save jsnyder/1345697 to your computer and use it in GitHub Desktop.
data acquisition setup
% ME224 Basic Data Output Script
try
stop(ai); delete(ai);
catch
end
% Settings
Fsi = 1000; % input sample rate
time_length = 1; % length of time to capture samples (seconds)
samples = time_length*Fsi; % How many samples to get
channels = 0; % Channels to get data on
% Get Handle for Analog Output Device
mccdaqinfo = daqhwinfo('mcc');
mccdaqids = find(ismember(mccdaqinfo.BoardNames,'USB-1208FS')==1);
% Create AO and AI handles
ai = analoginput('mcc', mccdaqinfo.InstalledBoardIds{mccdaqids(1)});
addchannel(ai,channels,'in');
set(ai,'SampleRate',Fsi);
set(ai,'SamplesPerTrigger',samples);
start(ai);
data = getdata(ai,samples);
% ME224 Basic Data Output Script
try
stop(ao); delete(ao);
catch
end
try
stop(ai); delete(ai);
catch
end
% Settings
Fso = 10000; % output sample rate
Fsi = 10000; % input sample rate
time_length = 5; % length of time to capture samples (seconds)
samples = time_length*Fsi; % How many samples to get
channels = 0; % Channels to get data on
% Get Handle for Analog Output Device
mccdaqinfo = daqhwinfo('mcc');
mccdaqids = find(ismember(mccdaqinfo.BoardNames,'USB-1208FS')==1);
% Create AO and AI handles
ao = analogoutput('mcc', mccdaqinfo.InstalledBoardIds{mccdaqids(2)});
ai = analoginput('mcc', mccdaqinfo.InstalledBoardIds{mccdaqids(1)});
addchannel(ao,channels,'out');
addchannel(ai,channels,'in');
set(ao,'SampleRate',Fso);
set(ai,'SampleRate',Fsi);
set(ai,'SamplesPerTrigger',samples);
t = 0:1./Fso:time_length-1./Fso;
outdata = (chirp(t,0.1,time_length-0.3,500)+1)';
putdata(ao, outdata);
start(ai);
start(ao);
[data time]= getdata(ai,samples);
flushdata(ai) % get rid of any queued data
figure(1);
[X,f] = myFFT(data - mean(data),Fsi);
[Xo,fo] = myFFT(outdata - mean(outdata),Fso);
plot(f,smooth(abs(X./Xo)));
set(gca,'xlim',[0 500]);
set(gca,'ylim',[0 3]);
xlabel('Frequency (Hz)')
ylabel('Vo/Vin')
function [X,freq]=myFFT(x,Fs)
x = x';
N = length(x);
if mod(N,2)
k = (-(N-1)/2):((N-1)/2); % if odd, use symmetrical k
else
k = (-N/2):((N-1)/2); % if even, one extra negative value
end
Tlen = N/Fs;
freq = k./Tlen; % scale k by time length to get frequencies
X=fft(x)./N; % scale fft by length
X=fftshift(X);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment