Previous Up Next

21.4.10  Discrete wavelet transform

The dwt and idwt commands are used for computing the discrete wavelet transform (DWT) of a signal and for computing the inverse transformation, i.e. reconstructing the original signal from the transform. These commands require the GSL library.

Examples

Signal compression.

In the first example (adapted from GSL documentation), we load a sample of size 256 from the MIT-BIH Arrhythmia Database (see here) and attempt to remove the noisy information.

data:=mid(col(csv2gen("/home/luka/Documents/MIT-BIH.csv","\t"),2),300,256):; listplot(data)

Now we transform the signal by using dwt and select the 20 largest components. We set other elements to zero and transform the result back using idwt.

tdata:=dwt(data):; p:=reverse(sortperm(abs(tdata))):; for k from 20 to 255 do tdata[p[k]]:=0; od:; res:=idwt(tdata):; listplot(res)

As you can see, the fast, low-amplitude oscillations are gone, but important features such as the spike are left practically intact. As a beneficial side-effect of denoising, you get the signal compressed by, in this case, about 92% in terms of memory usage.

Noise reduction in images.

Assuming that an image noisy.jpg is stored in Pictures folder, enter:

img:=image("/home/luka/Pictures/noisy.jpg")(grey)
     
an image of size 300×300 (grayscale)           

Use DWT to transform the image matrix and subsequently replace each component smaller than 55 (by absolute value) in the resulting matrix to zero, before transforming it back to obtain a noise-reduced image. (The threshold value was obtained by trial and error.) Note that dwt enlarges the input matrix to the size 512× 512, so be sure to crop the output of idwt back to 300× 300. To make sure that no component gets below zero or above 255, use the threshold command (see Section 21.3.4).

tdata:=dwt(img[0],image):; sdata:=threshold(tdata,55.0=0,'<=',abs=true):; nr:=image(1,threshold(round(subMat(idwt(sdata,image),0,0,299,299)),[0,255]))
     
an image of size 300×300 (grayscale)           

To display the original image and the processed image next to each other for comparison, enter:

display(img,0); display(nr,320); legend(-20i,"original"); legend(320-20i,"denoised")

Previous Up Next