Downsampling array data from counts per minute to counts per hour

46 次查看(过去 30 天)
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.

采纳的回答

Star Strider
Star Strider 2024-10-23,16:56
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 个评论
dormant
dormant 2024-10-24,12:12
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 2024-10-24,12:13
As always, my pleasure!
The retime function is extremely useful for this and other operations. Another is the synchronize function.

请先登录,再进行评论。

更多回答(3 个)

ScottB
ScottB 2024-10-23,16:41
if A represents chickensPerMinute then
B = A(1:60:end); should downsample to chickensPerHour just by indexing.

Steven Lord
Steven Lord 2024-10-23,17:22
Another potential approach would be to use the groupsummary function.

Harsh Sharma
Harsh Sharma 2024-10-24,15:02
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

类别

Help CenterFile Exchange 中查找有关 Multirate Signal Processing 的更多信息

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by