FIR Filter 10th Order with for loop

i'm trying to filter a signal with a 10th order differential equation and for loop, with the following code, but i'm keep getting this error
" unable to perform assignment because the indices on th left side are not compatible with the size of the right side"
N=10; % Order of Filter
FG=1;
Wn=FG/(F/2); %Nyquist
a=fir1(N,Wn);
mfilter = zeros(size(LHorizontal));
for k=1:length(LHorizontal)
z = [LHorizontal(k); k(1:N)];
LHorizontal(k) = a * z;
end
a is a 1x11 matrix
LHorizontal is a 6000x1 matrix
for some reason k comes out as "1", i don't know what is causing it (k) to come out as 1 and i believe that is what is causing the above stated error message.
I would be grateful for any Help in locating and fixing the code

 Accepted Answer

I'm not sure why you'd see the error message "[U]nable to perform assignment because the indices on th[e] left side are not compatible with the size of the right side" because I would expect a different error message to show up before that error can happen.
Namely, "Index exceeds matrix dimensions." or "Index exceeds the number of array elements. Index must not exceed 1." on this line:
z = [LHorizontal(k); k(1:N)];
because the expression k(1:N) will cause that error, since k is a scalar and N is 10. Check it out:
N = 10;
k = 1;
k(1:N)
Index exceeds the number of array elements. Index must not exceed 1.
In any case, that the for loop doesn't complete one iteration explains why k is 1 when the code stops.

4 Comments

("Answer" from @N. Alfred moved here:)
firstly thanks.
I have changed the code to this
N=10;
FG=1;
F=100;
Wn=FG/(F/2);
a=fir1(N,Wn);
mfilter = zeros(size(LHorizontal));
for k=1:length(LHorizontal)
z = [LHorizontal(k); z(1:end-1)];
mfilter(k) = a*z;
end
and now i have the error message -_-
"unable to perform assignment because the left and right sides have different number of elements" on the line below
mfilter(k) = a*z;
could you help
What is the value (or at least the size) of z before the code starts?
LHorizontal = rand(6000,1);
N=10;
FG=1;
F=100;
Wn=FG/(F/2);
a=fir1(N,Wn);
mfilter = zeros(size(LHorizontal));
for k=1:length(LHorizontal)
z = [LHorizontal(k); z(1:end-1)];
mfilter(k) = a*z;
end
The end operator must be used within an array index expression.
I get that error because z is not defined yet, in the first iteration of the loop.
z is a new variable i created to store values of an operation in. Same as "a" or "Wn" in this case. Its initial value can be zero.
Thank you.
OK. Now I am able to reproduce the same error:
LHorizontal = rand(6000,1);
z = 0;
N=10;
FG=1;
F=100;
Wn=FG/(F/2);
a=fir1(N,Wn);
mfilter = zeros(size(LHorizontal));
for k=1:length(LHorizontal)
z = [LHorizontal(k); z(1:end-1)];
mfilter(k) = a*z;
end
Unable to perform assignment because the left and right sides have a different number of elements.
The error happens because z is a scalar and a is 11-by-1, so a*z is 11-by-1, and you cannot store a non-scalar vector in a single element of an array, which is what mfilter(k) is.
In other words, here:
mfilter(k) = a*z;
the left-hand side is a single element, and the right-hand side is 11 elements, so it won't fit.
If you want to apply the filter with coefficients a to the signal LHorizontal, maybe you should use the filter function or the conv function.

Sign in to comment.

More Answers (0)

Tags

Asked:

on 2 Jun 2022

Commented:

on 2 Jun 2022

Community Treasure Hunt

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

Start Hunting!