-
Notifications
You must be signed in to change notification settings - Fork 3
/
2_hilbertize.py
executable file
·90 lines (81 loc) · 3.03 KB
/
2_hilbertize.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
#!/usr/bin/python3
from hilbertize_functions import final, dir#function to create final files
import matplotlib.pyplot as plt #directory for saving
import argparse
import os
import re
import png
import math
import csv
import numpy as np
from scipy.signal import hilbert
import matplotlib
matplotlib.use('Agg')
# Creating parser for powershell
parser = argparse.ArgumentParser(description='Hilbertizing sensor data')
parser.add_argument( # creates a list of csv files
'files',
metavar='FILES',
nargs='+',
help='List of CSV files with solo sensor data'
)
parser.add_argument(
'--window',
required=True,
type=int,
metavar='WINDOW',
help='Width of the frequency window'
)
args = parser.parse_args() # 2 arguments at all
cnt = 0
for one_file in args.files:
#for saving files by dirs
with open(dir.format(cnt)+one_file) as fi:
with open(dir.format(cnt)+one_file+"2_hilb.csv", 'w') as fo:
vals_init = [[float(x) for x in l.split()] for l in fi.readlines()]
vals = list(zip(*vals_init))
# Narrowband filtering + Hilbert transformation
res_fft = []
maxf = 0
for i, row in enumerate(vals):
fft = np.fft.rfft(row)
# this is to check the spectrum and find the carrying freauency
# near-zero freqencies somehow are exremely large
plt.plot(np.abs(fft)[10:])
maxf += np.abs(fft)[10:].argmax()
#plt.savefig(args.output + "_fft_" + str(i) + ".png")
# plt.clf()
mid = maxf/len(vals)
for i, row in enumerate(vals):
fft = np.fft.rfft(row)
for j, freq in enumerate(fft):
# if not (args.freq_min <= j <= args.freq_max):
if not (mid - args.window/2.0 <= j <= mid + args.window/2.0):
fft[j] = 0
res_fft.append(np.abs(hilbert(np.fft.irfft(fft))))
print(mid)
#saving spectrum
plt.savefig(dir.format(cnt)+ one_file + "2_spectrum_" + ".png")
plt.cla()
res = list(zip(*res_fft))
max_data = max(map(max, res))
min_data = min(map(min, res))
for r in res:
stri = ""
for t in r:
stri += str(round((t - min_data)/(max_data-min_data), 2))
stri += " "
print(stri, file=fo)
with open(dir.format(cnt)+ one_file+"2_HIHIHILB.csv", 'w') as fu:
for r in vals_init:
stri = ""
for t in r:
stri += str(round(t, 2))
stri += " "
print(stri, file=fu)
final(file=dir.format(cnt) +
one_file, val=vals_init, pref="2_prehilb.png")
# writing final graphics
final(file=dir.format(cnt) +
one_file, val=res, pref="2_hilb.png")
cnt+=1 #plussing to copy next files in another folder