Revision 3:8af0cef242b8

View differences:

dsp-block-by-block.py
1
import numpy as np
2
from scikits.audiolab import wavread
3

  
4
#################################################
5
############ EXTRACT AUDIO FROM FILE ############
6
#################################################
7

  
8
x, fs, enc = wavread("viola.wav")
9

  
10

  
11
#################################################
12
#### CALCULATE RMS OF EACH AUDIO BLOCK ####
13
#################################################
14

  
15
hop_size = 2048                             # set hop size
16
frame_size = 4096                           # set frame size
17
frame = np.zeros(frame_size)                # initialise frame with zeros
18
window = np.hanning(frame_size)             # create window of the same length as the hop size
19

  
20
# create empty numpy array to hold our 
21
rms = np.array([])
22

  
23
# run through signal frame by frame 
24
for n in range(0,x.size-hop_size,hop_size):
25
    
26
    # extract a segment of length hop_size
27
    buffer = x[n:n+hop_size]                               
28
    
29
    # add new segment to frame, shifting back samples of frame
30
    frame = np.append(frame[hop_size:frame_size],buffer)  
31
    
32
    # calculate RMS
33
    rms_val = np.sqrt(np.power(frame,2).mean())
34
    
35
    # add amplitude to our numpy array
36
    rms = np.append(rms,rms_val)
37

  
38
print rms
39
    
40
                                   
41
 
plotting.py
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
                                   

Also available in: Unified diff