Scaling a vector to a certain number of points

18 views (last 30 days)
I'd like to scale a vector to a certain sampling rate by interpolating between existing data points. I tried to use
interp(x,r)
where: x = the vector you want to scale, and r = the factor you want to scale x by.
The problem is, r has to be an integer. Any suggestions on how I could do this with a non-integer scaling factor?
Example
x = [0:2:22];
r = 2;
y = interp(x,r);
This return y = [0,1,2,3,...,22] but what if I wanted to use a scale factor r = 2.4?

Answers (1)

Star Strider
Star Strider on 26 Feb 2015
The interp function is part of the Signal Processing Toolbox. To do the interpolation you want to do, I would use the resample function instead. It will accept non-integer factors. There are several ways to create ‘p’ and ‘q’ for that, particularly the rats function.
  2 Comments
Stanley Kubrick
Stanley Kubrick on 26 Feb 2015
Hmm, resample worked but it appears that p and q have to be whole integers. In my case I set p to my desired frequency of 32hz, but had to round my existing frequency. This will get me close enough, but it is fairly peculiar.
Star Strider
Star Strider on 26 Feb 2015
That’s why I suggested rats. For a factor of 2.4 is should be easy enough to use 24 and 10. I haven’t used resample much recently, but it would be my choice.
If you wanted your vector to have 2.4 times as many points but sampled over the same interval [0,22], interp1 might also be an option:
x = [0:22];
y = sin(2*pi*x/22);
xi = linspace(0, 22, ceil(2.4*length(x)));
yi = interp1(x, y, xi);
figure(1)
plot(x, y, 'pb')
hold on
plot(xi, yi, '-r')
hold off
grid
Note: The ‘xi’ vector here is your re-defined ‘x’ vector, using linspace to create it. If that’s all you want to do, you can probably just stop there.
In most instances, there are many ways to get from where you are to where you want to go in MATLAB.

Sign in to comment.

Categories

Find more on Interpolation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!