Skip to content

biquad

A sweepable general purpose biquadratic digital filter.

Syntax

ares = biquad(asig, kb0, kb1, kb2, ka0, ka1, ka2 [, iskip])
ares biquad asig, kb0, kb1, kb2, ka0, ka1, ka2 [, iskip]

Initialization

iskip (optional, default=0) -- if non-zero, intialization will be skipped. Default value 0. (New in Csound version 3.50)

Performance

asig -- input signal

biquad is a general purpose biquadratic digital filter of the form:

\[ a_0 y(n) + a_1 y(n-1) + a_2 y(n-2) = b_0 x(n) + b_1 x(n-1) + b_2 x(n-2) \]

This filter has the following frequency response:

\[ H(z) = \frac{B(z)}{A(z)} = \frac{b_0 + b_1 z^{-1} + b_2 z^{-2}}{a_0 + a_1 z^{-1} + a_2 z^{-2}} \]

This type of filter is often encountered in digital signal processing literature. It allows six user-defined k-rate coefficients.

Examples

Here is an example of the biquad opcode. It uses the file biquad-modern.csd.

Example of the biquad opcode.
<CsoundSynthesizer>
<CsOptions>
; Select audio/midi flags here according to platform
; Audio out   Audio in    No messages
-odac   -d
; -iadc     -d     ;;;RT audio I/O
; For Non-realtime ouput leave only the line below:
; -o biquad.wav -W ;;; for file output any platform
</CsOptions>
<CsInstruments>

; Initialize the global variables.
sr = 44100
kr =  4410
ksmps = 10
nchnls = 2

; Instrument #1.
instr 1
  ; Get the values from the score.
  dur:i = p3
  amp:i = p4
  cps:i = cpspch(p5)
  fco:k = p6
  rez:k = p7

  ; Calculate the biquadratic filter's coefficients 
  fcon:k = 2*3.14159265*fco/sr
  alpha:k = 1-2*rez*cos(fcon)*cos(fcon)+rez*rez*cos(2*fcon)
  beta:k = rez*rez*sin(2*fcon)-2*rez*cos(fcon)*sin(fcon)
  gama:k = 1+cos(fcon)
  m1:k = alpha*gama+beta*sin(fcon)
  m2:k = alpha*gama-beta*sin(fcon)
  den:k = sqrt(m1*m1+m2*m2)
  b0:k = 1.5*(alpha*alpha+beta*beta)/den
  b1:k = b0
  b2:k = 0
  a0:k = 1
  a1:k = -2*rez*cos(fcon)
  a2:k = rez*rez

  ; Generate an input signal.
  xn:a = vco(1, cps, 1)

  ; Filter the input signal.
  yn:a = biquad(xn, b0, b1, b2, a0, a1, a2)
  yn *= amp/2
  outs(yn, yn)
endin

</CsInstruments>
<CsScore>

; Table #1, a sine wave.
f 1 0 16384 10 1

;    Sta  Dur  Amp    Pitch Fco   Rez
i 1  0.0  1.0  20000  6.00  1000  .8
i 1  1.0  1.0  20000  6.03  2000  .95
e

</CsScore>
</CsoundSynthesizer>

Here is an example of the biquad opcode. It uses the file biquad.csd.

Example of the biquad opcode.
<CsoundSynthesizer>
<CsOptions>
; Select audio/midi flags here according to platform
; Audio out   Audio in    No messages
-odac           -iadc     -d     ;;;RT audio I/O
; For Non-realtime ouput leave only the line below:
; -o biquad.wav -W ;;; for file output any platform
</CsOptions>
<CsInstruments>

; Initialize the global variables.
sr = 44100
kr = 4410
ksmps = 10
nchnls = 2

; Instrument #1.
instr 1
  ; Get the values from the score.
  idur = p3
  iamp = p4
  icps = cpspch(p5)
  kfco = p6
  krez = p7

  ; Calculate the biquadratic filter's coefficients 
  kfcon = 2*3.14159265*kfco/sr
  kalpha = 1-2*krez*cos(kfcon)*cos(kfcon)+krez*krez*cos(2*kfcon)
  kbeta = krez*krez*sin(2*kfcon)-2*krez*cos(kfcon)*sin(kfcon)
  kgama = 1+cos(kfcon)
  km1 = kalpha*kgama+kbeta*sin(kfcon)
  km2 = kalpha*kgama-kbeta*sin(kfcon)
  kden = sqrt(km1*km1+km2*km2)
  kb0 = 1.5*(kalpha*kalpha+kbeta*kbeta)/kden
  kb1 = kb0
  kb2 = 0
  ka0 = 1
  ka1 = -2*krez*cos(kfcon)
  ka2 = krez*krez

  ; Generate an input signal.
  axn vco 1, icps, 1

  ; Filter the input signal.
  ayn biquad axn, kb0, kb1, kb2, ka0, ka1, ka2
  outs ayn*iamp/2, ayn*iamp/2
