% Low Complexity Magnitude Approximation % LuminousLogic.com_ % Magnitude computation from complex numbers, mag = sqrt(I^2 + Q^2) can be % expensive to implement. The below algorithm is a low-cost alternative. % The approximation is, mag ~= alpha*max(|I|,|Q|) + beta*min(|I|,|Q|) % Alpha & Beta can be chosen to be hardware friendly (multiplies become shifts) % and optimized for either minimum RMS or minimum peak error. % How It Works % Absolute value operations limit I and Q to 0-90 degree range. % Max,min further limit values to 0-45 degree range. % In 0-45deg range, a linear combo of I&Q provides a good mag approximation. % Initialize Workspace clear all; close all; rand ('state',0); randn('state',0); % Simulation Parameters num_iter = 1E2; alpha = 1; beta = 1/4; % Create a set of rectangular data phase_I = rand(num_iter,1)*2*pi; phase_Q = rand(num_iter,1)*2*pi; rect_data = cos(phase_I) + 1i*sin(phase_I); % Introduce random attenuation between 0 and 70 dB atten_db = rand(size(rect_data)) * 70; rect_data = rect_data .* (10.^(-atten_db/20)); % Compute exact magnitudes mag_exact = abs(rect_data); mag_exact_db = 20*log10(mag_exact); % Approximate magnitudes mag_approx = zeros(size(mag_exact)); for i=1:num_iter mag_approx(i) = alpha*max(abs([real(rect_data(i)) imag(rect_data(i))])) + beta*min(abs([real(rect_data(i)) imag(rect_data(i))])); end mag_approx_db = 20*log10(mag_approx); % Plot Results figure; plot(mag_exact_db , 'k'); grid on; hold on; plot(mag_approx_db, 'r'); legend('Exact','Simple Approx'); xlabel('Carrier #'); ylabel('Magnitude [dB]')