forked from nogleo/gordata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignal.py
65 lines (56 loc) · 2.06 KB
/
signal.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
import numpy as np
import matplotlib.pyplot as plt
from scipy.fftpack import fft, fftfreq
from scipy.signal import butter, filtfilt
class TimeSignal:
def __init__(self, time, signal, sampling_rate):
self.time = time
self.signal = signal
self.sampling_rate = sampling_rate
def __repr__(self):
return f"TimeSignal(sampling_rate={self.sampling_rate}, duration={self.time[-1] - self.time[0]}s)"
def plot_signal(self):
plt.figure(figsize=(10, 4))
plt.plot(self.time, self.signal)
plt.xlabel('Time [s]')
plt.ylabel('Amplitude')
plt.title('Time Signal')
plt.grid(True)
plt.show()
def fft(self):
N = len(self.signal)
T = 1.0 / self.sampling_rate
yf = fft(self.signal)
xf = fftfreq(N, T)[:N//2]
return xf, 2.0/N * np.abs(yf[0:N//2])
def plot_fft(self):
xf, yf = self.fft()
plt.figure(figsize=(10, 4))
plt.plot(xf, yf)
plt.xlabel('Frequency [Hz]')
plt.ylabel('Amplitude')
plt.title('Frequency Spectrum')
plt.grid(True)
plt.show()
def filter_signal(self, lowcut, highcut, order=5):
nyquist = 0.5 * self.sampling_rate
low = lowcut / nyquist
high = highcut / nyquist
b, a = butter(order, [low, high], btype='band')
filtered_signal = filtfilt(b, a, self.signal)
return TimeSignal(self.time, filtered_signal, self.sampling_rate)
def basic_statistics(self):
return {
'mean': np.mean(self.signal),
'std_dev': np.std(self.signal),
'max': np.max(self.signal),
'min': np.min(self.signal)
}
def save_to_file(self, filename):
np.savetxt(filename, np.column_stack((self.time, self.signal)), delimiter=',', header='time,signal', comments='')
@classmethod
def load_from_file(cls, filename, sampling_rate):
data = np.loadtxt(filename, delimiter=',', skiprows=1)
time = data[:, 0]
signal = data[:, 1]
return cls(time, signal, sampling_rate)