endin


</CsInstruments>
<CsScore>

; Table #1, a sine wave.
f 1 0 16384 10 1

;    Sta  Dur  Amp    Pitch Fco   Rez
i 1  0.0  1.0  20000  6.00  1000  .8
i 1  1.0  1.0  20000  6.03  2000  .95
e


</CsScore>
</CsoundSynthesizer>

Here is another example of the biquad opcode used for modal synthesis. It uses the file biquad-2-modern.csd. See the Modal Frequency Ratios appendix for other frequency ratios.

Example of the biquad opcode for modal synthesis.
<CsoundSynthesizer>
<CsOptions>
; Select audio/midi flags here according to platform
; Audio out   Audio in
-odac
; -iadc    ;;;RT audio I/O
; For Non-realtime ouput leave only the line below:
; -o biquad-2.wav -W ;;; for file output any platform
</CsOptions>
<CsInstruments>
sr = 44100
kr =  4410
ksmps = 10
nchnls = 2

/*  modal synthesis using biquad filters as oscillators
    Example by Scott Lindroth 2007 */

instr 1
    denom:i = sr*0.5

    ipulseSpd = p4
    cps:i     = p5
    ipan      = p6
    amp:i     = p7
    modes:i   = p8

    pulse:a = mpulse(amp, 0)
    cps     = cpspch(cps)

    ; filter gain

    amp1:i = 600
    amp2:i = 1000
    amp3:i = 1000
    amp4:i = 1000
    amp5:i = 1000
    amp6:i = 1000

    ; resonance

    rpole1:i = 0.99999
    rpole2:i = rpole1
    rpole3:i = rpole1
    rpole4:i = rpole1
    rpole5:i = rpole1
    rpole6:i = rpole1

    ; modal frequencies

    if modes == 1 then
      f1:i = cps * 1            ;pot lid
      f2:i = cps * 6.27
      f3:i = cps * 3.2
      f4:i = cps * 9.92
      f5:i = cps * 14.15
      f6:i = cps * 6.23
    else ;                      ;modes != 1
      f1 = cps * 1              ;uniform wood bar
      f2 = cps * 2.572
      f3 = cps * 4.644
      f4 = cps * 6.984
      f5 = cps * 9.723
      f6 = cps * 12.0
    endif

    ; convert frequency to radian frequency

    theta1:i = (f1/denom) * $M_PI
    theta2:i = (f2/denom) * $M_PI
    theta3:i = (f3/denom) * $M_PI
    theta4:i = (f4/denom) * $M_PI
    theta5:i = (f5/denom) * $M_PI
    theta6:i = (f6/denom) * $M_PI

    ; calculate coefficients

    b11:i = -2 * rpole1 * cos(theta1)
    b21:i = rpole1 * rpole1
    b12:i = -2 * rpole2 * cos(theta2)
    b22:i = rpole2 * rpole2
    b13:i = -2 * rpole3 * cos(theta3)
    b23:i = rpole3 * rpole3
    b14:i = -2 * rpole4 * cos(theta4)
    b24:i = rpole4 * rpole4
    b15:i = -2 * rpole5 * cos(theta5)
    b25:i = rpole5 * rpole5
    b16:i = -2 * rpole6 * cos(theta6)
    b26:i = rpole6 * rpole6

    ;printk(1, b11)
    ;printk(1, b21)

    ;  also try setting the -1 coeff. to 0, but be sure to scale down the amplitude!

    sin1:a = biquad(pulse * amp1, 1, 0, -1, 1, b11, b21)
    sin2:a = biquad(pulse * amp2, 1, 0, -1, 1, b12, b22)
    sin3:a = biquad(pulse * amp3, 1, 0, -1, 1, b13, b23)
    sin4:a = biquad(pulse * amp4, 1, 0, -1, 1, b14, b24)
    sin5:a = biquad(pulse * amp5, 1, 0, -1, 1, b15, b25)
    sin6:a = biquad(pulse * amp6, 1, 0, -1, 1, b16, b26)

    afin = (sin1 + sin2 + sin3 + sin4 + sin5 + sin6)

    outs(afin * sqrt(ipan), afin*sqrt(1-ipan))

endin
</CsInstruments>
<CsScore>
;ins     st    dur  pulseSpd   pch    pan    amp    modes
i1        0    12    0       7.089    0      0.7    2
i1        .    .    .        7.09     1      .      .
i1        .    .    .        7.091    0.5    .      .

i1        0    12    0       8.039    0      0.7    2
i1        0    12    0       8.04     1      0.7    2
i1        0    12    0       8.041    0.5    0.7    2

i1        9    .    .        7.089    0      .      2
i1        .    .    .        7.09     1      .      .
i1        .    .    .        7.091    0.5    .      .

i1        9    12    0       8.019    0      0.7    2
i1        9    12    0       8.02     1      0.7    2
i1        9    12    0       8.021    0.5    0.7    2
e
</CsScore>
</CsoundSynthesizer>

