Scaling a vector to a certain number of points
17 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(1 个)
Star Strider
2015-2-26
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 个评论
Star Strider
2015-2-26
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!