Xǔ Yuè
Xǔ Yuè

Reputation: 21

resample function of MATLAB signal processing toolbox does not work with "linear" method

I'm trying to use the resample function, and I've found the following in the document of MATLAB signal processing toolbox (https://www.mathworks.com/help/signal/ref/resample.html#d117e155565):

The following is my code:

clear;

a = [1, 2, 3, 2, 0.5]; %original signal
tx = [0, 1, 2, 3, 4];
fs = 20; % resample frequency
[a_rs, ty] = resample(a, tx, fs, 'linear');

plot(tx, a, 'o', ty, a_rs, '.');
legend('original', 'resampled');

What I got is the following figure:

resampled signal vs original signal

Obviously it was not 'linearly' interpolated from the original signal. Instead, it seems a low pass filter was applied. Can anyone tell me what's wrong here? Thanks a lot?

Upvotes: 2

Views: 916

Answers (1)

Daniel
Daniel

Reputation: 36710

The documentation is somewhat difficult to read. You probably found:

y = resample(x,tx,___,method) specifies the interpolation method along with any of the arguments from previous syntaxes in this group. The interpolation method can be 'linear', 'pchip', or 'spline'.

Note: If x is not slowly varying, consider using interp1 with the 'pchip' interpolation method. It interpolates x using linear interpolation, not

So you have to look up where it actually uses interpolation. The resampling itself will be done by upfirdn, not by interpolation. For your call syntax the relevant sentence is:

The function interpolates x linearly onto a vector of uniformly spaced instants with the same endpoints and number of samples as tx. NaNs are treated as missing data and are ignored.

It interpolates prior to resampling with upfirdn. For this interpolation, you can change the method. I don't know the specific reason for the behaviour, but the function does what it is documented to do.

Upvotes: 1

Related Questions