# how to find average curve of n curves? n=3 in this case

1 view (last 30 days)
Zeynep Ertekin on 30 May 2023
Commented: Image Analyst on 31 May 2023
We have n curves, having similar characteristics at a specific interval. (n=3)
And I wanna find a "resulting " curve, that uses all three of them equally.
To be more specific, I would like to compute the average of 3 curves.
Regards,
(fig file is attached)
##### 2 CommentsShow 1 older commentHide 1 older comment
Zeynep Ertekin on 30 May 2023
i just have .fig image, however i do not know how to get the image data and assign it to an array.

Star Strider on 30 May 2023
Edited: Star Strider on 30 May 2023
This is a bit more involved than it first appears.
Getting the data from the .fig file is straightforward, however it then gets complicated. This is due to the fact that the different curves do not have the same frequency sampling intervals (in a time-domain signal this would be ‘sampling frequency’) or vector lengths. All those have to be equalised before it is possible to do anything with the vectors.
This code does all that, however it might be best to convert the decibel data to magnitude data and then do the statistics (I did not do that here), and then if necessary convert it back to decibel data. This nevertheless demonstrates the essential steps involved, and I leave the necessary decisions (and the requisite coding of them, that might require a different approach that the arithmetic mean) to you —
F = openfig('fig3b_XveKu_7....A_CST_S21.fig'); Lines = findobj(F, 'Type','line');
for k = 1:numel(Lines) % Get Data
xv{k,:} = Lines(k).XData;
yv{k,:} = Lines(k).YData;
n(k,:) = numel(xv{k});
x_stats(k,:) = [mean(diff(xv{k})); std(diff(xv{k})); mode(diff(xv{k})); 1/mode(diff(xv{k}))];
end
x_stats
x_stats = 3×4
0.0054 0.0010 0.0064 156.2500 0.0104 0.0000 0.0104 96.1555 0.0019 0.0006 0.0025 399.9146
Fsr = ceil(max(x_stats(:,4))) % Choose Resampling Sampling Frequency
Fsr = 400
xr = cell(size(Lines));
yr = cell(size(Lines));
for k = 1:numel(Lines) % Resample To Common Frequency Vector
[yr{k},xr{k}] = resne(yv{k}, xv{k}, Fsr);
nr(k,:) = numel(xr{k});
end
RowSizes = nr
RowSizes = 3×1
4162 4163 5001
minRowSize = min(RowSizes)
minRowSize = 4162
for k = 1:numel(Lines) % Trim All Vectors To Shortest Vector
xrt(k,:) = xr{k}(1:minRowSize);
yrt(k,:) = yr{k}(1:minRowSize);
end
yr_mean = mean(yrt);
yr_std = std(yrt);
yr_sem = yr_std / sqrt(size(yrt,1));
[min_sem,max_sem] = bounds(yr_sem)
min_sem = 0.0488
max_sem = 2.9489
yr_ci = tinv([0.025; 0.975], size(yrt,1)-1) * yr_sem + yr_mean;
figure
hp1 = plot(xrt(1,:), yr_mean, 'DisplayName','\mu');
hold on
% hp2 = plot(xrt(1,:), yr_ci, '--r', 'DisplayName','95% Confidence Intervals');
hold off
grid
ylim([-47 -31])
xlabel('Frequency')
ylabel('Magnitude (dB)') % legend([hp1 hp2(1)],'Location','best')
function [yr,xr] = resne(x,tx,Fsr) % Resample Eliminating End-Effects
LR = @(X,Y,x,y) [X(:) ones(size(X(:)))] * ([x(:) ones(size(x(:)))] \ y(:)); % Single-Line Linear Regression
for k = 1:size(x,1)
DeTr = LR(tx,x(k,:),tx([1 end]),x(k,[1 end])).';
[y(k,:),ty] = resample(x(k,:)-DeTr,tx,Fsr,'Dimension',2);
ReTr = LR(ty,y(k,:),tx([end 1]),x(k,[end 1])).';
yr(k,:) = y(k,:)+ReTr;
xr = ty;
end
end
The resample function can distort the ends of the signals it resamples if they are not very close to zero. The ‘resne’ (Resample, No End Effects) function here does that. It is not required, however it makes the results of the resample call neater.
EDIT — Corrected typographical errors.
.
Image Analyst on 31 May 2023
Yes, it does do that. Image Analyst on 30 May 2023
Try findobj or see if you can get XData and YData properties of the figure. Otherwise see if you can get the data or equations/formulas of the curves from whomever made up the fig file.

the cyclist on 30 May 2023
Edited: the cyclist on 30 May 2023
Here is how to get at the underlying data, from the figure file.
open("fig3b_XveKu_7.9%_S_parameters_VNA_CST_S21.fig")
h = findobj("Type","Line")
y1 = h(1).YData;
y2 = h(2).YData;
y3 = h(3).YData;
size(y1)
size(y2)
size(y3)
I see that the values are different lengths (sampled at different X values, presumably, but I didn't look more carefully). Therefore, you cannot do the average very simply.
One possibility would be to interpolate your curves before doing the averaging. The interp1 function is probably adequate, since you have very finely spaced data.