-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwaveletanasyn.py
84 lines (67 loc) · 2.67 KB
/
waveletanasyn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#Program to plot the frequency magnitude of a 3-level Wavelet filter bank, using pulses into the synthesis filter bank.
#Gerald Schuller, January 2025
import pywt
import numpy as np
import scipy.signal as sp
import matplotlib.pyplot as plt
def synthesis_wavelet_filter_bank(wavelet_name='db4', levels=3):
"""
Synthesize a signal from wavelet coefficients using a wavelet filter bank.
Args:
wavelet_name (str): The name of the wavelet (e.g., 'db4').
levels (int): Number of decomposition levels.
Returns:
None: Displays reconstruction process and result.
"""
# Create a sample signal
original_signal = np.sin(2 * np.pi * 5 * np.linspace(0, 1, 128)) + \
0.5 * np.sin(2 * np.pi * 15 * np.linspace(0, 1, 128))
# Decompose signal into wavelet coefficients
wavelet = pywt.Wavelet(wavelet_name)
coeffs = pywt.wavedec(original_signal, wavelet, level=levels)
print(f"Decomposed coefficients: {[len(c) for c in coeffs]}")
#print("coeffs=", coeffs)
for i in range(levels+1):
for c in range(len(coeffs)):
#print("c=", c)
#print("coeffs[c].shape=", coeffs[c].shape)
coeffs[c]=np.zeros(coeffs[c].shape)
coeffs[i][10]=1.0
# Reconstruction using waverec
reconstructed_signal = pywt.waverec(coeffs, wavelet)
# Ensure the reconstructed signal matches the original length
reconstructed_signal = reconstructed_signal[:len(original_signal)]
w,H=sp.freqz(reconstructed_signal)
plt.plot(w, 20*np.log10(np.abs(H)+1e-4))
#coeffs[c][10]=0.0 #for next iteration
plt.title("Magnitude Frequency Response of " +str(levels)+ " level Daubechies Wavelet Filter Bank")
plt.xlabel("Normalized frequency (pi is Nyquist frequency)")
plt.ylabel("dB")
#plt.savefig('wavelet_daubechies_3level_fr_syn.pdf')
plt.show()
"""
# Visualization
plt.figure(figsize=(12, 8))
# Original signal
plt.subplot(3, 1, 1)
plt.plot(original_signal, label='Original Signal')
plt.title('Original Signal')
plt.legend()
plt.grid()
# Reconstructed signal
plt.subplot(3, 1, 2)
plt.plot(reconstructed_signal, label='Reconstructed Signal', color='orange')
plt.title('Reconstructed Signal from Wavelet Coefficients')
plt.legend()
plt.grid()
# Difference
plt.subplot(3, 1, 3)
plt.plot(original_signal - reconstructed_signal, label='Difference', color='red')
plt.title('Difference (Original - Reconstructed)')
plt.legend()
plt.grid()
plt.tight_layout()
plt.show()
"""
# Example usage
synthesis_wavelet_filter_bank(wavelet_name='db4', levels=3)