I want to resample my signal with to new time. Currently the sampling time of my signal is 0.01s
, and the size of my signal and time array is 1*90001
.
I am trying to use resample(x,p,q)
in MATLAB, but I am a little bit confused.
Can somebody suggest the right way to use this function and how to resample my data to rate of 0.02s
instead of 0.01s
?
Code - this is how I am trying to use resample
, with example data.
t = [0:0.03:1];
x = sin(4*pi*t);
y = resample(x, 1, 2);
ty = resample(t,1,2);
figure (1);
stem(ty, y, 'r*');
hold on;
stem(t,x,'b')
hold off
Updated Code :
t = [0 2 3 7 8 9 10 11 12 17 18 19 20 24 25 26 27 28 29 31 32 33 35 37 41 ];
A = [0 0 1 2 3 5.2 0 -1.4 0 2 2.7 2 2.3 6 7.3 0 0 -8.6 0 1 1 2.5 3 4.8 2];
plot(t,A)
% Tx = min(diff(t));
Tx = 1:0.1:25;
B = interp1(t,A,Tx); %re-make example data to have decimal points on the x-axis
y = resample(B, 2, 1);
T = 0.05;
Ty = T / (2 / 1);
ty = (0:length(y)-1)*Ty;
% A = interp1(t,ref,t2);
% A = ref;
figure
plot(Tx,B,'b')
hold on
plot(ty,y,'r')
plot(t,A,'g')
hold off
First of all you do not need to resample time line.
It is much easier to define time sampling interval variable or sampling frequency variable: T = 0.03; Fs = 1/T;
So, x
resampling you perform right way: y = resample(x, 1, 2);
.
But the new time line must be reconstructed via adjusted sampling interval: Ty = T / (1 / 2); ty = (0:length(y)-1)*Ty;
The resample
function is suitable only for uniformly time distributed data points. If your original points are non-uniformly distributed, you need:
- Interpolate your
x
signal to the uniform time line with the smallest sampling interval from the original time line: Tx = min(diff(t));
. See for example interp1
function.
- Resample your interpolated uniformly time distributed (sampled) signal to the new sampling interval (
resample
function).
your original signal is sampled with uniform sampling interval of 10 ms and you want to decrease sampling down to 20 ms. Why don't you just take every second datapoint of your original signal?
y = x(1:2:end);
ty = t(1:2:end);
UPDATE
for non regularly spaced datasets it is possible to use function resample
as it is shown here: https://au.mathworks.com/help/signal/ref/resample.html#bungoxs
you can try
fs = 1/0.02;
[y, ty] = resample(x, t, fs, 1, 2)