引言
这里有一份csv文档,文档的第一列是时间序列,第二列是信号序列,对信号进行傅里叶变换。为了不失真地恢复模拟信号,采样频率应该不小于模拟信号频谱中最高频率的2倍,fs≥2fmax
import numpy
as np
from scipy
.fftpack
import fft
import matplotlib
.pyplot
as plt
from matplotlib
.pylab
import mpl
mpl
.rcParams
['font.sans-serif'] = ['SimHei']
mpl
.rcParams
['axes.unicode_minus'] = False
us1
= np
.loadtxt
("file1.csv", delimiter
=",", dtype
="float")
_x1
= us1
[:, 0] - us1
[0, 0]
_y1
= us1
[:, 1]
x
= _x1
y
= _y1
fft_y
= fft
(y
)
N
= x
.shape
[0]
x
= np
.linspace
(0, x
.shape
[0]/x
[-1], x
.shape
[0])
half_x
= x
[range(int(N
/ 2))]
abs_y
= np
.abs(fft_y
)
angle_y
= np
.angle
(fft_y
)
normalization_y
= abs_y
/ N
normalization_half_y
= normalization_y
[range(int(N
/ 2))]
plt
.figure
(figsize
=(40, 18), dpi
=80)
plt
.semilogx
(half_x
[:int(len(half_x
))], normalization_half_y
[:int(len(half_x
))], 'blue')
font2
= {'family': 'Times New Roman', 'weight': 'normal', 'size': 18, 'color': 'red'}
plt
.title
('Unilateral amplitude spectrum (normalized)', font2
)
plt
.xlabel
('Frequency/Hz', font2
)
plt
.ylabel
('Frequency component size', font2
)
plt
.savefig
('c.jpg')
plt
.show
()
傅里叶变换结果如下图所示:
批量处理
import numpy
as np
from scipy
.fftpack
import fft
import matplotlib
.pyplot
as plt
from matplotlib
.pylab
import mpl
zhfont
= mpl
.font_manager
.FontProperties
(fname
='C:\\Windows\\Fonts\\simsun.ttc')
fontcn
= {'fontproperties': zhfont
, 'size': 30, 'color': 'red'}
mpl
.rcParams
['font.sans-serif'] = ['SimHei']
mpl
.rcParams
['axes.unicode_minus'] = False
for i
in range(2, 5):
plt
.figure
(figsize
=(30, 15), dpi
=100)
a
= 'Tek00'+str(i
)
us1
= np
.loadtxt
(a
+".csv", delimiter
=",", dtype
="float")
_x1
= us1
[:, 0] - us1
[0, 0]
_y1
= us1
[:, 1]
x
= _x1
y
= _y1
fft_y
= fft
(y
)
N
= x
.shape
[0]
x
= np
.linspace
(0, x
.shape
[0]/x
[-1], x
.shape
[0])
half_x
= x
[range(int(N
/ 2))]
abs_y
= np
.abs(fft_y
)
angle_y
= np
.angle
(fft_y
)
normalization_y
= abs_y
/ N
normalization_half_y
= normalization_y
[range(int(N
/ 2))]
plt
.subplot
(1, 2, 1)
plt
.title
(a
+'.csv的原始数据', fontcn
)
plt
.plot
(_x1
, _y1
)
font2
= {'family': 'Times New Roman', 'weight': 'normal', 'size': 30, 'color': 'red'}
plt
.ylim
([-0.6, 0.6])
plt
.tick_params
(labelsize
=30)
plt
.xlabel
('时间/S', fontcn
)
plt
.ylabel
('电压/V', fontcn
)
plt
.subplot
(1, 2, 2)
plt
.title
(a
+ '.csv的傅里叶变换', fontcn
)
plt
.semilogx
(half_x
[:int(len(half_x
))], normalization_half_y
[:int(len(half_x
))], 'blue')
plt
.tick_params
(labelsize
=30)
plt
.ylim
([0, 0.004])
plt
.xlabel
('频率/Hz', fontcn
)
plt
.ylabel
('归一化频率分量', fontcn
)
plt
.savefig
(a
+'.png', dpi
=300)
转载请注明原文地址: https://lol.8miu.com/read-24401.html