if song1.sgram is not None: print(f"Spectrogram for {song1.songname} has…
- User
if song1.sgram is not None: print(f"Spectrogram for {song1.songname} has {np.sum(song1.sgram > 0)} values above 0") else: print(f"Spectrogram for {song1.songname} is None, check generation process")
if song2.sgram is not None: print(f"Spectrogram for {song2.songname} has {np.sum(song2.sgram > 0)} values above 0") else: print(f"Spectrogram for {song2.songname} is None, check generation process") - Assistant
The code snippet you provided is a Python script that checks if spectrogram data is present for two song objects (
song1andsong2). A spectrogram is a visual representation of the spectrum of frequencies of a signal as they vary with time. It is often used in audio processing.Let's take a closer look at your code:
For both
song1andsong2:-
It first checks if the
sgramattribute of the song object (which appears to be the spectrogram data) is notNone. This implies that the spectrogram should have been generated prior to this check. -
If the
sgramis notNone, it will print out a statement indicating the song name (accessed withsongnameattribute) along with the count of elements in the spectrogram that have values above 0. This count is obtained by usingnp.sum(song1.sgram > 0)wherenpis presumably an alias for the NumPy library which is used for numerical operations. The expressionsong1.sgram > 0generates a boolean array where each element isTrueif the corresponding element insgramis greater than 0, andFalseotherwise. Thenp.sum()function then sums this array, effectively counting the number ofTruevalues, or in this case, the number of values above 0. -
If the
sgramisNone, it will print out a message indicating that the spectrogram for the song isNoneand suggests that the process of generating the spectrogram should be checked.
There are a couple of points to consider regarding this code:
- The objects
song1andsong2should havesgramandsongnameattributes, or else the code will result in an error. - It assumes that the
sgramattribute, if notNone, will be a NumPy array or a similar data structure compatible with NumPy's greater-than operation (> 0) andnp.sum()function. - The message "check generation process" suggests that there is a separate process that is responsible for generating the spectrogram, which might need debugging if the
sgramisNone.
Please note that this code is a diagnostic tool. It won't generate spectrograms or fix problems related to their generation, but rather, it provides information that can help in identifying whether the generation process worked as intended and provides useful information for potential troubleshooting.
-
- User
Exception has occurred: TypeError (note: full exception trace is shown but execution is paused at: _run_module_as_main) '>' not supported between instances of 'NoneType' and 'int' File "C:\meu\audiofingerprints4.py", line 169, in main print(f"Spectrogram for {song1.songname} has {np.sum(song1.sgram > 0)} values above 0") File "C:\meu\audiofingerprints4.py", line 181, in main() File "C:\Users\vania\AppData\Local\Programs\Python\Python38\Lib\runpy.py", line 87, in _run_code exec(code, run_globals) File "C:\Users\vania\AppData\Local\Programs\Python\Python38\Lib\runpy.py", line 194, in _run_module_as_main (Current frame) return _run_code(code, main_globals, None, TypeError: '>' not supported between instances of 'NoneType' and 'int'
import numpy as np import pydub import scipy.signal import skimage.feature import datasketch import os import pickle from pydub import AudioSegment import matplotlib.pyplot as plt
Parameters for tuning the Audiofingerprinting algorithm
Parameters used in generating spectrogram
#---------------------------------- nperseg = 16 * 256 # window size overlap_ratio = 0.4 # degree of overlap, larger number->more overlap, denser fingerprint #----------------------------------
Parameters used in finding local peaks
#------------------------- min_peak_sep = 15 # larger sep -> less peaks -> less accuracy, but faster fingerprinting min_peak_amp = 10 # larger min amp -> less peaks -> less accuracy, but faster fingerprinting #-------------------------
Parameters used in generating fingerprint
#------------------------------ peak_connectivity = 15 # Number of neighboring peaks to use as target for each anchor peak_time_delta_min = 0 # Minimum spacing in time between peaks for anchor and target peak_time_delta_max = 200 # Maximum spacing in time between peaks for anchor and target #------------------------------
This class defines an AudioFP object that stores the name a song and its fingerprint.
It also contains all the functions used to read/record audio, generate spectrogram, find peaks,
generate fingerprint, and saving the object to file.
class AudioFP(): def init(self, filename=None): #def init(self, filename=None): self.songname = os.path.basename(filename) if filename else '' self.fingerprint = datasketch.MinHash(num_perm=512) self.framerate = [] # Adicione os seguintes atributos: self.channels = None # Para armazenar os dados de áudio de canais agregados self.f = None # Frequências do espectrograma self.t = None # Tempos do espectrograma self.sgram = None # Dados do espectrograma self.peaks = None # Picos encontrados no espectrograma
if filename: channels, self.framerate = self.read_audiofile(filename) f, t, sgram = self.generate_spectrogram(channels, self.framerate) fp, tp, peaks = self.find_peaks(f, t, sgram) self.generate_fingerprint(fp, tp, peaks) def read_audiofile(self, filename): # Assumindo que o formato é .wav audiofile = AudioSegment.from_wav(filename) # Convertendo para um único canal (mono) se necessário if audiofile.channels > 1: audiofile = audiofile.set_channels(1) songdata = np.array(audiofile.get_array_of_samples()) framerate = audiofile.frame_rate return songdata, framerate def generate_spectrogram(self, audiosignal, framerate): fs = framerate # sampling rate window = scipy.signal.get_window('hamming', nperseg) # window function noverlap = int(overlap_ratio * nperseg) # number of points to overlap # generate spectrogram from consecutive FFTs over the defined window f, t, sgram = scipy.signal.spectrogram(audiosignal, fs, window, nperseg, noverlap, mode='magnitude') sgram = 20 * np.log10(sgram) # transmorm linear output to dB scale sgram[sgram == -np.inf] = 0 # replace infs with zeros sgram = np.where(sgram > 0, 20 * np.log10(sgram), 0) return f, t, sgram def find_peaks(self, f, t, sgram): # Ajustar conforme a sua definição anterior de parâmetros coordinates = skimage.feature.peak_local_max(sgram, min_distance=min_peak_sep, threshold_abs=min_peak_amp) peaks = sgram[coordinates[:, 0], coordinates[:, 1]] tp = t[coordinates[:, 1]] fp = f[coordinates[:, 0]] return fp, tp, peaks def generate_fingerprint(self, fp, tp, peaks): s = [] # Empty list to contain data for fingerprint for i in range(len(peaks)): for j in range(1, peak_connectivity): if (i + j) < len(peaks): f1 = fp[i] f2 = fp[i + j] t1 = tp[i] t2 = tp[i + j] t_delta = t2 - t1 if t_delta >= peak_time_delta_min and t_delta <= peak_time_delta_max: encoded_data = str(np.rint(f1)) + str(np.rint(f2)) + str(np.rint(t_delta)) s.append(encoded_data) self.fingerprint.update(encoded_data.encode('utf8')) # Check if s is empty print(f"Generated fingerprint elements for {self.songname}: {len(s)}") if len(s) == 0: print(f"No fingerprint data generated for {self.songname}. Check peak detection and encoding.") # Compare fingerprints of two songsdef compare_fingerprints(s1, s2): jac_sim = calc_jaccard(s1, s2) if jac_sim >= 0.9: print('{} and {} are identical!'.format(s1.songname, s2.songname)) print('Jaccard similarity = ', jac_sim) elif jac_sim >= 0.1 and jac_sim < 0.9: print('{} and {} are quite similar'.format(s1.songname, s2.songname)) print('Jaccard similarity = ', jac_sim) elif jac_sim >= 0.05 and jac_sim < 0.1: print('{} and {} might have some similarity'.format(s1.songname, s2.songname)) print('Jaccard similarity = ', jac_sim) else: print('{} and {} are different'.format(s1.songname, s2.songname)) print('Jaccard similarity = ', jac_sim)
def calc_jaccard(s1, s2): s1_size = s1.fingerprint.count() s2_size = s2.fingerprint.count() union_s = s1.fingerprint.copy() union_s.merge(s2.fingerprint) union = union_s.count() inter = s1_size + s2_size - union if union == 0: return 0 # Nenhuma sobreposição entre os conjuntos; retornar similaridade zero.
jac_sim = inter / union return jac_simdef visualize_spectrogram(sgram, f, t, peaks=None): plt.figure(figsize=(10, 4)) plt.pcolormesh(t, f, 10 * np.log10(sgram), shading='gouraud') plt.ylabel('Frequency [Hz]') plt.xlabel('Time [sec]') plt.colorbar(format='%+2.0f dB')
if peaks is not None: fp, tp = peaks plt.scatter(tp, fp, c='red') plt.show()def main(): # Caminhos para os arquivos de áudio file1_path = r'c:\meu\a.wav' file2_path = r'c:\meu\b.wav'
# Criação dos objetos AudioFP para os arquivos de áudio song1 = AudioFP(filename=file1_path) song2 = AudioFP(filename=file2_path) if song1.sgram is not None: print(f"Spectrogram for {song1.songname} has {np.sum(song1.sgram > 0)} values above 0") else: print(f"Spectrogram for {song1.songname} is None, check generation process") if song2.sgram is not None: print(f"Spectrogram for {song2.songname} has {np.sum(song2.sgram > 0)} values above 0") else: print(f"Spectrogram for {song2.songname} is None, check generation process") # Verificação dos espectrogramas e picos print(f"Spectrogram for {song1.songname} has {np.sum(song1.sgram > 0)} values above 0") print(f"Spectrogram for {song2.songname} has {np.sum(song2.sgram > 0)} values above 0") # Dentro do método 'main': visualize_spectrogram(song1.sgram, song1.f, song1.t, peaks=song1.peaks) visualize_spectrogram(song2.sgram, song2.f, song2.t, peaks=song2.peaks) # Comparando as impressões digitais das músicas compare_fingerprints(song1, song2)if name== "main": main()
- Assistant
The error message you are seeing indicates that there is an attempt to compare
Noneto an integer with the'>'operator which is not valid in Python. This is most likely happening because thesgramattribute of one of yourAudioFPobjects isNonewhen you try to perform the comparisonsong1.sgram > 0.This can be confirmed by the fact that the error message points to the line in the code where you are trying to print the number of values above 0 in the
sgramattribute:print(f"Spectrogram for {song1.songname} has {np.sum(song1.sgram > 0)} values above 0")The first thing you should do is ensure this line only gets executed when
sgramis notNone. You have already included a check for this, but you then repeat the same print statement outside the conditional block, which is likely where it errors out.You need to remove or comment out the second occurrence of similar print statements. Here's the corrected snippet:
def main(): ... # Criação dos objetos AudioFP para os arquivos de áudio song1 = AudioFP(filename=file1_path) song2 = AudioFP(filename=file2_path) # The conditionals are already present, so no need to repeat the print statements outside if song1.sgram is not None: print(f"Spectrogram for {song1.songname} has {np.sum(song1.sgram > 0)} values above 0") else: print(f"Spectrogram for {song1.songname} is None, check generation process") if song2.sgram is not None: print(f"Spectrogram for {song2.songname} has {np.sum(song2.sgram > 0)} values above 0") else: print(f"Spectrogram for {song2.songname} is None, check generation process") ... # Now we call the visualization function only if the spectrograms are not None if song1.sgram is not None: visualize_spectrogram(song1.sgram, song1.f, song1.t, peaks=song1.peaks) if song2.sgram is not None: visualize_spectrogram(song2.sgram, song2.f, song2.t, peaks=song2.peaks) # Comparando as impressões digitais das músicas compare_fingerprints(song1, song2) if __name__== "__main__": main()By correcting the code in this way, you will prevent trying to compare
Nonewith an integer and only process and output information aboutsgramwhen it is notNone. - User
C:\meu\audiofingerprints4.py:75: RuntimeWarning: invalid value encountered in log10 sgram = np.where(sgram > 0, 20 * np.log10(sgram), 0) Spectrogram for a.wav is None, check generation process Spectrogram for b.wav is None, check generation process a.wav and b.wav are different Jaccard similarity = 0
- Assistant
The warning
RuntimeWarning: invalid value encountered in log10indicates that at some point,np.log10(sgram)was called with values ofsgramthat are not suitable for the logarithm function. The logarithm of 0 and negative values are undefined in the real number domain, which can lead to NaNs (Not a Number) in the output array.However, later on you receive
Spectrogram for a.wav is None, check generation processindicating that the spectrogram was not successfully generated and thesgramattribute remainsNone. This means the logical conditionif song1.sgram is not Noneis evaluating toFalseand therefore the generation process for the spectrogram should indeed be checked.Here's an itemized troubleshooting guide:
-
Check the audio file itself: Make sure that the file exists where you expect it to be, that it is a valid WAV file that is not corrupt, and that it contains audio data.
-
Review the
generate_spectrogramcode: Ensure that the spectrogram generation process correctly handles the audio data. There may be an issue in the step where you convert the audio waveform to a spectrogram. This could include incorrect parameters forscipy.signal.spectrogramor issues with the windowing function or overlap settings. -
Review preprocessing steps: Before taking the
log10of thesgram, you might need to add a small offset to avoid taking the log of 0, for example,sgram = 20 * np.log10(sgram + epsilon)whereepsilonis a small constant (e.g.,1e-10). -
RuntimeWarning: To address the
RuntimeWarning, ensure that you're never taking the log of 0 or a negative number. The line you have should already be taking care of this (sgram = np.where(sgram > 0, 20 * np.log10(sgram), 0)), but the warning suggests that there may be 0s or negatives insgramanyway. It could be due to rounding errors or other issues during the process. Double-check thegenerate_spectrogramfunction to ensure it handles all edge cases correctly. -
Debug output: Add print statements in the
generate_spectrogramfunction to display intermediate output. This can help you determine where the process is failing. -
Audio file handling: Double-check the
read_audiofilemethod to confirm that it correctly reads the data from the WAV files. -
Seek patterns in the errors: See if these errors are consistent across multiple runs and multiple files. Clues about when the errors occur might help pinpoint the underlying issue.
-
Look at the logged data: You should log the
spectrogram,frequencies (f), andtimes (t)after their generation and before any further steps that could modify or invalidate them. -
Use Assertions: Assert conditions throughout your code to ensure your assumptions (e.g., file not being None, arrays containing the expected number of data points) hold true. This way, your code will raise errors immediately when an assumption is violated, making debugging easier.
When you address the above points, you will hopefully be able to determine why the spectrogram generation is failing and rectify the situation.
-