-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathema1.py
115 lines (92 loc) · 3.24 KB
/
ema1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#coding:utf-8
# A class of Exponential Moving Average with Half-wave rectification, and smoothing via lpf
#
# Half-wave rectification until a few KHz signal.
# More than a few KHz signal is transformed to DC with ripple signal. And smooth ripple signal.
# Check version
# Python 3.6.4 on win32 (Windows 10)
# numpy 1.16.3
# matplotlib 2.1.1
# scipy 1.4.1
import numpy as np
from matplotlib import pyplot as plt
from scipy.signal import lfilter
from iir1 import *
class Class_EMA1(object):
def __init__(self, N=80, Clip_bottom=0.0, PrintOut=False):
# initalize
self.N= N
self.alfa= 2.0 / (self.N + 1.0)
self.Clip_bottom= Clip_bottom
if PrintOut:
print ('alfa (EMA)', self.alfa)
print ('half cycle (EMA)', self.N/2.8854)
def __call__(self, x, sr=48000, smooth=True):
# x dimension should be 1-zigenn
# Half-wave rectification
y= np.clip(x, self.Clip_bottom, None)
'''
# output numpy array
y2= np.empty( (len(x)), dtype=np.float32)
y2[0]= self.alfa * y[0]
for i in range( len(x) -1 ):
y2[i+1] = self.alfa * y[i] + (1.0- self.alfa) * y2[i]
'''
# use scipy's lfilter([b] [a]
y2,_ = lfilter( [self.alfa, 0.0],[1.0,self.alfa - 1.0], y, zi=[ y[0] * (1.0- self.alfa)])
if smooth:
return self.smoothing(y2, sr)
else:
return y2
def smoothing(self,x, sr=48000):
# smoothing via lpf
self.lpf=Class_IIR1(fc= sr / 30 , n_order=3, sampling_rate=sr)
return self.lpf.filtering(x)
def wav_show(self,y1,y2=None, y3=None, sr=48000):
# draw wavform
plt.figure()
plt.subplot(311)
plt.xlabel('time step')
plt.ylabel('amplitude')
tlist= np.arange( len(y1) ) * (1 / sr)
plt.plot( tlist, y1)
if y2 is not None:
plt.subplot(312)
plt.xlabel('time step')
plt.ylabel('amplitude')
tlist= np.arange( len(y2) ) * (1 /sr)
plt.plot( tlist, y2)
if y3 is not None:
plt.subplot(313)
plt.xlabel('time step')
plt.ylabel('amplitude')
tlist= np.arange( len(y3) ) * (1 / sr)
plt.plot( tlist, y3)
plt.grid()
plt.axis('tight')
plt.show()
if __name__ == '__main__':
#
from scipy import signal
from scipy.io.wavfile import read as wavread
# instance
ema1= Class_EMA1(PrintOut=True)
# load a sample wav
#path0='wav/400Hz-10dB_44100Hz_400msec.wav'
#path0='wav/1KHz-10dB_44100Hz_400msec.wav'
path0='wav/3KHz-10dB_44100Hz_400msec.wav'
#path0='wav/5KHz-10dB_44100Hz_400msec.wav'
try:
sr, y = wavread(path0)
except:
print ('error: wavread ')
sys.exit()
else:
yg= y / (2 ** 15)
print ('sampling rate ', sr)
print ('y.shape', yg.shape)
# process
y2=ema1( yg, sr, smooth=False)
y3=ema1.smoothing( y2, sr)
# draw wav
ema1.wav_show(yg, y2, y3, sr=sr)