 # Untitled

a guest
Dec 17th, 2017
263
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. % Test Cramer Rao
2.
3. close all, clc, clear all
4.
5. runs = 10;
6. MSE  = 0;
7. CRLB = 0;
8. interpFactor = 500;
9. for r = 1:runs
10. Iterations = 500;
11. N = 512;
12. Delay = 0;
13.
14. % 1 : Generate a Random White Gaussian Signal
15. % ---
16. signal = randn(1,N);
17. signal = repmat(signal,[Iterations,1]);
18.
19. % 2 : Generate the noise required to obtain a given SNR.
20. % ---
21. SNR = 5;
22. noise_variance = 1/SNR* var(signal(1,:));
23. noise  = sqrt(noise_variance)*randn(Iterations,N);
24.
25. % 3 : Calculate the actual SNR for both the zero-mean noise and zero-mean
26. %     signal
27. % ---
28. SNR = (rms(signal(1,:))/rms(noise(1,:)))^2;
29.
30. % 4 : Corrupt signal with generated noise
31. % ---
32. signal_w_noise = signal + noise;
33.
34. % 5 : Process cross correlations to calculate lags,
35. %     interpolate as defined
36. % ---
37. x1 = interpft(signal_w_noise(1,:),N*interpFactor);
38. lags = [];
39. for seg = 1:Iterations
40.     x2 = interpft(signal_w_noise(seg,:),N*interpFactor);
41.     [Correlation, lag_array] = xcov(x1,x2);
42.     [~,peak_index] = max(Correlation);
43.
44.     coarse = lag_array(peak_index);
45. %     fine = -(1/2)*(Correlation(peak_index+1) - Correlation(peak_index-1)) / (Correlation(peak_index+1) + Correlation(peak_index-1) - 2*Correlation(peak_index));
46.
47.     lag = coarse/interpFactor;
48.     lags = [lags lag];
49. end
50.
51. MSE  = MSE + (mean((lags - Delay).^2))/runs;
52. CRLB =  CRLB + ((3/((pi.^2)*(N))) * ((1 + 2 .* SNR)./(SNR.^2)))/runs;
53.
54. disp(CRLB/MSE)
55. end
56. % Calculate the mean square error and compare it to the CRLB
57. % ---
58.
59. disp(CRLB/MSE)
60.
61. % CRLB is LARGER than the supposed limit.