I have figured out that we couldn't build a standard real-time control loop with the Data Acquisition Toolbox in Matlab. The device itself is working with buffers which are the data queued into the output channel. So in my understanding, the real-time control loop is unrealizable because we couldn't influence the data queued into the buffer. Therefore, I decided to create a controlled signal source and then queue the new signal afterwards. Based on what I've created in the workspace, the original sweep sine wave is defined as a 6000000 scans buffer with sample interval of 0.00001. The frequency increase from 200 Hz to 500 Hz, amplitude factor is 1/10.
t = linspace(0, 60, 6000000)';
outputSignal1= chirp(t, 200, t(end), 500)*1/10;
Set the acquisition and generation rates
s.Rate = 100000;
Then I assumed a very basic control algorithm to control the amplitude of sweep. I introduced a variable(factor) to define the desired amplitude factor at each timestamps. for example, if we got the amplitude is 0.5, and we set a reference value equal to 0.4, then the factor should be F=0.4/0.5=0.8; Our code is defined as, (assume the reference value is equal to 0.4)
factor= 0.4/ max(evt.Data(:,2))
Then I saved the factor into a listener
lh = addlistener(s,'DataAvailable',@(src, event)inputReceived);
function inputReceived(src,event)
factor= 0.4/ max(evt.Data(:,2))
Timestamp = Timestamp + 1
if Timestamp > 60
disp('finished')
Timestamp = 1;
end
end
And the listener is fired once per second.
Figure 1: Factor of each timestamps shows in command window. After we acquired all factors for the 6000000 scans in the buffer, then we could create new signal in another workspace.
%% %Create a sweep signal, from 200 Hz to 500 Hz, duration is 60 sec. % queueoutputdata per second, each queue equal to 100000 scans; % Sample interval=0.00001=(x2-x1)/(n-1). % x2=1, x1=o; % increment equal to 5 Hz per second; % 60 intervals in total; %%
t = linspace(0, 1, 100000)';
outputSignal1 = chirp(t, 200, t(end), 205)*1/10* 1.3364;
queueOutputData(s,outputSignal1);
outputSignal2 = chirp(t, 205, t(end), 210)*1/10*1.3381;
queueOutputData(s,outputSignal2);
outputSignal3 = chirp(t, 210, t(end), 215)*1/10*1.3302;
queueOutputData(s,outputSignal3);
.
.
.
outputSignal58 = chirp(t, 485, t(end), 490)*1/10*1.3249;
queueOutputData(s,outputSignal58);
outputSignal59 = chirp(t, 490, t(end), 495)*1/10*1.3188;
queueOutputData(s,outputSignal59);
outputSignal60 = chirp(t, 495, t(end), 500)*1/10*1.3133;
queueOutputData(s,outputSignal60);
I just simply copy and paste every factor to each timestamps. There are 60 new outputsignals in total. If somebody could give me a better idea, like save the factor to a file for example .csv and then array all factors to the new signals, I would really recommend it. The following screenshots are the comparision of results;
But you can see from the second picture, there are several fluctuations evenly distributed between each buffers. We can see more clear when we zoom in to the picture.
I'm very confused about where does the noise comes from...It seems that the noise appeared in every begins or ends of each buffer. If someone could help me, please leave your comment in below. I would really appreciate it!
And one more question about my control design, what is an exact name for my loop? Using factors in the controlled signal source.. is this single-point acquisition and control? Please comment below this, thank you!