-
Notifications
You must be signed in to change notification settings - Fork 0
/
Figures.py
150 lines (134 loc) · 4.61 KB
/
Figures.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
import os
import matplotlib.pyplot as plt
import pandas as pd
from nptdms import TdmsFile
import numpy as np
import scipy.signal as signal
import pywt
import seaborn as sns
from scipy.stats import kurtosis, skew
from scipy.signal import welch, periodogram
from numpy.fft import fftshift, fft
from scipy.signal import find_peaks
import matplotlib.pyplot as plt
from librosa.display import specshow
import librosa
import numpy as np
import scipy.signal
import numpy as np
import scipy.signal
from pandas import DataFrame
import numpy as np
import scipy.signal
from pandas import DataFrame
import numpy as np
import scipy.signal
from pandas import DataFrame
sns.set(font_scale=1.5)
sns.set_style("whitegrid", {'axes.grid': False})
tdms_file = TdmsFile("Test3.tdms")
sample_rate = 25600
grp1_data = tdms_file.object('Untitled').as_dataframe()
Z = grp1_data['Z']
Y = grp1_data['Y']
X = grp1_data['X']
Feature_vectors = np.empty((0, 118))
lower_bound = 140000
upper_bound = 510000
for x in range(0, 14):
signal_window = (X[lower_bound:upper_bound])
win = 4 * sample_rate
freqs, psd = periodogram(signal_window, sample_rate)
x = psd
print('Detect peaks with minimum height and distance filters.')
indexes, value = scipy.signal.find_peaks(psd, height=0.0005, distance=50)
a = value['peak_heights']
sorted_list = sorted(a, reverse=True)
b = sorted_list[0:3]
print('Peaks are: %s' % (indexes))
markers_off = indexes.tolist()
plt.plot(x, '-gD', markevery=markers_off, linewidth=2.5, markerfacecolor='yellow', markeredgewidth=1.5)
plt.legend
plt.show()
# amplitude correction factor
corr = 0.5
# calculate the psd with welch
sample_freq, power = welch(X, fs=sample_rate, window="hann", nperseg=256, noverlap=18, scaling='spectrum')
# fftshift the output
sample_freq = fftshift(sample_freq)
power = fftshift(power) / corr
# check that the power sum is right
# print (sum(power))
plt.figure(figsize=(9.84, 3.94))
plt.plot(sample_freq, power)
plt.xlabel("Frequency (kHz)", fontsize=18)
plt.ylabel("Relative power (dB)", fontsize=18)
plt.show()
x = psd
print('Detect peaks with minimum height and distance filters.')
indexes, value = scipy.signal.find_peaks(power, height=0.005, distance=500)
print('Peaks are: %s' % (indexes))
markers_off = indexes.tolist()
plt.plot(x, '-gD', markevery=markers_off, linewidth=2.5, markerfacecolor='yellow', markeredgewidth=1.5)
plt.legend
plt.show()
win = 4 * sample_rate
freqs, psd = signal.welch(X, sample_rate, nperseg=win)
# Plot the power spectrum
sns.set(font_scale=1.2, style='white')
plt.figure(figsize=(8, 4))
plt.plot(freqs, psd, color='k', lw=2)
plt.xlabel('Frequency (Hz)')
plt.ylabel('Power spectral density (V^2 / Hz)')
plt.ylim([0, psd.max() * 1.1])
plt.title("Welch's periodogram")
plt.xlim([0, freqs.max()])
sns.despine()
x = psd
print('Detect peaks with minimum height and distance filters.')
indexes, value = scipy.signal.find_peaks(x, height=0.005, distance=50)
print('Peaks are: %s' % (indexes))
markers_off = indexes.tolist()
plt.plot(x, '-gD', markevery=markers_off, linewidth=2.5, markerfacecolor='yellow', markeredgewidth=1.5)
plt.legend
plt.show()
X1 = np.array(X)
D = librosa.amplitude_to_db(librosa.stft(X1, hop_length=2048), ref=np.max)
librosa.display.specshow(D, y_axis='linear', sr=sample_rate, hop_length=128, bins_per_octave=24)
plt.colorbar(format='%+2.0f dB')
plt.title('Linear-frequency power spectrogram')
plt.show()
# %%
cmap = plt.get_cmap('PiYG')
plt.figure(figsize=(9.84, 3.94))
plt.specgram(X, NFFT=4096, noverlap=2048, Fs=1, cmap=cmap)
plt.xlabel("Frequency (kHz)", fontsize=18)
plt.ylabel("Relative power (dB)", fontsize=18)
plt.show()
# %%
waveletname = 'sym5'
fig, axarr = plt.subplots(nrows=5, ncols=2, figsize=(10, 10))
for ii in range(5):
(data, coeff_d) = pywt.dwt(X1, waveletname)
axarr[ii, 0].plot(data, 'r')
axarr[ii, 1].plot(coeff_d, 'g')
axarr[ii, 0].set_ylabel("Level {}".format(ii + 1), fontsize=14, rotation=90)
axarr[ii, 0].set_yticklabels([])
if ii == 0:
axarr[ii, 0].set_title("Approximation coefficients", fontsize=14)
axarr[ii, 1].set_title("Detail coefficients", fontsize=14)
axarr[ii, 1].set_yticklabels([])
plt.tight_layout()
plt.show()
f, t, Zxx = signal.stft(X1, fs=sample_rate)
cmap = plt.get_cmap('PiYG')
fig, ax = plt.subplots(figsize=(14, 12))
im = ax.pcolormesh(t, f, np.abs(Zxx), cmap=cmap)
fig.colorbar(im)
ax.set_title("STFT Plot ", fontsize=24)
ax.set_xlabel("Time", fontsize=24)
ax.set_ylabel("Frequency", fontsize=24)
plt.tick_params(labelsize=20)
plt.show()
fig.savefig('Basic.png', dpi=300)
a = np.abs(Zxx)