-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenSpecFromAllFiles.py
343 lines (270 loc) · 12 KB
/
GenSpecFromAllFiles.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.signal import stft, cwt, ricker, hilbert
import pywt
from scipy.fftpack import fft
from librosa.feature import mfcc
import librosa
from sklearn.preprocessing import MinMaxScaler
# Base paths
base_csv_path = r"C:\InputFolder" # should contains csv files namely C1 to C10
base_output_path = r"C:\OutputFolder"
resolution=30
# List of classes from C1 to C10
classes = [f"C{i}" for i in range(1, 11)]
def process_cwt_spectrogram(data, output_folder, widths=np.arange(1, 31), sampling_rate=512, wavelet='morl'):
"""
Generate Continuous Wavelet Transform (CWT) spectrograms
"""
if data.empty:
print("Data is empty.")
return
x = data.iloc[:, 0].values # Get x-axis values
data_cols = data.iloc[:, 1:] # Get all columns except the first
for column in data_cols.columns:
print(f"Processing column: {column}")
y = data_cols[column].values
# Perform CWT using PyWavelets
cwtmatr, frequencies = pywt.cwt(y, widths, wavelet)
plt.figure(figsize=(10, 6))
plt.pcolormesh(x, widths, np.abs(cwtmatr), cmap='jet', shading='gouraud')
plt.axis('off')
plot_filename = os.path.join(output_folder, f'{column}.png')
plt.savefig(plot_filename, format='png', dpi=resolution, bbox_inches='tight', pad_inches=0)
plt.close()
print(f"Saved CWT image for {column} at {plot_filename}")
def process_stft_spectrogram(data, output_folder, sampling_rate=512, nperseg=256):
"""
Generate Short-Time Fourier Transform (STFT) spectrograms
"""
if data.empty:
print("Data is empty.")
return
x = data.iloc[:, 0].values # Get x-axis values
data_cols = data.iloc[:, 1:] # Get all columns except the first
for column in data_cols.columns:
print(f"Processing column: {column}")
y = data_cols[column].values
f, t, Zxx = stft(y, fs=sampling_rate, nperseg=nperseg)
t_scaled = np.interp(t * sampling_rate, np.arange(len(y)), x)
plt.figure(figsize=(10, 6))
plt.pcolormesh(t_scaled, f, np.abs(Zxx), shading='gouraud', cmap='jet')
plt.axis('off')
plot_filename = os.path.join(output_folder, f'{column}.png')
plt.savefig(plot_filename, format='png', dpi=resolution, bbox_inches='tight', pad_inches=0)
plt.close()
print(f"Saved STFT image for {column} at {plot_filename}")
def process_gaf_spectrogram(data, output_folder, image_size=128):
"""
Generate Gramian Angular Field (GAF) spectrograms
Uses first column for data scaling
"""
x = data.iloc[:, 0].values
data_cols = data.iloc[:, 1:]
#data_cols = data.iloc[:, 1] # Get only 2nd columns
scaler = MinMaxScaler(feature_range=(-1, 1))
for column in data_cols.columns:
y = data_cols[column].values
y_scaled = scaler.fit_transform(y.reshape(-1, 1)).flatten()
# Clip values to ensure they are in the valid range for arccos
y_scaled = np.clip(y_scaled, -1, 1)
# Calculate GAF matrix
phi = np.arccos(y_scaled)
r, c = np.meshgrid(phi, phi)
gaf = np.cos(r + c)
plt.figure(figsize=(8, 8))
plt.imshow(gaf, cmap='rainbow', origin='lower', extent=[x[0], x[-1], x[0], x[-1]])
plt.axis('off')
plot_filename = os.path.join(output_folder, f'{column}.png')
plt.savefig(plot_filename, format='png', dpi=resolution, bbox_inches='tight', pad_inches=0)
plt.close()
print(f"Saved GAF image for {column}")
def process_hht_spectrogram(data, output_folder, sampling_rate=512):
"""
Generate Hilbert-Huang Transform (HHT) spectrograms
Uses first column as x-axis values
"""
if data.empty:
print("Data is empty.")
return
x = data.iloc[:, 0].values # First column: time or x-axis
data_cols = data.iloc[:, 1:] # All other columns: signal data
for column in data_cols.columns:
print(f"Processing column: {column}")
y = data_cols[column].values
# Perform Hilbert transform
analytic_signal = hilbert(y)
amplitude_envelope = np.abs(analytic_signal)
instantaneous_phase = np.unwrap(np.angle(analytic_signal))
instantaneous_frequency = np.diff(instantaneous_phase) / (2.0 * np.pi) * sampling_rate
# Prepare grid and amplitude envelope for visualization
x_grid, y_grid = np.meshgrid(x[:-1], np.arange(len(amplitude_envelope[:-1])))
# Make C match the shape of x_grid and y_grid
C = np.tile(amplitude_envelope[:-1], (len(x[:-1]), 1)).T
plt.figure(figsize=(10, 6))
# Use shading='auto' to align dimensions
plt.pcolormesh(x_grid, y_grid, C, cmap='jet', shading='auto')
plt.axis('off')
plot_filename = os.path.join(output_folder, f'{column}.png')
plt.savefig(plot_filename, format='png', dpi=resolution, bbox_inches='tight', pad_inches=0)
plt.close()
print(f"Saved HHT image for {column} at {plot_filename}")
def process_dwt_spectrogram(data, output_folder, wavelet='db4', level=5):
"""
Generate Discrete Wavelet Transform (DWT) spectrograms
Uses first column for x-axis scaling
"""
x = data.iloc[:, 0].values
data_cols = data.iloc[:, 1:]
#data_cols = data.iloc[:, 1] # Get only 2nd columns
for column in data_cols.columns:
y = data_cols[column].values
# Perform DWT
coeffs = pywt.wavedec(y, wavelet, level=level)
# Plot coefficients with scaled x-axis
plt.figure(figsize=(10, 6))
for i, coeff in enumerate(coeffs):
plt.subplot(level + 1, 1, i + 1)
x_scaled = np.linspace(x[0], x[-1], len(coeff))
plt.plot(x_scaled, coeff)
plt.axis('off')
plt.tight_layout()
plot_filename = os.path.join(output_folder, f'{column}.png')
plt.savefig(plot_filename, format='png', dpi=resolution, bbox_inches='tight', pad_inches=0)
plt.close()
print(f"Saved DWT image for {column}")
def process_mfcc_spectrogram(data, output_folder, sampling_rate=512, n_mfcc=13):
"""
Generate Mel-frequency cepstral coefficients (MFCC) spectrograms
Uses first column for time scaling
"""
x = data.iloc[:, 0].values
data_cols = data.iloc[:, 1:]
#data_cols = data.iloc[:, 1] # Get only 2nd columns
for column in data_cols.columns:
y = data_cols[column].values
# Calculate MFCC
mfccs = mfcc(y=y, sr=sampling_rate, n_mfcc=n_mfcc)
plt.figure(figsize=(10, 6))
plt.pcolormesh(np.linspace(x[0], x[-1], mfccs.shape[1]),
np.arange(mfccs.shape[0]),
mfccs, cmap='jet')
plt.axis('off')
plot_filename = os.path.join(output_folder, f'{column}.png')
plt.savefig(plot_filename, format='png', dpi=resolution, bbox_inches='tight', pad_inches=0)
plt.close()
print(f"Saved MFCC image for {column}")
def process_rp_spectrogram(data, output_folder, embedding_dimension=10, time_delay=2):
"""
Generate Recurrence Plot (RP) spectrograms
Uses first column for plot scaling
"""
x = data.iloc[:, 0].values
data_cols = data.iloc[:, 1:]
#data_cols = data.iloc[:, 1] # Get only 2nd columns
for column in data_cols.columns:
y = data_cols[column].values
# Create time-delay embedding
N = len(y) - (embedding_dimension - 1) * time_delay
Y = np.zeros((N, embedding_dimension))
for i in range(embedding_dimension):
Y[:, i] = y[i * time_delay:i * time_delay + N]
# Calculate distance matrix
D = np.zeros((N, N))
for i in range(N):
for j in range(N):
D[i, j] = np.sqrt(np.sum((Y[i, :] - Y[j, :]) ** 2))
plt.figure(figsize=(8, 8))
plt.imshow(D, cmap='binary', origin='lower',
extent=[x[0], x[-1], x[0], x[-1]])
plt.axis('off')
plot_filename = os.path.join(output_folder, f'{column}.png')
plt.savefig(plot_filename, format='png', dpi=resolution, bbox_inches='tight', pad_inches=0)
plt.close()
print(f"Saved RP image for {column}")
def process_spectral_entropy(data, output_folder, window_size=256, overlap=128):
"""
Generate Spectral Entropy visualizations
Uses first column as x-axis values
"""
x = data.iloc[:, 0].values
data_cols = data.iloc[:, 1:]
#data_cols = data.iloc[:, 1] # Get only 2nd columns
for column in data_cols.columns:
y = data_cols[column].values
# Calculate spectral entropy for each window
entropy_values = []
x_values = []
for i in range(0, len(y) - window_size, overlap):
window = y[i:i + window_size]
spectrum = np.abs(fft(window)) ** 2
spectrum_normalized = spectrum / np.sum(spectrum)
entropy = -np.sum(spectrum_normalized * np.log2(spectrum_normalized + 1e-10))
entropy_values.append(entropy)
x_values.append(x[i + window_size // 2])
plt.figure(figsize=(10, 6))
plt.plot(x_values, entropy_values)
plt.axis('off')
plot_filename = os.path.join(output_folder, f'{column}.png')
plt.savefig(plot_filename, format='png', dpi=resolution, bbox_inches='tight', pad_inches=0)
plt.close()
print(f"Saved Spectral Entropy image for {column}")
def process_tf_embedding(data, output_folder, sampling_rate=512):
"""
Generate Time-Frequency Embedding visualizations using multiple representations
Uses first column as x-axis values
"""
x = data.iloc[:, 0].values
data_cols = data.iloc[:, 1:]
#data_cols = data.iloc[:, 1] # Get only 2nd columns
for column in data_cols.columns:
y = data_cols[column].values
# Calculate different time-frequency representations
f, t, Zxx = stft(y, fs=sampling_rate)
S = librosa.feature.melspectrogram(y=y, sr=sampling_rate)
# Scale time axes to match x values
t_scaled = np.interp(t * sampling_rate, np.arange(len(y)), x)
mel_x = np.linspace(x[0], x[-1], S.shape[1])
plt.figure(figsize=(12, 8))
plt.subplot(2, 1, 1)
plt.pcolormesh(t_scaled, f, np.abs(Zxx), shading='gouraud', cmap='jet')
plt.axis('off')
plt.subplot(2, 1, 2)
plt.pcolormesh(mel_x, np.arange(S.shape[0]),
librosa.power_to_db(S, ref=np.max), cmap='jet')
plt.axis('off')
plt.tight_layout()
plot_filename = os.path.join(output_folder, f'{column}.png')
plt.savefig(plot_filename, format='png', dpi=resolution, bbox_inches='tight', pad_inches=0)
plt.close()
print(f"Saved TF Embedding image for {column}")
def main(base_csv_path, base_output_path, classes):
for class_name in classes:
csv_file_path = os.path.join(base_csv_path, f"{class_name}.csv")
output_folder = os.path.join(base_output_path, class_name)
os.makedirs(output_folder, exist_ok=True)
print(f"Processing class: {class_name}")
print(f"Reading file: {csv_file_path}")
try:
data = pd.read_csv(csv_file_path)
if data.empty:
print(f"File {csv_file_path} is empty.")
continue
# Process functions (uncomment as needed)
#process_cwt_spectrogram(data, output_folder)
#process_gaf_spectrogram(data, output_folder)
#process_stft_spectrogram(data, output_folder)
#process_hht_spectrogram(data, output_folder)
#process_dwt_spectrogram(data, output_folder)
#process_mfcc_spectrogram(data, output_folder)
#process_rp_spectrogram(data, output_folder)
#process_spectral_entropy(data, output_folder)
process_tf_embedding(data, output_folder)
# Add other processing functions if needed
except Exception as e:
print(f"Error processing {class_name}: {str(e)}")
# Run the script
if __name__ == "__main__":
main(base_csv_path, base_output_path, classes)