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;
// 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, whichis 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.
intexp;
//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
}// endfor 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 signis 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;
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ß
Einstellungen und Berechtigungen
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
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.