-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwavelets.py
155 lines (128 loc) · 5.12 KB
/
wavelets.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import time
import numpy as np
from scipy import signal
import matplotlib.pyplot as plt
plt.style.use(["ggplot"])
class Ricker:
def __init__(self, high_freq, samples, dt, canvas):
self.high_freq = high_freq
self.samples = samples
self.dt = dt
self.canvas = canvas
def waveletRicker(self):
twlet = np.arange(self.samples) * (self.dt / 1000)
twlet = np.concatenate((np.flipud(-twlet[1:]), twlet), axis=0)
wlet = (
1.0 - 2.0 * (np.pi**2) * (self.high_freq**2) * (twlet**2)
) * np.exp(-(np.pi**2) * (self.high_freq**2) * (twlet**2))
return twlet, wlet
def plotRicker(self):
twlet, wlet = self.waveletRicker()
fft_r = abs(np.fft.rfft(wlet))
freqs_r = np.fft.rfftfreq(twlet.shape[0], d=4 / 1000)
fft_r = fft_r / np.max(fft_r)
ax = self.canvas.figure.add_subplot(211)
ax.plot(twlet, wlet, color="navy")
ax.set_title("Ricker Wavelet", fontsize=14)
ax.set_xlabel("Tempo (s)")
ax.set_ylabel("Amplitude")
ax1 = self.canvas.figure.add_subplot(212)
ax1.plot(freqs_r, fft_r, color="firebrick")
ax1.set_title("Ricker Spectrum", fontsize=14)
ax1.set_xlabel("Frequency (Hz)")
ax1.set_ylabel("Amplitude")
self.canvas.figure.set_tight_layout(True)
self.canvas.draw()
class Butterworth:
def __init__(self, high_freq, low_freq, samples, dt, canvas):
self.high_freq = high_freq
self.low_freq = low_freq
self.samples = samples
self.dt = dt
self.canvas = canvas
def waveletButter(self):
twlet = np.arange(self.samples) * (self.dt / 1000)
twlet = np.concatenate((np.flipud(-twlet[1:]), twlet), axis=0)
# Create impulse signal
imp = signal.unit_impulse(twlet.shape[0], "mid")
# Apply high-pass Butterworth filter
fs = 1000 * (1 / self.dt)
b, a = signal.butter(4, self.high_freq, fs=fs)
response_zp = signal.filtfilt(b, a, imp)
# Apply low-pass Butterworth filter
low_b, low_a = signal.butter(2, self.low_freq, "hp", fs=fs)
butter_wvlt = signal.filtfilt(low_b, low_a, response_zp)
return twlet, butter_wvlt
def plotButter(self):
twlet, butter_wvlt = self.waveletButter()
fft_b = abs(np.fft.rfft(butter_wvlt))
freqs_b = np.fft.rfftfreq(twlet.shape[0], d=4 / 1000)
fft_b = fft_b / np.max(fft_b)
ax = self.canvas.figure.add_subplot(211)
ax.plot(twlet, butter_wvlt, color="navy")
ax.set_xlabel("Tempo (s)")
ax.set_ylabel("Amplitude")
ax.set_title("Butterworth Wavelet", fontsize=14)
ax1 = self.canvas.figure.add_subplot(212)
ax1.plot(freqs_b, fft_b, color="firebrick")
ax1.set_title("Butterworth Spectrum", fontsize=14)
ax1.set_xlabel("Frequency (Hz)")
ax1.set_ylabel("Amplitude")
self.canvas.figure.set_tight_layout(True)
self.canvas.draw()
class Ormsby:
def __init__(
self,
high_freq,
low_freq,
high_cutoff_freq,
low_cutoff_freq,
samples,
time,
canvas,
):
self.low_cut_freq = low_cutoff_freq
self.low_pass_freq = low_freq
self.high_pass_freq = high_freq
self.high_cut_freq = high_cutoff_freq
self.samples = samples
self.time = time
self.canvas = canvas
def waveletOrmsby(self):
twlet = np.arange(self.samples) * (self.time / 1000)
twlet = np.concatenate((np.flipud(-twlet[1:]), twlet), axis=0)
first = (
((np.pi * self.high_cut_freq) ** 2)
/ (np.pi * self.high_cut_freq - np.pi * self.high_pass_freq)
) * (pow(np.sinc(self.high_cut_freq * twlet), 2))
second = (
((np.pi * self.high_pass_freq) ** 2)
/ (np.pi * self.high_cut_freq - np.pi * self.high_pass_freq)
) * (pow(np.sinc(self.high_pass_freq * twlet), 2))
third = (
((np.pi * self.low_pass_freq) ** 2)
/ (np.pi * self.low_pass_freq - np.pi * self.low_cut_freq)
) * (pow(np.sinc(self.low_pass_freq * twlet), 2))
fourth = (
((np.pi * self.low_cut_freq) ** 2)
/ (np.pi * self.low_pass_freq - np.pi * self.low_cut_freq)
) * (pow(np.sinc(self.low_cut_freq * twlet), 2))
wvlet = (first - second) - (third - fourth)
return twlet, wvlet
def plotOrmsby(self):
twlet, wvlet = self.waveletOrmsby()
fft_o = abs(np.fft.rfft(wvlet))
freqs_o = np.fft.rfftfreq(twlet.shape[0], d=4 / 1000)
fft_o = fft_o / np.max(fft_o)
ax = self.canvas.figure.add_subplot(211)
ax.plot(twlet, wvlet, color="navy")
ax.set_title("Ormsby Wavelet", fontsize=14)
ax.set_xlabel("Time (s)")
ax.set_ylabel("Amplitude")
ax1 = self.canvas.figure.add_subplot(212)
ax1.plot(freqs_o, fft_o, color="firebrick")
ax1.set_title("Ormsby Spectrum", fontsize=14)
ax1.set_xlabel("Frequency (Hz)")
ax1.set_ylabel("Amplitude")
self.canvas.figure.set_tight_layout(True)
self.canvas.draw()