Calcuating equally-spaced sums from unevenly-spaced time-series data

2 vues (au cours des 30 derniers jours)
dormant
dormant le 4 Août 2022
Modifié(e) : Cris LaPierre le 5 Août 2022
I need to create an equally-spaced vector of values which are the sums from a vector of unequally-spaced values.
More specifically, I have a file which has the number of bytes received and the time they were received. There are usually more than a million lines in the file. I want to plot the data rate every five minutes. To make things more difficult, the timestamps might not always be in order.
This is my code so far. I could write a loop to populate kbps, but is there an easier way to do it?
D = textscan( fid, '%f %d\n' );
datim = D{1}; % Posix timestamp
bytes = D{2}; % Number of bytes
t1 = dateshift(datetime(min(datim), 'ConvertFrom', 'posixtime'), 'start','day');
t2 = dateshift(datetime(max(datim), 'ConvertFrom', 'posixtime'), 'end','day');
t = t1:minutes(5):t2;
kbps = nan( size(t) );
And here are the variables:

Réponse acceptée

Cris LaPierre
Cris LaPierre le 4 Août 2022
Modifié(e) : Cris LaPierre le 5 Août 2022
I would put your datetime and bytes into a table (consider cell2table). Convert your times to datetime, and then use sortrows to place them in order (if desired).
With the data in a table, you can use groupsummary to compute the number of datapoints in bins you specify, where groupbins is the bin edges.
You could use the data you already have captured in t for your edges:
Consider sharing your data and we can test actual code. You can attach it using the paperclip icon.
  4 commentaires
dormant
dormant le 5 Août 2022
Here is my working code. Many, many, many thanks.
D = readtable( fileSniff );
D = sortrows(D,1);
D.Var1 = datetime( D.Var1, 'ConvertFrom', 'posixtime' );
tBeg = dateshift(D.Var1(1), 'start', 'hour');
tEnd = dateshift(D.Var1(end), 'end', 'hour');
T = tBeg:minutes(5):tEnd;
G = groupsummary(D,'Var1',T,'sum');
Cris LaPierre
Cris LaPierre le 5 Août 2022
Modifié(e) : Cris LaPierre le 5 Août 2022
Nice job and nice solution. You are very welcome.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by