How to convert a code from C language to Matlab

85 views (last 30 days)
EROL EREN GÖZLÜ
EROL EREN GÖZLÜ on 5 May 2019
Edited: Walter Roberson on 14 Jan 2022
#include <math.h>
#include <stdio.h>
/*********************************************************************
*
*
* Name: mag_est.c
*
* Synopsis:
*
* Demonstrates and tests the "Alpha * Min + Beta * Max" magnitude
* estimation algorithm.
*
* Description:
*
* This program demonstrates the "Alpha, Beta" algorithm for
* estimating the magnitude of a complex number. Compared to
* calculating the magnitude directly using sqrt(I^2 + Q^2), this
* estimation is very quick.
*
* Various values of Alpha and Beta can be used to trade among RMS
* error, peak error, and coefficient complexity. This program
* includes a table of the most useful values, and it prints out the
* resulting RMS and peak errors.
*
* Copyright 1999 Grant R. Griffin
*
* The Wide Open License (WOL)
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without
* fee, provided that the above copyright notice and this license
* appear in all source copies. THIS SOFTWARE IS PROVIDED "AS IS"
* WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND. See
* http://scopeplot.com/dg/wol.htm for more information.
*
*********************************************************************/
/********************************************************************/
double alpha_beta_mag(double alpha, double beta, double inphase,
double quadrature)
{
/* magnitude ~= alpha * max(|I|, |Q|) + beta * min(|I|, |Q|) */
double abs_inphase = fabs(inphase);
double abs_quadrature = fabs(quadrature);
if (abs_inphase > abs_quadrature) {
return alpha * abs_inphase + beta * abs_quadrature;
} else {
return alpha * abs_quadrature + beta * abs_inphase;
}
}
/*********************************************************************/
double decibels(double linear)
{
#define SMALL 1e-20
if (linear <= SMALL) {
linear = SMALL;
}
return 20.0 * log10(linear);
}
/*********************************************************************/
void test_alpha_beta(char *name, double alpha, double beta,
int num_points)
{
#define PI 3.141592653589793
int ii;
double phase, real, imag, err, avg_err, rms_err;
double peak_err = 0.0;
double sum_err = 0.0;
double sum_err_sqrd = 0.0;
double delta_phase = (2.0 * PI) / num_points;
for (ii = 0; ii < num_points; ii++) {
phase = delta_phase * ii;
real = cos(phase);
imag = sin(phase);
err = sqrt(real * real + imag * imag)
- alpha_beta_mag(alpha, beta, real, imag);
sum_err += err;
sum_err_sqrd += err * err;
err = fabs(err);
if (err > peak_err) {
peak_err = err;
}
}
avg_err = sum_err / num_points;
rms_err = sqrt(sum_err_sqrd / num_points);
printf("%-16s %14.12lf %14.12lf %9.6lf %4.1lf %4.1lf\n",
name, alpha, beta, avg_err, decibels(rms_err),
decibels(peak_err));
}
/*********************************************************************/
void main(void)
{
#define NUM_CHECK_POINTS 100000
typedef struct tagALPHA_BETA {
char *name;
double alpha;
double beta;
} ALPHA_BETA;
#define NUM_ALPHA_BETA 16
const ALPHA_BETA coeff[NUM_ALPHA_BETA] = {
{ "Min RMS Err", 0.947543636291, 0.3924854250920 },
{ "Min Peak Err", 0.960433870103, 0.3978247347593 },
{ "Min RMS w/ Avg=0", 0.948059448969, 0.3926990816987 },
{ "1, Min RMS Err", 1.0, 0.323260990 },
{ "1, Min Peak Err", 1.0, 0.335982538 },
{ "1, 1/2", 1.0, 1.0 / 2.0 },
{ "1, 1/4", 1.0, 1.0 / 4.0 },
{ "Frerking", 1.0, 0.4 },
{ "1, 11/32", 1.0, 11.0 / 32.0 },
{ "1, 3/8", 1.0, 3.0 / 8.0 },
{ "15/16, 15/32", 15.0 / 16.0, 15.0 / 32.0 },
{ "15/16, 1/2", 15.0 / 16.0, 1.0 / 2.0 },
{ "31/32, 11/32", 31.0 / 32.0, 11.0 / 32.0 },
{ "31/32, 3/8", 31.0 / 32.0, 3.0 / 8.0 },
{ "61/64, 3/8", 61.0 / 64.0, 3.0 / 8.0 },
{ "61/64, 13/32", 61.0 / 64.0, 13.0 / 32.0 }
};
int ii;
printf("\n Alpha * Max + Beta * Min Magnitude Estimator\n\n");
printf("Name Alpha Beta Avg Err RMS Peak\n");
printf(" (linear) (dB) (dB)\n");
printf("---------------------------------------------------------------------\n");
for (ii = 0; ii < NUM_ALPHA_BETA; ii++) {
test_alpha_beta(coeff[ii].name, coeff[ii].alpha, coeff[ii].beta, 1024);
}
}
  4 Comments

