Guest User

Untitled

a guest
Dec 15th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.19 KB | None | 0 0
  1. % main parameters
  2. Fs = 4; % sampling frequency in case of
  3. uniform sampling
  4. startTime = 1; % start of sampling time
  5. endTime = 400; % end of sampling time
  6. % Note 1/100 Hz resolution will be
  7. % obtained only if the time period
  8. is
  9. % longer than 100 seconeds.
  10. % frequencies of y(t) and x(t)
  11. yFrequencies = [1.01,1.02];
  12. yPhases = [0 ,0 ];
  13. xFrequencies = 0.05:0.01:2.00; % you may try other frequencies
  14. %xPhases = 2*pi*rand(size(xFrequencies)); % you may try this
  15. xPhases = zeros(size(xFrequencies));
  16. %--------------------------------------------------------------------------
  17. Building samples
  18. -------------------------------------------------------
  19. if Ctrl.DoUniformSampling == true
  20. 2
  21. % build time vector and find number of samples
  22. t = (startTime:1/Fs:endTime)'; % time vector
  23. N = length(t); % number of samples
  24. else
  25. % creat non-uniform samples
  26. N = Fs*(endTime-startTime); % number of samples
  27. t = startTime + rand(N,1)*(endTime-startTime);
  28. t = sort(t);
  29. end
  30. % build samples of y(t)
  31. yn = zeros(N,1); % initialize with zer
  32. for i = 1 : length(yFrequencies)
  33. yn = yn + cos(2*pi*yFrequencies(i).*t+yPhases(i));
  34. end
  35. % build samples of x(t)
  36. xn = zeros(N,1); % initialize with zer
  37. for i = 1 : length(xFrequencies)
  38. xn = xn + cos(2*pi*xFrequencies(i).*t+xPhases(i));
  39. end
  40. % build samples of y' (these are measurements)
  41. ypn = yn + xn;
  42. %--------------------------------------------------------------------------
  43. Linear least sqaure estimation
  44. -----------------------------------------
  45. For linear estimation we have to expany the cos function in y''. In this case we have cos(a+b) = cos(a)cos(b)
  46. - sin(a)sin(b). Therefor, y'' = A*cos(2*pi*f*t+p) => y'' = A*cos(2*pi*f*t)*cos(p) -A*sin(2*pi*f*t)*sin(p)
  47. => y'' = A*cos(p)*cos(2*pi*f*t) -A*sin(p)*sin(2*pi*f*t) => y'' = B *cos(2*pi*f*t) -C *sin(2*pi*f*t)
  48. where B = A*cos(p) and C = A*sin(p)
  49. % now we are going for all frequecies in x estimate the amplitude
  50. and
  51. % phase from the samples of y'
  52. % In matrix form for each row we have
  53. % ynp[n] = [cos(2*pi*f*t[n]) -sin(2*pi*f*t[n])] * [B C]'
  54. % and for all rows we have
  55. % Yp(N x 1) = H(N x 2) * transpose([B C])
  56. % where H is a matrix built from [cos(2*pi*f*t[n]) -
  57. sin(2*pi*f*t[n])]
  58. % for all sampling times
  59. % define two vectors to store the results
  60. linearEstAmps = zeros(length(xFrequencies),1);
  61. linearEstPhases = zeros(length(xFrequencies),1);
  62. for i = 1 : length(xFrequencies)
  63. 3
  64. % Yp is already calculated
  65. Yp = ypn;
  66. % building H
  67. H = [cos(2*pi*xFrequencies(i)*t) -
  68. sin(2*pi*xFrequencies(i)*t)];
  69. % least square estimation
  70. % ref: https://en.wikipedia.org/wiki/
  71. Linear_least_squares_(mathematics)#Motivational_example
  72. %estBC = (transpose(H) * H)^-1*transpose(H) * Yp;
  73. estBC = pinv(H) * Yp; % due to numerical error it is
  74. better to use pinv
  75. % find B and C estimate
  76. estB = estBC(1);
  77. estC = estBC(2);
  78. % find amplitude and phase
  79. estA = sqrt(estB.^2 + estC.^2); % as A = sqrt(B^2 + C^2)
  80. estP = atan2(estC,estB); % as P = atan(C/B)
  81. % store the results
  82. linearEstAmps(i) = estA;
  83. linearEstPhases(i) = estP;
  84. end
  85. % plot amplitude and phase
  86. figure(1)
  87. clf
  88. subplot(2,1,1)
  89. bar(xFrequencies,linearEstAmps,'b')
  90. xlabel('frequencies')
  91. ylabel('estimated amplitude')
  92. grid on
  93. grid minor
  94. xlim([xFrequencies(1) xFrequencies(end)])
  95. hold on
  96. title('performance of linear LS (expect is 1)')
  97. subplot(2,1,2)
  98. bar(xFrequencies,linearEstPhases,'b')
  99. xlabel('frequencies')
  100. ylabel('estimated phase [rad]')
  101. title('(expect is 0)')
  102. grid on
  103. grid minor
  104. xlim([xFrequencies(1) xFrequencies(end)])
  105. %--------------------------------------------------------------------------
  106. 4
  107. Non-Linear least sqaure estimation
  108. -------------------------------------
  109. as we know y'' is a non-linear function of the phase parameter. Therefore, we need to use non-linear LS
  110. algorithm, and solve the problem iteratively. we need to find the derivaties of y'' w.r.t. the A and P dy''/
  111. dA = cos(2*pi*f*t+P) dy''/dP = -A*sin(2*pi*f*t+P)
  112. numOfIteration = 70;
  113. convergenceRho = 0.2;
  114. % define two vectors to store the results
  115. nonLinearEstAmps = zeros(length(xFrequencies),1);
  116. nonLinearEstPhases = zeros(length(xFrequencies),1);
  117. for i = 1 : length(xFrequencies)
  118. % Yp is already calculated
  119. Yp = ypn;
  120. % Initialize estimation point
  121. A = 1;
  122. P = 0;
  123. for k = 1 : numOfIteration
  124. % build y'' samples from the estimation
  125. Yppn = A*cos(2*pi*xFrequencies(i)*t+P);
  126. 5
  127. % find residual (measurements - estimated samples)
  128. r = Yp - Yppn;
  129. % build Jacobian matrix J = [df/dx1 df/dx2]
  130. % reference: https://en.wikipedia.org/wiki/
  131. Jacobian_matrix_and_determinant
  132. J = [cos(2*pi*xFrequencies(i)*t+P),-
  133. A*sin(2*pi*xFrequencies(i)*t+P)];
  134. % find new A and P
  135. estAP = [A;P] + convergenceRho.*pinv(J)*r;
  136. % update A and P
  137. A = estAP(1);
  138. P = estAP(2);
  139. % remove ambiguities
  140. if A < 0
  141. A = -A;
  142. P = P + pi;
  143. end
  144. end
  145. % store the results
  146. nonLinearEstAmps(i) = A;
  147. nonLinearEstPhases(i) = P;
  148. end
  149. % plot amplitude and phase
  150. figure(2)
  151. clf
  152. subplot(2,1,1)
  153. bar(xFrequencies,nonLinearEstAmps,'b')
  154. xlabel('frequencies')
  155. ylabel('estimated amplitude')
  156. grid on
  157. grid minor
  158. xlim([xFrequencies(1) xFrequencies(end)])
  159. hold on
  160. title('performance of non-linear LS using Gauss-Newton')
  161. subplot(2,1,2)
  162. bar(xFrequencies,nonLinearEstPhases,'b')
  163. xlabel('frequencies')
  164. ylabel('estimated phase [rad]')
  165. grid on
  166. grid minor
  167. xlim([xFrequencies(1) xFrequencies(end)])
  168. 6
  169. Analysis
  170. ---------------------------------------------------------------
  171. find estimate of y' from estimated frequencies and phases
  172. estLinearYp = zeros(N,1);
  173. estNonLinearYp = zeros(N,1);
  174. for i = 1 : length(xFrequencies)
  175. estLinearYp = estLinearYp + ...
  176. linearEstAmps(i)*cos(2*pi*xFrequencies(i)*t +
  177. linearEstPhases(i));
  178. estNonLinearYp = estNonLinearYp + ...
  179. nonLinearEstAmps(i)*cos(2*pi*xFrequencies(i)*t +
  180. nonLinearEstPhases(i));
  181. end
  182. figure(3)
  183. plot(t,ypn,'b',...
  184. t,estLinearYp,'r--',...
  185. t,estNonLinearYp,'k-.')
  186. xlabel('time in sec')
  187. ylabel('amplitude')
  188. grid on
  189. grid minor
  190. title('performance of the estimation')
  191. 7
  192. legend('Actual','linear-est','non-linear-est')
  193. % find sum error
  194. estLinearError_dB = 10*log10(sum(abs(ypn - estLinearYp
  195. ).^2));
  196. estNonLinearError_dB = 10*log10(sum(abs(ypn -
  197. estNonLinearYp).^2));
  198. fprintf('Estimation error: n tLinear = %0.2f dBntNonlinear
  199. = %0.2f dBn',...
  200. estLinearError_dB,estNonLinearError_dB)
  201. Estimation error:
  202. Linear = 12.21 dB
  203. Non-linear = 12.21 dB
Add Comment
Please, Sign In to add comment