#!/usr/bin/env python # coding: utf-8 # ## [HW4] # MAT 201A Winter | Jing Yan | theuniqueeye@gmail.com # #### Description: # Step1 : To three audio files, I compute the DFTs of the entire files and then identify the indexs of the most prominent peaks in the magnitude spectrums. # # Step2: Take the sample rate as 44100Hz, I Calculate the frequencies (in Hz) correspond to the bins with the most prominent peaks. # In[33]: # import libraries get_ipython().run_line_magic('pylab', 'inline') from __future__ import print_function from __future__ import division # In[34]: # read three audio files from scipy.io import wavfile glockenspiel = wavfile.read('glockenspiel.wav') piano = wavfile.read('piano.wav') tom = wavfile.read('tom.wav') # In[35]: # print the information of three wave files print("glockenspiel:", glockenspiel) print("piano:", piano) print("tom:", tom) # In[36]: # diaplay of each audio from IPython.display import Audio Audio(glockenspiel[1],rate=44100) # In[37]: Audio(piano[1],rate=44100) # In[38]: Audio(tom[1],rate=44100) # In[85]: # do DFT to each entire file a = fft.rfft(glockenspiel[1]) b = fft.rfft(piano[1]) c = fft.rfft(tom[1]) # print("a:", a, "b:", b, "c:", c) # the output is a complex ndarray # In[150]: # draw the magnitude spectrum of the DFT results rcParams['figure.figsize'] = (12, 12) subplot(3,1,1) plot(abs(a)) xlabel("frequency",size = 8) ylabel("magnitude",size = 8) title("the magnitude spectrum of glockenspiel") subplot(3,1,2) plot(abs(b)) xlabel("frequency",size = 8) ylabel("magnitude",size = 8) title("the magnitude spectrum of piano") subplot(3,1,3) plot(abs(c)) xlabel("frequency",size = 8) ylabel("magnitude",size = 8) title("the magnitude spectrum of tom") # In[67]: # compute the index of the most prominent peak index_a = argmax(abs(a)) index_b = argmax(abs(b)) index_c = argmax(abs(c)) print(index_a, index_b, index_c) # In[103]: max(abs(c)) # #### Thus, the indexs of the most prominent peak in those magnitude spectrum is 1158 for glockenspiel, 58 for piano, 81 for tom. # In[151]: # show the position of maximum magnitude with red dot. subplot(3,1,1) xlim(0,2000) plot(abs(a)) xlabel("frequency",size = 8) ylabel("magnitude",size = 8) title("zoom in view of the magnitude spectrum of glockenspiel") scatter(index_a, max(abs(a)), 50, color ='red') subplot(3,1,2) xlim(0,200) plot(abs(b)) xlabel("frequency",size = 8) ylabel("magnitude",size = 8) title("zoom in view of the magnitude spectrum of piano") scatter(index_b, max(abs(b)), 50, color ='red') subplot(3,1,3) xlim(0,200) plot(abs(c)) xlabel("frequency",size = 8) ylabel("magnitude",size = 8) title("zoom in view of the magnitude spectrum of tom") scatter(index_c, max(abs(c)), 50, color ='red') # In[80]: sr = 44100 freq_a = index_a / len(abs(a)) * (sr/2) freq_b = index_b / len(abs(b)) * (sr/2) freq_c = index_c / len(abs(c)) * (sr/2) print(freq_a,freq_b, freq_c) # #### Thus, the frequencies (in Hz) correspond to the bins with the most prominent peaks is 1323.27Hz for glockenspiel, 78.05Hz for piano, and 92.56Hz for tom. #