Question: Implement compute _ spectrogram to generate the spectrogram from an audio signal. Use audio _ utils.local _ peaks ( ) to detect local peaks. Apply
Implement computespectrogram to generate the spectrogram from an audio signal.
Use audioutils.localpeaks to detect local peaks.
Apply thresholding to select the largest peaks.
Build a database of peak pairs using the specified constraints and store them using a hash table.
Implement the song identification function, comparing the fingerprint of a recorded or loaded clip against the database.
Create a commandline interface to control the programs behavior.
Test your implementation using the provided unit tests. audioutils:
import numpy as np
import pyaudio
from scipy import ndimage, signal
from scipy import io as spio
CHUNK
FORMAT pyaudio.paInt
RATE
CHANNELS
def recordaudioduration fs:
Sets up the audio stream and records audio for a given duration.
Parameters
duration : float
duration of the recording in seconds
fs : int
sampling rate in Hz
Returns
ndarray
mono audio signal sampled at fs Hz and normalized to have a zero mean
p pyaudio.PyAudio
# printpgetdefaultinputdeviceinfo
stream popen
formatFORMAT,
channelsCHANNELS,
rateRATE,
inputTrue,
framesperbufferCHUNK,
print recording"
frames
for in range intRATE CHUNK duration:
data stream.readCHUNK
frames.appendnpfrombufferdata dtypenpint
print done recording"
stream.stopstream
stream.close
pterminate
audio npconcatenateframesastypefloat
audio resampleaudio RATE, fs
audio npmeanaudio
return audio
def wavprocessingwavfile, fs:
Loads a wav file and resamples it to fs Hz
Parameters
wavfile : str
path to the wave file
fs : int
desired sampling rate in Hz
Returns
ndarray
audio signal sampled at fs Hz and normalized to have a zero mean
# Load the audio file
audioall spio.wavfile.readwavfile
fsorig audioall
audio audioallastypefloat
# combine channels
if lenaudioshape:
audio npmeanaudio axis
# remove the mean
audio npmeanaudio
# resample the audio file
audio resampleaudio fsorig, fs
return audio
def localpeaksspectrum gs:
Finds the location of the local peaks in a gs x gs neighborhood.
Parameters
spectrum : ndarray
D array, the spectrogram of the signal
gs : int
neighborhood size
Returns
ndarray
boolean array, the local peaks
# create a footprint for the maximum filter
footprint nponesgs gs
# apply the maximum filter to the spectrum
maxspectrum ndimage.maximumfilterspectrum footprintfootprint
# the local peaks are where the original spectrum is equal to the filtered spectrum
localpeak spectrum maxspectrum
return localpeak
def resamplext fsold, fsnew:
# resample the audio file to kHz
denom npgcdfsold, fsnew
L fsold denom
M fsnew denom
xt signal.resamplepolyxt M L
return xt
if namemain:
fs
duration
audiotest recordaudioduration fs
spio.wavfile.writetestwav", fs audiotest.astypenpint
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