Here is another example of the biquad opcode used for modal synthesis. It uses the file biquad-2.csd. See the Modal Frequency Ratios appendix for other frequency ratios.

Example of the biquad opcode for modal synthesis.
<CsoundSynthesizer>
<CsOptions>
; Select audio/midi flags here according to platform
; Audio out   Audio in
-odac           -iadc    ;;;RT audio I/O
; For Non-realtime ouput leave only the line below:
; -o biquad-2.wav -W ;;; for file output any platform
</CsOptions>
<CsInstruments>
sr = 44100
kr = 4410
ksmps = 10
nchnls = 2

/*  modal synthesis using biquad filters as oscillators
    Example by Scott Lindroth 2007 */


instr 1

    ipi = 3.1415926
    idenom = sr*0.5

    ipulseSpd = p4
    icps     = p5
    ipan = p6
    iamp    = p7
    iModes = p8

    apulse    mpulse iamp, 0

    icps    = cpspch( icps )

    ; filter gain

    iamp1 = 600
    iamp2 = 1000
    iamp3 = 1000
    iamp4 = 1000
    iamp5 = 1000
    iamp6 = 1000

    ; resonance

    irpole1 = 0.99999
    irpole2 = irpole1
    irpole3 = irpole1
    irpole4 = irpole1
    irpole5 = irpole1
    irpole6 = irpole1

    ; modal frequencies

    if (iModes == 1) goto modes1
    if (iModes == 2) goto modes2

    modes1:
    if1    = icps * 1            ;pot lid
    if2    = icps * 6.27
    if3    = icps * 3.2
    if4    = icps * 9.92
    if5    = icps * 14.15
    if6    = icps * 6.23
    goto nextPart

    modes2:
    if1     = icps * 1            ;uniform wood bar
    if2     = icps * 2.572
    if3     = icps * 4.644
    if4     = icps * 6.984
    if5     = icps * 9.723
    if6     = icps * 12.0
    goto nextPart

    nextPart:

    ; convert frequency to radian frequency

    itheta1 = (if1/idenom) * ipi
    itheta2 = (if2/idenom) * ipi
    itheta3 = (if3/idenom) * ipi
    itheta4 = (if4/idenom) * ipi
    itheta5 = (if5/idenom) * ipi
    itheta6 = (if6/idenom) * ipi

    ; calculate coefficients

    ib11 = -2 * irpole1 * cos(itheta1)
    ib21 = irpole1 * irpole1
    ib12 = -2 * irpole2 * cos(itheta2)
    ib22 = irpole2 * irpole2
    ib13 = -2 * irpole3 * cos(itheta3)
    ib23 = irpole3 * irpole3
    ib14 = -2 * irpole4 * cos(itheta4)
    ib24 = irpole4 * irpole4
    ib15 = -2 * irpole5 * cos(itheta5)
    ib25 = irpole5 * irpole5
    ib16 = -2 * irpole6 * cos(itheta6)
    ib26 = irpole6 * irpole6

    ;printk 1, ib 11
    ;printk 1, ib 21

    ;  also try setting the -1 coeff. to 0, but be sure to scale down the amplitude!

    asin1     biquad  apulse * iamp1, 1, 0, -1, 1, ib11, ib21
         asin2       biquad  apulse * iamp2, 1, 0, -1, 1, ib12, ib22
         asin3       biquad  apulse * iamp3, 1, 0, -1, 1, ib13, ib23
         asin4       biquad  apulse * iamp4, 1, 0, -1, 1, ib14, ib24
         asin5       biquad  apulse * iamp5, 1, 0, -1, 1, ib15, ib25
         asin6       biquad  apulse * iamp6, 1, 0, -1, 1, ib16, ib26


    afin    =    (asin1 + asin2 + asin3 + asin4 + asin5 + asin6)

    outs        afin * sqrt(p6), afin*sqrt(1-p6)

endin
</CsInstruments>
<CsScore>
;ins     st    dur  pulseSpd   pch    pan    amp     Modes
i1        0    12    0       7.089    0      0.7    2
i1        .    .    .        7.09     1      .      .
i1        .    .    .        7.091    0.5    .      .

i1        0    12    0       8.039    0      0.7    2
i1        0    12    0       8.04     1      0.7    2
i1        0    12    0       8.041    0.5    0.7    2

i1        9    .    .        7.089    0      .      2
i1        .    .    .        7.09     1      .      .
i1        .    .    .        7.091    0.5    .      .

i1        9    12    0       8.019    0      0.7    2
i1        9    12    0       8.02     1      0.7    2
i1        9    12    0       8.021    0.5    0.7    2
e
</CsScore>
</CsoundSynthesizer>

See also

Standard filters: Biquad filters

Credits

Author: Hans Mikelson
October 1998

New in Csound version 3.49