# Implementing FIR/IIR "alike" filter in R

I am new to signal processing. I am trying to simulate something similar to IIR/FIR filter with $k$ delays to imitate acoustic echo reflection. The difference equations for FIR and IIR respectively are as follows:

\begin{equation} y(n) = x(n) + \sum_{D=1}^kA(n)x(n-D)+v(n)\;\;\;\;\; (1) \end{equation} \begin{equation} y(n) = x(n) + Ay(n-D(n))+v(n)\;\;\;\;\;\;\;\;\;(2) \end{equation} where $D$ is a delay in samples, the coefficient $A(n)$ describes the changing attenuation related from object reflection and $v(n) ∼ N(0, 10^{−3})$ is the noise.

Equation $(1)$ and $(2)$ can be found on section $VII$, second paragraph of this paper

How to can I implement this? I started by writing the following code in R.

install.packages("signal")
t <- seq(0, 1, len = 100)
x <- rnorm(100) + rnorm(length(t),0,0.001)
y <- filter(Arma(b = 0.1, a = 0.1), x)


Unfortunately, the above approach does not allow me to have $a=0$ or $b=0$.

Remark

Maybe following function is generating equation $(1)$:

fir1(39, 0.3)+rnorm(40,0,0.001)


The second equation perhaps is called flange IIR filter, where the delay is not constant, but changing with time. This effect imitates time stretching of the audio signal caused by moving and changing objects in the room.

# Response to the answer below

The ARMA equation

$$y(n)=\sum_{0}^Ma(m)x(n−m)+\sum_{k=1}^Kb(k)y(n−k)$$

i.e. AR part is as follows:

$$y(n)=\sum_{0}^Ma(m)x(n−m)$$

and MA part is as follows:

$$\sum_{k=1}^Kb(k)y(n−k)$$

which don't match with equations $(1)$ and $(2)$. In equation $(1)$ the coefficient $A$ depends on $n$. The coefficient update at each iteration.

Parameters

t <- seq(1, 4000, by = 1)
x<- sin(2*pi*t*2.3)
A<- rnorm(4000)
v<- rnorm(4000,0,0.001)
k <- 20


Equation $(1)$

for(i in 1:4000){
if (i>k){
y[i]<- x[i]+ A[i]*sum(x[(i-(k-1)):i]) + v[i]
}else{
y[i]<- x[i]+ A[i]*sum(x[1:i])
}
}


Equation $(2)$

for(i in 1:4000){
if (i>k){
y[i]<- x[i]+ A[i]*(y[i-i%%k]) + v[i]
}else{
y[i]<- x[i]+ A[i]*(y) + v[i]
}
}


Does the solution make sense? Or do I need to generate $x(n)$ using FIR and IIR filter?

• Why don't you use signal package to avoid reinventing the wheel? May 10 '18 at 13:00
• Can you please help me on generating above filters (2 difference equations) using signal package? May 10 '18 at 13:52
• Check page 23 in this file. The function filter() does exactly what you are asking for. May 10 '18 at 15:45
• Thank you. I think this takes care of the FIR filter, and I will read the document and hopefully will be able to implement both filters. May 10 '18 at 15:54
• hay, do you wanna define what you mean by "IRR"? May 10 '18 at 18:32

The function filter() in the signal package does exactly what you are asking for. Note that, by default, it uses ARMA filters (a combination of AR and MA). Thanks to that, you can implement both FIR and IIR filters, because the difference equation for an ARMA model is:
$$y(n) = \sum_{m=0}^{M} a(m)x(n-m)+\sum_{k=1}^{K} b(k)y(n-k)$$
To achieve your FIR filter, note that it is enough to set all $b$ coefficients to $0$. For the IIR filter, you should set $a(0)=1$ and every other value of $a$ to $0$.
Another maybe obvious yet important observation is that you can add the noise $v(n)$ at the end of the script, because the noise is white (at least that's what I understand from your question). Thus, there is no correlation between past and present samples of the noise, letting you adding it after applying the filter.
• Thank you for the answer. I tried to implement what you suggested. Maybe it's me not understanding correctly. I don't get a sensible answer by setting the values of $a=0$ or $b=0$. Following is the what I did t <- seq(0, 1, len = 100);x <- rnorm(50) + 0.25*rnorm(length(t));z <- filter(Arma(b = 0.1, a = 0.1), x)  May 10 '18 at 17:16