Skip to content

Instantly share code, notes, and snippets.

@jsnyder
Created June 20, 2011 22:11
Show Gist options
  • Save jsnyder/1036727 to your computer and use it in GitHub Desktop.
Save jsnyder/1036727 to your computer and use it in GitHub Desktop.
Acquire data using attached 6218 DAQ in MATLAB and plot FFT
% Clear out old handles for devices
try
stop(ai)
delete(ai)
catch
end
% Settings
Fs = 100000; % Number of samples to collect per second
time_length = 2; % length of time to capture samples (seconds)
samples = time_length*Fs; % How many samples of data to get
channels = [0]'; % Channels to get data on
% Get Handle for Analog Input Device
daqinfo = daqhwinfo('nidaq');
daqid = find(ismember(daqinfo.BoardNames, 'USB-6218')==1);
ai= analoginput('nidaq', daqinfo.InstalledBoardIds{daqid});
% Apply settings to device
addchannel(ai,channels,'in');
setverify(ai.Channel,'InputRange',[-5 5]);
setverify (ai,'SampleRate',Fs);
setverify(ai,'InputType','SingleEnded');
setverify(ai,'SamplesPerTrigger', samples);
start(ai);
wait(ai,time_length+1);
[data, dtime, abstime,events]= getdata(ai, samples);
flushdata(ai); % get rid of any queued data
%%
L = length(data);
NFFT = 2^nextpow2(L);
UDATA = fft(data,NFFT)/L;
f = Fs/2*linspace(0,1,NFFT/2+1);
figure; clf;
sig_mag = 2*abs(UDATA(1:NFFT/2+1));
[pk n] = max(sig_mag);
f(n)
semilogy(f,sig_mag);
set(gca,'xlim',[0 4000])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment