MAT 201A Winter 2016
Homework 5
yitianshao@umail.ucsb.edu
Using the same audio files from HW4, find the index (lag) of the most prominent peak in each clip's autocorrelation function. Compute the frequencies (in Hz) corresponding to these peaks. How do these results compare to your findings in HW4?
# MAT 201A Winter 2016
# Homework 5 : Using the same audio files from HW4, find the index (lag) of the most prominent peak in each clip's
# autocorrelation function. Compute the frequencies (in Hz) corresponding to these peaks. How do these results compare
# to your findings in HW4?
# Hint: use the acorr function and compute the periods corresponding to each peak using the lags (recall that these
# files are sampled at 44100 Hz).
# ======================================================================================================================
__author__ = 'Yitian Shao'
# yitianshao@umail.ucsb.edu
# Created on 02/06/2016
# The code convert a input audio signal to a image data (spectrogram)
# ======================================================================================================================
%pylab inline
from __future__ import print_function
from __future__ import division
from scipy.io import wavfile
from IPython.display import Audio
warnings.filterwarnings("ignore",category=DeprecationWarning) # Ignore warning
matplotlib.rcParams.update({'font.size': 16}) # Resize the font in figures
# ----------------------------------------------------------------------------------------------------------------------
# Import audios and play them (glockenspiel.wav, piano.wav, tom.wav)
sr1,glockenspiel = wavfile.read('glockenspiel.wav')
sr2,piano = wavfile.read('piano.wav')
sr3,tom = wavfile.read('tom.wav')
# ----------------------------------------------------------------------------------------------------------------------
# Plot the audio signal "glockenspiel"
figure(figsize=(25,6))
plot(glockenspiel)
xlabel('Sample number')
ylabel('Magnitude')
title('glockenspiel')
# Play the audio signal "glockenspiel"
Audio(glockenspiel, rate = sr1)
Populating the interactive namespace from numpy and matplotlib
# Plot the audio signal "piano"
figure(figsize=(25,6))
plot(glockenspiel)
xlabel('Sample number')
ylabel('Magnitude')
title('piano')
# Play the audio signal "piano"
Audio(piano, rate = sr2)
# Plot the audio signal "tom"
figure(figsize=(25,6))
plot(glockenspiel)
xlabel('Sample number')
ylabel('Magnitude')
title('tom')
# Play the audio signal "tom"
Audio(tom, rate = sr3)
# ----------------------------------------------------------------------------------------------------------------------
# Function to find the most prominent peak
# ----------------------------------------------------------------------------------------------------------------------
def specPeak( sig, sr, pltRangeX ):
# Initialize a figure for display purpose
figure(figsize=(25,15))
subplot(2,1,1)
# Perform auto-correlation of the input signal
lags, c, _, _ = acorr(sig.astype(double), maxlags = 0.5 * sr);
# Keep only auto-correlation results with positve lags
lags = lags[ceil(len(lags)/2):]
c = c[ceil(len(c)/2):]
# Conver auto-correlation results to binary value
biTh = where(c > 0.1, 1, -1)
# Compute the difference of the binary auto-correlation results
corrDiff = diff(biTh)/2
# Find the index of the head (Beginning point) of period during which exact one highest correlation exists
headInd = lags[corrDiff == 1]
# Compute difference for every two adjacent period head index in order to compute the frequency of the period
# (The difference between two adjacent period head index equals to period length)
peakFreqs = sr/diff(headInd) # (peakFreq = 1/( (1/sr) * period_length )
# Display -----------------------------------------------------------------------
# Plot the procedure of obtaining period frequencies (of the peak)
plot(c , 'r')
plot(biTh, 'c--')
plot(headInd, corrDiff[corrDiff == 1], 'ro')
xlim(0, pltRangeX)
ylim(-1.2, 1.2)
xlabel('Sampling point')
ylabel('Magnitude')
title('Obtaining period frequencies (result trancated for display)')
# Plot the histogram of these identified period frequencies (of the peak)
subplot(2,1,2)
hist(peakFreqs, 40, normed = True, facecolor = 'red', alpha = 0.5)
xlabel('Frequency (Hz)')
ylabel('Percentage (%) ')
title('Possible peak frequency')
# -------------------------------------------------------------------------------
# Use median to decide the peak frequency (Final results)
return median(peakFreqs)
peakFreq1 = specPeak( glockenspiel, sr1, 200 )
print('The most prominent peak frequency of "glockenspiel" is %.2f Hz.' % peakFreq1)
The most prominent peak frequency of "glockenspiel" is 1336.36 Hz.
peakFreq2 = specPeak( piano, sr2, 3000 )
print('The most prominent peak frequency of "piano" is %.2f Hz.' % peakFreq2)
The most prominent peak frequency of "piano" is 77.64 Hz.
peakFreq3 = specPeak( tom, sr3, 3000 )
print('The most prominent peak frequency of "tom" is %.2f Hz.' % peakFreq3)
The most prominent peak frequency of "tom" is 91.12 Hz.