0001 function vv = add_noise( SNR, v1, v2, options)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
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
0039
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);
0060 v0 = add_noise( 2, v1);
0061 randn('state',0);
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);