PyWavelets library provides an easy method to reconstruct the signal from its decomposed multilevel coefficients, i.e. inverse discrete wavelet transform method.
pywt.waverec(coeffs,'wavelet',mode='sym')
Paremeters:
In the articles of single level discrete wavelet reconstruction, it has showed that the reconstructed signal will have different lengths for the original signal with even and odd lengths. We will discuss this issue in multi-level reconstruction as well.
First, let's see the reconstruction result of the original signal with even length.
import pywt
S_even = list(range(1,101))
In this example, we use wavelet sym2
.
# Maximum decomposed
data_len = len(S_even)
w = pywt.Wavelet('sym2')
filter_len = w.dec_len
max_l = pywt.dwt_max_level(data_len, filter_len)
print(max_l)
5
We use signal extension mode of periodic
in the example.
coeffs = pywt.wavedec(S_even, 'sym2','periodic',level=max_l)
# or
(cA5,cD5,cD4,cD3,cD2,cD1) = pywt.wavedec(S_even, 'sym2','periodic',level=max_l)
S_even_r = pywt.waverec(coeffs,'sym2','periodic')
print(S_even_r)
[ 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. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100.]
From the above result, we cannot see the difference between the reconstructed signal and the original signal, except that reconstructed values are floats.
We compare the reconstructed signal with the original one in terms of length and errors.
# length
print(len(S_even))
print(len(S_even_r))
100 100
The above results show that they have the same length.
# average absolute error
print(abs(sum(S_even-S_even_r)/len(S_even)))
1.245672009986265e-11
# maximum absolute error
error = max(abs(S_even-S_even_r))
print(error)
1.0168399455778854e-10
The results show that there are a very small average absolute error and maximum absolute error, which can be ignored.
In the articles of single level discrete wavelet reconstruction, it has already displayed that the reconstructed signal has different length if the original signal has odd length. Let's investigate this issue on the multilevel in the following example.
# create an odd-length signal
S_odd = list(range(1,102))
# maximum decomposition level
data_len = len(S_odd)
w = pywt.Wavelet('sym2')
filter_len = w.dec_len
max_l = pywt.dwt_max_level(data_len, filter_len)
print(max_l)
# decomposition to maximum level
coeffs = pywt.wavedec(S_odd, 'sym2','periodic',level=max_l)
5
S_odd_r = pywt.waverec(coeffs,'sym2','periodic')
print(S_odd_r)
[ 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. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 1.]
The result shows that there is an extra value added at the end. Thus, the constructed signal should be the one excluding the last value. We use a simple signal in the example, because we can see the result very easily in order to better understand the method. However, we should compare the length of the constructed signal with the original signal for a complex and longer signal in reality.
S_odd_r = S_odd_r[:-1]
print(S_odd_r)
[ 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. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101.]
# average absolute error
print(sum(abs(S_odd-S_odd_r)/len(S_odd)))
2.038917329801365e-11
# maximum absolute error
error = max(abs(S_odd-S_odd_r))
print(error)
7.65947305580994e-11
The above errors confirm that the signal reconstruction is very accuracy.
This article displays the method to reconstruct a 1D signal from the decomposed approximation and detailed coefficients at multiple levels with Discrete wavelet transform (DWT) using PyWavelets library in Python. It mainly shows three topics: (1) the method to reconstruct signals with even lengths; (2) the method to reconstruct signals with odd lengths; and (3) evaluation of the reconstruction accuracy.