I have two arrays with different lengths (due to different sampling rates) that I need to compare. I'd like to downsample the larger array to match the smaller one in length, however the factor is not an integer but a decimal.
For an example:
a =
1 1.375 1.75 2.125 2.5 2.875 3.25
b =
1 2 3
Is there any way to manipulate these arrays to match lengths?
That's easy to do with clever use of interp1
. The trick is that the keypoints used for interpolation is an array going from 1 up to as many values as you have in a
which we will call N
, and the interpolated keypoints would be a linearly increasing array where the first point is 1, the last point is N
and you evenly divide up this range to have as many points as there are in b
.
Simply put:
anew = interp1(1:numel(a), a, linspace(1, N, numel(b)));
linspace
generates a linearly increasing array from 1 to N = numel(a)
for as many points as you want, which we determine as the total number of elements in b
. This exactly specifies the right keypoints you want to give you a downsampled version of a
that matches the length of b
, though there will be some interpolation required. The default interpolation method is linear.
Using the sample input from a
you provided, we get:
>> anew
anew =
1.0000 2.1250 3.2500