How can I write an FIR filter function

조회 수: 4 (최근 30일)
Aliyu Suleiman Liman
Aliyu Suleiman Liman 2019년 2월 12일
댓글: Aliyu Suleiman Liman 2019년 2월 12일
Hi! I'm trying to write a function that performs FIR filtering without having to use the built-in function.
so I have this code at the moment.
function y = myFIR(x,h)
M = length(x);
L = length(h);
y = zeros(0,((M+L)-1));
for n = 1:M+1
for k = 1:L+1
Y1 = (h(k).*x(n-k));
y(n)= sum(Y1);
end
end
where (h) is an array consisting of the impulse response, (x) is an array consisting of the input sequence and (y) is a vector containing the filtered samples.
but I have been getting the error below;
Array indices must be positive integers or logical values.
Error in myFIR (line 7)
Y1 = (h(k).*x(n-k));
I have really tried everything I could but it just doesnt seem to go. I will really be glad if anyone can help figure out the problem.
Thank you.
  댓글 수: 2
KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 2월 12일
편집: KALYAN ACHARJYA 2019년 2월 12일
Can you define x and h inputs, examples?
Aliyu Suleiman Liman
Aliyu Suleiman Liman 2019년 2월 12일
Yh sure!
h = { 0.2 0.3 0.2}
x = {1 2 3 4 5 6 7 8 9 10}

댓글을 달려면 로그인하십시오.

답변 (1개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 2월 12일
편집: KALYAN ACHARJYA 2019년 2월 12일
My suggestion to avoid the error, In the loop, during first iterations when when n=1 and k=1, then following statement
Y1 = (h(k).*x(n-k));
x(n-k) becomes x(0), which is not allowed in indexing vectors. x(Positives only)
  댓글 수: 1
Aliyu Suleiman Liman
Aliyu Suleiman Liman 2019년 2월 12일
I tried using (k=0:L) but I was still getting the same error

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Digital Filtering에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by