MATLAB: Error in script Subscript indices must either be real positive integers or logicals.

error in script subscript indices must either be real positive integers or logicals.

%a
w = 0.44*pi;
w1 = 0.3*pi;
w2 = 0.7*pi;
l=10;
a=2/l;
nn=0:l;
ww = -pi:(pi/100):pi; %-- omega hat
h = a*cos(w*nn);
h1 = a*cos(w1*nn);
h2 = a*cos(w2*nn);
hh=conv(h,h1);
hh1=conv(hh,h2);
hhh=freqz(ww,1,hh1)
plot(ww,abs(hhh(ww)))
This is my code, i got error at last line and i don't know how to fix that. Can anyone tell me how to fix it. Thanks

Best Answer

ww = -pi:(pi/100):pi; %-- omega hat
after the above line, ww is a vector of values that are not going to be positive integers.
hhh=freqz(ww,1,hh1)
"h - Frequency response, returned as a vector. If you specify n, h has length n. If you do not specify n, or specify n as the empty vector, h has length 512."
Therefore after the above call, hhh is a vector.
plot(ww,abs(hhh(ww)))
hhh(ww) is a request to index vector hhh at locations ww. However, ww is not positive integers, so the indexing fails.
I would suggest you just want
plot(ww, abs(hhh))