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.

Statistics Download as Zip
| Branch: | Revision:

root / 9-dsp-block-by-block.py

History | View | Annotate | Download (1.71 KB)

1 4:2b996e1d64da adamstark
import numpy as np
2 6:fd0f9d0615b2 adamstark
import pylab as plt
3 4:2b996e1d64da adamstark
from scikits.audiolab import wavread
4
5
#################################################
6
############ EXTRACT AUDIO FROM FILE ############
7
#################################################
8
9
x, fs, enc = wavread("viola.wav")
10
11
12
#################################################
13 6:fd0f9d0615b2 adamstark
####### CALCULATE RMS OF EACH AUDIO BLOCK #######
14 4:2b996e1d64da adamstark
#################################################
15
16 6:fd0f9d0615b2 adamstark
hop_size = 512                             # set hop size
17
frame_size = hop_size*2                     # set frame size
18 4:2b996e1d64da adamstark
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 6:fd0f9d0615b2 adamstark
    frame = x[n:n+hop_size]
29
30
    # calculate RMS
31
    rms_val = np.sqrt(np.power(frame,2).mean())
32
33
    # add amplitude to our numpy array
34
    rms = np.append(rms,rms_val)
35
36
print rms
37
38
""" I AM THE OVERLAP VERSION
39
# run through signal frame by frame
40
for n in range(0,x.size-hop_size,hop_size):
41

42
    # extract a segment of length hop_size
43 4:2b996e1d64da adamstark
    buffer = x[n:n+hop_size]
44

45
    # add new segment to frame, shifting back samples of frame
46
    frame = np.append(frame[hop_size:frame_size],buffer)
47

48
    # calculate RMS
49
    rms_val = np.sqrt(np.power(frame,2).mean())
50

51
    # add amplitude to our numpy array
52
    rms = np.append(rms,rms_val)
53

54
print rms
55 6:fd0f9d0615b2 adamstark
"""
56
57
plt.plot(rms)
58
plt.title("RMS")
59
plt.xlabel("time")
60
plt.ylabel("value")
61
plt.show()
62 4:2b996e1d64da adamstark