-
Notifications
You must be signed in to change notification settings - Fork 0
/
波形和傅里叶分析.py
89 lines (71 loc) · 2.42 KB
/
波形和傅里叶分析.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
import matplotlib.pyplot as plt
import numpy as np
from scipy.io import wavfile
plt.rcParams['figure.dpi'] = 100
plt.rcParams['figure.figsize'] = (9, 7)
# 如果源文件不是16bit,可以用sox input.wav -r 16 -b 44.1k output.wav转一下。terminal终端中操作
sampFreq, sound = wavfile.read('smf27Analysis.wav')
sound = sound / 2.0**15
#print(sound.shape)
length_in_s = sound.shape[0] / sampFreq
# print(length_in_s) #5s的音频
# plt.subplot(2,1,1)
# plt.plot(sound, 'r')
# plt.xlabel("frequency")
# plt.show()
time = np.arange(sound.shape[0]) / sound.shape[0] * length_in_s
plt.subplot(2,1,1)
plt.plot(time, sound[:], 'r')
plt.xlabel("time, s")
plt.show() #这是全部的d-t图
signal = sound[:]
plt.plot(time[6000:7000], signal[6000:7000])
plt.xlabel("time, s")
plt.ylabel("Signal, relative units")
plt.show() #这是部分放大的时候图像
# 傅里叶变换,分析频率
fft_spectrum = np.fft.rfft(signal)
freq = np.fft.rfftfreq(signal.size, d=1./sampFreq)
fft_spectrum_abs = np.abs(fft_spectrum)
plt.plot(freq, fft_spectrum_abs)
plt.xlabel("frequency, Hz")
plt.ylabel("Amplitude, units")
plt.show()
for i,f in enumerate(fft_spectrum_abs):
if f > 350: #滤掉噪音
print('frequency = {} Hz with amplitude {} '.format(np.round(freq[i],1), np.round(f)))
import matplotlib.pyplot as plt
import numpy as np
from scipy.io import wavfile
plt.rcParams['figure.dpi'] = 100
plt.rcParams['figure.figsize'] = (9, 7)
sampFreq, sound = wavfile.read('smf27Analysis.wav')
sound = sound / 2.0**15
#print(sound.shape)
length_in_s = sound.shape[0] / sampFreq
# print(length_in_s) #5s的音频
# plt.subplot(2,1,1)
# plt.plot(sound, 'r')
# plt.xlabel("frequency")
# plt.show()
time = np.arange(sound.shape[0]) / sound.shape[0] * length_in_s
plt.subplot(2,1,1)
plt.plot(time, sound[:], 'r')
plt.xlabel("time, s")
plt.show() #这是全部的d-t图
signal = sound[:]
plt.plot(time[6000:7000], signal[6000:7000])
plt.xlabel("time, s")
plt.ylabel("Signal, relative units")
plt.show() #这是部分放大的时候图像
# 傅里叶变换,分析频率
fft_spectrum = np.fft.rfft(signal)
freq = np.fft.rfftfreq(signal.size, d=1./sampFreq)
fft_spectrum_abs = np.abs(fft_spectrum)
plt.plot(freq, fft_spectrum_abs)
plt.xlabel("frequency, Hz")
plt.ylabel("Amplitude, units")
plt.show()
for i,f in enumerate(fft_spectrum_abs):
if f > 350: #滤掉噪音
print('frequency = {} Hz with amplitude {} '.format(np.round(freq[i],1), np.round(f)))