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
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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
0 Commenti
Risposte (2)
David Hill
il 8 Set 2022
Did you look at movmean
r=randi(100,1,100);
M=6;
m=movmean(r,M,'Endpoints','discard')
1 Commento
David Hill
il 9 Set 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)
compare
m=movmean(absval,11)';
m(1:20)
Walter Roberson
il 8 Set 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 Commenti
Walter Roberson
il 9 Set 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."
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!