Skip to content

Commit 68d2e11

Browse files
Apply suggestions from code review
Co-authored-by: Christian Clauss <[email protected]>
1 parent a1cb36c commit 68d2e11

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

Diff for: machine_learning/mfcc.py

+6-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
MFCC (Mel Frequency Cepstral Coefficients) Calculation
2+
Mel Frequency Cepstral Coefficients (MFCC) Calculation
33
44
MFCC is a feature widely used in audio and speech processing to represent the
55
short-term power spectrum of a sound signal in a more compact and
@@ -138,12 +138,10 @@ def normalize(audio: np.ndarray) -> np.ndarray:
138138
max_abs_value = np.max(np.abs(audio))
139139

140140
# Divide the entire audio signal by the maximum absolute value
141-
normalized_audio = audio / max_abs_value
141+
return audio / max_abs_value
142142

143-
return normalized_audio
144143

145-
146-
def frame(
144+
def audio_frames(
147145
audio: np.ndarray,
148146
sample_rate: int,
149147
hop_length: int = 20,
@@ -168,7 +166,7 @@ def frame(
168166
audio = np.pad(audio, int(ftt_size / 2), mode="reflect")
169167

170168
# Calculate the number of frames
171-
frame_num = int((len(audio) - ftt_size) / hop_size) + 1
169+
frame_count = int((len(audio) - ftt_size) / hop_size) + 1
172170

173171
# Initialize an array to store the frames
174172
frames = np.zeros((frame_num, ftt_size))
@@ -206,9 +204,7 @@ def calculate_fft(audio_windowed: np.ndarray, ftt_size: int = 1024) -> np.ndarra
206204
audio_fft[:, n] = fft.fft(audio_transposed[:, n], axis=0)[: audio_fft.shape[0]]
207205

208206
# Transpose the FFT results back to the original shape
209-
audio_fft = np.transpose(audio_fft)
210-
211-
return audio_fft
207+
return np.transpose(audio_fft)
212208

213209

214210
def calculate_signal_power(audio_fft: np.ndarray) -> np.ndarray:
@@ -289,9 +285,7 @@ def mel_spaced_filterbank(
289285
# normalize filters
290286
# taken from the librosa library
291287
enorm = 2.0 / (mel_freqs[2 : mel_filter_num + 2] - mel_freqs[:mel_filter_num])
292-
filters *= enorm[:, np.newaxis]
293-
294-
return filters
288+
return filters * enorm[:, np.newaxis]
295289

296290

297291
def get_filters(filter_points: np.ndarray, ftt_size: int) -> np.ndarray:

0 commit comments

Comments
 (0)