-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeana.py
138 lines (109 loc) · 3.88 KB
/
speana.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# -*- coding: utf-8 -*-
"""
Name:
Purpose:
Specification:
Environment:
Python 3.6.0
"""
import numpy as np
from PyQt4 import QtGui, QtCore
import pyqtgraph as pg
import sys
import scipy.fftpack
from scipy import signal
import pandas as pd
class SpeAna(QtGui.QWidget):
def __init__(self):
super().__init__()
self.time = None
self.sig = None
self.waveGraph = WaveGraph()
self.tLabel = QtGui.QLabel('Time Interval [s]')
self.tEdit = QtGui.QLineEdit('5E-6')
self.tEdit.textChanged.connect(self.handleTime)
self.fileBtn = QtGui.QPushButton('Open File')
self.fileBtn.clicked.connect(self.showFileDialog)
self.fileIdc = QtGui.QLineEdit('None')
self.fileIdc.setReadOnly(True)
self.adpLabel = QtGui.QLabel('Data of ')
self.adpEdit = QtGui.QComboBox()
self.adpEdit.addItem('Keyence')
self.adpEdit.addItem('Hara')
self.execBtn = QtGui.QPushButton("Analyze")
self.execBtn.clicked.connect(self.execFFT)
self.winLabel = QtGui.QLabel('Window')
self.winEdit = QtGui.QComboBox()
self.winEdit.addItem('Rect')
self.winEdit.addItem('Hanning')
grid = QtGui.QGridLayout()
self.setLayout(grid)
grid.addWidget(self.fileBtn, 0, 0)
grid.addWidget(self.fileIdc, 0, 1)
grid.addWidget(self.adpLabel, 0, 2)
grid.addWidget(self.adpEdit, 0, 3)
grid.addWidget(self.tLabel, 1, 0)
grid.addWidget(self.tEdit, 1, 1)
grid.addWidget(self.winLabel, 1, 2)
grid.addWidget(self.winEdit, 1, 3)
grid.addWidget(self.execBtn, 2, 0, 1, 4)
grid.addWidget(self.waveGraph, 3, 0, 1, 4)
def handleTime(self, text):
try:
dt = float(text)
N = len(self.sig)
self.time = np.arange(0, N*dt, dt)
self.waveGraph.draw0(self.time, self.sig)
except ValueError:
pass
def showFileDialog(self):
fd = QtGui.QFileDialog()
self.fName = fd.getOpenFileName()
self.fileIdc.setText(self.fName)
self.aqData()
def aqData(self):
if self.adpEdit.currentText() == 'Keyence':
df = pd.read_csv(self.fName, encoding='cp932', header=None)
self.sig = np.array(df.iloc[:, 2])
elif self.adpEdit.currentText() == 'Hara':
df = pd.read_csv(self.fName, encoding='cp932', header=None)
self.sig = np.array(df.iloc[:, 5])
N = len(self.sig)
dt = float(self.tEdit.text())
self.time = np.arange(0, N*dt, dt)
self.waveGraph.draw0(self.time, self.sig)
def execFFT(self):
N = len(self.time)
dt = float(self.tEdit.text())
winName = self.winEdit.currentText()
if winName == 'Rect':
win = np.ones(N)
elif winName == 'Hanning':
win = signal.hann(N)
fSig = scipy.fftpack.fft(self.sig * win)
freq = scipy.fftpack.fftfreq(N, dt)
ampSig = [np.sqrt(c.real ** 2 + c.imag ** 2) for c in fSig]
self.waveGraph.draw1(freq[:int(N/2)], ampSig[:int(N/2)])
class WaveGraph(pg.GraphicsWindow):
def __init__(self):
super().__init__()
self.plot0 = self.addPlot(row=1, col=0)
self.plot0.setLabels(bottom=('time', 's'), left=('signal', 'a.u.'))
self.plot0.showGrid(x=True, y=True)
self.plot1 = self.addPlot(row=1, col=1)
self.plot1.setLabels(bottom=('frequency', 'Hz'), left=('intensity', 'a.u.'))
self.plot1.showGrid(x=True, y=True)
# self.plot1.setLogMode(y=True)
def draw0(self, x, y):
self.plot0.clear()
self.plot0.plot(x, y, pen='r')
def draw1(self, x, y):
self.plot1.clear()
self.plot1.plot(x, y, pen='r')
def main():
app = QtGui.QApplication(sys.argv)
speAna = SpeAna()
speAna.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()