필터 지우기
필터 지우기

Calculate average over each specific interval

조회 수: 16 (최근 30일)
Saad Alqahtani
Saad Alqahtani 2021년 7월 7일
댓글: Saad Alqahtani 2021년 7월 8일
Hi,
I'm trying to calculate the mean every 5 seconds of data till the end.
so x= (1,63369) % my data in Newton Meter which is about 30.9419 seconds long
t = 5*2048 % 5 seconds interval.
I'd like to have your help in (1- how to make it calculate the mean in increaments from start till the end 2- how to deal with the last posrtion of the data that shorter than 5 seconds)
any help would be appreciate it. Thanks in advance.
  댓글 수: 2
Image Analyst
Image Analyst 2021년 7월 7일
Please attach your time and signal data (I'm guessing it's t and x) in a .mat file with the paperclip icon
save('answers.mat', 't', 'x');
so we can try some things.
Saad Alqahtani
Saad Alqahtani 2021년 7월 7일
Definitely. Please see attachment. Thanks!

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

채택된 답변

dpb
dpb 2021년 7월 7일
편집: dpb 2021년 7월 8일
2) first -- how to deal with the last posrtion of the data that shorter than 5 seconds"
How do you WANT to deal with it? You can either ignore it and have fix(numel/5) samples or average the values that do have in the last segment -- it's your choice.
1) The classic MATLAB way is to reshape() and use mean() --
a) Ignore last partial segment --
N=5; % number elements to average over
mnX=mean(reshape(x(1:N*fix(numel(x)/N)),N,[])); % average over N elements ignoring end values
b) Deal with odd end
x=[x nan(1,N*ceil(numel(x)/N)-numel(x))]; % augment vector to make even
mnX=mean(reshape(x,N,[]),1,'omitnan'); % average over N elements ignoring NaN elements
  댓글 수: 3
dpb
dpb 2021년 7월 8일
Must've made a typo somewhere; works for me---
>> x=rand(1,63369);
>> numel(x)
ans =
63369
>> N
N =
5
>> ceil(numel(x)/5)
ans =
12674
>> N*ceil(numel(x)/N)
ans =
63370
>> ans/N
ans =
12674
>> x=[x nan(1,N*ceil(numel(x)/N)-numel(x))];
>> numel(x)
ans =
63370
>> mnX=mean(reshape(x(1:5*fix(numel(x)/5)),5,[]));
>> whos mnX
Name Size Bytes Class Attributes
mnX 1x12674 101392 double
>>
Saad Alqahtani
Saad Alqahtani 2021년 7월 8일
(b) works now as well. It looks like I had some typo somewhere. Thank you so much. I really appreciate your help!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by