WICHTIG: Der Betrieb von goMatlab.de wird privat finanziert fortgesetzt. - Mehr Infos...

Mein MATLAB Forum - goMatlab.de

Mein MATLAB Forum

 
Gast > Registrieren       Autologin?   

Partner:




Forum
      Option
[Erweitert]
  • Diese Seite per Mail weiterempfehlen
     


Gehe zu:  
Neues Thema eröffnen Neue Antwort erstellen

Bitoperationen

 

Steeef
Forum-Newbie

Forum-Newbie


Beiträge: 7
Anmeldedatum: 02.06.14
Wohnort: ---
Version: ---
     Beitrag Verfasst am: 04.06.2014, 09:18     Titel: Bitoperationen
  Antworten mit Zitat      
Hallo zusammen,

ich bin Neuling in Sachen Matlab und habe sogleich die ersten Probleme. Die Matlab Dokumentation zu den geläufigen Bitor und Bitand Funktionen habe ich bereits studiert, werde aber nicht besonders schlau daraus.
Es handelt sich um die untenstehenden Code um den mu-Law Algorithmus, welchen ich implementieren möchte.

Ich habe folgenden c-Code den ich gerne in Matlab übersetzen möchte:
Code:

    unsigned char encode(short sample){
       short BIAS = 132;//0x84
       short CLIP = 32635;

       //Convert sample to sign-magnitude
       int sign = sample & 0x8000;
       if(sign != 0){
         sample = (short)-sample;
         sign = 0x80;
       }// end if

       // Because of the bias that is added, allowing
       // a value larger than CLIP would result in
       // integer overflow, so clip it.
       if(sample > CLIP) sample = CLIP;

       // Convert from 16-bit linear PCM to ulaw
       // Adding this bias guarantees a 1 bit in the
       // exponent region of the data, which is the
       // eight bits to the right of the sign bit.
       sample += BIAS;

       //Exponent value is the position of the first
       // 1 to the right of the sign bit in the
       // exponent region of the data.
       // Find the position of the first 1 to the
       // right of the sign bit, counting from right
       // to left in the exponent region.  The
       // exponent position (value) can range from 0
       // to 7.
       // Could use a table lookup but will compute
       // on the fly instead because that is better
       // for teaching the algorithm.
       int exp;
       //Shift sign bit off to the left
       short temp = (short)(sample << 1);
       for(exp = 7; exp > 0; exp--){
         if((temp & 0x8000) != 0) break;// found it
         temp = (short)(temp << 1);// shift and loop
       }// end for loop

       // The mantissa is the four bits to the right
       // of the first 1 bit in the exponent region.
       // Shift those four bits to the four lsb of
       // the 16-bit value.
       temp = (short)(sample >> (exp + 3));
       // Mask and save those four bits
       int mantis = temp & 0x000f;
       // Construct the complement of the ulaw byte.
       // Set the sign bit in the msb of the 8-bit
       // byte.  The value of sign is either 0x00 or
       // 0x80.
       // Position the exponent in the three bits to
       // the right of the sign bit.
       // Set the 4-bit mantissa in the four lsb of
       // the byte.
       // Note that the one's complement of this
       // value will be returned.
       char ulawByte = (char)(sign | (exp << 4) | mantis);

       //Now complement to create actual ulaw byte
       // and return it.
       return (char)~ulawByte;
                           
     }//end encode        
 


Meine bisherigen Erfolge in Matlab:
Code:

SAMPLE = int32(60);

BIAS = int32(132);
CLIP = int32(32635);
MASKE1 = int32(32768);     %0x8000
MASKE2 = int32(15);          %0x000f

SIGN =  bitand(SAMPLE,MASKE1);

    if SIGN ~= 0
        SAMPLE = -SAMPLE;
        SIGN = 128;               %0x80
    end
   
    if SAMPLE > CLIP
        SAMPLE = CLIP;
    end  
   
    SAMPLE = SAMPLE + BIAS;
    SAMPLE = bitshift(SAMPLE,1);
    TEMP = SAMPLE;
   
    EXP = 0;
    EXP = int8(EXP);
   
    for EXP=7:-1:(EXP>0)
        TEMP_HILF = bitand(TEMP, MASKE1);
        if (TEMP_HILF ~= 0)
            break;
        end
        TEMP = bitshift(TEMP,1);
    end
   
    EXP_HOCH = int8(EXP + 3);
    SAMPLE = bitshift(SAMPLE,-EXP_HOCH);
    TEMP = SAMPLE;
    MANTIS = bitand(TEMP,MASKE2);
   
    MANTIS = uint8(MANTIS);
    EXP = uint8(EXP);
    SIGN = uint8(SIGN);
   
    EXP = bitshift(EXP,4);
    SCHRITT1 = bitor(EXP, MANTIS,'uint8');
    uLAW = uint8(bitor(SIGN,SCHRITT1,'uint8'));
    uLAW = bitcmp(uLAW);
 



Die Ergebnisse für die Randwerte -32768 bis -1 bzw. 1 bis 32767 stimmen.
Ergebnis für -32768 = 0
Ergebnis für -1 = 126
Ergebnis für 32767 = 128
Ergebnis für 1 = 255

Mein Problem: Nehme ich als Sample Wert den Wert 60 an, so gibt mir der Algorithmus die Zahl 255 aus, was eigentlich nicht sein darf. Als Ergebnis erwarte ich eine Zahl um die 247 rum.

Mache ich irgendetwas bei den Datentypen oder bei den Operationen falsch?

Gruß
Private Nachricht senden Benutzer-Profile anzeigen


Neues Thema eröffnen Neue Antwort erstellen



Einstellungen und Berechtigungen
Beiträge der letzten Zeit anzeigen:

Du kannst Beiträge in dieses Forum schreiben.
Du kannst auf Beiträge in diesem Forum antworten.
Du kannst deine Beiträge in diesem Forum nicht bearbeiten.
Du kannst deine Beiträge in diesem Forum nicht löschen.
Du kannst an Umfragen in diesem Forum nicht mitmachen.
Du kannst Dateien in diesem Forum posten
Du kannst Dateien in diesem Forum herunterladen
.





 Impressum  | Nutzungsbedingungen  | Datenschutz | FAQ | goMatlab RSS Button RSS

Hosted by:


Copyright © 2007 - 2025 goMatlab.de | Dies ist keine offizielle Website der Firma The Mathworks

MATLAB, Simulink, Stateflow, Handle Graphics, Real-Time Workshop, SimBiology, SimHydraulics, SimEvents, and xPC TargetBox are registered trademarks and The MathWorks, the L-shaped membrane logo, and Embedded MATLAB are trademarks of The MathWorks, Inc.