Mercurial > hg > audio-bootcamp-planning
changeset 3:8af0cef242b8
added two drum audio files and some block by block processing and plottig examples
| author | Adam <adamstark.uk@gmail.com> |
|---|---|
| date | Wed, 12 Sep 2012 21:38:54 +0100 |
| parents | 7b9a06f64ab1 |
| children | 2b996e1d64da |
| files | drums_mono.wav drums_stereo.wav dsp-block-by-block.py plotting.py |
| diffstat | 4 files changed, 89 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dsp-block-by-block.py Wed Sep 12 21:38:54 2012 +0100 @@ -0,0 +1,41 @@ +import numpy as np +from scikits.audiolab import wavread + +################################################# +############ EXTRACT AUDIO FROM FILE ############ +################################################# + +x, fs, enc = wavread("viola.wav") + + +################################################# +#### CALCULATE RMS OF EACH AUDIO BLOCK #### +################################################# + +hop_size = 2048 # set hop size +frame_size = 4096 # set frame size +frame = np.zeros(frame_size) # initialise frame with zeros +window = np.hanning(frame_size) # create window of the same length as the hop size + +# create empty numpy array to hold our +rms = np.array([]) + +# run through signal frame by frame +for n in range(0,x.size-hop_size,hop_size): + + # extract a segment of length hop_size + buffer = x[n:n+hop_size] + + # add new segment to frame, shifting back samples of frame + frame = np.append(frame[hop_size:frame_size],buffer) + + # calculate RMS + rms_val = np.sqrt(np.power(frame,2).mean()) + + # add amplitude to our numpy array + rms = np.append(rms,rms_val) + +print rms + + + \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plotting.py Wed Sep 12 21:38:54 2012 +0100 @@ -0,0 +1,48 @@ +import numpy as np +from scikits.audiolab import wavread +from matplotlib import pylab as plt + +################################################# +############ EXTRACT AUDIO FROM FILE ############ +################################################# + +x, fs, enc = wavread("drums_mono.wav") + + +################################################# +#### CALCULATE RMS OF EACH AUDIO BLOCK #### +################################################# + +hop_size = 1024 # set hop size +frame_size = hop_size*2 # set frame size +frame = np.zeros(frame_size) # initialise frame with zeros +window = np.hanning(frame_size) # create window of the same length as the hop size + +# create empty numpy array to hold our +rms = np.array([]) + +# run through signal frame by frame +for n in range(0,x.size-hop_size,hop_size): + + # extract a segment of length hop_size + buffer = x[n:n+hop_size] + + # add new segment to frame, shifting back samples of frame + frame = np.append(frame[hop_size:frame_size],buffer) + + # calculate RMS + rms_val = np.sqrt(np.power(frame,2).mean()) + + # add amplitude to our numpy array + rms = np.append(rms,rms_val) + +print rms + + +plt.plot(rms) +plt.title("RMS") +plt.xlabel("time") +plt.ylabel("value") +plt.show() + +
