How to interpolate data with different sample times

8 views (last 30 days)
I'm trying to align two structures such that I can subtract one from the other. Right now this isn't possible due to struct A being longer than Struct B, and we know that two structs need to be the same length to perform operations like subtracting. These structures have a field for time (in datenum format) and a field for value (lets say units of Watts)
The data I'm dealing with is for an entire day (00:00 to 23:59) and the sample times of the two data structs are roughly 50ms. To be exact Struct A has a sample time of 50.06ms according to my calculation and Struct B has a sample time of 50.01ms.. So for a 24hr period for Struct A there are 1723428 samples with a corresponding datenum value, and for Struct B there are 1709086 samples with a corresponding datenum value.
My question is how can I interpolate Struct B such that there are the same amount of samples as Struct A, allowing me to perform the subtraction I want, but without compromosing the data. I'd like to add data points at evenly spaced intervals so that it doesn't mess up anything, Thanks :)
StructA:
StructB:

Answers (2)

dpb
dpb on 3 Sep 2024
Two basic choices
resample in the Signal Processing TB will let you simply resample B by the ratio of the sizes with a phase-maintaining filtering operation...
B.newValue=resample(B.value,numel(A.value),numel(B.value));
or use interp1 and the time data from A
B.newValue=interp1(B.time,B.value,A.time);

Star Strider
Star Strider on 3 Sep 2024
These structures have a field for time (in datenum format) and a field for value (lets say units of Watts)’
I would create them as timetable arrays (it would be easier to convert the datenum values to datetime arrays first, and put those into a table or directly into a timetable) and then use the retime function with an appropriatee ‘dt’ value (the 'TimeStep',dt name-value pair) to create arrays of the same size sampled at the same times. Another option could be to use the synchronize function.
I believe all these are available in R2021b, however I don’t remember exactly when the various options were introduced.

Categories

Find more on Dates and Time in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!