Skip to content

Commit

Permalink
com
Browse files Browse the repository at this point in the history
  • Loading branch information
Ihor committed Apr 21, 2024
1 parent 1a0f426 commit 9818736
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 25 deletions.
Binary file modified __pycache__/constellations.cpython-310.pyc
Binary file not shown.
Binary file modified __pycache__/fft_just_do_it.cpython-310.pyc
Binary file not shown.
33 changes: 20 additions & 13 deletions constellations.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import numpy as np
from scipy import signal
from scipy.io.wavfile import read
from fft_just_do_it import stft, fftfreq, find_peaks
from fft_just_do_it import find_peaks


def create_constellation(audio, Fs, chunk_size=10000):
window_length_seconds = 0.5
def create_constellation(audio, Fs, chunk_size=5000):
window_length_seconds = 0.05
window_length_samples = int(window_length_seconds * Fs)
window_length_samples += window_length_samples % 2
num_peaks = 15
Expand All @@ -15,23 +14,31 @@ def create_constellation(audio, Fs, chunk_size=10000):
for start in range(0, len(audio), chunk_size):
end = start + chunk_size
chunk = audio[start:end]

amount_to_pad = window_length_samples - chunk.size % window_length_samples
padded_chunk = np.pad(chunk, (0, amount_to_pad))
stft_result = stft(padded_chunk, window_length_samples, window_length_samples)
frequencies = fftfreq(window_length_samples, 1/Fs)

frequencies, times, stft_result = signal.stft(
padded_chunk, Fs, nperseg=1024, nfft=window_length_samples, return_onesided=True
)

for time_idx, window in enumerate(stft_result.T):
spectrum = abs(window)
peak_results = find_peaks(spectrum, prominence=0, distance=200)
peaks = peak_results['peaks']
props = {'prominences': peak_results['prominences']}
spectrum = np.squeeze(spectrum).ravel() # перетворення у 1-D масив
print(spectrum.shape)

peaks, props = signal.find_peaks(spectrum, prominence=0)

n_peaks = min(num_peaks, len(peaks))
largest_peaks = np.argpartition(props["prominences"], -n_peaks)[-n_peaks:]
for peak in peaks[largest_peaks]:
frequency = frequencies[peak]
constellation_map.append([time_idx + start, frequency])
if peak < len(frequencies):
frequency = frequencies[peak]
constellation_map.append([time_idx + start, frequency])

return constellation_map

Fs, input = read("Rain Over Me.wav")
constellation_map = create_constellation(input, Fs)
if __name__ == "__main__":
Fs, audio_input = read("Rain Over Me.wav")
constellation = create_constellation(audio_input, Fs)
print(constellation)
20 changes: 11 additions & 9 deletions fft_just_do_it.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ def stft(signal, window_length, hop_length):
num_windows = 1 + (signal_length - window_length) // hop_length

# Ініціалізуємо масив для збереження результату STFT
stft_result = np.zeros((num_windows, window_length), dtype=signal.dtype)
stft_result = np.zeros((num_windows, window_length), dtype=np.complex128)

# Цикл по вікнам
# Цикл по вікнах
for i in range(num_windows):
# Обчислюємо початок та кінець поточного вікна
start = i * hop_length
Expand All @@ -31,12 +31,13 @@ def stft(signal, window_length, hop_length):
windowed_signal *= np.hanning(window_length)

# Обчислюємо швидке перетворення Фур'є (FFT)
stft_result = np.zeros((num_windows, window_length), dtype=np.float64)
fft_result = np.fft.fft(windowed_signal)

# Записуємо результат FFT у відповідний рядок масиву stft_result
stft_result[i, :] = fft_result

return stft_result


def fftfreq(n, d=1.0):
"""Compute the sample frequencies for an FFT of length n.
Expand Down Expand Up @@ -115,11 +116,11 @@ def find_peaks(x, prominence=None, distance=None):

# Apply distance filter
if distance is not None:
filtered_peaks = []
filtered_peak_heights = []
filtered_left_bases = []
filtered_right_bases = []
filtered_prominences = []
filtered_peaks = [peaks[0]]
filtered_peak_heights = [peak_heights[0]]
filtered_left_bases = [left_bases[0]]
filtered_right_bases = [right_bases[0]]
filtered_prominences = [prominences[0]]

for i in range(1, len(peaks)):
if peaks[i] - peaks[i - 1] >= distance:
Expand All @@ -140,3 +141,4 @@ def find_peaks(x, prominence=None, distance=None):
'prominences': np.array(prominences),
'left_bases': np.array(left_bases),
'right_bases': np.array(right_bases)}

6 changes: 3 additions & 3 deletions hashes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

from constellations import create_constellation

Fs, audio_input = read("Rain Over Me.wav")

constellation_map = create_constellation(audio_input, Fs)
upper_frequency = 23_000
frequency_bits = 10

Expand All @@ -21,3 +18,6 @@ def create_hashes(constellation_map, song_id=None):
hash = int(freq_binned) | (int(other_freq_binned) << 10) | (int(diff) << 20)
hashes[hash] = (time, song_id)
return hashes

if __name__ == "__main__":
print(create_hashes([1,2,3], 2))
8 changes: 8 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import pickle

# Відкриття файлу для читання
with open("song_index.pickle", "rb") as file:
# Завантаження даних з файлу
database = pickle.load(file)
print(database)
# Тепер ви можете використовувати зміну database для доступу до даних

0 comments on commit 9818736

Please sign in to comment.