Phase Maps

Music 103: Sound Analysis, Synthesis, Perception

Michael A. Casey, Dartmouth College


In [1]:
from bregman.suite import *

In [19]:
x,sr,fmt = wavread('/home/mkc/Music/Environmental/ChernobylDawn.wav', first=10*44100,last=20*44100)
F = LinearFrequencySpectrum(x[:,0], nfft=4096, wfft=4096, nhop=1024)
dphi = (2*pi * F.nhop * arange(F.nfft/2+1)) / float(F.nfft)
freqs = (F.sample_rate * arange(F.nfft/2+1)) / float(F.nfft)
afreqs = (2*pi * arange(F.nfft/2+1)) / float(F.nfft)
F.feature_plot(dbscale=1,normalize=1)



In [20]:
A = angle(F.STFT[:,64:66]) # Phase in middle of 4th note
U = (A[:,1] - A[:,0]) - dphi # Phase devisation (from channel's precession)
U = U - np.round(U/(2*pi))*2*pi # Phase unwrapping to range [-pi .. pi] 
plot(freqs,unwrap(U+dphi)/pi)
grid(); xlabel('Frequency'); ylabel('Phase'); axis('tight');title('Phase Map at time 65')


Out[20]:
<matplotlib.text.Text at 0xad3daf0c>

In [21]:
#A = diff(angle(F.STFT),1) # Complete Phase Map
#U = c_[angle(F.STFT[:,0]), A - matrix(dphi).T ]
#U = U - np.round(U/(2*pi))*2*pi
U = F._phase_map()
feature_plot(U,cmap=cm.jet)
title('Phase Deviations Map')
fig,axes = subplots(1, 2, figsize=(12.75,4))
im=axes[0].imshow(cos(U),aspect='auto',origin='bottom',cmap=cm.jet)
#feature_plot(cos(U),cmap=cm.hot,nofig=1)
axes[0].set_title('Phase Deviations Map (Cos)')
colorbar(im,ax=axes[0])

im=axes[1].imshow(sin(U), aspect='auto',origin='bottom',cmap=cm.jet)
#feature_plot(sin(U),cmap=cm.hot,nofig=1)
axes[1].set_title('Phase Deviations Map (Sin)')
colorbar(im,ax=axes[1])

fig.subplots_adjust(wspace=0.1)



In [5]:
%cd /home/mkc/exp/signals/wavelets
import sparseapprox as S


/home/mkc/exp/signals/wavelets

In [6]:
SS = S.SparseApproxSpectrum(n_components=25, patch_size=(4,4))
SS.extract_codes(U, standardize=0)


(1000000, 16)
Dictionary learning from data...
Out[6]:
<sparseapprox.SparseApproxSpectrum at 0xacff718c>

In [7]:
SS.plot_codes(cmap=cm.hot,interpolation=None)



In [8]:
SS.reconstruct_spectrum()


Out[8]:
<sparseapprox.SparseApproxSpectrum at 0xacff718c>

In [22]:
F.inverse(X_hat=randn(*F.X.shape)*atleast_2d(F.X.mean(1)).T,Phi_hat=SS.X_hat,pvoc=0)


Out[22]:
array([  0.00000000e+00,   5.61682343e-08,   9.10269575e-08, ...,
         4.76463388e-08,   4.62748146e-08,   0.00000000e+00])

In [23]:
play(balance_signal(F.x_hat))


Period size is 64 , Buffer size is 22050

In [24]:
dPhi = array(F.dPhi)
feature_plot(dPhi)
figure()
feature_plot(mod(np.diff(angle(F.STFT),axis=1)-atleast_2d(F.dphi).T,2*pi)-pi)


<matplotlib.figure.Figure at 0xac8b706c>

In [24]:


In [ ]: