A very basic Haar Discrete Wavelet Transform for Python 1.4.0 to 3.8.0...


 
Thread Tools Search this Thread
Operating Systems OS X (Apple) A very basic Haar Discrete Wavelet Transform for Python 1.4.0 to 3.8.0...
# 1  
Old 03-12-2020
A very basic Haar Discrete Wavelet Transform for Python 1.4.0 to 3.8.0...

Hi guys and gals...
(Apologies for any typos, etc...)

Another code snippet mainly for the AMIGA but works from Python 1.4.0 to 3.8.0 on just about any platform, hence the first upload here. I will upload to the AMIGA community in a week or so to let the dust settle on here.

A Haar One Dimension Discrete Wavelet Transform.

OSX 10.14.6, the AMIGA and some Linux flavours does not have access to the PyWavelets module, (pywt) so this was created. I personally have a Two Dimension version too but from this you can create your own, Linux Mint 19 does not have it in their repository
To create this very basic 1D version, only floating point arithmetic is required.
This means that many languages could be used AND shells like ksh(93) and the now default zsh for OSX Mojave.
In this case I have used Python but I might translate to ksh(93) to put it alongside my DFT, Discrete Fourier Transform, using ksh(93)

DFT using pure ksh ONLY!

I was considering using integer maths but I think, much like the DFT I was working on, this is not possible for large RADIX 2 data sizes.

This is in its simplest form and there is a URL in the code to check against your values, and, also a PDF learning reference too...
Code:
# DWT.py
#
# Basic Python 1D Haar DWT, Discrete Wavelet Transform, using internal default Python floating point maths only.
# Works on Python Versions 1.4.0 to 2.0.1 and 2.4.6, (for the AMIGA A1200), to 3.8.0rc1
# on other platforms without modification.
# Developed on OSX 10.12.x to 10.14.6, using Python Versions 2.6.x, 2.7.x, 3.5.2 and 3.8.0.
# Issued as CC0, Public Domain, B.walker, G0LCU, to https://www.unix.com first 12_03_2020.
#
# URL to check results against:
# https://www.unf.edu/~ddreibel/camp/wave8.html

# Integer values as floats used here for default DEMO 8 sample size.
# The 8 test samples; sample size must be a power of 2.
#
# DEMO 1:
# START:
# 56.0, 40.0, 8.0, 24.0, 48.0, 48.0, 40.0, 16.0
#
# STEPS IN BETWEEN:
# 48.0, 16.0, 48.0, 28.0, 8.0, -8.0, 0.0, 12.0
# 32.0, 38.0, 16.0, 10.0, 8.0, -8.0, 0.0, 12.0
#
# FINAL RESULT:
# 35.0, -3.0, 16.0, 10.0, 8.0, -8.0, 0.0, 12.0
# Works from Python 1.4.0 to Python 3.5.2.
#
# Learning reference here:
# https://www.math.aau.dk/digitalAssets/120/120646_r-2003-24.pdf
#
# LISTING=[56.0, 40.0, 8.0, 24.0, 48.0, 48.0, 40.0, 16.0]

# Transform creates genuine floating point values.
# DEMO 2:
# START:
# 6, 12, 15, 15, 14, 12, 120, 116
#
# FINAL RESULT:
# 38.75, -26.75, -3.0, -52.5, -3.0, 0.0, 1.0, 2.0
#
# LISTING=[6, 12, 15, 15, 14, 12, 120, 116]

# Basic transform.
# DEMO 3:
# START:
# 1, 2, 3, 4, 5, 6, 7, 8
#
# FINAL RESULT:
# 4.5, -2.0, -1.0, -1.0, -0.5, -0.5, -0.5, -0.5
#
# LISTING=[1, 2, 3, 4, 5, 6, 7, 8]

# Using padding of 0.0 for last three places.
# DEMO 4:
# START:
# 17.0, 3.0, 31.0, 213.0, 99.0, 0.0, 0.0, 0.0
#
# FINAL RESULT:
# 45.375, 20.625, -56.0, 24.75, 7.0, -91.0, 49.5, 0.0
#
LISTING=[17.0, 3.0, 31.0, 213.0, 99.0, 0.0, 0.0, 0.0]

print("Eight input values:")
print(LISTING)

# Haar 1D DWT function.
def DiscreteHaarWaveletTransform(DATA=[]):

    LENGTH=len(DATA)
    RESULT=[]
    while 1:
        TEMP=[0]*LENGTH

        for INDEX in range(0,LENGTH,2):
            MEAN_PAIR=(DATA[INDEX]+DATA[INDEX+1])/2.0
            DIFFERENCE=DATA[INDEX]-MEAN_PAIR
            TEMP[int(INDEX/2)]=MEAN_PAIR
            TEMP[int(LENGTH/2)+int(INDEX/2)]=DIFFERENCE

        DATA=TEMP
        RESULT=TEMP[int(LENGTH/2):]+RESULT
        LENGTH=int(LENGTH/2)

        if LENGTH==1:
            RESULT=TEMP[0:1]+RESULT
            return RESULT

# Obtain the results.
DWTDATA=DiscreteHaarWaveletTransform(LISTING)

print("DWT results:")
print(DWTDATA)

All constructive criticism welcome...
Enjoy.
This User Gave Thanks to wisecracker For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Programming

Transform SQL-Query to not use subqueries

Hi, I have a working SQL-Query here, in which I'm using subqueries to get the needed result. The idea behind this, is to get a list of networks which have 2 tags set in the database. Because of the way it is stored, i get a tuple for each Tag, so I get a duplicate of all networks. The... (1 Reply)
Discussion started by: stomp
1 Replies

2. Shell Programming and Scripting

Transform columns to matrix

The following code transform the matrix to columns. Is it possible to do it other way around ( get the input from the output) ? input y1 y2 y3 y4 y5 x1 0.3 0.5 2.3 3.1 5.1 x2 1.2 4.1 3.5 1.7 1.2 x3 3.1 2.1 1.0 4.1 2.1 x4 5.0 4.0 6.0 7.0 1.1 output x1 y1 0.3 x2 y1 1.2 x3... (1 Reply)
Discussion started by: quincyjones
1 Replies

3. Programming

STL transform question

Hi all, I pass to the transform algorithm two vectors, and the suma function. #include <algorithm> #include <iostream> #include <iterator> #include <vector> using namespace std; class Duo{ public: int one; int two; }; Duo suma(Duo first, Duo last){ Duo ret; ... (1 Reply)
Discussion started by: santiagorf
1 Replies

4. UNIX for Dummies Questions & Answers

How to log transform a text file?

How can I log transform (log 2) a text file which is a single column of numbers? (4 Replies)
Discussion started by: evelibertine
4 Replies

5. Shell Programming and Scripting

How to transform a string to a variable?

Hi all, I'm fairly new to shell scripting. My problem: I'm getting a string, "$XXOGL_TOP", from a database table. I then want to get the value of this variable in the shell script (which is a search path). But it doesn't dissolve but remains a string "$XXOGL_TOP". Any ideas on how to get the... (3 Replies)
Discussion started by: Kerstin
3 Replies

6. UNIX for Dummies Questions & Answers

Use awk to log transform

Hello. I'm trying to use the awk command to convert certains fields to lgo base 2, not base 10. I'm trying command lines like: awk '$2 $5 $7 {print log(2)($2), log(2)($5), $7) filein.txt > fileout.txt I'm trying to make a deadline. Thanks for helping a newbie. Here's some fresh karma... (1 Reply)
Discussion started by: jmzcal
1 Replies
Login or Register to Ask a Question