MATLAB Answers

0

Difference between signal passed to Simulink and immediately logged

Asked by gbernardi on 7 Jun 2018
Latest activity Commented on by gbernardi on 12 Jun 2018
I'm working with some Simulink models (2016a) where I pass some Matlab workspace signals to Simulink using the From Workspace block. As a sanity check, I tried to log the very same signal to Matlab using the To Workspace block and I noticed something weird.
Essentially, I get no error between the original Matlab signal and the logged Simulink signal for the first half of the simulation, while for the remaining part of the simulation there is some error building up between the signals. It's not a big error, compared to the signals I'm using, but I'm just wondering if there's any way to get exactly the same result (it doesn't seem to be machine precision).
This is a screenshot of the model (which I also attached):
and this is what I obtain comparing the signals (notice how the difference is of several order of mags, but abruptly goes from 0 to some 1e-13 value):
The code I run
fs = 16e3; % sampling freq
siglen = 1; % 1 second
siglen_samp_rec = siglen*fs;
data = 0.1*rand(1,siglen_samp_rec).';
time_test_ts = (0:1/fs:(siglen_samp_rec-1)/fs);
% Save to time series
test_ts = timeseries(data,time_test_ts);
options = simset('SrcWorkspace','current');
tic
sim('test_diff',[],options)
toc
test_TD = squeeze(test_tsl.signals.values);
figure;
subplot(211); plot(data); hold on; plot(test_TD(1:end-1)); title 'Signals'
subplot(212); plot((data-test_TD(1:end-1))); title 'Error'
norm(abs(data(1:siglen*fs)-test_TD(1:siglen*fs)))

  0 Comments

Sign in to comment.

1 Answer

Answer by Mark McBroom on 9 Jun 2018

Is the "interpolate data" option set for the "from workspace" block? I am guessing there are slight nuerical differences between Simulink time at a given time step and the time in your timeseries. As a result, Simulink is interpolating your timeseries data to match with current simulink time, causing a slight difference.

  1 Comment

Yeah, you're right. The "interpolate data" option is indeed set. As a check I tried to plot the timeseries field "time" versus the field "time" from the "To Workspace" block and they're diverging from each other, too.
However, if I untick the "interpolate data" option, the results deviate even more.
I'd suppose the strategy to obtain the same results would be that of removing the numerical differences between the time instants in the timeseries and those in Simulink. Perhaps in the "Model configuration parameters"
I'm asking since for the long signals I'm using these numerical differences build up in a quite substantial manner.

Sign in to comment.