Advertisement
Guest User

Untitled

a guest
Jan 9th, 2017
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 3.46 KB | None | 0 0
  1. % Параметры.
  2. OUTPUT_WIDTH = 9;
  3. PHASE_WIDTH = 18;
  4. Fd = 50e6;
  5. Fc = 3.12345e6;
  6. ACC_WIDTH = 32;
  7. X0 = 128;
  8. N = 32768;
  9.  
  10. iterations = OUTPUT_WIDTH;
  11. phase_incr = int32(((2.0 ^ 32) * Fc) / Fd);
  12.  
  13. % Таблица арктангенсов.
  14. atan_table = zeros(1, iterations, 'int32');
  15. for i = 1:iterations
  16.     atan_table(i) = (atan(2 ^ -(i - 1))  / pi) * (2 ^ (PHASE_WIDTH - 1));
  17. end
  18.  
  19. % Заполнение массива фаз.
  20. phase = zeros(1, N, 'int32');
  21. phase_acc = int32(0);
  22. for i = 1:N
  23.     p = int64(phase_acc) + int64(phase_incr);
  24.     if (p > intmax('int32'))
  25.         p = int64(intmin('int32')) + (p - int64(intmax('int32')));
  26.     end
  27.     phase_acc = p;
  28.     phase(i) = bitshift(phase_acc, -32 + PHASE_WIDTH);
  29. end
  30.  
  31. % CORDIC вариант с делением.
  32. CORDIC1_X = int32([]);
  33. for p = phase
  34.     x = zeros(1, iterations + 1, 'int32');
  35.     y = zeros(1, iterations + 1, 'int32');
  36.     z = zeros(1, iterations + 1, 'int32');
  37.     quadrant = bitand(bitshift(p, -(PHASE_WIDTH - 2)), 3);
  38.     if (quadrant == 0 || quadrant == 3)
  39.         x(1) = X0;
  40.         z(1) = p;
  41.     end
  42.     if (quadrant == 1)
  43.         y(1) = X0;
  44.         z(1) = bitand(p, (2 ^ (PHASE_WIDTH - 2)) - 1);
  45.     end
  46.     if (quadrant == 2)
  47.         y(1) = -X0;
  48.         z(1) = bitor(p, bitshift(3, (PHASE_WIDTH - 2)));
  49.     end
  50.     for i = 1:iterations
  51.         if (z(i) > 0)
  52.             x(i + 1) = x(i) - fix(double(y(i)) / (2.0 ^ (i - 1)));
  53.             y(i + 1) = y(i) + fix(double(x(i)) / (2.0 ^ (i - 1)));
  54.             z(i + 1) = z(i) - atan_table(i);
  55.         else
  56.             x(i + 1) = x(i) + fix(double(y(i)) / (2.0 ^ (i - 1)));
  57.             y(i + 1) = y(i) - fix(double(x(i)) / (2.0 ^ (i - 1)));
  58.             z(i + 1) = z(i) + atan_table(i);
  59.         end
  60.     end
  61.     CORDIC1_X = cat(1, CORDIC1_X, x(iterations + 1));
  62.     y(iterations + 1);
  63. end
  64.  
  65. % CORDIC вариант со сдвигом.
  66. CORDIC2_X = int32([]);
  67. for p = phase
  68.     x = zeros(1, iterations + 1, 'int32');
  69.     y = zeros(1, iterations + 1, 'int32');
  70.     z = zeros(1, iterations + 1, 'int32');
  71.     quadrant = bitand(bitshift(p, -(PHASE_WIDTH - 2)), 3);
  72.     if (quadrant == 0 || quadrant == 3)
  73.         x(1) = X0;
  74.         z(1) = p;
  75.     end
  76.     if (quadrant == 1)
  77.         y(1) = X0;
  78.         z(1) = bitand(p, (2 ^ (PHASE_WIDTH - 2)) - 1);
  79.     end
  80.     if (quadrant == 2)
  81.         y(1) = -X0;
  82.         z(1) = bitor(p, bitshift(3, (PHASE_WIDTH - 2)));
  83.     end
  84.     for i = 1:iterations
  85.         if (z(i) > 0)
  86.             x(i + 1) = x(i) - bitshift(y(i), -(i - 1));
  87.             y(i + 1) = y(i) + bitshift(x(i), -(i - 1));
  88.             z(i + 1) = z(i) - atan_table(i);
  89.         else
  90.             x(i + 1) = x(i) + bitshift(y(i), -(i - 1));
  91.             y(i + 1) = y(i) - bitshift(x(i), -(i - 1));
  92.             z(i + 1) = z(i) + atan_table(i);
  93.         end
  94.     end
  95.     CORDIC2_X = cat(1, CORDIC2_X, x(iterations + 1));
  96.     y(iterations + 1);
  97. end
  98.  
  99. F = Fd * (-N/2:N/2-1) / N;
  100.  
  101. CORDIC1_X = (double(CORDIC1_X));
  102. CORDIC1_F = fftshift(fft(CORDIC1_X));
  103. CORDIC1_P = CORDIC1_F .* conj(CORDIC1_F) / (N * N);
  104.  
  105. CORDIC2_X = (double(CORDIC2_X));
  106. CORDIC2_F = fftshift(fft(CORDIC2_X));
  107. CORDIC2_P = CORDIC2_F .* conj(CORDIC2_F) / (N * N);
  108.  
  109. figure;
  110. subplot(2,1,1);
  111. plot(F, 10 * log10(CORDIC1_P), 'k');
  112. title('Целочисленное деление')
  113. grid on
  114.  
  115. subplot(2,1,2);
  116. plot(F, 10 * log10(CORDIC2_P), 'b');
  117. title('Логический сдвиг')
  118. grid on
  119.  
  120. xlabel('Частота, Гц');
  121. ylabel('Мощность, дБ');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement