Skip to content

swtanal

Generates a stationary wavelet transform (SWT) stream from a mono audio signal.

Syntax

w:swt = swtanal(asig, iN, iH, ifamily, iorder [, ilevels [, iboundary [, iwindow [, iola]]]])
w:swt swtanal asig, iN, iH, ifamily, iorder, [ilevels], [iboundary], [iwindow], [iola]

Initialization

asig -- input audio signal.

iN -- analysis frame size. It must be at least 2. It also sets the synthesis latency in samples.

iH -- hop size in samples. It must be in the range 1 to iN. A hop smaller than the frame size produces overlapping frames.

ifamily -- wavelet family: 0 for Haar, 1 for Daubechies, 2 for Symlet, or 3 for Coiflet.

iorder -- wavelet order. Haar supports order 1, Daubechies orders 1 to 8, Symlet orders 2 to 8, and Coiflet orders 1 to 5. The filter length must fit inside one frame.

ilevels -- number of decomposition levels. When omitted or negative, the largest number of levels that fits the frame and wavelet is used. A value of 0 selects one level.

iboundary -- boundary mode. Only 0 (periodic/circular) is currently supported, and it is the default.

iwindow -- analysis window: 0 for no window, or 1 for the square-root Hann window used for overlap-add. The default is 1. A window is required when iH < iN.

iola -- synthesis mode: 0 for automatic, 1 for direct reconstruction, or 2 for overlap-add. The default is 0. Automatic mode chooses direct reconstruction when iH == iN and overlap-add otherwise. Direct mode requires iH == iN; overlap-add mode requires a window.

The coefficients are stored in one flat vector per frame. The layout is [cA_J | cD_J | ... | cD_1], where cA_J is the approximation band at the coarsest level and cD_1 is the finest detail band. Each band has iN samples, so the frame capacity is (ilevels + 1) * iN.

Performance

swtanal fills one frame every iH samples after the first iN samples have been collected. Each frame covers the input interval [m * iH, m * iH + iN). Frames are stored in a self-describing wst signal.

swtsynth begins output for frame m at sample m * iH + iN. The first iN output samples therefore contain the startup latency. With direct reconstruction and no coefficient edits, the steady-state output reproduces the input delayed by iN samples. With overlap-add, matching analysis and synthesis windows are applied and normalised by the accumulated window power.

Invalid frame or hop sizes, unsupported wavelet families or orders, unsupported boundary modes, missing windows for overlap-add, and direct mode with iH != iN are reported as initialisation errors.

Examples

Here is a simple analysis and resynthesis example. It uses the file swtanal.csd.

Example of the swtanal and swtsynth opcodes.
<CsoundSynthesizer>
<CsOptions>
; Select audio/midi flags here according to platform
; Audio out   Audio in
-odac    ;;;RT audio I/O
; For non-realtime output leave only the line below:
; -o swtanal.wav -W ;;; for file output any platform
</CsOptions>
<CsInstruments>

sr = 44100
ksmps = 32
nchnls = 1
0dbfs = 1

instr 1
  iN = 512
  iH = 128
  iFamily = 1          ; Daubechies
  iOrder = 4
  iLevels = 4
  iBoundary = 0        ; periodic
  iWindow = 1          ; square-root Hann
  iOla = 2             ; overlap-add

  aInput = 0.2 * oscili(1, 440) + 0.05
  wIn:swt swtanal aInput, iN, iH, iFamily, iOrder, iLevels, iBoundary, iWindow, iOla
  aOut = swtsynth(wIn)
  out aOut
endin

</CsInstruments>
<CsScore>
i 1 0 3
e
</CsScore>
</CsoundSynthesizer>

See also