# Manually Windowing an IIR Filter in Matlab

I am currently trying to window an analog IIR filter in order to obtain an FIR filter. Though I realize the fir1 function exists within Matlab, I was wondering to see if it could be done step-by-step to produce a notch filter at 0 Hz to eliminate any DC gains in the system. However, I quickly realized that I may not know what I'm doing :)

Firstly, I created a butterworth filter containing the characteristics that I'm looking for just for a comparison. Once the Chebyshev II analog filter was created, the magnitude and phase response was viewed. It doesn't show a bandstop at 0 Hz, but my guess is because it hasn't yet been windowed to show this result? I also wasn't too sure how to combine the window and analog filter, and would love it if someone pointed me in the right direction :(

Fs = 250;
n = 5;
Rs = 40;
Wn = [0.1*2/Fs, 0.2*2/Fs];
[b0,a0] = butter(3,Wn,'stop');
[b1,a1] = cheby2(n,Rs,Wn,'stop','s');
freqz(b0, a0, 4096, Fs);
w = triang(250);
y = filter(b1,a1,w);
• Would you like to see a very easy way to create a DC IIR notch filter, or is this approach you are taking educational for you? – Dan Boschen Mar 31 '17 at 21:32
• @Fat32 Perfect, thank you for your help Fat32! Everything has become much clearer for me moving forward :) – Andrew T Mar 31 '17 at 22:43
• @Fat32 my solution doesn't answer his question but your response does-- you should paste it as the answer! I think I already posted the notch filter solution elsewhere anyway unless that was the passband version...but this would be the first order equivalent so even simpler. – Dan Boschen Mar 31 '17 at 22:55
• Ok @DanBoschen this time I'll make an answer from the comments... (In fact I do put comments when I don't want to post an answer, but anyway, let this be an exception!) – Fat32 Mar 31 '17 at 23:01
• @Fat32 Look how happy and excited you made him? It's a great answer! – Dan Boschen Mar 31 '17 at 23:20

In the classical world of digital filters, an infinite impulse response (IIR) filter is used for its computational efficiency while achieving supreme performance compared to a finite impulse response (FIR) filter.

Usually, implementing an IIR filter happens in the time domain, with an architecture of direct form-I, II or else which provides a solution of the linear constant coefficient difference equation (LCCDE) describing the filter.

$$\sum_{k=0}^{N} a[k]y[n-k] = \sum_{k=0}^{M} b[k]x[n-k]$$

which is typically called an $N^{th}$ order LCCDE.

However, when there happens a need for implementing the IIR filter using time domain convolution instead;

$$y[n] = h[n] \star x[n]$$

then this requires the practical availability of the impulse response $h[n]$ of the IIR filter which is infinite length by definition.

To obtain a finite length, FIR, approximation $h[n]$ to the IIR digital filter, first choose your analog prototype filter, then apply the necessary transforms to get the digital IIR equivalent filter coefficients $a[k]$ and $b[k]$, and compute the truncated impulse response $h[n]$ of this filter, from $a[k]$ and $b[k]$ to a length that's adequate for the purpose of application and finally apply a further windowing to $h[n]$ if that's necessary...

The following MATLAB/OCTAVE command will give you a length $N$ truncated impulse response from IIR coefficients $a[k]$ and $b[k]$ with zero initial conditions.

h = filter(b,a,[1 zeros(1,N-1)]);
• Ah, an even better well-formulated answer that helps me even further! Thanks again for all of your help, my mind was jumbled for a bit and you got me back on the right path and then some :) – Andrew T Apr 1 '17 at 18:58

a DC-blocking filter is a high-pass filter. i wouldn't use the term "notch filter at 0 Hz" for it. usually, notch filters are for frequencies greater than zero and that means there are two notches at frequencies that are negatives of each other.

usually the DC-blocking high-pass filter is a discrete-time differentiator (has a zero at $z=1$) followed by a leaky integrator. a leaky integrator is a LPF. so you can window off just the LPF response to get an FIR and then pass that FIR impulse response through the differentiator (since that is FIR or length 2) and get another FIR that is one sample longer.

• Potato, poetaughto :) Technicalities aside, it seems you were able to decrypt my intentions fine ;) I completely agree with your solution! But I wanted to tackle filters from this angle so that I could get a better understanding of the process of design and how/why the steps are related :) Though, I have a further question for you - if a filter needed to be implemented to eliminate a 60 Hz signal from the power-lines. Would you recommend a certain way over others regarding the design for this? – Andrew T Apr 1 '17 at 19:50
• notch IIR filters are the most common way to eliminate a single frequency component. and IIR high-pass filters are the most common way to eliminate DC. – robert bristow-johnson Apr 1 '17 at 20:55