forked from nswarup14/Structural-Health-Monitoring
/
CWT_noise.py
67 lines (50 loc) · 1.37 KB
/
CWT_noise.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
import numpy as np
import pywt
import matplotlib.pyplot as plt
import csv
import sys
import math
from pandas import read_csv
import pandas as pd
def main(file_name):
d=0; #take d also as input
#load a data y
y = []
"""
with open(file_name,'r+') as f:
reader = csv.reader(f,delimiter = ' ', quotechar='|')
for number in reader:
y.append(number[d])
ts=y
(ca, cd) = pywt.dwt(ts,'haar')
"""
series = read_csv(file_name, header=0, index_col=0, squeeze=True)
series.columns = ['a', 'b', 'c', 'd']
series['e'] = pow((series['a']*series['a'] + series['b']*series['b'] + series['c']*series['c']), 0.5)
df1 = pd.DataFrame({'$a': series['e']})
df= df1.iloc[:,0]
#print(df)
(ca, cd) = pywt.dwt(df,'haar')
cat = pywt.threshold(ca, np.std(ca)/2, 'soft')
cdt = pywt.threshold(cd, np.std(cd)/2, 'soft')
ts_rec = pywt.idwt(cat, cdt, 'haar')
plt.close('all')
plt.subplot(211)
# Original coefficients
plt.plot(ca, '--*b')
plt.plot(cd, '--*r')
# Thresholded coefficients
plt.plot(cat, '--*c')
plt.plot(cdt, '--*m')
plt.legend(['ca','cd','ca_thresh', 'cd_thresh'], loc=0)
plt.grid(True)
plt.subplot(212)
#plt.plot(ts)
plt.plot(df)
#plt.hold('on')
plt.plot(ts_rec, 'r')
plt.legend(['original signal', 'reconstructed signal'])
plt.grid(True)
plt.show()
if __name__=='__main__':
main()