同事开玩笑说:你这个python程序要是外流出去了,可能会有危险
引言
公司因为业务原因,购入了一些高灵敏高精度的振动传感器。老板说:“拿去进行测试,看看数据如何?”
吭哧吭哧接入数据,一看,确实精度和灵敏度非常高。具体多高呢?
将传感器固定在相关的结构物上,在办公室中人的声音但凡大一些,都能引起 波形振动!
好家伙,这么高的灵敏度!众所周知,声音也是一种振动能量,能否通过这个传感器,将结构物因为声音引发的 振动转换为音频 呢?
同事说:“要是你这个程序开发了,再搞个传感器,贴在外墙,是不是就能听到屋里说话的声音了,这个外流出去很危险哦!”
怎么样,是否有黑科技的味道了!接下来看看如何操作的吧!
1、采集信号
首先需要通过 python 采集信号,具体如何获取呢?每个厂家都有不同的方式,主要有两种:
- 通过串口
- 通过socket
这两种方式的不同之处在于,串口 需要连接数据线,而 socket 仅需要保证在一个局域网中即可。除了这些区别,一般而言,在数据采集的指令上是没有多大区别的。
串口接入
串口接入需要安装库:
pip install pyserial
接入方法:
import serial
ser = serial.Serial('COM3', 9600)
while True:
try:
data = ser.read(1)
except:
break
socket接入
socket接入方式其实也还是有两种:程序作为客户端、程序作为服务端。
作为客户端
import socket
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('192.168.2.113', 5000))
client.settimeout(60)
client.sendall('登录包信息')
# 开启线程获取数据
receive_thread = threading.Thread(target=receive_handle, args=())
receive_thread.start()
# 开启线程发送数据
send_thread = threading.Thread(target=send_handle, args=())
send_thread.start()
作为服务端
from socketserver import TCPServer, ThreadingMixIn
class CustomServer(TCPServer):
allow_reuse_address = True
def __init__(self, server_address, RequestHandlerClass, bind_and_activate=True):
TCPServer.__init__(self, server_address, RequestHandlerClass, bind_and_activate=bind_and_activate)
class CustomThreadingTCPServer(ThreadingMixIn, CustomServer):
pass
class Handle(socketserver.BaseRequestHandler):
pass
server = CustomThreadingTCPServer(
('0.0.0.0', 6600),
Handle
)
server.daemon_threads = True
server.serve_forever()
这里有个技巧:通过 bind_and_activate为True,可以在 Linux 系统下实现迅速重启,否则需要等待资源释放后才能重启。
2、信号保存
成功接入信号以后,我们就可以将 振动信号 保存在一些文件中,例如 txt:
信号文件
3、信号分析
我们可以首先通过 matplotlib 来绘制信号,首先安装库:
pip install matplotlib
绘制信号代码
# -*- coding: utf-8 -*-
from matplotlib import pyplot as plt
import matplotlib
matplotlib.use('TKAgg')
if __name__ == '__main__':
path = 'xxxx.txt'
data = []
with open(path, 'r') as f:
for line in f:
try:
v = float(line)
data.append(v)
except:
continue
plt.plot(data)
plt.show()
信号绘制
可以看到信号被成功绘制出来了,然后我们将 振动数据转换为音频数据。
首先安装库:
pip install scipy
转换代码
import numpy as np
from scipy import interpolate
def resample( values, original_sampling_rate=200, target_sampling_rate=44100):
time_values = np.linspace(0, len(values) * (1 / original_sampling_rate), len(values))
f = interpolate.interp1d(time_values, values, kind='cubic')
new_time_values = np.linspace(0, len(values) * (1 / original_sampling_rate),
int(target_sampling_rate / original_sampling_rate * len(values)))
vibration_array = f(new_time_values)
return vibration_array
def make_audio(values=[], sampling_rate=200, target_sampling_rate=8000):
values = np.array(values)
resample_values = resample(values, sampling_rate, target_sampling_rate)
min_value = -1
max_value = 1
# 进行最小-最大归一化
normalized_num = (resample_values - resample_values.min()) / (resample_values.max() - resample_values.min()) * (
max_value - min_value) + min_value
avg = sum(normalized_num) / len(normalized_num)
normalized_num = [v - avg for v in normalized_num]
return normalized_num
由于振动传感器的采集仪采样频率不一定很高,所以我们需要将振动的频率通过 升采变为音频频率。
得到数据后,我们将其转换为 音频文件,首先需要安装库。
pip install soundfile
转换音频文件
import soundfile as sf
audio_data = make_audio(data, 200, 8000)
sf.write('test.wav', audio_data, 8000)
4、音频处理
转换出来的音频文件,可能人的声音不是特别清晰,我们可以使用开源软件进行处理。
下载 Audacity ,它是一个开源的音频处理软件,地址是:https://www.audacityteam.org/
通过它,我们就可以调整生成的音频质量:
软件界面
结尾
目前,文章中的音频处理是在软件中的,其实在代码中通过一些算法就可以调整声音的输出,我就不在这篇文章展开了,感兴趣的可以去了解了解 滤波、谱减法、小波等算法。