這是我想用 Python 翻譯的 MATLAB 代碼。
function [SNR] = bvpsnr(BVP, FS, HR, PlotTF)
HR_F=HR/60;
NyquistF = FS/2;
FResBPM = 0.5; %resolution (bpm) of bins in power spectrum used to determine PR and SNR
N = (60*2*NyquistF)/FResBPM; %number of bins in power spectrum
%% Construct Periodogram
[Pxx,F] = periodogram(BVP,hamming(length(BVP)),N,FS);
GTMask1 = (F >= HR_F-0.1)&(F <= HR_F 0.1);
GTMask2 = (F >= HR_F*2-0.2)&(F <= HR_F*2 0.2);
SPower = sum(Pxx(GTMask1|GTMask2));
FMask2 = (F >= 0.5)&(F <= 4);
AllPower = sum(Pxx(FMask2));
SNR = pow2db(SPower/(AllPower-SPower));
在這里我嘗試用python翻譯
def pow2db(x):
return 10 * log10(x)
def SNR(bvp, fps, hr):
HR_F = hr/60
Nyquist = fps/2
FResBPM = 0.5
N = (60*2*Nyquist)/FResBPM
print(N)
f_set, Pxx_den = welch(bvp, fps, window='hann', nperseg=32)
GTMask1 = f_set >= HR_F-0.1 and f_set <= HR_F 0.1
GTMask2 = f_set >= HR_F*2-0.2 and f_set <= HR_F*2 0.2
SPower = sum(Pxx_den[GTMask1:GTMask2])
FMask2 = f_set >= 0.5 and f_set <=3
AllPower = sum(Pxx_den[FMask2])
SNR = pow2db(SPower/(AllPower-SPower))
當我使用我的資料運行以下代碼時,出現錯誤:
GTMask1 = f_set >= HR_F-0.1 and f_set <= HR_F 0.1 ValueError: 具有多個元素的陣列的真值不明確。使用 a.any() 或 a.all()
uj5u.com熱心網友回復:
要按元素比較 numpy 陣列,請使用&運算子(邏輯和):
GTMask1 = (f_set >= HR_F-0.1) & (f_set <= HR_F 0.1)
GTMask2 = (f_set >= HR_F*2-0.2) & (f_set <= HR_F*2 0.2)
對于邏輯或使用|:
SPower = sum(Pxx_den[GTMask1|GTMask2])
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/475418.html
上一篇:繪圖的單獨初始化和顯示
