Advertisement
575

course

575
Oct 13th, 2022
643
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.91 KB | None | 0 0
  1. from pylab import *
  2. import pywt as pw
  3. from numpy import *
  4.  
  5. SCALE = 3
  6. N = 2 ** SCALE
  7.  
  8. def f1(x):
  9.     return np.sin(2 * x)
  10.  
  11. def f2(x):
  12.     return np.sin(x ** 2)
  13.  
  14. def f3():
  15.     return np.sin(2 * np.pi * (2 ** np.linspace(2, 10, 1024)) * np.arange(1024) / 48000) + np.random.normal(0, 1, 1024) * 0.15
  16.  
  17. def check_level(level):
  18.     max_level = int(np.log2(N))
  19.     if level is None:
  20.         return max_level
  21.     elif level < 0:
  22.         raise ValueError("Level value of %d is too low. Minimum level is 0 ." % level)
  23.     elif level > max_level:
  24.         print(("Level value of {0} is too high. The maximum level value of {1} will be used.").format(level, max_level))
  25.         return max_level
  26.     return level
  27.  
  28. def dwt(data):
  29.     size = len(data) // 2
  30.     cA = np.zeros(size)
  31.     cD = np.zeros(size)
  32.  
  33.     for i, j in zip(range(0, len(data), 2), range(size)):
  34.         c = 2 * (data[i] + data[i + 1]) / np.sqrt(N)
  35.         cA[j] = c
  36.  
  37.     for i, j in zip(range(0, len(data), 2), range(size)):
  38.         c = 2 * (data[i] - data[i + 1]) / np.sqrt(N)
  39.         cD[j] = c
  40.  
  41.     return cA, cD
  42.  
  43. def wavedec (data, level = None):
  44.     coeffs_list = []
  45.  
  46.     level = check_level(level)
  47.     if level == 0:
  48.         return[np.array(data)]
  49.     a = data
  50.    
  51.     for i in range(level):
  52.         a, d = dwt(a)
  53.         coeffs_list.append(d)
  54.  
  55.     coeffs_list.append(a)
  56.     coeffs_list.reverse()
  57.  
  58.     return coeffs_list
  59.  
  60. def idwt(a, d):
  61.     res = []
  62.     for i in range(len(a)):
  63.         x = (a[i] + d[i]) * np.sqrt(N) / 4
  64.         y = (a[i] - d[i]) * np.sqrt(N) / 4
  65.         res.extend([x, y])
  66.     return np.array(res)
  67.  
  68. def waverec(coeffs):
  69.     if len(coeffs) < 1:
  70.         raise ValueError("Coefficient list too short (minimum 1 arrays required).")
  71.     elif len(coeffs) == 1:
  72.  
  73.         return coeffs[0]
  74.  
  75.     a, ds = coeffs[0], coeffs[1:]
  76.  
  77.     for d in ds:
  78.         a = idwt(a, d)
  79.     return a
  80.  
  81. x = linspace(0, 8, num = 1024)
  82. chirp_signal = np.sin(2 * np.pi * (2 ** np.linspace(2, 10, 1024)) * np.arange(1024) / 48000) + np.random.normal(0, 1, 1024) * 0.15
  83. fig, ax = subplots(figsize=(6, 1))
  84. ax.set_title("Исходный сигнал")
  85. ax.plot(chirp_signal)
  86. show()
  87. x = linspace(0, 50, num = 1024)
  88. y = np.sin(2 * np.pi * (2 ** np.linspace(2, 10, 1024)) * np.arange(1024) / 48000) + np.random.normal(0, 1, 1024) * 0.15
  89. st='sym5'
  90. (cA, cD) = pywt.dwt(y,st)
  91. subplot(2, 1, 1)
  92. plot(cA,'b',linewidth=2, label='cA,level-1')
  93. title("Коэффициенты аппроксимации")
  94. legend(loc='best')
  95. grid()
  96. subplot(2, 1, 2)
  97. plot(cD,'r',linewidth=2, label='cD,level-1')
  98. title("Коэффициенты детализации")
  99. grid()
  100. legend(loc='best')
  101. show()
  102.  
  103. (cA, cD) = pywt.dwt(cA,st)
  104. subplot(2, 1, 1)
  105. plot(cA,'b',linewidth=2, label='cA,level-2')
  106. title("Коэффициенты аппроксимации")
  107. legend(loc='best')
  108. grid()
  109.  
  110. subplot(2, 1, 2)
  111. plot(cD,'r',linewidth=2, label='cD,level-2')
  112. title("Коэффициенты детализации")
  113. grid()
  114. legend(loc='best')
  115. show()
  116.  
  117. (cA, cD) = pywt.dwt(cA,st)
  118. subplot(2, 1, 1)
  119. plot(cA,'b',linewidth=2, label='cA,level-3')
  120. title("Коэффициенты аппроксимации")
  121. legend(loc='best')
  122. grid()
  123.  
  124. subplot(2, 1, 2)
  125. plot(cD,'r',linewidth=2, label='cD,level-3')
  126. title("Коэффициенты детализации")
  127. grid()
  128. legend(loc='best')
  129. show()
  130.  
  131. (cA, cD) = pywt.dwt(cA,st)
  132. subplot(2, 1, 1)
  133. plot(cA,'b',linewidth=2, label='cA,level-4')
  134. title("Коэффициенты аппроксимации")
  135. legend(loc='best')
  136. grid()
  137.  
  138. subplot(2, 1, 2)
  139. plot(cD,'r',linewidth=2, label='cD,level-4')
  140. title("Коэффициенты детализации")
  141. grid()
  142. legend(loc='best')
  143. show()
  144.  
  145. signal = [1, 2, 3, 4, 5, 6, 7, 8]
  146. print('Signal: ', signal)
  147. (cA, cD) = pywt.dwt(signal, 'db1')
  148. print('Coefficients in wavelet transform:', '\ncA: ', cA, '\nCd: ', cD)
  149. signal_restored = pywt.idwt(cA, cD, 'db1')
  150. print('Signal restored: ',signal_restored)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement