# Least Squares Approximation for FIR Filter Design

Following this paper , I am trying to make a least-squares algorithm in MATLAB, but for type I (I know about firls()).

N = 41;
M = (N-1)/2;

wp=0.2;
ws = 0.4;
K = 2;
fp = wp/2;
fs = ws/2;
q= [fp+K*(1-fs), (fp*sinc(fp*[1:2*M])-K*fs*sinc(fs*[1:2*M]))];
Q1 = toeplitz(q([0:M]+1));
Q2 = hankel(q([0:M]+1),q([M:2*M]+1));
Q = (Q1 + Q2)/2;
b = fp*sinc(fp*[0:M]');
a = Q\b;
h = [a(M+1:-1:2)/2; a(1); a(2:M)/2]

• See these links: dsp.stackexchange.com/questions/40754/… , and savannah.gnu.org/bugs/index.php?51310 . They are for Octave, but they should work in Matlab, too. (Did you also mean you need it for type II, instead of I? I'm asking because the paper already is for type I.) – a concerned citizen Nov 2 '17 at 11:24
• yes it is for type I but the problem in the desired response, in paper the passe band(pb) is between [0 w0] and the stop band(sb) [w0 pi],how to do in case if pb=[-wp1 wp2 ] ,the sb1=[-pi ws1] and sb2=[ws2 pi]. – N.na91 Nov 2 '17 at 12:39
• Ah, I see now. This paper's LS algorithm deals without a specified transition band (what you want with separate wp and ws, instead of a single wc), but there are some other papers where you can specify it: search for "constrained least squares", and you should find other papers from Selesnick and Burrus (and probably others). Still, this paper will get you the desired wp and ws the way it's calculated, but you got something wrong in there: fp=/=wp/2, but fp=wp/pi`. It's possible you got wrong results because of this. – a concerned citizen Nov 3 '17 at 7:05
• In the 'constrained least squares FIR filter' must be used 'fircls',so use the functions 'fdesign.bandpass' and 'design' – N.na91 Nov 3 '17 at 13:48
• Thank you for your feedback,but in the 'constrained least squares FIR filter' must be used 'fircls',so use the functions 'fdesign.bandpass' and 'design' ,how to use it in this program matlab. – N.na91 Nov 3 '17 at 14:08