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

조회 수: 4 (최근 30일)
Zeynep Ertekin
Zeynep Ertekin 2023년 5월 30일
댓글: Image Analyst 2023년 5월 31일
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
the cyclist
the cyclist 2023년 5월 30일
What information do you have available to you? Do you have only the image? Do you have the underlying data? (Please post the data if you have it.)
Zeynep Ertekin
Zeynep Ertekin 2023년 5월 30일
i just have .fig image, however i do not know how to get the image data and assign it to an array.

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2023년 5월 30일
편집: Star Strider 2023년 5월 30일
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.
.
  댓글 수: 3
Star Strider
Star Strider 2023년 5월 30일
To be more specific, I would like to compute the average of 3 curves.
So I provided code that does just that.
Mine is not to reason why ...
— With apologies to Alfred, Lord Tennyson
.
Image Analyst
Image Analyst 2023년 5월 31일
Yes, it does do that.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Image Analyst
Image Analyst 2023년 5월 30일
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
the cyclist 2023년 5월 30일
편집: the cyclist 2023년 5월 30일
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.

카테고리

Help CenterFile Exchange에서 Least Squares에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by