Downsampling array data from counts per minute to counts per hour

조회 수: 3 (최근 30일)
dormant
dormant 2024년 10월 23일
답변: Harsh 2024년 10월 24일
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일
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일
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일
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일
if A represents chickensPerMinute then
B = A(1:60:end); should downsample to chickensPerHour just by indexing.
  댓글 수: 1
dormant
dormant 2024년 10월 24일
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

댓글을 달려면 로그인하십시오.


Steven Lord
Steven Lord 2024년 10월 23일
Another potential approach would be to use the groupsummary function.

Harsh
Harsh 2024년 10월 24일
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