2 views (last 30 days)

Show older comments

I have carried out various series of measurements from which I would like to form arithmetic mean values.

The problem is that one series of measurements has 1200 data points (Vector_1), the second only 1000 (Vector_2) and the third 800 data points (Vector_3).

I tried to adapt this to the largest vector using interpolation:

maxLength = max([length(Vector_1), length(Vector_2), length(Vector_2)]);

xFit = 1:maxLength;

IP_Vector_1 = interp1(1:length(Vector_1), Vector_1, xFit);

IP_Vector_2 = interp1(1:length(Vector_2), Vector_2, xFit);

IP_Vector_3 = interp1(1:length(Vector_3), Vector_3, xFit);

However, this code does not seem to distribute the interpolation evenly, but rather puts it at the end (with NaN). Does anyone have any idea what the problem is or have another suggestion how that could be solved elegantly in Matlab?

Many Thanks!

Jan
on 26 Jan 2021

Edited: Jan
on 26 Jan 2021

n1 = length(Vector_1);

n2 = length(Vector_2);

n3 = length(Vector_3);

nMax = max([n1, n2, n3]);

IP_Vector_1 = interp1(1:n1, Vector_1, linspace(1, n1, nMax));

IP_Vector_2 = interp1(1:n2, Vector_2, linspace(1, n2, nMax));

IP_Vector_3 = interp1(1:n3, Vector_3, linspace(1, n3, nMax));

Now the vectors have all nMax steps. Interpolating a vector with x=1:10 at the steps x = 1:20 appends 10 NaNs, because thius is an extrapolation. You need the interval [1, 10] split into nMax steps instead:

1:((10 - 1) / (nMax - 1)):10

% or nicer:

linspace(1, 10, nMax)

Note 1: Normalizing with linear interpolation can destroy important information, if the sampling frequency is low:

v1 = [1, 10, 1];

v2 = [1, 1, 1, 1];

vi1 = interp1(1:n1, v1, linspace(1, n1, nMax)) % [1, 7, 7, 1]

vi2 = interp1(1:n2, v2, linspace(1, n2, nMax)) % [1, 1, 1, 1]

The large peak in v1 is damped. So it is a better idea to use nMax = q * max([n1, n2, n3]) with q = 2 or 5. In a smart program this factor is implemented as variable such that you can compare the results for different scaling factors.

Note 2: If this is time-ciritical, use FEX: ScaleTime, which interpolates faster than INTERP1 or GriddedInterpolant.

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

Start Hunting!