Index exceeds the number of array elements error

I am trying to do a recursion operation to get the output of a butterworth filter (A, B) 'y', where 'x' is the vector of a soundfile. I am getting the error 'Index exceeds the number of array elements error. Index must not exceed 28153' (28153 is the length of x) in line 17 and i was wondering if anyone could spot what is going wrong in the code?
Thanks in advance!
a = A
b = [0 B]
y0 = 0
x0 = 0
n = 1:length(x)
z = ones(1, length(n))
y1 = recur(a, b, n, z, x0, y0)
function y = recur(a,b,n,z,x0,y0)
N = length(a);
M = length(b)-1;
y = [y0 zeros(1,length(n))];
z = [x0 z];
a1 = a(length(a):-1:1); % reverses the elements in a
b1 = b(length(b):-1:1);
for i=N+1:N+length(n),
y(i) = -a1*y(i-N:i-1)' + b1*z(i-N:i-N+M)';
end
y = y(N+1:N+length(n));
end

1 件のコメント

Dyuman Joshi
Dyuman Joshi 2023 年 11 月 3 日
Where do you get the error, in which line?
Also copy and paste the full error message i.e. all of the red text.
There are many undefined parameters in your code, so we can not run your code and see where the error occurs.

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

回答 (1 件)

Torsten
Torsten 2023 年 11 月 3 日

0 投票

y(i-N:i-1) is a vector of length N
z(i-N:i-N+M) is a vector of length M+1
y(i) is a single value of length 1
How do you imagine
y(i) = -a1*y(i-N:i-1)' + b1*z(i-N:i-N+M)';
should work ?

1 件のコメント

Dyuman Joshi
Dyuman Joshi 2023 年 11 月 3 日
The dimensions of a and b are not known as well, the matrix multiplication might be not be possible.

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

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

製品

リリース

R2023a

質問済み:

2023 年 11 月 3 日

コメント済み:

2023 年 11 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by