To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
The primary repository for this project is hosted at https://github.com/Codasign/york-software-bootcamp-audio-day.git .
This repository is a read-only copy which is updated automatically every hour.
root / plotting.py @ 3:8af0cef242b8
History | View | Annotate | Download (1.35 KB)
| 1 |
import numpy as np |
|---|---|
| 2 |
from scikits.audiolab import wavread |
| 3 |
from matplotlib import pylab as plt |
| 4 |
|
| 5 |
#################################################
|
| 6 |
############ EXTRACT AUDIO FROM FILE ############
|
| 7 |
#################################################
|
| 8 |
|
| 9 |
x, fs, enc = wavread("drums_mono.wav")
|
| 10 |
|
| 11 |
|
| 12 |
#################################################
|
| 13 |
#### CALCULATE RMS OF EACH AUDIO BLOCK ####
|
| 14 |
#################################################
|
| 15 |
|
| 16 |
hop_size = 1024 # set hop size |
| 17 |
frame_size = hop_size*2 # set frame size |
| 18 |
frame = np.zeros(frame_size) # initialise frame with zeros
|
| 19 |
window = np.hanning(frame_size) # create window of the same length as the hop size
|
| 20 |
|
| 21 |
# create empty numpy array to hold our
|
| 22 |
rms = np.array([]) |
| 23 |
|
| 24 |
# run through signal frame by frame
|
| 25 |
for n in range(0,x.size-hop_size,hop_size): |
| 26 |
|
| 27 |
# extract a segment of length hop_size
|
| 28 |
buffer = x[n:n+hop_size] |
| 29 |
|
| 30 |
# add new segment to frame, shifting back samples of frame
|
| 31 |
frame = np.append(frame[hop_size:frame_size],buffer)
|
| 32 |
|
| 33 |
# calculate RMS
|
| 34 |
rms_val = np.sqrt(np.power(frame,2).mean())
|
| 35 |
|
| 36 |
# add amplitude to our numpy array
|
| 37 |
rms = np.append(rms,rms_val) |
| 38 |
|
| 39 |
print rms
|
| 40 |
|
| 41 |
|
| 42 |
plt.plot(rms) |
| 43 |
plt.title("RMS")
|
| 44 |
plt.xlabel("time")
|
| 45 |
plt.ylabel("value")
|
| 46 |
plt.show() |
| 47 |
|
| 48 |
|