フィルターのクリア

Recursive vector operation without for loops

3 ビュー (過去 30 日間)
Herbert
Herbert 2013 年 7 月 11 日
Sometimes one want to create a vector from some recursive expression, for example when we have fibonacci:
x(0)=0, x(1)=1, x(i)=x(i-1)+x(i-2)
Is there a function in matlab which creates arrays recursively, for example:
fib = recursivevector(start=[0,1], expr=@(i,x) x(1)+x(2), len=20)
Or, a bit more complicated, we already have some vector and want to change it recursively, as is done in a exponential smoothing filter:
x2(1) = x(1), x2(i) = a*x(i) + (1-a)*x2(i-1)
Which we could then execute as:
denoised = recursivevector(start=[noised(1)], expr=@(i,x) a*x(1)+(1-a)*noised(2), len=length(noised))
In for-loops this would go like this:
function y = exponentialdenoise(x,a)
y=zeros(size(x));
y(1)=x(1);
for i=2:length(x)
y(i)=a*x(i)+(1-a)*y(i-1);
end
end
I strongly dislike the for-loops ;) of course and prefer a version without them.

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 7 月 11 日
編集済み: Azzi Abdelmalek 2013 年 7 月 11 日
Your equation can be written as:
%x(i)-x(i-1)-x(i-2)=u(i) with u(i)=0
D=[1 -1 -1];
N=1
y=filter(N,D,zeros(1,10),[0 1])
  4 件のコメント
Herbert
Herbert 2013 年 7 月 11 日
編集済み: Herbert 2013 年 7 月 11 日
I resolved it as:
exponentialdenoise = @(x, a) filter([a 0], [1, -(1-a)], x, 0)
Which is equivalent to what you say (with a factor -1), only my initial condition (which I determined empirically) needs to be 0 to set y(1)=x(1).
Jan
Jan 2013 年 7 月 11 日
And if run-time matters, you can try: FEX: FilterM.

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

その他の回答 (1 件)

Jan
Jan 2013 年 7 月 11 日
Why do you dislike loops? They solve problems efficiently. And yes, sometimes there are even faster methods.
function y = exponentialdenoise(x,a)
y = zeros(size(x));
y(1) = x(1);
n = length(x);
y(2:n) = a * x(2:n) - (1-a) * x(1:n-1);
end
  1 件のコメント
Herbert
Herbert 2013 年 7 月 11 日
I made a mistake in my maltab code, sorry, this is not what I am looking for. I want y(i-1) at the right side of the =.

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by