# FIR design using firpm (Parks-Mclellan)

Can you please help me with some ideas on how to implement an FIR filter in Matlab using Parks-Mclellan algorithm?

My specifications are the passband cut-off frequency, the transition bandwidth. The I also have the passband ripple, which is an alternating value $\delta_p=\pm0.2$ and the stopband ripple $\delta_s$. This because the weigth in the stopband frequency range comes from: $\rho(\omega)=\delta_p/\delta_s$. The design using firpm should meet these specifications.

I have two troubles: The first one is that I have to find the optimal minimum order of the filter N to get the desired design without using any Matlab builtin function (I saw there is one called firpmord, but again I can't use it), so I imagine I need some kind of iteration but don't know how to put this in code. Also can't find a way to account for the alternating ripple to use it in my calculations. I'm just looking for some advice on how to achieve this.

I'm just starting, trying to catch it:

 wp=0.1; w_tr=0.3; w_stop=1; Gp=1; dp=0.2; % this should be plus minus ds=0.05; N=100 h=firpm(N,[0 wp w_tr w_stop],[Gp Gp 0 0],[1,dp/ds]) [h_m,w]=freqz(h,1); semilogy(w,abs(h_m));

First of all make sure that your $\delta_p$ value is linear (and not in dB). The function firpm needs a linear value. Don't worry about the passband error ripple having both signs; the value of $\delta_p$ used to compute the weighting function is just the maximum error magnitude, which is $0.2$ in your case. So you correctly defined the stopband weight by $0.2/0.05=4$.
The question is if you need to write an algorithm that automatically finds the necessary filter order, or if you can just try to find the order manually. If you need an automatic procedure you could use the second output argument of [h,err]=firpm(...), which is the maximum approximation error. You could design filters with increasing orders in a loop until the error returned as a second argument from firpm is smaller than the desired value for $\delta_p$ (because the approximation error equals weight times deviation, and since you chose the passband weight to be $1$, the error equals $\delta_p$).
Finally, note that the stopband edge equals the passband edge plus the transition bandwidth. So if the transition bandwidth is really $0.3$, as in your example, then the stopband edge would be the cut-off frequency (passband edge) plus $0.3$ (which equals $0.4$ in your example). These specifications are extremely mild and can be satisfied by a filter of very low order.