Downsampling array data from counts per minute to counts per hour

2 vues (au cours des 30 derniers jours)
dormant
dormant le 23 Oct 2024
Réponse apportée : Harsh le 24 Oct 2024
I have an evenly sampled array of data, say chickensPerMinute.
I want to downsample this by a factor of 60 to return chickensPerHour.
Is there a MATLAB function to do this, akin to the downsample function which returns mean values for each downsampling window.

Réponse acceptée

Star Strider
Star Strider le 23 Oct 2024
That depends on what you want to do.
I would use a timetable and the retime function —
Time = datetime(2004,10,23,17,00,00)+minutes(0:299).';
Chicken_Rate = randi(9,numel(Time),1);
TT_Chickens = timetable(Time,Chicken_Rate)
TT_Chickens = 300x1 timetable
Time Chicken_Rate ____________________ ____________ 23-Oct-2004 17:00:00 5 23-Oct-2004 17:01:00 7 23-Oct-2004 17:02:00 7 23-Oct-2004 17:03:00 2 23-Oct-2004 17:04:00 5 23-Oct-2004 17:05:00 6 23-Oct-2004 17:06:00 4 23-Oct-2004 17:07:00 2 23-Oct-2004 17:08:00 5 23-Oct-2004 17:09:00 4 23-Oct-2004 17:10:00 8 23-Oct-2004 17:11:00 5 23-Oct-2004 17:12:00 3 23-Oct-2004 17:13:00 9 23-Oct-2004 17:14:00 7 23-Oct-2004 17:15:00 4
TT_Chickens_Hourly = retime(TT_Chickens,'hourly','sum')
TT_Chickens_Hourly = 5x1 timetable
Time Chicken_Rate ____________________ ____________ 23-Oct-2004 17:00:00 326 23-Oct-2004 18:00:00 296 23-Oct-2004 19:00:00 266 23-Oct-2004 20:00:00 281 23-Oct-2004 21:00:00 303
.
  2 commentaires
dormant
dormant le 24 Oct 2024
Many thanks for introducing me to a new way of MATLAB. timetable looks as if it could also be useful in other scripts,
Star Strider
Star Strider le 24 Oct 2024
As always, my pleasure!
The retime function is extremely useful for this and other operations. Another is the synchronize function.

Connectez-vous pour commenter.

Plus de réponses (3)

ScottB
ScottB le 23 Oct 2024
if A represents chickensPerMinute then
B = A(1:60:end); should downsample to chickensPerHour just by indexing.
  1 commentaire
dormant
dormant le 24 Oct 2024
That doesn't work:
>> A = [ ones(1,90) zeros(1,120) 2*ones(1,150) ];
>> B = A(1:60:end)
B =
1 1 0 0 2 2

Connectez-vous pour commenter.


Steven Lord
Steven Lord le 23 Oct 2024
Another potential approach would be to use the groupsummary function.
  1 commentaire
dormant
dormant le 24 Oct 2024
Thanks, I've used groupsummary before, but forgotten about it.

Connectez-vous pour commenter.


Harsh
Harsh le 24 Oct 2024
Hi dormant,
There can be multiple ways to downsample your data depending on the initial data type. But if your input is just an evenly sampled array you can use the “reshape” and “mean” functions in MATLAB to achieve your desired result. Below is an example code that illustrates how to do so:
% An array containing chicken values for 120 minutes
chickensPerMinute = randi([1 10],1,120)
chickensPerMinute = 1×120
1 1 8 1 2 6 3 8 3 5 5 6 10 9 5 5 8 4 10 6 2 8 6 1 6 7 3 1 8 10
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% each column in reshaped array corresponds to chickens in the respective hour
reshapedArr = reshape(chickensPerMinute,60,[])
reshapedArr = 60×2
1 1 1 9 8 9 1 9 2 5 6 1 3 1 8 9 3 8 5 8
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% taking mean in column direction gives the avg chickens per hour
chickensPerHour = mean(reshapedArr,1)
chickensPerHour = 1×2
5.5000 5.2000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
You can look at the following documentation link to understand how “reshape” function works - https://www.mathworks.com/help/matlab/ref/double.reshape.html

Produits


Version

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by