Converting my code to matlab
    6 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
Hi guys, I want as one time to conver my C++ code to matlab code, I just want to do that one time and Im newbie in matlab(generally not using matlab), could anyone help me how could I implement that in matlab? thanks alot.
c++ code: (what the code is returning a specific substring that's found after syncword's substring in a give string)
using MatchVec = std::vector<Match>;
uint16_t calcCRC(std::uint8_t crcData, std::uint16_t crcReg);
int PacketIndex=0;
template <typename T, unsigned  N>
    constexpr unsigned countof(T const (&)[N]) noexcept
    {
    return N;
    }
MatchVec findPattern(const std::string &str, const std::string &pattern)
{
    MatchVec vec;
    Match m;
    std::string::size_type index, pos = 0;
    while ((index = str.find(pattern, pos)) != std::string::npos) {
        m.first = index; //startof Syncword
        index += pattern.length();
        m.second = str.substr(index,264+16); // 264 for payload size and more two byte for CRC 
        vec.push_back(m);
        pos = index;
    }
    return vec;
}
uint16_t calcCRC(uint8_t crcData, uint16_t crcReg)
{
    uint8_t i;
    for (i = 0; i < 8; i++)
    {
        if (((crcReg & 0x8000) >> 8) ^ (crcData & 0x80))
        {
            crcReg = (crcReg << 1) ^ CRC16_POLY;
        }
        else
        {
            crcReg = (crcReg << 1);
        }
        crcData <<= 1;
    }
    return crcReg;
}
unsigned int valueOf(const std::string &substring) {
    char retChar = '\0';
    std::uint8_t total = 0;
    int counter = 1;
    for (int i=byteSize; i>0; i--) {
        if (substring[i-1] == '1') total += counter;
        if (substring[i-1] != ' ') counter *= 2;        
    }
    return total;
}
void deBin(const std::string &rx) {
    unsigned int ByteConvertRxBuffer[35] ={0};
    uint8_t rxBuffer[countof(ByteConvertRxBuffer)];
    size_t ireal=0;  // notice that last two last bytes are for CRC , this means last two index here are CRC value for CRC0 , CRC1 .
    uint16_t checksum; // CRC of 2byte i.e 16bits.
    bool crcOK = false;
    uint8_t i;
    checksum = CRC_INIT;
    std::cout<<"{ ";
    for (size_t i=0; i<rx.length(); i+=byteSize) { 
        char nibble[byteSize];
        for (int j=0; j<byteSize; j++) nibble[j] = rx[i+j];
        std:: cout<< valueOf(nibble); // prints the value of each Byte-8bit inside each packet , dealing with each packet separately.
        ByteConvertRxBuffer[ireal++]=valueOf(nibble);
        if (i+byteSize != rx.length() ) std::cout<<", ";
    }
    std:: cout<<"}"<<endl;
    for(int i=0;i<countof(ByteConvertRxBuffer);i++)
    {
        rxBuffer[i]=ByteConvertRxBuffer[i];
    }
    for(i = 0; i < sizeof(rxBuffer); i++) 
    {
        checksum = calcCRC(rxBuffer[i], checksum);
    }
    PacketIndex++;
    if(checksum == 0)
    {
        crcOK = true;
        std::cout<<"  your packet number "<<PacketIndex<<" :" <<"Valid"<<endl;
    }
    else
    {
    crcOK = false;
    std::cout<<"your packet number "<<PacketIndex<<" :" <<"inValid"<<endl;
    }
}
int main() /*int main(int argc, char** argv){}*/
{
    std::string PacketData = "011101111001001010101010101010101010101010101100100110000101101010001110111100010000000000000011001010010000010111000011011001100100101110101100100101000011110110110001100110000111100110001100110000010111101111010000101101011100110011000000101101111110100010111100010100110000111111010110000101101111110011001111001011000011001001001100000001000100101101000110000010000111111110111111110";
    std::string SyncWord = "10010011000010110101000111011110"; // syncword pattern
    size_t notfound = PacketData.find(SyncWord); 
    assert(notfound != string::npos) ; //if not found as exit and don't complete. 
    auto matches = findPattern(PacketData, SyncWord );
    for(auto &match : matches) {
        std::cout << "Match found at position: " << match.first << std::endl;
        std::cout << "String is " << match.second << std::endl;
        deBin( match.second );
    }
    return 0;
}
2 comentarios
  Stephen23
      
      
 el 9 de Ag. de 2020
				Original question by Mohamed Jamal asked on 6th of August 2020 retrieved from Google Cache:
"Converting my code to matlab"
Hi guys, I want as one time to conver my C++ code to matlab code, I just want to do that one time and Im newbie in matlab(generally not using matlab), could anyone help me how could I implement that in matlab? thanks alot.
c++ code: (what the code is returning a specific substring that's found after syncword's substring in a give string)
using MatchVec = std::vector<Match>;
uint16_t calcCRC(std::uint8_t crcData, std::uint16_t crcReg);
int PacketIndex=0;
template <typename T, unsigned  N>
    constexpr unsigned countof(T const (&)[N]) noexcept
    {
    return N;
    }
