I'm having a problem averaging multiple curves using interp1

7 views (last 30 days)
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!

Accepted Answer

Chris
Chris on 11 Mar 2023
Edited: Chris on 11 Mar 2023
  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 Comments

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 11 Mar 2023
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.

Community Treasure Hunt

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

Start Hunting!