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

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$.


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


i.e. AR part is as follows:


and MA part is as follows:


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


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]
    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]
    y[i]<- 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?

  • $\begingroup$ Why don't you use signal package to avoid reinventing the wheel? $\endgroup$
    – Tendero
    May 10, 2018 at 13:00
  • $\begingroup$ Can you please help me on generating above filters (2 difference equations) using signal package? $\endgroup$
    – Waqas
    May 10, 2018 at 13:52
  • $\begingroup$ Check page 23 in this file. The function filter() does exactly what you are asking for. $\endgroup$
    – Tendero
    May 10, 2018 at 15:45
  • $\begingroup$ 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. $\endgroup$
    – Waqas
    May 10, 2018 at 15:54
  • $\begingroup$ hay, do you wanna define what you mean by "IRR"? $\endgroup$ May 10, 2018 at 18:32

1 Answer 1


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.

  • $\begingroup$ 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) $\endgroup$
    – Waqas
    May 10, 2018 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.