MatchVec findPattern(const std::string &str, const std::string &pattern)
{
    MatchVec vec;
    Match m;
    std::string::size_type index, pos = 0;
    while ((index = str.find(pattern, pos)) != std::string::npos) {
        m.first = index; //startof Syncword
        index += pattern.length();
        m.second = str.substr(index,264+16); // 264 for payload size and more two byte for CRC 
        vec.push_back(m);
        pos = index;
    }
    return vec;
}
uint16_t calcCRC(uint8_t crcData, uint16_t crcReg)
{
    uint8_t i;
    for (i = 0; i < 8; i++)
    {
        if (((crcReg & 0x8000) >> 8) ^ (crcData & 0x80))
        {
            crcReg = (crcReg << 1) ^ CRC16_POLY;
        }
        else
        {
            crcReg = (crcReg << 1);
        }
        crcData <<= 1;
    }
    return crcReg;
}
unsigned int valueOf(const std::string &substring) {
    char retChar = '\0';
    std::uint8_t total = 0;
    int counter = 1;
    for (int i=byteSize; i>0; i--) {
        if (substring[i-1] == '1') total += counter;
        if (substring[i-1] != ' ') counter *= 2;        
    }
    return total;
}
void deBin(const std::string &rx) {
    unsigned int ByteConvertRxBuffer[35] ={0};
    uint8_t rxBuffer[countof(ByteConvertRxBuffer)];
    size_t ireal=0;  // notice that last two last bytes are for CRC , this means last two index here are CRC value for CRC0 , CRC1 .
    uint16_t checksum; // CRC of 2byte i.e 16bits.
    bool crcOK = false;
    uint8_t i;
    checksum = CRC_INIT;
    std::cout<<"{ ";
    for (size_t i=0; i<rx.length(); i+=byteSize) { 
        char nibble[byteSize];
        for (int j=0; j<byteSize; j++) nibble[j] = rx[i+j];
        std:: cout<< valueOf(nibble); // prints the value of each Byte-8bit inside each packet , dealing with each packet separately.
        ByteConvertRxBuffer[ireal++]=valueOf(nibble);
        if (i+byteSize != rx.length() ) std::cout<<", ";
    }
    std:: cout<<"}"<<endl;
    for(int i=0;i<countof(ByteConvertRxBuffer);i++)
    {
        rxBuffer[i]=ByteConvertRxBuffer[i];
    }
    for(i = 0; i < sizeof(rxBuffer); i++) 
    {
        checksum = calcCRC(rxBuffer[i], checksum);
    }
    PacketIndex++;
    if(checksum == 0)
    {
        crcOK = true;
        std::cout<<"  your packet number "<<PacketIndex<<" :" <<"Valid"<<endl;
    }
    else
    {
    crcOK = false;
    std::cout<<"your packet number "<<PacketIndex<<" :" <<"inValid"<<endl;
    }
}
int main() /*int main(int argc, char** argv){}*/
{
    std::string PacketData = "011101111001001010101010101010101010101010101100100110000101101010001110111100010000000000000011001010010000010111000011011001100100101110101100100101000011110110110001100110000111100110001100110000010111101111010000101101011100110011000000101101111110100010111100010100110000111111010110000101101111110011001111001011000011001001001100000001000100101101000110000010000111111110111111110";
    std::string SyncWord = "10010011000010110101000111011110"; // syncword pattern
    size_t notfound = PacketData.find(SyncWord); 
    assert(notfound != string::npos) ; //if not found as exit and don't complete. 
    auto matches = findPattern(PacketData, SyncWord );
    for(auto &match : matches) {
        std::cout << "Match found at position: " << match.first << std::endl;
        std::cout << "String is " << match.second << std::endl;
        deBin( match.second );
    }
    return 0;
}
Respuestas (1)
  Image Analyst
      
      
 el 6 de Ag. de 2020
        You don't need to declare the type of a variable in advance (uint8, int, etc.)
Opening braces are not needed to start a loop and a loop ends with the "end" keyword.  Replace 
}
with 
end
Replace
/* comment....
with
%
And for loops generally have this format:
for startingValue : stepValue : endingValue
Replace brackets by parentheses:
    substring[i-1]
with
    substring(i-1)
And != is ~= in MATLAB.
Other than that, just try it and handle errors as you encounter them.  It's a good way to learn MATLAB syntax.
For bit shifting, check out bitshift() and a whole family of bit functions that start with "bit".
2 comentarios
  Image Analyst
      
      
 el 9 de Ag. de 2020
				No.  You deleted your original question.  Why would I want to help you now, and how could I?
Ver también
Categorías
				Más información sobre Logical en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



