Created
October 31, 2017 18:35
-
-
Save MaxHalford/6953602065249c9a6c6e75052a5d4307 to your computer and use it in GitHub Desktop.
Haar wavelet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
import matplotlib.pyplot as plt | |
import numpy as np | |
from scipy import stats | |
# Generate some data | |
a = 1.99 | |
# Create a figure | |
fig = plt.figure() | |
ax = fig.add_subplot(111) | |
def plot(dist, label): | |
x = np.linspace(stats.gamma.ppf(0.01, a), stats.gamma.ppf(0.99, a), len(dist)).tolist() | |
ax.plot(x, dist, lw=2, label=label) | |
# Plot the true PDF | |
x = np.linspace(stats.gamma.ppf(0.01, a), stats.gamma.ppf(0.99, a), 300).tolist() | |
y = stats.gamma(a).pdf(x) | |
plot(y, 'PDF') | |
# Apply the Haar wavelet | |
def decompose(x: list) -> (list, list): | |
averages = [0] * math.ceil(len(x) / 2) | |
coefficients = [0] * math.ceil(len(x) / 2) | |
for i, j in enumerate(range(0, len(x), 2)): | |
averages[i] = (x[j] + x[min(j+1, len(x)-1)]) / 2 | |
coefficients[i] = averages[i] - x[j] | |
return averages, coefficients | |
w, c = decompose(y) | |
plot(w, 'Haar 1') | |
for i in range(3): | |
w, c = decompose(w) | |
plot(w, f'Haar {i+2}') | |
plt.legend() | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment