Guest User

Untitled

a guest
Nov 16th, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. %spectrum_compressed.m
  2.  
  3. %different mat files can be used by just removing the comment below. Now, it is active for lowpass.mat
  4.  
  5.  
  6.  
  7. %Computation of Phi (The matrix of N_block and M_ruler) to compress the
  8.  
  9. %exisiting data.
  10.  
  11. %load('lowpasssignal.mat');
  12. %load('multibandsignal.mat');
  13. %load('highpasssignal.mat');
  14. load('bandpasssignal.mat');
  15.  
  16. mn = dsp.Mean;
  17. y_sig_new = y_sig;
  18. y_sig_mean = mn(y_sig_new);
  19. y_sig_updated = y_sig_new - y_sig_mean;
  20. %y_sig_updated = y_sig_new;
  21.  
  22.  
  23. Phi = zeros(M_ruler, N_block);
  24. for i = 1:M_ruler
  25. Phi(i, ruler_index(i)) = 1;
  26. end;
  27.  
  28. fs = 2* pi; %sampling of 2*pi becuase signal is complex valued
  29.  
  30. %computation of Ry - Autocorrelation of y in a M * M matrix and putting
  31. %every element such that it becomes a M^2 * 1 matrix.
  32.  
  33. Ry_final = zeros(M_ruler^2, 1);
  34.  
  35.  
  36. for i=1:M_ruler
  37.  
  38. Ry_original_columns = zeros(M_ruler, 1);
  39.  
  40.  
  41. for l=1:M_ruler
  42. Ry_original_columns(l) = Ry_eval(l - 1, i - 1, y_sig_updated);
  43. end
  44.  
  45. Ry_final((i - 1)*M_ruler + 1:i * M_ruler) = Ry_original_columns;
  46. end
  47.  
  48.  
  49.  
  50.  
  51. %Computation of T matrix to have Rx in a column matrix of size (N^2 * (2N -1))
  52.  
  53. T_final = zeros(N_block^2, 2*N_block - 1);
  54.  
  55. ID_mat = eye(2*N_block - 1);
  56.  
  57. for j=1:N_block^2
  58. T_final(j, :) = ID_mat(mod(j - 1 + (N_block - 2) * floor((j - 1) / N_block),(2 * N_block - 1))+1, :);
  59. end
  60.  
  61.  
  62. %Computation of RC matrix
  63.  
  64. Kron_Phi = kron(Phi, Phi);
  65. R_c = Kron_Phi * T_final;
  66.  
  67. % Prediction of Rx, Rx_cap by Least Square:
  68.  
  69. Rx_cap = (R_c' * R_c)\R_c' * Ry_final;
  70.  
  71. %Retranslation of Rx from Rx_cap
  72.  
  73. Rx_cap_column = T_final * Rx_cap;
  74.  
  75. %R_x original form
  76.  
  77. Rx_cap_N_N = zeros(N_block);
  78.  
  79. for i = 1:N_block
  80. Rx_cap_N_N(:,i) = Rx_cap_column((i - 1) * N_block + 1: i * N_block);
  81. end
  82.  
  83. %Direct Dft PSD
  84.  
  85. Px_cap = fft(Rx_cap);
  86.  
  87. freq_px = 0:fs/length(Px_cap):fs;
  88.  
  89. freq_px_updated = freq_px(1:length(freq_px)-1);
  90.  
  91. hold on
  92. plot(freq_px_updated/pi, abs(Px_cap), 'LineWidth',2);
  93. grid on
  94. title('Power Spec Est of X - Band Pass Signal')
  95. xlabel('Normalized Frequency (\times\pi rad/sample)')
  96. ylabel('Power')
  97.  
  98.  
  99. %PSD using Yule_Walker AR model.
  100.  
  101. %Solutions Yule_walker equations for AR using levinson algorithm:
  102. %Trying number of coefficients as the lenghth from 1 to N_block - 1.
  103.  
  104. ang_freq = 0:fs/length(Rx_cap):fs;
  105. ang_freq = ang_freq(1:length(ang_freq)-1);
  106.  
  107.  
  108. % Yule_Walker equations to solve for A and B matrix:
  109.  
  110. Rx_Y = Rx_cap_N_N(2:end, 2:end);
  111. rx_Y = Rx_cap_N_N(2:end, 1);
  112.  
  113. % Processes for AR 1 to AR max i.e N_block - 1
  114.  
  115. P = N_block - 1;
  116.  
  117. err = zeros(P, 1);
  118.  
  119. for l = 1:P
  120. Rx_l = Rx_Y(1:l, 1:l);
  121.  
  122. rx_l = rx_Y(1:l);
  123.  
  124. a_m = -Rx_l\rx_l; % A matrix with l co-efficients
  125.  
  126. epsilon = Rx_l(1,1);
  127. denominator = 0;
  128.  
  129. for m = 1:l
  130. epsilon = epsilon + a_m(m) * conj(rx_l(m));
  131. denominator = denominator + a_m(m) * exp(-1i * m* ang_freq);
  132. end
  133. b_0 = sqrt(epsilon);
  134.  
  135. err(l) = length(y_sig) * log10(epsilon) + 2 * l; % To find out the best parametric solution
  136. Px_l = epsilon ./ (1+ denominator); % Power spectrum complex
  137.  
  138. %plot(ang_freq/pi, abs(Px_l)) %This can be commented out to analyze all the plots from order 1 to 83
  139.  
  140. end
  141.  
  142. % For AR 12 process for example
  143.  
  144. l = 12;
  145.  
  146. Rx_l = Rx_Y(1:l, 1:l);
  147.  
  148. rx_l = rx_Y(1:l);
  149.  
  150. a_m = -Rx_l\rx_l; % a matrix with l co-efficients
  151.  
  152. epsilon = Rx_l(1,1);
  153.  
  154. denominator = 0;
  155.  
  156. for m = 1:l
  157. epsilon = epsilon + a_m(m) * conj(rx_l(m));
  158. denominator = denominator + a_m(m) * exp(-1i * m* ang_freq);
  159. end
  160. b_0 = sqrt(epsilon);
  161.  
  162. Px_l = epsilon ./ abs(1+ denominator); % Power spectrum complex
  163.  
  164.  
  165. hold on
  166. plot(ang_freq/pi, abs(Px_l), 'LineWidth',2)
  167.  
  168.  
  169. grid on
  170. %title('PSD | AR model')
  171. xlabel('Normalized Frequency (\times\pi rad/sample)')
  172. ylabel('Power Spectrum | AR process')
  173.  
  174. % The minimum of variable err gives the best estimate with AR processe, the
  175. % suitable number of parameters required in the AR process.
  176.  
  177. [v, index] = min(err); %value and index of err to find the best parameter value for the AR process.
  178. l = index; % optimal index
  179.  
  180. Rx_l = Rx_Y(1:l, 1:l);
  181.  
  182. rx_l = rx_Y(1:l);
  183.  
  184. a_m = -Rx_l\rx_l; % a matrix with l co-efficients
  185.  
  186. epsilon = Rx_l(1,1);
  187.  
  188. denominator = 0;
  189.  
  190. for m = 1:l
  191. epsilon = epsilon + a_m(m) * conj(rx_l(m));
  192. denominator = denominator + a_m(m) * exp(-1i * m* ang_freq);
  193. end
  194. b_0 = sqrt(epsilon);
  195.  
  196. Px_l = (b_0)^2 ./ abs(1+ denominator); % Power spectrum complex
  197. hold on
  198. plot(ang_freq/pi, abs(Px_l), 'LineWidth',2)
  199.  
  200. grid on
  201. %title('PSD | AR model')
  202. xlabel('Normalized Frequency (\times\pi rad/sample)')
  203. ylabel('Power Spectrum | AR Process')
  204.  
  205. legend('Using DFT of Estimated Rx', 'AR Process with l = 12', 'AR with l = 52')
Add Comment
Please, Sign In to add comment