Resampling a vector to change its length

조회 수: 215 (최근 30일)
Guido Ascenso
Guido Ascenso 2016년 8월 28일
답변: Abdul Manan Khan 2021년 8월 3일
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.

채택된 답변

Star Strider
Star Strider 2016년 8월 28일
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
Star Strider
Star Strider 2016년 8월 28일
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 2019년 4월 23일
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.

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

추가 답변 (2개)

Image Analyst
Image Analyst 2016년 8월 28일
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
Mvs Chandrashekhar
Mvs Chandrashekhar 2021년 1월 26일
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 2021년 1월 26일
편집: Mvs Chandrashekhar 2021년 1월 26일
%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)

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


Abdul Manan Khan
Abdul Manan Khan 2021년 8월 3일
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

카테고리

Help CenterFile Exchange에서 Multirate Signal Processing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by