I'm having a problem averaging multiple curves using interp1

Hi everyone.
I have multiple polarisation curves that I want to display the averge of. I tried using linspace to create a base vector and interpolating using interp1. Unfortunately that hasn't properly worked for me and I was hoping someone might be able to help.
Thank you in advance!

 采纳的回答

  1. You take the mean and max of the U values; I believe you want the I values instead.
  2. You have plenty of data points, so the default linear interpolation will follow the trend better.
  3. Some data at the end will have to be excluded from the mean curve. You could use the 'omitnan' flag, but that will cause a discontinuity in the curve.
% Mittelung mehrerer Messungen
clearvars
[filenames, pathname] = uigetfile('MultiSelect', 'on', '*.*');
fullname = fullfile(pathname,filenames);
clear savename
for z = 1:length(fullname)
load (fullname{1,z})
loadDaten{1,z} = Daten;
end
Daten = loadDaten;
IVC_mean = cell (3,length(fullname));
var = zeros(1,length(fullname));
Names = string(var);
Imax = zeros (1,length(fullname));
Umax = zeros (1,length(fullname));
Umin = zeros (1,length(fullname));
for z = 1:length(fullname)
% Messdaten
Ewe = Daten{1,z}(:,7);
I = Daten{1,z}(:,8).*1000;
Ismooth = smoothdata(I,'sgolay');
% Details der Messung
savename{1,z} = extractBefore(filenames{1,z},".");
Names(z) = savename {1,z};
% sortieren
IVC_mean{1,z} = Ewe;
IVC_mean{2,z} = abs(Ismooth);
IVC_mean{3,z} = extractAfter(strrep(savename{1,z},'_',' '),' ');
% % outlier
% pp = isoutlier(IVC_mean{2,z});
% ind = find(pp);
% IVC_mean{4,z} = ind;
% IVC_mean{5,z} = IVC_mean{2,z};
% IVC_mean{5,z}(ind) = NaN;
% einzeln plot
h = scatter(IVC_mean{2,z},IVC_mean{1,z});
xlabel(['I']);ylabel(['U']);
hold on
% Grenzen für xq
% IVC_mean{6,z} = min(IVC_mean{1,z});
% IVC_mean{7,z} = max(IVC_mean{1,z});
IVC_mean{6,z} = min(IVC_mean{2,z});
IVC_mean{7,z} = max(IVC_mean{2,z});
Umin(z) = IVC_mean{6,z};
Umax(z) = IVC_mean{7,z};
end
%
% Interpolation
Umin = min(Umin);
Umax = max(Umax);
% vorgegebener Bezugsvektor
UC = linspace(Umin,Umax,10000);
for z = 1:length(fullname)
% IVC_mean{8,z} = interp1(IVC_mean{2,z},IVC_mean{1,z},UC,'spline');
IVC_mean{8,z} = interp1(IVC_mean{2,z},IVC_mean{1,z},UC,'linear');
h2 = plot(UC,IVC_mean{8,z},'k--','LineWidth',2);
hold on
end
mfit = mean(cat(1,IVC_mean{8,:}));
plot(UC, mfit,'m','LineWidth',2);

5 个评论

I've tried usign the I values before but when I do it says the sample points must be unique and quite a lot of them aren't. Technically i measured the current over a linear potential sweep, so I should be fine using the U values I think. Displaying U over I is just the conventional thing to do...
Some data at the end will have to be excluded from the mean curve
They are using 'spline' which does interpolation by default, so none of the results will be nan.
Perhaps it is "good enough" to interpolate at the tails, perhaps not. But if not then instead of taking the min() of the min() and the max of the max, Umin = max(Umin) and Umax = min(Umax) would create an interval that included only the portion common to all three curves, which would avoid any need for interpolation.
@Philip Krämer As Walter pointed out, the script you uploaded used the range of IVC_mean{1,:}, which had been assigned the values of Ewe. The script then fits U values along that reduced range ([0.3, 1.3]). The splines go wild outside of the interpolated range. You can fit U along the range of I just by changing three lines:
IVC_mean{6,z} = min(IVC_mean{2,z});
IVC_mean{7,z} = max(IVC_mean{2,z});
Then:
h2 = scatter(UC, IVC_mean{8,z});
Since the splines still go wild on the shorter fits, you can zoom in with the axes to see your range of interest (or plot along a reduced range of I/UC)
xlim([0.1 1.6])

请先登录,再进行评论。

更多回答(1 个)

h = scatter(IVC_mean{2,z},IVC_mean{1,z});
So {1} is used as y values and {2} is used as x values.
IVC_mean{6,z} = min(IVC_mean{1,z});
IVC_mean{7,z} = max(IVC_mean{1,z});
min and max of the y values.
Umin = min(Umin);
Umax = max(Umax);
UC = linspace(Umin,Umax,10000);
smallest y and greatest y
IVC_mean{8,z} = interp1(IVC_mean{2,z},IVC_mean{1,z},UC,'spline');
you pass in known x values and corresponding known y values and you query based on UC, which is based on y values, not on x values.

类别

帮助中心File Exchange 中查找有关 Interpolation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by