Sign in to comment.

Accepted Answer

Jan
Jan on 6 May 2019
Edited: Jan on 6 May 2019
Try this one - it is not debugged and written in the forum's interface:
function main
NUM_CHECK_POINTS = 100000;
coeff = { ...
'Min RMS Err', 0.947543636291, 0.3924854250920; ...
'Min Peak Err', 0.960433870103, 0.3978247347593; ...
'Min RMS w/ Avg=0', 0.948059448969, 0.3926990816987; ...
'1, Min RMS Err', 1.0, 0.323260990; ...
'1, Min Peak Err', 1.0, 0.335982538; ...
'1, 1/2', 1.0, 1.0 / 2.0 ; ...
'1, 1/4', 1.0, 1.0 / 4.0 ; ...
'Frerking', 1.0, 0.4 ; ...
'1, 11/32', 1.0, 11.0 / 32.0; ...
'1, 3/8', 1.0, 3.0 / 8.0 ; ...
'15/16, 15/32', 15.0 / 16.0, 15.0 / 32.0; ...
'15/16, 1/2', 15.0 / 16.0, 1.0 / 2.0 ; ...
'31/32, 11/32', 31.0 / 32.0, 11.0 / 32.0; ...
'31/32, 3/8', 31.0 / 32.0, 3.0 / 8.0 ; ...
'61/64, 3/8', 61.0 / 64.0, 3.0 / 8.0 ; ...
'61/64, 13/32', 61.0 / 64.0, 13.0 / 32.0};
fprintf('\n Alpha * Max + Beta * Min Magnitude Estimator\n\n');
fprintf('Name Alpha Beta Avg Err RMS Peak\n');
fprintf(' (linear) (dB) (dB)\n');
printf('---------------------------------------------------------------------\n');
for ii = 1:size(coeff, 1)
test_alpha_beta(coeff{ii, 1}, coeff{ii, 2}, coeff{ii, 3}, 1024);
end
end
function R = alpha_beta_mag(alpha, beta, inphase, quadrature)
% magnitude ~= alpha * max(|I|, |Q|) + beta * min(|I|, |Q|) */
abs_inphase = abs(inphase);
abs_quadrature = abs(quadrature);
if abs_inphase > abs_quadrature
R = alpha * abs_inphase + beta * abs_quadrature;
else
R = alpha * abs_quadrature + beta * abs_inphase;
end
end
function R = decibels(linear)
R = 20.0 * log10(max(1e-20, linear));
end
function R = test_alpha_beta(name, alpha, beta, num_points)
peak_err = 0.0;
sum_err = 0.0;
sum_err_sqrd = 0.0;
delta_phase = (2.0 * Ppi) / num_points;
for ii = 0:num_points - 1
phase = delta_phase * ii;
real = cos(phase);
imag = sin(phase);
err = sqrt(real * real + imag * imag) ...
- alpha_beta_mag(alpha, beta, real, imag);
sum_err = sum_err + err;
sum_err_sqrd = sum_err_sqrd + err * err;
err = abs(err);
if (err > peak_err)
peak_err = err;
end
end
avg_err = sum_err / num_points;
rms_err = sqrt(sum_err_sqrd / num_points);
fprintf('%-16s %14.12f %14.12f %9.6f %4.1f %4.1f\n',
name, alpha, beta, avg_err, decibels(rms_err),
decibels(peak_err));
end
It took 2 minutes to convert is from C to Matlab.

More Answers (1)

Dilshana O
Dilshana O on 7 Jun 2021
Hei plzz convert this c code into matlab code #include<stdio.h> void main(){
float p1,p2,p3; float delta=1; scanf("%f%f%f",&p1,&p2,&p3); float d = p2; if(p1 != p2) { if(p1>p2) { if(p2>p3) { d=d-delta; delta=delta*2; } else if(p2=p3) { d=d-delta; delta=delta*2; } else { delta=delta/2; } } else { if(p3>p2) { d=d+delta; delta=delta*2; } else if(p2=p3) { d=d+delta; delta=delta*2; } else { delta=delta/2; } } } else if(p1==p2) { if(p2>p3) { if(p2>p3) { d=d-delta; delta=delta/2; } else if(p2<p3) { d=d-delta; delta=delta*2; } else { delta=delta*2; } } }
}
  3 Comments
Rik
Rik on 14 Jan 2022
@Mohsen, you must be joking.
Have a read here and here. It will greatly improve your chances of getting an answer.
This answer (including your comment) will be deleted shortly.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by