How to up-sample both signals and labels together?

I have ECG-signals and corresponding labels with the sampling rate of 250 Hz (attached).
Attached "sig.mat" is the ECG-signal and "labl.mat" is the labels, where five signals and five labels (5x1 cell).
I want to up-sample these signals and labels coherently (synchronized in time after upsampling) with 360 Hz.
I heard I can use "resample" function for the signal, but I don't know it for the labels.
I may use labeler app, but this is too slow.
I would appreciate it if anyone could help me on how to label automatically in accordance with the up-sampled signals.
Thanks,

 Accepted Answer

%websave("upsamp.mat", "https://www.mathworks.com/matlabcentral/answers/uploaded_files/1642771/upsamp.mat");
websave("upsamp_R.mat", "https://www.mathworks.com/matlabcentral/answers/uploaded_files/1642906/upsamp_R.mat");
load upsamp_R
fs = 250;
fsnew = 360;
for i =1:length(sig)
% signal and label
s = sig{i};
l = labl{i};
% categories
c = categories(l);
% original and new time
t = (0:length(s)-1)/fs;
tnew = t(1):(1/fsnew):t(end);
% interpolation
snew = interp1(t, s, tnew, 'linear');
lnew = interp1(t, double(l), tnew, 'nearest');
% convert back to categorical data
lnew = categorical(lnew, 1:length(c), c);
signew{i, 1} = snew;
lablnew{i, 1} = lnew;
end
sig
sig = 5×1 cell array
{[ -0.4456 -0.3906 -0.4456 -0.5006 -0.4731 -0.5555 -0.5830 -0.6380 -0.5280 -0.5280 -0.6105 -0.6380 -0.5280 -0.6105 -0.5555 -0.5830 -0.5280 -0.5280 -0.5830 -0.5555 -0.4731 … ] (1×5000 double)} {[-0.0973 -0.0211 0.0297 -0.0211 -0.0211 0.0805 0.1567 0.1821 0.1821 0.2329 0.3345 0.3345 0.3599 0.4107 0.4615 0.5123 0.5123 0.5631 0.6139 0.5885 0.5377 0.4869 0.5885 0.6139 … ] (1×5000 double)} {[ 0.2383 0.3253 0.4124 0.4124 0.3834 0.3543 0.4414 0.4704 0.3834 0.4124 0.4704 0.5284 0.4994 0.4124 0.4124 0.4704 0.5284 0.4124 0.4124 0.4704 0.4704 0.3543 0.3253 0.3834 … ] (1×5000 double)} {[ -0.6125 -0.5854 -0.5854 -0.5311 -0.5311 -0.4768 -0.5854 -0.6397 -0.5311 -0.6125 -0.6397 -0.6397 -0.5583 -0.5583 -0.6397 -0.7211 -0.6397 -0.5854 -0.6668 -0.7754 -0.7211 … ] (1×5000 double)} {[ -0.3252 -0.2707 -0.2707 -0.3797 -0.3797 -0.3252 -0.2980 -0.3252 -0.3524 -0.2707 -0.2435 -0.2980 -0.4069 -0.3797 -0.2707 -0.3252 -0.3797 -0.3252 -0.2707 -0.2162 -0.2435 … ] (1×5000 double)}
signew
signew = 5×1 cell array
{[ -0.4456 -0.4074 -0.4120 -0.4502 -0.4883 -0.4876 -0.4868 -0.5441 -0.5708 -0.5968 -0.6349 -0.5677 -0.5280 -0.5303 -0.5876 -0.6220 -0.6258 -0.5494 -0.5693 -0.5998 -0.5616 … ] (1×7199 double)} {[-0.0973 -0.0444 -0.0014 0.0255 -0.0098 -0.0211 -0.0042 0.0664 0.1228 0.1630 0.1807 0.1821 0.1990 0.2357 0.3063 0.3345 0.3373 0.3549 0.3853 0.4206 0.4558 0.4911 0.5123 0.5123 … ] (1×7199 double)} {[ 0.2383 0.2987 0.3592 0.4124 0.4124 0.3987 0.3785 0.3584 0.4027 0.4486 0.4688 0.4148 0.3930 0.4140 0.4543 0.4946 0.5252 0.5051 0.4559 0.4124 0.4124 0.4462 0.4865 0.5268 … ] (1×7199 double)} {[ -0.6125 -0.5937 -0.5854 -0.5809 -0.5432 -0.5311 -0.5221 -0.4844 -0.5372 -0.5990 -0.6367 -0.5703 -0.5583 -0.6133 -0.6321 -0.6397 -0.6306 -0.5741 -0.5583 -0.5741 -0.6306 … ] (1×7199 double)} {[ -0.3252 -0.2874 -0.2707 -0.2798 -0.3555 -0.3797 -0.3706 -0.3328 -0.3101 -0.3048 -0.3237 -0.3426 -0.3252 -0.2700 -0.2510 -0.2662 -0.3101 -0.3858 -0.3933 -0.3585 -0.2828 … ] (1×7199 double)}
labl
labl = 5×1 cell array
{[n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a … ] (1×5000 categorical) } {[n/a P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P … ] (1×5000 categorical) } {[T T T T T T T T T T T T T T T T T T T T T T T T T T T T n/a n/a n/a … ] (1×5000 categorical) } {[n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a … ] (1×5000 categorical) } {[T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T … ] (1×5000 categorical)}
lablnew
lablnew = 5×1 cell array
{[n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a … ] (1×7199 categorical) } {[n/a P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P P … ] (1×7199 categorical) } {[T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T … ] (1×7199 categorical)} {[n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a … ] (1×7199 categorical) } {[T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T … ] (1×7199 categorical)}
whos
Name Size Bytes Class Attributes ans 1x37 74 char c 4x1 432 cell cmdout 1x33 66 char fs 1x1 8 double fsnew 1x1 8 double i 1x1 8 double l 1x5000 5434 categorical labl 5x1 27690 cell lablnew 5x1 38685 cell lnew 1x7199 7633 categorical s 1x5000 40000 double sig 5x1 200520 cell signew 5x1 288480 cell snew 1x7199 57592 double t 1x5000 40000 double tnew 1x7199 57592 double
figure
subplot(211); plot(s)
subplot(212); plot(snew)
figure;
subplot(211); plot(double(l))
subplot(212); plot(double(lnew))

5 Comments

Thank you very much, great!
I realized that I uploaded wrong label data.
I would appreciate it if you would be able to make sure with the new label data although it is no dbout on your work.
Thanks!
It works with new data as updated above.
Mibang
Mibang on 15 Mar 2024
Edited: Mibang on 15 Mar 2024
Great, thanks, I also did it with your code, but deleted because yours is better.
May I ask one? Why is the new data has the length of 7199 instead of 7200?
360/250=1.44
7200/5000 = 1.44, but 7199/5000 = 1.4398.
Are you able to make the length 7200 rather than 7199? Or, 7199 is correct?
Thanks!
Besides, why t(end) ~= tnew(end)?; t(end)==19.9960 and tnew(end)==19.9944.
It is due to the way we produce new time:
t = (0:length(s)-1)/fs;
tnew = t(1):(1/fsnew):t(end);
We don't extend the time after the original end of the time. You can extend it if you like by add a small time after t(end).
I see, that is why...I will think about it more while I accept your answer.
Many thanks!

Sign in to comment.

More Answers (0)

Asked:

on 14 Mar 2024

Commented:

on 15 Mar 2024

Community Treasure Hunt

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

Start Hunting!