python测量信号信噪比
功能:测量信号信噪比
输入S为纯信号,是一个numpy的1D张量
输入SN为带噪信号,是一个numpy的1D张量
输出snr为信噪比,单位为dB,是一个32为的float数
调用格式{snr=SNR_singlech(S,SN)}
def SNR_singlech(S, SN):
S = S-np.mean(S)# 消除直流分量
S = S/np.max(np.abs(S))#幅值归一化
mean_S = (np.sum(S))/(len(S))#纯信号的平均值
PS = np.sum((S-mean_S)*(S-mean_S))
PN = np.sum((S-SN)*(S-SN))
snr=10*math.log((PS/PN), 10)
return(snr)
运行:
验证数据来自:python实现给定信号生成任意信噪比的带噪声信号https://blog.csdn.net/weixin_43545253/article/details/113721360
import numpy as np
import math
def SNR_singlech(S, SN):
S = S-np.mean(S)# 消除直流分量
S = S/np.max(np.abs(S))#幅值归一化
mean_S = (np.sum(S))/(len(S))#纯信号的平均值
PS = np.sum((S-mean_S)*(S-mean_S))
PN = np.sum((S-SN)*(S-SN))
snr=10*math.log((PS/PN), 10)
return(snr)
S = np.array([1.,2.,3.,4.])
SN = np.array([-2.49164413, -2.36641841, 0.65279453, 0.2471447])
snr = SNR_singlech(S, SN)
# snr = np.round(snr)#四舍五入
print('snr=', snr)
结果:
snr= -5.0000000056726615
参考:《MATLAB在语音信号分析与合成中的应用》