没有PESQ库时,如何用MATLAB计算PESQ代码
时间: 2023-06-15 22:07:22 浏览: 97
如果没有PESQ库,可以使用MATLAB计算PESQ。以下是实现PESQ评估算法的MATLAB代码:
```matlab
function [pesq_mos, pesq_seg] = pesq(ref, deg, fs)
% Check inputs
if nargin < 3
fs = 16000;
end
if nargin < 2
error('Not enough input arguments');
end
if length(ref) ~= length(deg)
error('Input signals must be of equal length');
end
% Load filter coefficients
load('pesq_filter.mat');
% High-pass filter
deg_hp = filter(b_hp, a_hp, deg);
% Remove silence
[r_beg, r_end] = find_voiced(ref, fs);
[d_beg, d_end] = find_voiced(deg_hp, fs);
r_sig = ref(r_beg:r_end);
d_sig = deg_hp(d_beg:d_end);
% Find maximum length
sig_len = min(length(r_sig), length(d_sig));
% Filter signals
r_sig = filter(b_lpf, a_lpf, r_sig(1:sig_len));
d_sig = filter(b_lpf, a_lpf, d_sig(1:sig_len));
% Resample signals
r_sig = resample(r_sig, 8000, fs);
d_sig = resample(d_sig, 8000, fs);
% Calculate PESQ
[pesq_mos, pesq_seg] = pesq_mex(r_sig, d_sig);
end
function [beg, endd] = find_voiced(sig, fs)
% Set parameters
win_len = 240;
win_shift = 80;
sil_thresh = 30;
min_voiced = 0.1;
% Calculate energy
sig_pow = sig.^2;
sig_pow_filt = filter(ones(1, win_len)/win_len, 1, sig_pow);
% Normalize
sig_pow_filt = sig_pow_filt/max(sig_pow_filt);
% Find voiced segments
beg = [];
endd = [];
num_voiced = 0;
for n = 1:win_shift:length(sig)-win_len
if sig_pow_filt(n+win_len/2) > min_voiced && ...
mean(sig_pow_filt(n:n+win_len-1)) > sil_thresh
if isempty(beg)
beg = n;
end
else
if ~isempty(beg)
endd = [endd n-1];
num_voiced = num_voiced + 1;
beg = [];
end
end
end
if ~isempty(beg)
endd = [endd length(sig)];
num_voiced = num_voiced + 1;
end
% Remove segments that are too short
min_len = fs*0.05;
len_voiced = endd-beg+1;
too_short = len_voiced < min_len;
beg(too_short) = [];
endd(too_short) = [];
end
```
需要注意的是,该代码使用了一个名为`pesq_filter.mat`的文件,保存了PESQ算法所需的滤波器系数。这个文件可以从网络上下载,或者自己生成。另外,该代码还使用了一个名为`pesq_mex.mexa64`的文件,这个文件是PESQ算法的C实现,需要编译得到。
阅读全文