The definition and calculation method of signal-to-noise ratio

1. Definition of signal-to-noise ratio

The English name is called SNR or S/N (SIGNAL-NOISE RATIO), also known as the signal-to-noise ratio. Refers to the ratio of signal to noise in an electronic device or electronic system . The signal here refers to the electronic signal from the outside of the device that needs to be processed by this device, and the noise refers to the irregular extra signal (or information) that does not exist in the original signal after passing through the device, and this kind The signal does not change with the original signal.

2. Calculation method

The signal-to-noise ratio is a ratio , and it can also be considered as a multiple . In communication and electronic engineering, we hope that the higher the signal-to-noise ratio, the better. For example, the strength of signal noise is 10 times, 100 times, or 1000 times that of noise. Since this number may be a very large number, we use decibels (dB) to express it. It is defined as: "the difference between the common logarithm of the ratio of two quantities of the same power or quantities that can be compared with powers multiplied by 10 equals 1". The formula is as follows:
N d B = 10 lg P s P n N_{dB}=10\mathrm{lg}\frac{P_s}{P_n}NdB=10 l g _PnPs
Therefore, the definition of SNR is:
SNR = 10 lg P s P n SNR=10\mathrm{lg}\frac{P_s}{P_n}SNR=10 l g _PnPs
where P s P_sPsis the power of the signal, P n P_nPnis the power of the noise, lg \mathrm{lg}l g represents the base 10 logarithm.
If you know the signal-to-noise ratio and signal power, you can know that the noise power is:
P n = P s 1 0 ( SNR / 10 ) P_n=\frac{P_s}{10^{(SNR/10)}}Pn=10(SNR/10)Ps
So for a digital signal, how to find its power?
Suppose a digital signal x ( n ) x(n)The sequence length of x ( n ) isNNN , then its power is:
P = ∑ n = 0 N − 1 x ( n ) 2 NP = \frac{\sum_{n=0}^{N-1}x(n)^{2}}{ N}P=Nn=0N1x(n)2
Chen Qing a concept here, the power of the signal is obtained by dividing the signal energy by the time. The case where the signal energy is continuous is for xxAfter x is squared, the integral is calculated, and in the discrete case, the sum is naturally replaced by the integral.

3. Derivation formula

Signal power: P s P_sPs, the power of the noise: P n P_nPn, and the signal-to-noise ratio: SNR SNRS N R , three values, as long as any two values ​​are known, the third value can be obtained.
Example: by the power of the signal:P s P_sPsand SNR: SNR SNRS N R , then the noise power:
P n = P s 1 0 SNR / 10 P_n=\frac{P_s}{10^{SNR/10}}Pn=10SNR/10Ps

4. Matlab hands-on implementation

Let's generate a signal first:

T = 1; %仿真时间
dt = 0.001; %采样率
t = 0: dt: 1-dt; %时间向量
N = length(t); %序列长度
f = 5; %信号频率,Hz为单位
a = cos(2*pi*f*t); %周期信号
plot(t, a);
p = sum(a.^2)/N;

insert image description here
The power of this signal: 0.5 w.
The power of white noise is defined by its variance.
How to generate white noise?
randn in Matlab can generate a random sequence with a mean of 0 and a variance of 1 (that is, a power of 1).

n = randn(N,1);  %产生噪声,功率为1
plot(t,n)
sum(n.^2)/N

insert image description here
How to generate white noise with a specified power, for example, we want to generate a power of P n P_nPnThe white noise of:

n = sqrt(P_n)*randn(N,1);  %产生噪声,功率为P_n

We can verify its correctness:

p  = 10; %功率为p
noise = sqrt(p)*randn(10000,1); %生成功率为p的噪声
sum(noise.^2)/length(noise)  %输出计算得到的噪声

output:

ans =

    9.9994

So this is correct when programming. We can also use the function that comes with matlab to generate Gaussian white noise. wgn(m,n,p) produces an m n Gaussian white noise matrix of power p (dBW) , where p is the output intensity in dbW. The operational relationship between watts and dB W:
d BW = 10 lg ( P 1 W ) \mathrm{dBW} =10\mathrm{lg}(\frac{P}{1\mathrm{W}})dBW=10 l g ( _1W _P)
Therefore, 10w = 10
log10(10)dbW. Program verification:

p = 10;
noise = wgn(10000,1,10*log10(p));
sum(noise.^2)/length(noise)

output:

ans =

    9.8748

There is a slight error.
Having figured out how to generate noise with a fixed SNR, the procedure for adding a specified SNR to a signal is as follows:

clc;clear all;close all;
SNR = 10; %信噪比为10
T = 1; %仿真时间
dt = 0.001; %采样率
t = 0: dt: 1-dt; %时间向量
N = length(t); %序列长度
f = 5; %信号频率,Hz为单位
a = cos(2*pi*f*t); %周期信号
plot(t, a, 'k');
P_s = sum(a.^2)/N; %信号功率
P_n = P_s/(10^(SNR/10)); %计算噪声功率
n = sqrt(P_n)*randn(N,1);  %产生噪声,功率为P_n
hold on %保持绘图界面
plot(t,n,'r');
sum(n.^2)/N
nosie_a = a' + n; %添加噪声后的信号
hold on %保持绘图界面
plot(t,nosie_a,'g');
legend('Pure signal','Noise','Noise signal');

insert image description here

4. Write your own function

The above program is just to add a specific signal-to-noise ratio to a signal. For convenience, a function can be written.

%%-------------------------------------------------
%% 给输入信号添加一定信噪比
%% 输入参数 pureSignal : 输入信号
%%         SNR : 信噪比,dB为单位
%% 输出参数 noiseSignal
%% 输出参数 Location: 估计的时延参数 
%%huasir @shenZhen
%%2022.4.5
%%-------------------------------------------------
%%-------------------------------------------------
function [noiseSignal] = mySNR(pureSignal, SNR)
[m,n] = size(pureSignal);
Power_pureSignal = sum(abs(pureSignal).^2)/length(pureSignal);
Power_noise = Power_pureSignal/(10^(SNR/10)); %计算噪声功率
noise = sqrt(Power_noise)*randn(m,n); %生成噪声
noiseSignal = noise + pureSignal; %给信号添加噪声
end

5. Functions that come with Matlab

In matlab, the following functions can be used to add a certain amount of noise to the signal:

y = awgn(x,snr,'measured') 

Where x is the original input signal, snr is the signal-to-noise ratio, and 'measured' means to measure the power of the input signal before adding noise. If there is no such parameter, it will default the power of the input signal to 0. In fact, in many cases the power of our input signal cannot be 0.
Let's verify the correctness of this code:

clc;clear all;close all;
SNR = 10; %信噪比为10
T = 1; %仿真时间
dt = 0.001; %采样率
t = 0: dt: 1-dt; %时间向量
N = length(t); %序列长度
f = 5; %信号频率,Hz为单位
a = cos(2*pi*f*t); %周期信号
plot(t, a, 'k');
P_s = sum(a.^2)/N; %信号功率

noise_a = awgn(a,SNR,'measured') ; %添加噪声后的信号,调用matlab函数
hold on %保持绘图界面
plot(t,noise_a,'g');
legend('Pure signal','Noise signal');
noise = noise_a - a; %做差求噪声
P_n = sum(noise.^2)/N;
my_SNR = 10*log10(P_s/P_n) %计算得到的信噪比

The signal-to-noise ratio I set is 10, and the final calculated signal-to-noise ratio is

my_SNR =

    9.8643

There is a certain error, and it is currently unclear why this happens.

Guess you like

Origin blog.csdn.net/wzz110011/article/details/123970959