How can I add zeros to a vector to make a continuous time axis from a discontinuous one?
5 views (last 30 days)
Show older comments
I have need to create a continuous time axis in milliseconds to by adding zeros to a vector where data is missing. If you load the file, you can see
time =
'3225 sec'
'3226 sec'
'3879.9 sec'
'3880.9 sec'
dat =
4×3 timetable
Record Time ECG1 ECG2 ECG1_ECG2
___________ _______________ _______________ _______________
3225 sec {1000×1 double} {1000×1 double} {1000×1 double}
3226 sec {1000×1 double} {1000×1 double} {1000×1 double}
3879.9 sec {1000×1 double} {1000×1 double} {1000×1 double}
3880.9 sec {1000×1 double} {1000×1 double} {1000×1 double}
Data was recorded at 1kHz but some data was lost. In the end, I need to save the EKG1 variable as a double in milliseconds. I am trying to upsample the time vector to milliseconds and add zeros to the data axis where it's missing
load('sample.mat');
EKG = cat(2,cell2mat(dat{:,1}),cell2mat(dat{:,2}),cell2mat(dat{:,3})); % Convert timetable to matrix
EKG1 = [EKG(1:2000,:); zeros(653900,3); EKG(2001:end,:)]; % Add some zeros to the middle manually
recorded_times = dat.Properties.RowTimes; % extract the record times
ms=milliseconds(recorded_times(1):recorded_times(end))'; % I am trying to convert the axis to milliseconds but it doesn't work
I can see the amount of missing time from recorded_times with diff()
diff(time)
ans =
3×1 duration array
1 sec
653.85 sec
1 sec
This seems to work great except for being off by 0.05 seconds, but then
find(diff(time)>1)
ans =
0×1 empty double column vector
doesn't work seemingly because this is a duration? Not sure. I just need to add 653.9 milliseconds worth of zeros to my EKG variable, which I have done manually but I need to repeat this process for multiple datasets, sometimes with multiple spots of missing data.
0 Comments
Answers (1)
Star Strider
on 10 Dec 2021
I‘m not certain what data are in the table, however the first column appears to be the length of the recordings. There do not appear to be any missing values in the two EKG records that I looked at, however since there is no accompanying time vector for the EKG traces, it isn’t possible to determine if any EKG data are missing. And wth no time vector, interpolation is impossible, since there are no gaps to fill.
If a time vector for each record were present., the Signal Processing Toolbox resample function would be the preferred interpolation method, since it includes an anti-aliasing filter. This is important if signal processing is the ultimate goal.
.
10 Comments
Dustin7
on 11 Dec 2021
You would be correct if random samples were missing, but this isn't the case. I know that the data is missing between 3226.999 and 3879.9 seconds and I can make a time axis based on knowing the sampling rate of 1 kHz. Matlab reads data in EDF+D format as in the sample.mat in my orignal post, i.e., without the full time vector, which needs to be created anyway.
Star Strider
on 11 Dec 2021
It would certainly be possible to careat a time vector that spanned the entire time the EKG is supposed to be recorded, and then calculate the indices that were actually recorded and plot them against the full time vector, however filling the missing information would be impossible.
Another trivial example —
t1 = [1:0.1:10 20:0.1:30];
y1 = sin(2*pi*t1*0.1) + randn(size(t1))/10;
figure
plot(y1)
grid
title('Original y_1 Without Time Vector')
t2 = linspace(min(t1), max(t1), t1(end)/((t1(2)-t1(1))));
y2 = interp1(t1, y1, t2);
figure
plot(t2, y2)
grid
title('Interpolated Signal To Fill Missing Region')
.
See Also
Categories
Find more on Logical 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!




