could anyone help me to convert c code to matlab code

// comments
  1. include math.h
  2. include "mex.h"
void greedyAlgSingleUser( double requiredRate, // in, the required bits/sec/Hz for user double *channelStateInformation, // in, the channel state information mwSize numOfLevels, // in, the length of discrete levles // of bitrate based on modulation, for example the BBSK,QPSK,MQAM double *levelsRate, // in, the bitrate per Hz per second for discrete levels of modulation double *levelsPower, // in, the required power for tranmitting bits based on modulation and BER mwSize subChannelLength, // in, number of subchannels double *powerOfSubChannel, // out, power transmitted in each subchannel double *rateOfSubchannel // out, rate transmitted in each subchannel ){
double deltaPower[256]; // the difference between power of the next stage ralative to the current stage
int deltaIndex[256]; // the iindex for determining the levels of different subchannels
double temp = 0; // temparary variable
int tempIndex = 0; // temparary index
double totalRateBuffer =0; // buffer for computing the capacity provided
int n; // counter
// initialization
for(n = 0 ; n < subChannelLength;n++){
// computing the required power to go to the next state for each subchannel
deltaPower[n] = (levelsPower[1]-levelsPower[0])/pow(channelStateInformation[n],2);
*(powerOfSubChannel + n) = 0; // initialization
*(rateOfSubchannel + n) = 0; // initialization
*(deltaIndex + n) = 0; // initialization
};
// main Program plus iteration
while((totalRateBuffer < requiredRate) ){ // do it untile the required rate is satisfied
tempIndex = 0; // reset the temparary variables
temp = 1e4; // reset the temparary variables (choosing a very high number)
// sorting algorithm find the subchannel in which the required power
// is less than the other subchannel
for(n = 0 ; n < subChannelLength; n++){
if(deltaIndex[n] < numOfLevels - 1){
if(*(deltaPower + n) < temp){
temp = *(deltaPower + n);
tempIndex = n;
}
}
}
deltaIndex[tempIndex] = deltaIndex[tempIndex] + (int) 1;
// incresing the required power and rate of the selected subchannel
powerOfSubChannel[tempIndex] = *(levelsPower+deltaIndex[tempIndex]);
rateOfSubchannel[tempIndex] = *(levelsRate +deltaIndex[tempIndex]);
// next step to compute the delta power
for(n = 0 ; n < subChannelLength;n++){
if(deltaIndex[n] < numOfLevels - 1){ // excluding the subchannels with the highest level
*(deltaPower + n) = (*(levelsPower+deltaIndex[n]+1)-*(levelsPower+deltaIndex[n]))/
pow(channelStateInformation[n],2);}
else{
*(deltaPower + n) = 4e3; //choosing a very high number to exclude the subchannels with the highest level
};
}
// total rate buffer computation
totalRateBuffer = totalRateBuffer +
*(levelsRate +deltaIndex[tempIndex]) - *(levelsRate +deltaIndex[tempIndex] - 1);
};
};
// mex function void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { // definition of parameters double *channelStateInformation, *levelsRate, *levelsPower,requiredRate; mwSize numOfLevels,subChannelLength; double totalRequiredPower,*powerOfSubChannel,*rateOfSubchannel;
// inputs extracting for C program
requiredRate = mxGetScalar(prhs[0]);
channelStateInformation = mxGetPr(prhs[1]);
subChannelLength = mxGetM(prhs[1]);
levelsRate = mxGetPr(prhs[2]);
numOfLevels = mxGetM(prhs[2]);
levelsPower = mxGetPr(prhs[3]);
// wornings in the bad definition of inputs
if (nrhs != 4)
mexErrMsgTxt("Four inputs argument required.");
if(nlhs > 2)
mexErrMsgTxt("Too many output arguments.");
if(mxGetN(prhs[0]) != 1 || mxGetN(prhs[1]) != 1 || mxGetN(prhs[2])!=1 || mxGetN(prhs[1])!=1)
mexErrMsgTxt("All inputs must be in column vector.");
if(mxGetM(prhs[0])>1)
mexErrMsgTxt("The first input must be scalar.");
if(mxGetM(prhs[2]) != mxGetM(prhs[3]))
mexErrMsgTxt("the length of the third and fourth inputs must be the same.");
if(subChannelLength > 256)
mexErrMsgTxt("the maximum subchannel number is 256.");
if(requiredRate > (double)subChannelLength*levelsRate[numOfLevels-1])
mexErrMsgTxt("the required rate cannot be supported.");
// outputs memory allocation
plhs[0] = mxCreateDoubleMatrix(subChannelLength ,1,mxREAL);
powerOfSubChannel = mxGetPr(plhs[0]);
plhs[1] = mxCreateDoubleMatrix(subChannelLength ,1,mxREAL);
rateOfSubchannel = mxGetPr(plhs[1]);
// calling the c function
greedyAlgSingleUser(
requiredRate, // in
channelStateInformation, // in
numOfLevels, // in
levelsRate, // in
levelsPower, // in
subChannelLength, // in
powerOfSubChannel, // out
rateOfSubchannel // out
);
};

답변 (0개)

이 질문은 마감되었습니다.

태그

질문:

2017년 11월 22일

마감:

2017년 11월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by