add_noise

PURPOSE ^

ADD_NOISE: Add a given SNR to EIDORS data

SYNOPSIS ^

function vv = add_noise( SNR, v1, v2, options)

DESCRIPTION ^

 ADD_NOISE: Add a given SNR to EIDORS data
 v1_w_noise = add_noise( SNR, v1, v2, options)

 Usage:
  v1= add_noise( SNR, v1 )           - add noise to v1 where signal = v1
  v1= add_noise( SNR, v1, v2)        - add noise to v1 where signal = v1 - v2
  v1= add_noise( SNR, v1, v2,'norm') - add noise to v1 where signal = (v1-v2)/v2

 SNR is defined in terms of power SNR =  || signal || / || noise ||

 In many cases, the same noise is desired for each sample. It is easiest to reset
 the pseudorandom number seeds, using
  rng('default'); v1 = add_noise ...

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function vv = add_noise( SNR, v1, v2, options)
0002 % ADD_NOISE: Add a given SNR to EIDORS data
0003 % v1_w_noise = add_noise( SNR, v1, v2, options)
0004 %
0005 % Usage:
0006 %  v1= add_noise( SNR, v1 )           - add noise to v1 where signal = v1
0007 %  v1= add_noise( SNR, v1, v2)        - add noise to v1 where signal = v1 - v2
0008 %  v1= add_noise( SNR, v1, v2,'norm') - add noise to v1 where signal = (v1-v2)/v2
0009 %
0010 % SNR is defined in terms of power SNR =  || signal || / || noise ||
0011 %
0012 % In many cases, the same noise is desired for each sample. It is easiest to reset
0013 % the pseudorandom number seeds, using
0014 %  rng('default'); v1 = add_noise ...
0015 
0016 % (C) 2010 Andy Adler. License: GPL v2 or v3. $Id: add_noise.m 6251 2022-03-25 16:01:26Z aadler $
0017 
0018 if ischar(SNR) && strcmp(SNR,'UNIT_TEST'); do_unit_test; return; end
0019 
0020 if nargin>=2; try; v1 = v1.meas; end; end
0021 if nargin>=3; try; v2 = v2.meas; end;
0022    if any(size(v1)~=size(v2)); v2 = v2*ones(1,size(v1,2)); end
0023 end
0024 
0025    
0026 if nargin == 2;
0027    signal = v1;
0028 elseif nargin==3;
0029    signal = v1 - v2;
0030 elseif strcmp( options, 'norm' )
0031    signal = (v1 - v2) ./ v2;
0032 else
0033    error('add_noise: input arguments not understood');
0034 end
0035    
0036 noise = randn(size(signal));
0037 
0038 % SNR = norm(signal)/norm(noise)
0039 % so  scale norm(noise) by norm(signal)/SNR
0040 
0041 noise = noise *norm(signal) / norm(noise) / SNR;
0042 
0043 vv = eidors_obj('data','from add_noise');
0044 vv.meas = v1 + noise;
0045 
0046 function do_unit_test
0047     v1 = 2.0*ones(10,1);
0048     v2 = 2.1*ones(10,1);
0049     
0050     v0 = add_noise( 2, v1);
0051     SNR_test(2, v0.meas - v1, v1);
0052 
0053     v0 = add_noise(.1, v1, v2);
0054     SNR_test(.1, v0.meas - v1, v1 - v2);
0055 
0056     v0 = add_noise(.9, v1, v2, 'norm' );
0057     SNR_test(.9, v0.meas - v1 , (v1 - v2)./v2);
0058 
0059     randn('state',0); % rng('default');
0060     v0 = add_noise( 2, v1);
0061     randn('state',0); % rng('default');
0062     v1 = add_noise( 2, v1);
0063     unit_test_cmp('RNG reset', v0, v1);
0064 
0065 function SNR_test(SNRspec, noi, sig)
0066     SNR = norm(sig)/norm(noi);
0067     unit_test_cmp('SNR_test', SNR, SNRspec, 1e-13);

Generated on Fri 30-Dec-2022 19:44:54 by m2html © 2005