landak

R&B test

Feb 12th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.50 KB | None | 0 0
  1. function rbtest
  2.  
  3. DEBUG = 0;
  4.  
  5. % number of samples per second
  6.  
  7. fs = [1000 100000];
  8.  
  9. % snr
  10.  
  11. snr = [1:3:120];
  12.  
  13. f_std = zeros(length(fs), length(snr));
  14.  
  15. % number of iterations
  16.  
  17. iter = 2;
  18.  
  19. % number of steps
  20.  
  21. nsteps = 25;
  22.  
  23. % initial zero padding
  24.  
  25. K = 2;
  26.  
  27. for w = [1:length(fs)]
  28.  
  29.     f_bias = zeros(1, iter);
  30.  
  31.     for q = [1:length(snr)]
  32.  
  33.         for i = [1:iter]
  34.  
  35.             % generate random frequency and phase
  36.  
  37.             fr = rand() * fs(w) / 2;
  38.     %         fr = fs(w)/4;
  39.  
  40.             ph = rand() * 2*pi;
  41.  
  42.             % generate test signal
  43.  
  44.             ideal_sgn = sqrt(2)*sin([0:fs(w)-1]/fs(w) * 2*pi * fr + ph);
  45.  
  46.             sgn = awgn (ideal_sgn, snr(q) - 10*log10(fs(w)));
  47.  
  48.             fourier_abs = abs(fft([sgn zeros(1, length(sgn)*(K-1))]));
  49.     %         fourier_abs = abs(fft([hann(length(sgn))'.*sgn zeros(1, length(sgn)*(K-1))]));
  50.             [my, mi] = max(fourier_abs(1:end/2));
  51.             xc = [mi-2 mi]/K;
  52.             for st = [1:nsteps]
  53.                 yout = dichotomy(xc);
  54.                 if (yout(3) > yout(1))
  55.                     xc = [(xc(2)+xc(1))/2 xc(2)];
  56.                 else
  57.                     xc = [xc(1) (xc(2)+xc(1))/2];
  58.                 end
  59.             end
  60.            
  61.             xres = (xc(2)+xc(1))/2;
  62.            
  63.             % taking possible overflow into account
  64.             if (xres < 0)
  65.                 xres = -xres;
  66.             elseif (xres >= fs(w)/2)
  67.                 xres = fs(w) - xres;
  68.             end
  69.            
  70.             f_bias(i) = xres - fr;
  71.  
  72.             if (DEBUG == 1)
  73.                 fr
  74.                 mi    
  75.                 plot([0:fs(w)*K-1]/K, fourier_abs);
  76.             end
  77.  
  78.         end
  79.  
  80.         % sum(f_bias)/length(f_bias)
  81.         f_std(w, q) = sqrt(sum(f_bias.^2)/length(f_bias));
  82.     end
  83. end
  84.  
  85. figure
  86.     semilogy(snr, f_std(1, :), 'k-x', snr, f_std(2, :), 'b-*', snr, sqrt(3/(2*pi^2)*10.^(-0.1*snr)), 'r-o');
  87.     xlabel ('SNR (1 sec), dB');
  88.     ylabel ('Standard deviation, Hz')
  89.     legend (sprintf('fs = %d Hz', fs(1)), sprintf('fs = %d Hz', fs(2)), 'MCRB');
  90.     title (sprintf('Standard deviation of frequency estimation for a real tone of 1 sec duration,\n%d iterations, FFT with K=%d zero padding, %d dichotomy steps', iter, K, nsteps))
  91.    
  92.    
  93. function yo = dichotomy(xi)
  94.     if (length(xi) ~= 2)
  95.         display('error function dichotomy');
  96.         return;
  97.     end
  98.  
  99.     xin = [xi(1) (xi(1)+xi(2))/2 xi(2)];
  100.    
  101.     yo = abs( sgn * exp(xin'*j*[0:fs(w)-1]/fs(w) * 2*pi  )' );
  102. end
  103.  
  104. end
Advertisement
Add Comment
Please, Sign In to add comment