Created
September 19, 2013 18:18
-
-
Save wwwslinger/6627623 to your computer and use it in GitHub Desktop.
Read a .wav audio file, create the spectrogram, run ISA for components.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import wave, struct, numpy as np, matplotlib.mlab as mlab, pylab as pl | |
def wavToArr(wavefile): | |
w = wave.open(wavefile,"rb") | |
p = w.getparams() | |
s = w.readframes(p[3]) | |
w.close() | |
sd = np.fromstring(s, np.int16) | |
return sd,p | |
def wavToSpec(wavefile,log=False,norm=False): | |
wavArr,wavParams = wavToArr(wavefile) | |
print wavParams | |
return mlab.specgram(wavArr, NFFT=256,Fs=wavParams[2],detrend=detrend_mean,window=window_hanning,noverlap=128,sides='onesided',scale_by_freq=True) | |
wavArr,wavParams = wavToArr("bat_speech.wav") | |
hf = pl.figure(); ax=hf.add_subplot(1,1,1) | |
ax.plot(wavArr) | |
Pxx, freqs, bins = wavToSpec("bat_speech.wav") | |
Pxx += 0.0001 | |
freqs += (len(wavArr) / wavParams[2]) / 2. | |
hf=pl.figure(figsize=(12,12)); | |
ax = hf.add_subplot(2,1,1); | |
#plot spectrogram as decibals | |
hm = ax.imshow(10*np.log10(Pxx),interpolation='nearest',origin='lower',aspect='auto') | |
hf.colorbar(hm) | |
ylcnt = len(ax.get_yticklabels()) | |
ycnt = len(freqs) | |
ylstep = int(ycnt / ylcnt) | |
ax.set_yticklabels([ int(freqs[f]) for f in xrange(0,ycnt,ylstep) ]) | |
from sklearn.decomposition import PCA, FastICA | |
ncomps = 7 | |
# reduce dimensionality with PCA | |
pca = PCA(n_components=ncomps) | |
y = Pxx.copy().T | |
pc = pca.fit(y).transform(y) | |
# run ICA | |
ica = FastICA(n_components=ncomps,random_state=42) | |
z = ica.fit(pc).transform(pc).T | |
hf = pl.figure() | |
for p in xrange(ncomps): | |
ax = hf.add_subplot(ncomps,1,p+1) | |
ax.plot(z[p]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In my answer to http://dsp.stackexchange.com/questions/10767/audio-signal-separation-identifying-interest-points/10784#10784