Logic test has me stumped

1 回表示 (過去 30 日間)
sco1
sco1 2011 年 5 月 11 日
I'm in the process of rewriting a GUI I created a few years ago for doing analysis on some tensile test data. Major task is simplifying/streamlining the code and adding more functionality for when we do additional testing. One of the things we noticed while looking at the test data is that the INSTRON machine we have will sometimes insert random bursts of data. For example, sampling at 10Hz, there will be clusters of data points every once and a while at about a bajillion hertz, and then it goes back to the normal sample rate.
I made a loop to detect these loops and interpolate around them in order to get a data set that will match up with the timestamped video data we're using for optical measurements. When I was debugging the loop, I noticed it was first catching at row 4, but the first burst of data in the sample I was using wasn't until row 94.
Here's the simplified loop:
for = 2:numel(time)
if time(i) - 0.1 ~= time(i-1)
%interpolation code
end
end
My first guess was that MATLAB was carrying an error term somewhere, so I tried to round the data next.
for = 2:numel(time)
test1 = str2num(sprintf('%4d',time(i))); test2 = str2num(sprintf('%4d',time(i-1)));
if (test1 - 0.1) ~= test2
%interpolation code
end
end
With the same results.
So I decided to go back to the basics and test out the logic. I made 2 attempts:
(0.3 - 0.2) == 0.1
test1 = 0.1; test2 = (0.3-0.2); test1 == test2
Both of which return 0. I'm under the impression that the logic should return 1. I have a feeling I'm just doing something wrong, but I have no idea what and it has me completely stumped.

採用された回答

Sean de Wolski
Sean de Wolski 2011 年 5 月 11 日
The daily floating point stump:
Ps. We also use an Instron and never have that issue to my knowledge.
  1 件のコメント
sco1
sco1 2011 年 5 月 11 日
Bah, so my initial hunch was correct and I spent an afternoon questioning my sanity anyway! I should have listened to myself and my hazy recollection of my numerical methods coursework.
I added in a tolerance after spending entirely too much time trying to convince myself I wasn't transported to an alternate universe (I even checked 2*2 to make sure it was still 5).
As to the instron data, I had never seen it before until I did the testing. I've dumped a sample in below.

サインインしてコメントする。

その他の回答 (1 件)

sco1
sco1 2011 年 5 月 11 日
Sample burst:
94
94.1
94.2
94.3
94.4
94.5
94.5176
94.5186
94.5196
94.5206
94.5216
94.5226
94.5236
94.5246
94.5256
94.5266
94.5276
94.5286
94.5296
94.5306
94.5316
94.5326
94.6
94.7
  2 件のコメント
Sean de Wolski
Sean de Wolski 2011 年 5 月 11 日
Well you could bin the data and take its average (or median etc.):
y = 1:length(x); %example y data
[junk, bin] = histc(x,93.95:0.1:94.85); %bin it
y2 = accumarray(bin,y,[],@mean); %accumulate and mean bins
sco1
sco1 2011 年 5 月 11 日
The rest of the data points are uniform enough that interpolating over the entire data set doesn't noticeably affect the data. It was just an interesting phenomenon that still hasn't gotten figured out, but it was throwing errors when trying to match to the video measurements because the arrays were different sizes.
But that wasn't the problem, forgetting basic MATLAB knowledge is ;)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Distribution Plots についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by