How to plot arrays of different sizes using interp1?
10 次查看(过去 30 天)
显示 更早的评论
Hi,
I'm trying to compare a simlation result against a measured result, however I'm having difficulty aligning the two sets of data on a single plot. The simulation data has a much higher x-axis resolution than the measured data. Ordinarily I would use an interp1 to match mismatched arrays of different sizes, but the time between each sample is also different in this case. I've attached both sets of data I'm using.
So far all my attempts to use interp1, etc have failed, with nonsense outputs.
Assuming I am interested in getting these two graphs to align as best as possible in the x-axis (they have a definite shared shape), what would be the best approach to achieve this?
Thanks in advance.
0 个评论
回答(2 个)
Star Strider
2019-10-28
Your posted files have only one column each.
I would expect two columns — one for the independent variable and a matching one for the dependent variable. You can create the independent variable vectors if they are both regularly sampled, and you know the sampling frequency of each.
The Signal Processing Toolbox resample function might make this easier, if you know the sampling frequencies for each, and that both are regularly sampled.
0 个评论
Thomas Satterly
2019-10-28
I would suggest using cross-correlation to find out where these two signals match up the best. The data you posted needs a little work - It's only one dimensional, and it looks like the experimental data is smaller in magnitude by a factor of 100. However, we can still get pretty good results. I assumed each signal was sampled over the same amount of time, multiplied the experiemental signal by 100, and used a cheap cross-correlation function to find where the signals match the best:
function testCrossCorrelation(sim, exp)
% Cheap cross correlation of two 1D signals
% Massage the data to be the same size
% Also, since the X axis wasn't defined, let's make it from 0 to 1
x = linspace(0, 1, max(numel(sim), numel(exp)));
s1 = interp1(linspace(0, 1, numel(sim)), sim, x);
s2 = interp1(linspace(0, 1, numel(exp)), exp, x);
% Sweep over a range of offsets
offset = -round(numel(x / 4)):round(numel(x / 4));
scores = zeros(size(offset));
for i = 1:numel(offset)
scores(i) = crossCorrelation(s1, s2, offset(i));
end
% Find the best correlation
[~, best] = max(scores);
% Plot the correlation scores
figure;
plot(offset, scores);
xlabel('Offset');
ylabel('Score');
% Plot the best result
figure;
hold on;
plot(x, s1, 'DisplayName', 'Simulation');
plot(x + offset(best) * diff(x(1:2)), s2, 'DisplayName', 'Experimental');
legend('show');
function score = crossCorrelation(d1, d2, offset)
if offset == 0
score = sum(d1 .* d2);
elseif offset < 0
score = sum(d1(1:end + offset) .* d2(-offset + 1:end));
else
score = sum(d1(offset + 1:end) .* d2(1:end - offset));
end
end
end
Assuming you loaded the data into Matab as "sim" and "exp", just call:
testCrossCorrelation(sim, exp * 100)
You'll quickly notice that assuming each signal was sampled over the same amount of time seems like a bad assumption - the ramp up and tail on the simulated data are both longer than experimental (or maybe the simulation is not accurate). If you have the associated time indecies for each signal, you could get a more reasonable match and be more confident in the comparison
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!