Prabin1 commited on
Commit
6a9e591
·
verified ·
1 Parent(s): 5448ba6

Update audio_features.py

Browse files
Files changed (1) hide show
  1. audio_features.py +27 -29
audio_features.py CHANGED
@@ -5,36 +5,34 @@ SR = 16000
5
  N_COEFFS = 20
6
 
7
  def extract_mfcc(y, sr=SR, n_mfcc=N_COEFFS):
8
- return librosa.feature.mfcc(y=y, sr=sr, n_mfcc=n_mfcc)
9
 
10
  def extract_lfcc(y, sr=SR, n_lfcc=N_COEFFS):
11
- S = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=n_lfcc, fmin=0, fmax=sr/2)
12
- return librosa.power_to_db(S)
13
 
14
  def extract_features_with_time_series(file_path, sr=SR, n_coeffs=N_COEFFS):
15
- try:
16
- y, _ = librosa.load(file_path, sr=sr, mono=True)
17
- y, _ = librosa.effects.trim(y)
18
-
19
- ```
20
- if np.max(np.abs(y)) > 0:
21
- y = y / np.max(np.abs(y))
22
-
23
- mfccs = extract_mfcc(y, sr, n_mfcc=n_coeffs)
24
- lfccs = extract_lfcc(y, sr, n_lfcc=n_coeffs)
25
- chroma = librosa.feature.chroma_stft(y=y, sr=sr)
26
- spec_centroid = librosa.feature.spectral_centroid(y=y, sr=sr)
27
- spec_bandwidth = librosa.feature.spectral_bandwidth(y=y, sr=sr)
28
- zcr = librosa.feature.zero_crossing_rate(y)
29
-
30
- features_to_stack = [mfccs, lfccs, chroma, spec_centroid, spec_bandwidth, zcr]
31
- max_len = max(f.shape[1] for f in features_to_stack)
32
- padded_features = [librosa.util.fix_length(f, size=max_len, axis=1) for f in features_to_stack]
33
-
34
- stacked_features = np.vstack(padded_features).astype(np.float32)
35
- return stacked_features.T
36
-
37
- except Exception as e:
38
- print(f"[extract_features] Error processing {file_path}: {e}")
39
- return None
40
- ```
 
5
  N_COEFFS = 20
6
 
7
  def extract_mfcc(y, sr=SR, n_mfcc=N_COEFFS):
8
+ return librosa.feature.mfcc(y=y, sr=sr, n_mfcc=n_mfcc)
9
 
10
  def extract_lfcc(y, sr=SR, n_lfcc=N_COEFFS):
11
+ S = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=n_lfcc, fmin=0, fmax=sr/2)
12
+ return librosa.power_to_db(S)
13
 
14
  def extract_features_with_time_series(file_path, sr=SR, n_coeffs=N_COEFFS):
15
+ try:
16
+ y, _ = librosa.load(file_path, sr=sr, mono=True)
17
+ y, _ = librosa.effects.trim(y)
18
+
19
+ if np.max(np.abs(y)) > 0:
20
+ y = y / np.max(np.abs(y))
21
+
22
+ mfccs = extract_mfcc(y, sr, n_mfcc=n_coeffs)
23
+ lfccs = extract_lfcc(y, sr, n_lfcc=n_coeffs)
24
+ chroma = librosa.feature.chroma_stft(y=y, sr=sr)
25
+ spec_centroid = librosa.feature.spectral_centroid(y=y, sr=sr)
26
+ spec_bandwidth = librosa.feature.spectral_bandwidth(y=y, sr=sr)
27
+ zcr = librosa.feature.zero_crossing_rate(y)
28
+
29
+ features_to_stack = [mfccs, lfccs, chroma, spec_centroid, spec_bandwidth, zcr]
30
+ max_len = max(f.shape[1] for f in features_to_stack)
31
+ padded_features = [librosa.util.fix_length(f, size=max_len, axis=1) for f in features_to_stack]
32
+
33
+ stacked_features = np.vstack(padded_features).astype(np.float32)
34
+ return stacked_features.T
35
+
36
+ except Exception as e:
37
+ print(f"[extract_features] Error processing {file_path}: {e}")
38
+ return None