## Methods for Single-Level Discrete Wavelet Transform of 1D Signals in Python (III): Partial Reconstruction

Master the wavelet partial reconstruction methods using easily understanding examples

The methods for 1D Single-Level Discrete Wavelet Transform in Python will be discussed in the following 3 parts:

(III): Wavelet Partial Reconstruction

We have discussed the methods on one-stage wavelet decomposition of 1D signal and the signal reconstruction in the last two parts. In this part, we come to the third part: methods for signal partial reconstruction. In this part, we will apply the partial wavelet construction process, which have been talked in one of the previous posts. I suggest you reading that process part if you are not familiar with it.

### 1. Upcoef method

1.1 Method

The level 1 approximation and detail (A1 and D1) can be directly reconstructed from the coefficients cA1 and cD1 using `upcoef`. This is exactly the opposite process of `downcoef`, which was talked in the partial decomposition methods.

`pywt.upcoef(part, coeffs, wavelet, level=1, take=0)`

#### Parameters:

part: the input coefficients type:

• ‘a’: approximation reconstruction is performed
• ‘d’: detail reconstruction is performed

coeffs: Coefficients array to reconstruct

wavelet : Wavelet used in the transform.

level: If level value is specified then a multilevel reconstruction is performed

take: Take the central part of length equal to ‘take’ from the result. Default is 0.

#### Return:

• A (or A1): the first level approximation of the original signal
• D (or D1): the first level detail of the original signal

#### 1.2 Example

Here, we use the odd length signal to see if there also produces an extra value during reconstruction.

(1) Decompose the signal

```import pywt

S = list(range(1,100))

(cA1,cD1) = pywt.dwt(S,'db2','symmetric')```

(2) Partially reconstruct the signal

We construct the approximation (A1) and detail (D1) of the signal.

```A1 = pywt.upcoef(‘a’, cA1, ‘db2’, level=1, take=len(S))
D1 = pywt.upcoef(‘d’, cD1, ‘db2’, level=1, take=len(S))

print('The approximation: \n',A1)
print('The detail: \n',D1)```
```The approximation:
[ 1.51225953  1.70424682  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.0625     98.10825318 98.61274047]
The detail:
[-5.12259526e-01  2.95753175e-01  9.62059197e-17 -1.55084387e-16
2.49881212e-16 -2.10628900e-16  1.57009246e-16 -1.57009246e-16
1.28274560e-16 -2.06779182e-16  2.56549119e-16 -4.13558365e-16
6.85506356e-16 -5.28497110e-16  3.71487864e-16 -2.14478618e-16
-5.74693726e-17 -9.95398733e-17  3.14018492e-16 -3.14018492e-16
4.28957237e-16 -1.14938745e-16 -3.14018492e-16  3.14018492e-16
-4.28957237e-16  1.14938745e-16  5.43895982e-16  8.41410013e-17
-1.05699422e-15  7.42975729e-16 -7.16304100e-16 -3.82760621e-16
1.99904970e-15 -1.68503120e-15  1.02619648e-15 -1.65423346e-15
2.51214793e-15 -2.51214793e-15  3.43165790e-15 -9.19509962e-16
-3.43165790e-15  9.19509962e-16  3.20178041e-15 -1.31766945e-15
-1.71582895e-15  4.59754981e-16  7.96318986e-16 -2.05239295e-15
4.22797688e-15 -2.97190291e-15  1.02619648e-15 -1.65423346e-15
2.74202542e-15 -2.11398844e-15  1.02619648e-15 -1.65423346e-15
2.97190291e-15 -1.71582895e-15 -6.89632471e-16 -1.19447848e-15
4.45785437e-15 -2.57374342e-15 -2.29877490e-16 -3.98159493e-16
1.25607397e-15 -1.25607397e-15  7.96318986e-16 -2.05239295e-15
4.68773186e-15 -2.17558393e-15 -1.94570644e-15  6.15954879e-17
2.51214793e-15 -2.51214793e-15  2.74202542e-15 -2.11398844e-15
3.36564005e-16 -2.84871194e-15  6.05049234e-15 -6.67852933e-15
9.37546373e-15 -4.35116786e-15 -3.20178041e-15  1.31766945e-15
3.36564005e-16 -2.84871194e-15  6.51024733e-15 -5.88221034e-15
5.94380583e-15 -3.43165790e-15 -6.89632471e-16 -1.19447848e-15
4.22797688e-15 -2.97190291e-15  5.66441496e-16 -2.45055245e-15
-6.25000000e-02 -1.08253175e-01  3.87259526e-01]```

(3) Check the length

