Spaces:
Sleeping
Sleeping
File size: 1,909 Bytes
ffa9b64 |
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 |
import numpy as np
def slice_songs(X, Y, S,
sr=22050,
hop_length=512,
length_in_seconds=30,
overlap=0.5):
"""
Slice spectrograms into smaller splits with overlap.
Parameters:
X: Array of spectrograms
Y: Array of labels
S: Array of song names
sr: Sample rate (default: 22050)
hop_length: Hop length used in spectrogram creation (default: 512)
length_in_seconds: Length of each slice in seconds (default: 30)
overlap: Overlap ratio between consecutive slices (default: 0.5 for 50% overlap)
"""
# Compute the number of frames for the desired slice length
frames_per_second = sr / hop_length
slice_length_frames = int(length_in_seconds * frames_per_second)
# Calculate hop size for overlapping (stride)
stride = int(slice_length_frames * (1 - overlap))
# Initialize lists for sliced data
X_slices = []
Y_slices = []
S_slices = []
# Slice each spectrogram
for i, spectrogram in enumerate(X):
num_frames = spectrogram.shape[1]
# Calculate start positions for all slices
start_positions = range(0, num_frames - slice_length_frames + 1, stride)
for start_frame in start_positions:
end_frame = start_frame + slice_length_frames
# Extract the slice
slice_ = spectrogram[:, start_frame:end_frame]
# Only add if the slice is the expected length
if slice_.shape[1] == slice_length_frames:
X_slices.append(slice_)
Y_slices.append(Y[i])
S_slices.append(S[i])
# Convert lists to numpy arrays
X_slices = np.array(X_slices)
Y_slices = np.array(Y_slices)
S_slices = np.array(S_slices)
return X_slices, Y_slices, S_slices |