create Matlab function to preform a running average filter

12 ビュー (過去 30 日間)
AngelsaAtWar
AngelsaAtWar 2013 年 4 月 19 日
回答済み: Ma sa 2016 年 11 月 28 日
hi there I am clueless on how to create Matlab function to preform a running average filter using the following data:
[1 9 10 11 12 13 14 15 14 13 12 11 10 9 1]
may someone help me please and thank you
  1 件のコメント
Yao Li
Yao Li 2013 年 4 月 19 日
I think it depends on the precision of the filter. Do you want to calculate the mean value of ervery 3 points, 5 points or 7 points, etc?

サインインしてコメントする。

採用された回答

Jan
Jan 2013 年 4 月 19 日
編集済み: Jan 2013 年 4 月 19 日
You can use filter to achieve this:
x = [1 9 10 11 12 13 14 15 14 13 12 11 10 9 1];
n = 3; % Number of elements to be averaged
y = filter(ones(1, n) / n, 1, x);
But perhaps this does not satisfy your demands at the edges. You can do it manually also:
m = (n - 1) / 2;
len = size(x, 2);
y = x;
div = ones(size(x));
for k = 1:m
k2 = 2*k; % Slightly faster
z = zeros(1, k);
y = y + [z, x(1:len - k2) + x(1 + k2:len), z];
div(k + 1:len - k) = div(k + 1:len - k) + 2;
end
y = y ./ div;
Here the first and last elements are not changed, while the 2nd and 2nd last are averaged using 3 elements for all n >= 3.
You will find much more approachs, when you search in the FileExchange. It is a good idea to check, if others have published a solution there, before you ask in the forum.

その他の回答 (1 件)

Ma sa
Ma sa 2016 年 11 月 28 日
How can i creat an average filter of size(3*3)??

カテゴリ

Help Center および File ExchangeAudio Plugin Creation and Hosting についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by