Resampling a vector to change its length

226 views (last 30 days)
Hi all,
I have a series of vectors that all have different lengths (100<length<150), and I want to standardise their length to 100 frames so that I can put them into a matrix and perform PCA on it. I have tried using the resample function to reduce the length of my vectors one by one, but I am having issues with the ripples at the beginning and end of my resampled vectors. For example, let's say I have a vector x with 111 values. What I wrote is:
x2 = resample(x,100,111);
plot(x,'LineWidth',2); hold on; plot(x2,'LineWidth',2); legend('Original','Resampled')
This yielded the following graph:
I have tried to get rid of the ripples by manipulating the value n of the antialiasing filter (y = resample(x,p,q, n)). The best solution I got was with n=4, which yielded this:
which is better, but clearly still not good enough. If I increase the value of n above 4, it gets worse. At this point I am not sure how to handle this issue, and any help would be much appreciated! I don't want to necessarily use the function resample, the main thing I am trying to do here is to reduce my 111-frames long vector into a 100-frames long one, while retaining as much of the data as possible.
Thanks.

Accepted Answer

Star Strider
Star Strider on 28 Aug 2016
The resample function is intended for signal processing applications, and applies an FIR anti-aliasing filter to prevent aliasing. This is particularly important if you are upsampling a signal. If you simply want to equalise vector lengths, use the interp1 function.
  3 Comments
Star Strider
Star Strider on 28 Aug 2016
My pleasure.
I’ve used both, being aware of their intended applications. (For example, use resample for a signal, and interp1 for the accompanying time vector, to make both equal length and appropriate.)
Deepak George Thomas
Deepak George Thomas on 23 Apr 2019
Hello Star Strider,
Thanks for the comment I am interested in making my time series vectors equal in size and I was wondering if you could provide me some sample code for the description above.

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 28 Aug 2016
Try imresize():
x = rand(1,111);
plot(x, 'bo-');
grid on;
hold on;
% Resize to 100 long
x2 = imresize(x, [1,100]);
plot(x2, 'rd-');
  2 Comments
Mvs Chandrashekhar
Mvs Chandrashekhar on 26 Jan 2021
This worked really nicely for me, where I had two signals at different sample rates, and I wanted to down "sample" the higher sample rate data to line up with the lower rate time vector. It avoided any strange artefacts possible with the anti-aliasing FIR filter.
Mvs Chandrashekhar
Mvs Chandrashekhar on 26 Jan 2021
Edited: Mvs Chandrashekhar on 26 Jan 2021
%how to resize a vector using imresize()
%https://www.mathworks.com/help/images/ref/imresize.html
%for your data, use
%original data
t=0:0.01:10;
x=sin(t);
plot(t, x)
hold on
%resize vector
y=imresize(x, [1,47]); %actual data downsized to 47 length
t_down=imresize(t, [1,47]); %time downsized to 47 length
scatter(t_down, y)

Sign in to comment.


Abdul Manan Khan
Abdul Manan Khan on 3 Aug 2021
Let's say you 'x' low sampled data (let's say time) and corresponding output 'v'. Now, you have another variable 'xq' which is high sampled and you want to plot 'v' with respect to 'xq', then you can use following code.
x = 0:pi/4:2*pi; % low sampled data
xq = 0:pi/16:2*pi; % high sampled data
v = sin(x); % result from low sampled data
vq2 = interp1(x,v,xq,'spline'); % high sampled data interpolation
plot(x,v,'o',xq,vq2,':.');
shg

Community Treasure Hunt

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

Start Hunting!