can you help me fix this moving average filter it keeps giving me errors, I suppose to design a moving average filter from the filter equation bellow

7 views (last 30 days)
smoothed=MovingAvgSmoothing(absval,Length1);
plot(smoothed)
function smoothed = MovingAvgSmoothing(data, M)
% Initialize the smoothed output variable
% windowWidth = M
% smoothed = movmean(data, windowWidth);
l=length(data)
smoothed =zeros(size(data))
window_half = (M-1)/2;
for n = 0:.0001:l
lower_window = n - window_half;
upper_window = n + window_half;
sm=sum(table2array(data(lower_window:upper_window)))
%Calculate the moving average for each sample of data using a window length M
if (upper_window >=l)&&(lower_window <=0)
lower_window =1 ;
smoothed(n) =( 1/M*sum(data(lower_window:upper_window)));
elseif (upper_window >= l) && (lower_window > 0)
upper_window = l;
smoothed(n) =( 1/M*sum(data(lower_window:upper_window)));
else
smoothed(n)= 1/M*sm;
end
end
end

Answers (2)

David Hill
David Hill on 8 Sep 2022
Did you look at movmean
r=randi(100,1,100);
M=6;
m=movmean(r,M,'Endpoints','discard')
m = 1×95
53.6667 51.8333 41.5000 42.5000 49.3333 56.3333 64.5000 68.3333 82.0000 75.0000 63.6667 56.5000 51.1667 41.5000 39.5000 51.1667 61.5000 70.0000 62.5000 58.3333 47.8333 39.6667 33.6667 29.0000 38.5000 43.1667 45.1667 53.5000 56.1667 52.0000
  1 Comment
David Hill
David Hill on 9 Sep 2022
absval=rand(9000,1);
M=10;
for n=1:numel(absval)
lower_window=max(1,n-M/2);
upper_window=min(numel(absval),n+M/2);%does not discard the endpoints
absval_movmean(n)=mean(absval(lower_window:upper_window));
end
absval_movmean(1:20)
ans = 1×20
0.6059 0.5506 0.4849 0.5025 0.4918 0.4625 0.5000 0.4483 0.3915 0.3331 0.3839 0.3936 0.3748 0.4412 0.4000 0.4519 0.4878 0.4694 0.4580 0.5185
compare
m=movmean(absval,11)';
m(1:20)
ans = 1×20
0.6059 0.5506 0.4849 0.5025 0.4918 0.4625 0.5000 0.4483 0.3915 0.3331 0.3839 0.3936 0.3748 0.4412 0.4000 0.4519 0.4878 0.4694 0.4580 0.5185

Sign in to comment.


Walter Roberson
Walter Roberson on 8 Sep 2022
window_half = (M-1)/2;
You are controlling M from a slider, so it will not always be odd valued, so window_half will not always be an integer. For your example it would be (247-1)/2 = 123 which is okay in itself.
for n = 0:.0001:l
n is going to be fractions, l*10000 + 1 different values starting with 0 then 0.0001 and so on
lower_window = n - window_half;
0 minus 123 is negative 123
sm=sum(table2array(data(lower_window:upper_window)))
lower_window is negative, so lower_window:upper_window is going to include a bunch of negative values. You use those negative values as indices into data, which is an error.
We cannot tell for sure from your images what the datatype of absval is, but it seems most likely that it is double precision, so data is likely to be double precision. If data(lower_window:upper_window) were successful then data(lower_window:upperwindow) would be a double precision array. But you try to table2array() it which would be a problem if data is double precision.
Is it possible that data is a table? No, it is not: when you index a table using () indices, you must index with separate row and column. It it were a table then
sm=sum(table2array(data(lower_window:upper_window, :)));
would potentially be valid. But if it were a table with multiple variables then be sure to specify the dimension to sum along.
Your indexing of data should be by integers, such as 1:l and not according to time like 0.0001 .
You need to decide what to do at the beginning of the signal and at the end of the signal. Do you want to skip doing the average until you can fill a full window behind the current point? Do you want to always use a full window's worth before the current point, by padding with leading zeros for data that does not exist? Do you want to include whatever is available in the window, making sure that you take into account the number of actual samples available when you take the mean?
  3 Comments
Walter Roberson
Walter Roberson on 9 Sep 2022
No, I cannot suggest code revisions until you tell us how you want to deal with the issues I raised in the paragraph starting "You need to decide what to do at the beginning of the signal and at the end of the signal."

Sign in to comment.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!