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.

Edit after the answer

install.packages("signal")
t &lt;- seq(0, 1, len = 100)
x &lt;- rnorm(100) + rnorm(length(t),0,0.001)
y &lt;- 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.

Probably an answer

Parameters

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


Equation $(1)$

for(i in 1:4000){
  if (i&gt;k){
    y[i]&lt;- x[i]+ A[i]*sum(x[(i-(k-1)):i]) + v[i]
  }else{
    y[i]&lt;- x[i]+ A[i]*sum(x[1:i]) 
  }
}


Equation $(2)$   

for(i in 1:4000){
  if (i&gt;k){
    y[i]&lt;- x[i]+ A[i]*(y[i-i%%k]) + v[i]
  }else{
    y[i]&lt;- x[i]+ A[i]*(y[1]) + v[i]
    }
}


Does the solution make sense? Or do I need to generate $x(n)$ using FIR and IIR filter?
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.