From 5411913a980da3c0a11442f8ffd723d75d5fb265 Mon Sep 17 00:00:00 2001 From: Chigozirim Igweamaka Date: Sat, 8 Mar 2025 07:43:24 +0100 Subject: [PATCH] refactor: improve comments and variable names for clarity. --- shazam/fft.go | 8 ++++---- shazam/fingerprint.go | 1 - shazam/image.go | 3 --- shazam/spectrogram.go | 17 ++++++++--------- 4 files changed, 12 insertions(+), 17 deletions(-) diff --git a/shazam/fft.go b/shazam/fft.go index a3d3198..2fee6cb 100644 --- a/shazam/fft.go +++ b/shazam/fft.go @@ -4,20 +4,20 @@ import ( "math" ) -// Fft performs the Fast Fourier Transform on the input signal. +// FFT computes the Fast Fourier Transform (FFT) of the input data, +// converting the signal from the time domain to the frequency domain. +// For better understanding, refer to this video: https://www.youtube.com/watch?v=spUNpyF58BY func FFT(input []float64) []complex128 { - // Convert input to complex128 complexArray := make([]complex128, len(input)) for i, v := range input { complexArray[i] = complex(v, 0) } fftResult := make([]complex128, len(complexArray)) - copy(fftResult, complexArray) // Copy input to result buffer + copy(fftResult, complexArray) return recursiveFFT(fftResult) } -// recursiveFFT performs the recursive FFT algorithm. func recursiveFFT(complexArray []complex128) []complex128 { N := len(complexArray) if N <= 1 { diff --git a/shazam/fingerprint.go b/shazam/fingerprint.go index 046cffd..a29398b 100644 --- a/shazam/fingerprint.go +++ b/shazam/fingerprint.go @@ -11,7 +11,6 @@ const ( ) // Fingerprint generates fingerprints from a list of peaks and stores them in an array. -// The fingerprints are encoded using a 32-bit integer format and stored in an array. // Each fingerprint consists of an address and a couple. // The address is a hash. The couple contains the anchor time and the song ID. func Fingerprint(peaks []Peak, songID uint32) map[uint32]models.Couple { diff --git a/shazam/image.go b/shazam/image.go index fb87a22..0a758a2 100644 --- a/shazam/image.go +++ b/shazam/image.go @@ -11,11 +11,9 @@ import ( // ConvertSpectrogramToImage converts a spectrogram to a heat map image func SpectrogramToImage(spectrogram [][]complex128, outputPath string) error { - // Determine dimensions of the spectrogram numWindows := len(spectrogram) numFreqBins := len(spectrogram[0]) - // Create a new grayscale image img := image.NewGray(image.Rect(0, 0, numFreqBins, numWindows)) // Scale the values in the spectrogram to the range [0, 255] @@ -38,7 +36,6 @@ func SpectrogramToImage(spectrogram [][]complex128, outputPath string) error { } } - // Save the image to a PNG file file, err := os.Create(outputPath) if err != nil { return err diff --git a/shazam/spectrogram.go b/shazam/spectrogram.go index 6f2ebc8..e18ae53 100644 --- a/shazam/spectrogram.go +++ b/shazam/spectrogram.go @@ -14,19 +14,18 @@ const ( hopSize = freqBinSize / 32 ) -func Spectrogram(samples []float64, sampleRate int) ([][]complex128, error) { +func Spectrogram(sample []float64, sampleRate int) ([][]complex128, error) { lpf := NewLowPassFilter(maxFreq, float64(sampleRate)) - filteredSamples := lpf.Filter(samples) + filteredSample := lpf.Filter(sample) - downsampledSamples, err := Downsample(filteredSamples, sampleRate, sampleRate/dspRatio) + downsampledSample, err := Downsample(filteredSample, sampleRate, sampleRate/dspRatio) if err != nil { - return nil, fmt.Errorf("couldn't downsample audio samples: %v", err) + return nil, fmt.Errorf("couldn't downsample audio sample: %v", err) } - numOfWindows := len(downsampledSamples) / (freqBinSize - hopSize) + numOfWindows := len(downsampledSample) / (freqBinSize - hopSize) spectrogram := make([][]complex128, numOfWindows) - // Apply Hamming window function window := make([]float64, freqBinSize) for i := range window { window[i] = 0.54 - 0.46*math.Cos(2*math.Pi*float64(i)/(float64(freqBinSize)-1)) @@ -36,12 +35,12 @@ func Spectrogram(samples []float64, sampleRate int) ([][]complex128, error) { for i := 0; i < numOfWindows; i++ { start := i * hopSize end := start + freqBinSize - if end > len(downsampledSamples) { - end = len(downsampledSamples) + if end > len(downsampledSample) { + end = len(downsampledSample) } bin := make([]float64, freqBinSize) - copy(bin, downsampledSamples[start:end]) + copy(bin, downsampledSample[start:end]) // Apply Hamming window for j := range window {