FORMATION OF SIGNAL CORRUPTED WITH NOISE
Function snrs(signal,noise,snr) forms an array of a signal that is corrupted with noise and signal-to-noise ratio (SNR) has a specified value snr.
We suppose that SNR was defined as a ratio of the root-mean-square (rms) value of amplitudes of a signal and a noise. It is supposed that value of SNR was expressed in decibels (dB). It is supposed, too, that both the signal and the noise were measured at the same points, within the same bandwidth, and across the same impedance. Thus for calculations we used the following formula:
SNRdb = 20log10(As
SNRdb = 20log10(As / An)
As and An are rms values of amplitudes of a signal and a noise respectively.
where As and An are rms values of amplitudes of a signal and a noise respectively.
s = snrs(signal,noise,snr) -
signal is an array of input signal.
noise is an array of noise; it is considered that noise has the same sampling frequency as the sampling frequency of signal.
Both signal and noise should have floating point format.
snr is a signal-to-noise ratio, dB.
s is an array of corrupted signal.
Then, an array noi of noise that has the same length as signal is built. After repetition of noise m times, where (m-1)*numel(noise) < numel(signal) <= m*numel(noise), the first numel(signal) samples of new array form the arrray noi. If numel(signal) = numel(noise), then noi = noise. After multiplying noi by appropriate coefficient ns, a product to input signal is added. And it is an array s. s = signal + noi * ns.
If length of noise less than length of signal we can change type of noise aggression. s = snrs(signal,noise,snr,1) apply noise only to the first numel(noise) samples of signal. To this end array noise is padded with trailing zeros to length numel(signal).
[s,stnr]=snrs(...) returns stnr, which is a signal-to-noise ratio of signal and noi.
fs =1200; % Sampling frequency, Hz T= .1; % Signal duration, s t0=1/fs; % Sampling time, s q=fs/1000; % Samples per ms L = floor(T*fs); % Length of a signal, samples t = (0:L)*t0; % Time vector x=sin(2*pi*50*t); % 50 Hz sinusoid randn('state',0); % Default initial state nx=randn([1,numel(t)]); % Zero-mean random noise for snr=[0 10] [s,stnr]=snrs(x,nx,snr); figure plot(fs*t(1:L)/q,s(1:L),'.-',fs*t(1:L)/q,x((1:L)));grid on title(['Signal corrupted with zero-mean random noise. SNR = ' ... num2str(snr)]) xlabel('Time, ms') legend('Corrupted signal','Signal','Location','southoutside'); legend boxoff end disp(sprintf('%s %.1f','stnr =',stnr)) fl=floor(numel(t)/4); s=snrs(x,nx(1:fl),snr,1); figure plot(fs*t(1:fl+1)/q,s(1:fl+1),'.-',fs*t(fl+1:L)/q,x((fl+1:L)),'+-') grid on title(['Signal partly corrupted with zero-mean random noise. SNR = ' ... num2str(snr)]) xlabel('Time, ms') legend('Corrupted part of a signal','Non-corrupted part of a signal', ... 'Location','southoutside'); legend boxoff
stnr = -2.0