-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaqNIwithMatlab.m
125 lines (105 loc) · 4.42 KB
/
daqNIwithMatlab.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
clear;clc;
% name of the record file. name by the time it is created.
CurrentTime = clock;
prefix = ['NIRec_', num2str(round(CurrentTime(3)),'%.2d'), '_', ...
num2str(round(CurrentTime(2)),'%.2d'), '_',...
num2str(round(CurrentTime(4)),'%.2d'), '_', ...
num2str(round(CurrentTime(5)),'%.2d')];
chan_num = 6; %set between 1 and 8
fs = 40000; % set the sample rate, max rate is 250000/chan_num
max_fs = floor(250e3/chan_num);
if fs > max_fs
fs = max_fs;
disp(['requested rate was too high, using ' num2str(max_fs),'Hz instead'])
end
global param;
param.fs = fs;
param.dispCH = 1;
param.prefix = prefix;
param.index = 1;
param.filename = [prefix, '_1.bin'];
param.fid = fopen(param.filename,'w');
param.maxLen = 500000000; % define the maximum length of each saved files. you can change this
param.dispLen = fs;
param.dispSig = zeros(1,fs);
param.dispPos = 1;
param.dispFactor = param.dispLen/param.fs;
param.specStep = 512;
param.specYLen = 1024;
% param.specXLen = floor(param.dispLen/param.specYLen);
param.specXLen = floor((param.dispLen-param.specYLen+param.specStep)/param.specStep);
param.specData = zeros(param.specYLen,param.specXLen);
param.specCalPos = 1;
param.specXPos = 1;
%Setup Session, Add Channels and Configure Parameters
d = daq.getDevices;
list_id={d(:).ID};
ses = daq.createSession('ni'); %Create the data acquisition session with directsound!
ses.Rate = fs; %Define Sampling rate
ses.IsContinuous = true;
% add chan_num channels to the session. It can be customized
for ii=0:(chan_num-1)
chan_name = ['ai',num2str(ii)];
ses.addAnalogInputChannel('Dev1',chan_name,'Voltage');
end
lh = addlistener(ses,'DataAvailable',@(src, event)logData(src,event,prefix));
disp('Start receiving');
% start the recording
startBackground(ses);
str = 'recording';
% waiting for input. If the input is from 1-6, show the signal of corresponding channel.
% recording is stopped only when you input 'stop'
while (~strcmp(str,'stop'))
str = input('input number to change observation or input ''stop'' to stop recording\n','s');
num = str2double(str);
if ((num >= 1) && (num <= chan_num))
param.dispCH = round(num);
param.dispSig = zeros(1,fs);
param.dispPos = 1;
param.specData = zeros(param.specYLen,param.specXLen);
param.specCalPos = 1;
param.specXPos = 1;
end
end
stop(ses);
ses.release();
ses.IsContinuous = false;
delete(lh);
disp('Stop receiving');
fclose(param.fid); % close the last file opened
function logData(src, evt, prefix)
% Add the time stamp and the data values to data. To write data sequentially,
% transpose the matrix.
% Copyright 2011 The MathWorks, Inc.
global param;
lenRx = length(evt.TimeStamps);
data = [evt.TimeStamps, evt.Data]' ;
fwrite(param.fid,data,'double');
s = dir(param.filename);
lenFile = s.bytes;
if (lenFile >= param.maxLen)
fclose(param.fid); % close current file
param.index = param.index+1;% increase number of index
param.filename = [prefix, '_',num2str(param.index),'.bin']; % new file name
param.fid = fopen(param.filename,'w');
%disp(['change record file to ',param.filename]);
end
param.dispSig(mod(param.dispPos+(0:lenRx-1)-1,param.dispLen)+1) = data(param.dispCH+1,:);
param.dispPos = mod(param.dispPos+lenRx-1,param.dispLen)+1;
figure(100);subplot(211);
plot(1/param.fs:1/param.fs:param.dispLen/param.fs,[param.dispSig(param.dispPos:end),param.dispSig(1:param.dispPos-1)]);grid on;box on;
xlabel('Time(s)');ylabel('Amplitude(V)');title('time domain');
Len = mod(param.dispPos-param.specCalPos-1,param.dispLen)+1;
Num = floor((Len-param.specYLen+param.specStep)/param.specStep);
while (Num > 0)
idx = mod(param.specCalPos+(1:param.specYLen)-1,param.dispLen)+1;
param.specData(:,param.specXPos) = transpose(abs(fft(param.dispSig(idx))));
% disp(num2str([Num,idx([1,end]),param.specCalPos,param.specXPos]));
param.specXPos = mod(param.specXPos,param.specXLen)+1;
param.specCalPos = mod(param.specCalPos+param.specStep-1,param.dispLen)+1;
Num = Num-1;
end
figure(100);subplot(212);
imagesc((1:param.specXLen)/param.specXLen*param.dispFactor,(1:param.specYLen/2)/param.specYLen*param.fs,param.specData(1:param.specYLen/2,[param.specXPos:end,1:param.specXPos-1]));
xlabel('Time(s)');ylabel('Frequency(V)');title('spectrogram');
end