-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudio_Fingerprinting.py
158 lines (125 loc) · 4.96 KB
/
Audio_Fingerprinting.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
#libraries
import numpy as np
from operator import itemgetter
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import soundfile as sf
import hashlib
import logging
from typing import List, Tuple
from scipy import signal
from scipy.io import wavfile
from scipy.ndimage import maximum_filter
from scipy.ndimage import (binary_erosion,generate_binary_structure,iterate_structure)
#constants
wsize = 4096
wratio = 0.5
CONNECTIVITY_MASK = 8
DEFAULT_AMP_MIN = 10
DEFAULT_FAN_VALUE = 5 # 15 was the original value.
#get the imput audio file
#read the audio file
#data, samplerate = sf.read('Lavender_Town_Japan.wav')
#convert to mono the signal
#discrete time processing
#using the fast fourier transform to convert the audio file to frequency domain
#peack finding from spectrogram
def get_peaks(inputsignal,amp_min):
struct = generate_binary_structure(2, CONNECTIVITY_MASK)
neighborhood = iterate_structure(struct, 20)
# Find local peaks using our fliter shape. Filtro pasa altos
local_max = maximum_filter(inputsignal, footprint=neighborhood) == inputsignal
background = (inputsignal == 0)
eroded_background = binary_erosion(background, structure=neighborhood, border_value=1)
# Boolean mask of arr2D with True at peaks.
detected_peaks = local_max ^ eroded_background
# Extract peaks
amps = inputsignal[detected_peaks]
freqs, times = np.where(detected_peaks)
# Filter peaks
amps = amps.flatten()
#Get indices for frequency and time
filteridx = np.where(amps > amp_min)
freqs_filter = freqs[filteridx]
times_filter = times[filteridx]
#scatter plot of the peaks
fig, ax = plt.subplots()
ax.imshow(inputsignal)
ax.scatter(times_filter, freqs_filter, c='r')
ax.set_xlabel('Time')
ax.set_ylabel('Frequency')
ax.set_title('Spectrogram of input signal recorded')
plt.gca().invert_yaxis()
plt.show()
return list(zip(freqs_filter, times_filter))
#get_peaks(arr2D, DEFAULT_AMP_MIN)
#hashing the peaks
def hash_peaks(peaks: List[Tuple[int, int]],fan_value) -> List[tuple[str, int]]:
# frequencies are in the first position of the tuples
idx_freq = 0
# times are in the second position of the tuples
idx_time = 1
peaks.sort(key=itemgetter(1))
hashes = []
for i in range(len(peaks)):
for j in range(1, fan_value):
if (i + j) < len(peaks):
freq1 = peaks[i][idx_freq]
freq2 = peaks[i + j][idx_freq]
t1 = peaks[i][idx_time]
t2 = peaks[i + j][idx_time]
t_delta = t2 - t1
if 0 <= t_delta <= 200: #Between 0 and 200 Hash time delta interval pairs of peaks along with the time delta in between
h = hashlib.sha1(f"{str(freq1)}|{str(freq2)}|{str(t_delta)}".encode('utf-8'))
hashes.append((h.hexdigest()[0:20], t1))
return hashes
#fingerprint function
def fingerprint(audio_file, segment_size=10, fan_value=DEFAULT_FAN_VALUE, amp_min=DEFAULT_AMP_MIN):
data=audio_file
data = np.mean(data, axis=1)
#samplig input signal to 44100Hz
samplerate = 44100
#data= signal.resample(data, int(len(data)*samplerate/len(data)))
segmentSize=2
seconds = data.shape[0] / samplerate
segments = seconds / segmentSize
samplesPerSegment = int(data.shape[0] / segments)
plt.plot(data)
plt.xlabel('Sample')
plt.ylabel('Amplitude')
plt.title('Input recorded file')
plt.show()
#continuos time processing
#using the fast fourier transform to convert the audio file to frequency domain
#fft = np.fft.fft(data)
#plot the audio file in frequency domain
#plt.plot(fft)
#plt.show()
#plt.plot(data)
#plt.xlabel('Sample')
#plt.ylabel('Amplitude')
#plt.subplot(212)
#plt.specgram(data[0:samplesPerSegment],Fs=samplerate, mode='psd')
#plt.xlabel('Time')
#plt.ylabel('Frequency')
#plt.show()
fft = np.fft.fft(data)
#peack finding from spectrogram
#peaks, _ = signal.find_peaks(fft, height=0)
#plt.plot(peaks, fft[peaks], "x")
#plt.plot(np.zeros_like(fft), "--", color="gray")
#plt.show()
#FFT the signal and extract frequency components
arr2D = mlab.specgram(data,NFFT=wsize,Fs=44100,window=mlab.window_hanning,noverlap=int(wsize * wratio))[0]
# Apply log transform since specgram function returns linear array. 0s are excluded to avoid np warning.
arr2D = 10 * np.log10(arr2D, out=np.zeros_like(arr2D), where=(arr2D != 0))
#graphical representation of the spectrogram
plt.imshow(arr2D, cmap='hot', interpolation='nearest')
plt.xlabel('Time')
plt.ylabel('Frequency')
plt.title('Spectrogram of the input recorded file')
plt.show()
#find local maxima
peaks = get_peaks(arr2D, amp_min)
#hash the peaks
return hash_peaks(peaks, fan_value)