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

1 view (last 30 days)

Show older comments

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 Comments

### Accepted Answer

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

Fsr = ceil(max(x_stats(:,4))) % Choose Resampling Sampling Frequency

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

minRowSize = min(RowSizes)

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)

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 Comments

### More Answers (2)

Image Analyst
on 30 May 2023

##### 0 Comments

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.

##### 0 Comments

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!