In [1]:
# PLCA Example                                                                  
import os
import os.path
import bregman
from bregman.suite import *

In [3]:
examples_path = os.path.split(bregman.__file__)[0]+os.sep+'examples'+os.sep+'5_plca.py'
execfile(examples_path)


Example 1: Standard PLCA with cqft features split into n=4 components
Period size is 32 , Buffer size is 22050
Period size is 32 , Buffer size is 22050
Period size is 32 , Buffer size is 22050
Period size is 32 , Buffer size is 22050

In [3]:
play(x) # the amen break is loaded for you                                      
p # inspect the feature parameters


Period size is 64 , Buffer size is 22050
Out[3]:
{'feature': 'cqft',
 'hi': 16000,
 'intensify': False,
 'lcoef': 1,
 'lo': 62.5,
 'log10': False,
 'magnitude': True,
 'nbpo': 12,
 'ncoef': 10,
 'nfft': 16384,
 'nhop': 2048,
 'onsets': False,
 'power_ext': '.power',
 'sample_rate': 44100,
 'verbosity': 0,
 'wfft': 8192,
 'window': 'hamm'}

In [4]:
#1. PLCA, run the following examples                                            
#help ex_1a()                                                                   
w,z,h = ex_1a(x, p, n=4)


Example 1: Standard PLCA with cqft features split into n=4 components
Period size is 32 , Buffer size is 22050
Period size is 32 , Buffer size is 22050
Period size is 32 , Buffer size is 22050
Period size is 32 , Buffer size is 22050

In [ ]:
#help ex_1b()                                                                   
#try the following with different values of alphaZ, alphaH, n                   
w,z,h = ex_1b(x, p, n=4, alphaZ=-0.01, alphaH=-0.00001)

In [5]:
#2. Shift invariant PLCA (time-shift only)                                      
#Using a 2D basis with a 1D convolution kenel for each component                

#help ex_2a()                                                                   
# Try the following with different values of n and win                          
w,z,h = ex_2a(x, p, n=4, win=5)
imagesc(w[:,0,:]) # show first basis function                                   
imagesc(w[:,0,:]) # show second basis function                                  
# etc..                                                                         
figure()
plot(h[0,:]) # plot first time kernel                                           
plot(h[1,:]) # plot second time kernel


Example 2: Time-shift invariant PLCA with cqft features split into n=4 components, component window length=5
Period size is 32 , Buffer size is 22050
Period size is 32 , Buffer size is 22050
Period size is 32 , Buffer size is 22050
Period size is 32 , Buffer size is 22050
Out[5]:
[<matplotlib.lines.Line2D at 0xaf2f112c>]

In [ ]:
#help ex_2b()                                                                   
# Try the following with different values of alphaZ, alphaH, win                
w,z,h = ex_2b(x, p, n=10, win=5, alphaZ=-0.01, alphaH=-0.00001)

In [ ]:
#3. 2D shift-invariant PLCA, no shift in frequency                              
#help ex_3a                                                                     
# Try the following with different values of n and win=(1,10) and (1,3)         
w,z,h = ex_3a(x, p, n=4, win=(1,5))
imagesc(w[:,0,:]) # show first basis function                                   
imagesc(h[0,:,:]) # show first 2D convolution kernel (actually 1D, no freq shif\
t)

In [6]:
#help ex_3b()                                                                   
w,z,h = ex_3b(x, p, n=1, win=(24,5))
imagesc(w[:,0,:]) # show only basis function                                    
imagesc(h[0,:,:]) # show only 2D convolution kernel (with freq shifts)


Example 3: Time-frequency-shift invariant PLCA with cqft features split into n=1 components, component num shifts=24,window length=5
Period size is 32 , Buffer size is 22050

In [7]:
# 4. Doing it on a pitched example                                              
audio_file = audio_dir+os.sep+'gmin.wav'
x, sr, fmt = wavread(audio_file)
p['nbpo'] = 36 # increase CQFT frequency resolution                             
w,z,h = ex_3b(x, p, n=6, win=(36,5))
imagesc(w[:,0,:]) # show only basis function                                    
imagesc(h[0,:,:]) # show only 2D convolution kernel (with freq shifts)          
# The latter is a piono roll transcription, with wrapping at the extremes of the pitch range                                                                   
# So, we have essentially achived an automatic transcription.


Example 3: Time-frequency-shift invariant PLCA with cqft features split into n=6 components, component num shifts=36,window length=5
Period size is 32 , Buffer size is 22050
Period size is 32 , Buffer size is 22050
Period size is 32 , Buffer size is 22050
Period size is 32 , Buffer size is 22050
Period size is 32 , Buffer size is 22050
Period size is 32 , Buffer size is 22050

In [6]:
# Reconstruction
F = LogFrequencySpectrum(x, **p)
X = SIPLCA2.reconstruct(w,z,h, circular=False)
F.X = X

In [7]:
x_hat = F.inverse(usewin=True)
play(balance_signal(x_hat))


Period size is 64 , Buffer size is 22050

In [8]:
# 5.                                                                            
# For a 20s-30s piece of music of your choosing, perform a 2D shift-invariant analysis                                                                         
# optimize the parameters so that your analysis "makes sense"                   
# Be prepared to present your analysis, and any interesting conclusions, in class. Make a 2-3 page writeup of your analysis,                                   
# with figures.                                                                 
#                                                                               
# Some Ideas: finding riffs in pop music, analysis of sound objects in Musique Concrete, analysis of texture in Ligeti or Lachemann                            
#

In [ ]: