我有一个干净的信号(曼彻斯特编码),以及包含噪声的相同信号 - 我必须用什么公式来获得信噪比?
%manchester code
T = length(bits)/bitrate; % full time of bit sequence
n = 200;
N = n*length(bits);
dt = T/N;
t = 0:dt:T;
x = zeros(1,length(t)); % output signal
for i = 0:length(bits)-1
if bits(i+1) == 1
x(i*n+1:(i+0.5)*n) = 1;
x((i+0.5)*n+1:(i+1)*n) = -1;
else
x(i*n+1:(i+0.5)*n) = -1;
x((i+0.5)*n+1:(i+1)*n) = 1;
end
end
答案 0 :(得分:0)
在Matlab中执行此操作相对容易,有几种方法。值得一提的是,信号处理工具箱中有snr
- 函数。如果您没有,请使用如here所述的RMS值。
零均值信号的替代方法是使用具有标准偏差或方差的方法。 重要事项:它们仅在平均值(期望值)为零时才起作用。
这里是代码:
% random data
bits = randi(2,1,20)-1;
bitrate = 1;
% manchester code
T = length(bits)/bitrate; % full time of bit sequence
n = 200;
N = n*length(bits);
dt = T/N;
t = 0:dt:T;
x = zeros(1,length(t)); % output signal
for i = 0:length(bits)-1
if bits(i+1) == 1
x(i*n+1:(i+0.5)*n) = 1;
x((i+0.5)*n+1:(i+1)*n) = -1;
else
x(i*n+1:(i+0.5)*n) = -1;
x((i+0.5)*n+1:(i+1)*n) = 1;
end
end
% add offset to test (approach with standard deviation
% and variance are NOT going to produce the correct SNR then)
%x = x + 1;
% x is the clean signal
s = x + 0.1*randn(size(x)); % noisy signal
e = s - x; % noise
% Matlab function in the Signal Processing Toolbox
SNR = snr(x,e)
% with RMS values
rms_x = sqrt(mean(x.^2));
rms_e = sqrt(mean(e.^2));
SNR = 10*log10((rms_x/rms_e)^2) % SNR in dB
SNR = 20*log10(rms_x/rms_e) % SNR in dB
% with standard deviation (only for signals with zero-mean)
sigma_x = std(x);
sigma_e = std(e);
SNR = 20*log10(sigma_x/sigma_e) % SNR in dB
% with variance (only for signals with zero-mean)
var_x = var(x);
var_e = var(e);
SNR = 10*log10(var_x/var_e) % SNR in dB
结果如下:
SNR =
20.1780
SNR =
20.1780
SNR =
20.1780
SNR =
20.1781
SNR =
20.1781