Scaling a vector to a certain number of points
16 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (1)
Star Strider
el 26 de Feb. de 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 comentarios
Star Strider
el 26 de Feb. de 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.
Ver también
Categorías
Más información sobre Interpolation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!