```print('Length of the original signal:',len(S))
print('Length of the Approximation:',len(A1))
print('Length of the Detail:',len(D1))```
```Length of the original signal: 99
Length of the Approximation: 99
Length of the Detail: 99```

The above results show that there is no extra value produced during the transform using `upcoeff`.

### 2. Inverse discrete wavelet transform method

We can also use the inverse discrete wavelet transform method (`idwt`), which we discussed in the post of partial reconstruction process. Please read that post if you are not familiar with the process, or this method might be hard to understand.

```# we use A and D here to compare the methods
A = pywt.idwt(cA1,None,'db2','symmetric') # [0,0,0,0,0,0]
D = pywt.idwt(None,cD1,'db2','symmetric') # [0,0,0,0,0,0]

print('The approximation: \n',A)
print('The detail: \n',D)```
```The approximation:
[ 1.51225953  1.70424682  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.0625     98.10825318 98.61274047 99.26225953]
The detail:
[-5.12259526e-01  2.95753175e-01  9.62059197e-17 -1.55084387e-16
2.49881212e-16 -2.10628900e-16  1.57009246e-16 -1.57009246e-16
1.28274560e-16 -2.06779182e-16  2.56549119e-16 -4.13558365e-16
6.85506356e-16 -5.28497110e-16  3.71487864e-16 -2.14478618e-16
-5.74693726e-17 -9.95398733e-17  3.14018492e-16 -3.14018492e-16
4.28957237e-16 -1.14938745e-16 -3.14018492e-16  3.14018492e-16
-4.28957237e-16  1.14938745e-16  5.43895982e-16  8.41410013e-17
-1.05699422e-15  7.42975729e-16 -7.16304100e-16 -3.82760621e-16
1.99904970e-15 -1.68503120e-15  1.02619648e-15 -1.65423346e-15
2.51214793e-15 -2.51214793e-15  3.43165790e-15 -9.19509962e-16
-3.43165790e-15  9.19509962e-16  3.20178041e-15 -1.31766945e-15
-1.71582895e-15  4.59754981e-16  7.96318986e-16 -2.05239295e-15
4.22797688e-15 -2.97190291e-15  1.02619648e-15 -1.65423346e-15
2.74202542e-15 -2.11398844e-15  1.02619648e-15 -1.65423346e-15
2.97190291e-15 -1.71582895e-15 -6.89632471e-16 -1.19447848e-15
4.45785437e-15 -2.57374342e-15 -2.29877490e-16 -3.98159493e-16
1.25607397e-15 -1.25607397e-15  7.96318986e-16 -2.05239295e-15
4.68773186e-15 -2.17558393e-15 -1.94570644e-15  6.15954879e-17
2.51214793e-15 -2.51214793e-15  2.74202542e-15 -2.11398844e-15
3.36564005e-16 -2.84871194e-15  6.05049234e-15 -6.67852933e-15
9.37546373e-15 -4.35116786e-15 -3.20178041e-15  1.31766945e-15
3.36564005e-16 -2.84871194e-15  6.51024733e-15 -5.88221034e-15
5.94380583e-15 -3.43165790e-15 -6.89632471e-16 -1.19447848e-15
4.22797688e-15 -2.97190291e-15  5.66441496e-16 -2.45055245e-15
-6.25000000e-02 -1.08253175e-01  3.87259526e-01 -2.62259526e-01]```
```# Check the lengths
print(‘Length of the original signal:’,len(S))
print(‘Length of the Approximation:’,len(A))
print(‘Length of the Detail:’,len(D))```
```Length of the original signal: 99
Length of the Approximation: 100
Length of the Detail: 100```

The results show that there is an extra value produced at the end of each of the approximation and detail using `idwt` method. The real approximation and detail are the ones after removing the last values.

```A = A[:-1]
D = D[:-1]```

### 3. Compare the methods

We compare to see if there are different between these two methods.

```print('The different of the constructed approxiamtions:',sum(A-A1)/len(S))
print('The different of the constructed details:',sum(D-D1)/len(S))```
```The different of the constructed approxiamtions: 0.0
The different of the constructed details: 0.0```

So, we can get the same results from the two methods.

### 4. Reconstruct the Signal from Approximation and Detail

We have already discussed in a previous post that the signal can be reconstructed the reconstructed detail and approximation since they are the true constituents of the original signal.

Since the two partial reconstruction are the same, we can use the approximation and details obtained from any one of the two methods.

```S_recons = A1 + D1
print(S_recons)```
```[ 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.]```

Or:

```S_recons2 = A + D
print(S_recons2)```
```[ 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.]```