Resampling a vector to change its length

68 ビュー (過去 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 Center および File ExchangeMultirate Signal Processing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by