Hadamard code: Difference between revisions

From ciapini
Jump to navigation Jump to search
No edit summary
No edit summary
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
trasformata che mappa un messagio di lunghezza n su una matrice di dimensione 2^(n-1), rileva 2^(n-2) errori e ne corregge 2^(n-3)
trasformata che mappa un messagio di lunghezza n su una matrice di dimensione 2^(n-1), rileva 2^(n-2) errori e ne corregge 2^(n-3)


perche la il codice sia lineare e' bene che n sia una potenza di 2
perche il codice sia lineare e' necessario che n sia una potenza di 2


due righe della matrice differiscono in n/2 posizioni
due righe della matrice differiscono in n/2 posizioni
'''packing ortogonale''' ogni block code di un carattere viene associato a un bit del simbolo. in questo modo il carattere da trasmettere e' mappato nel domini odella frequenza, la sua rappresentazione come hadamard code nel dominio del tempo.
'''interleaving''' TODO


<source lang="C">
<source lang="C">
Line 14: Line 18:
#include <stdio.h>
#include <stdio.h>


uint8_t BlockDecodeChar(int16_t *DataMatrix, uint16_t HadamardCodeSize);
uint8_t BitsPerModulationSymbol,
void BlockEncodeChar(int16_t *DataMatrix, uint8_t Input, uint16_t HadamardCodeSize);
  AlphabetSize,
  BitsPerInput,
  HadamardCodeSize,
  SymbolsPerHadamardCode,
  HadamardCodesPerInterleaverBlock,
  InterleaverBlockSize,
  SymbolsPerInterleaverBlock;
 
uint8_t BlockDecodeChar(int16_t *DataMatrix);
void BlockEncodeChar(int16_t *DataMatrix, uint8_t Input);
uint8_t InterleaveBlock(uint8_t *InData, int16_t *DataMatrix, uint8_t *ModulationSymbolsBuffer);


void main(void){   
void main(void){   
   uint16_t HadamardCodeSize,
   // alfabeto
    BitsPerModulationSymbol,
  BitsPerModulationSymbol = 4; // bit rappresentati da un simbolo della modulazione
    ModulationSymbols,
  AlphabetSize = (1 << BitsPerModulationSymbol); // dimensione alfabeto modulato
    CharactersPerModulationBlock,
 
    ModulationBlockSize;
  // FEC
  BitsPerInput = 4; // dimensione singolo dato in ingresso
  HadamardCodeSize = 1 << (BitsPerInput - 1); // dimensione  del singolo codice hadamard in uscita 2^(n-1)
  SymbolsPerHadamardCode = (HadamardCodeSize/BitsPerModulationSymbol);
    
    
   uint8_t BitsPerCharacter,
   // interleaver
    CharPosition;
  HadamardCodesPerInterleaverBlock =  32; // per arrivare ai 128 voluti da aes
  InterleaverBlockSize  = (HadamardCodesPerInterleaverBlock * HadamardCodeSize); // dimensione del blocco di interleaving
  SymbolsPerInterleaverBlock = (SymbolsPerHadamardCode * HadamardCodesPerInterleaverBlock);
 
  // buffer matrici
  int16_t *FHT_Buffer;
  FHT_Buffer = malloc(HadamardCodeSize*sizeof(int16_t));
    
    
   BitsPerCharacter = 7; // dimensione singolo dato in ingresso
   // buffer modulazione
   HadamardCodeSize = 1 << (BitsPerCharacter - 1); // dimensione  del singolo codice hadamard in uscita 2^(n-1)
  uint8_t *ModulationSymbolsBuffer;
   ModulationSymbolsBuffer = malloc(SymbolsPerInterleaverBlock*sizeof(uint8_t));
    
    
   BitsPerModulationSymbol = 6; // bit rappresentati da un simbolo della modulazione
   // caratteri in ingresso
   ModulationSymbols = 1 << BitsPerModulationSymbol; // dimensione alfabeto modulato
   unsigned char InString[16] = { 0 };
  strcpy(InString,"ciao mondo cane"); 
    
    
   CharactersPerModulationBlock = BitsPerModulationSymbol; // caratteri in un blocco modulazione
   // dati input codice FEC
   ModulationBlockSize = BitsPerModulationSymbol * HadamardCodeSize; // dimensioni blocco modulazione
   uint8_t InData[32] = { 0 };
    
    
   printf("HadamardCodeSize %u\n", HadamardCodeSize); 
   // dati out codice FEC
   printf("ModulationSymbols %u\n", ModulationSymbols);
   uint8_t OutData[32] = { 0 };
    
    
   // buffer matrici
   // caratteri in uscita
   int16_t *FHT_Buffer;
   unsigned char OutString[16] = { 0 };
  FHT_Buffer = malloc(HadamardCodeSize*sizeof(int16_t));


   // buffer modulazione
   // ##################### //
   int16_t *ModulationSymbolsBuffer;
   
   ModulationSymbolsBuffer = malloc(HadamardCodeSize*sizeof(int16_t));
   printf("# HadamardCodeSize %u\n", HadamardCodeSize);
   printf("# AlphabetSize %u\n", AlphabetSize); 
  printf("# HadamardCodesPerInterleaverBlock %u\n", HadamardCodesPerInterleaverBlock);
  printf("# InterleaverBlockSize %u\n", InterleaverBlockSize); 
  printf("# SymbolsPerInterleaverBlock %u\n", SymbolsPerInterleaverBlock)
   
  printf("In: %s\n", InString);


   // dato in ingresso
   // formatta dati in ingresso, lsnibble first
   unsigned char InString[16];
   for (uint8_t TimeBit = 0; TimeBit < 16; TimeBit++){
  strcpy(InString,"ciao mondo");
      InData[TimeBit * 2] |= InString[TimeBit] & 0b1111;
      InData[(TimeBit * 2) + 1] |= (InString[TimeBit] >> 4) & 0b1111;
  }
    
    
   // impacchetta un modulation block
   InterleaveBlock(InData, FHT_Buffer, ModulationSymbolsBuffer);
  for (uint8_t CharPosition = 0; CharPosition < BitsPerModulationSymbol; CharPosition++){
      printf("encoding char %c pos %d\n", InString[CharPosition], CharPosition);
      BlockEncodeChar(FHT_Buffer, InString[CharPosition], HadamardCodeSize);     
      OrthogonalPack(FHT_Buffer, ModulationSymbolsBuffer, CharPosition, HadamardCodeSize);
  }


   // pulisci buffer
   // pulisci buffer
   memset(FHT_Buffer, 0, HadamardCodeSize*sizeof(int16_t));
   memset(FHT_Buffer, 0, HadamardCodeSize*sizeof(int16_t));
   printbuf(ModulationSymbolsBuffer, HadamardCodeSize, 8);
   printbuf(ModulationSymbolsBuffer, SymbolsPerInterleaverBlock, 8);
 
  // disturbo
  ModulationSymbolsBuffer[2] = 42;
  ModulationSymbolsBuffer[6] = 42;
  ModulationSymbolsBuffer[12] = 42;
  ModulationSymbolsBuffer[11] = 42;
    
    
   //ModulationSymbolsBuffer[15] = ModulationSymbolsBuffer[25];
   // deinterleave
   //ModulationSymbolsBuffer[19] = ModulationSymbolsBuffer[22];
   DeInterleaveBlock(OutData, FHT_Buffer, ModulationSymbolsBuffer);
    
    
   // disimpacchetta un modulation block
   // formatta dati in uscita
   for (uint8_t CharPosition = 0; CharPosition < BitsPerModulationSymbol; CharPosition++){
   for (uint8_t TimeBit = 0; TimeBit < 16; TimeBit++){
       OrthogonalUnPack(FHT_Buffer, ModulationSymbolsBuffer, CharPosition, HadamardCodeSize);
       OutString[TimeBit] |= (OutData[TimeBit * 2] & 0b1111);
        
       OutString[TimeBit] |= (OutData[(TimeBit * 2) + 1] & 0b1111) << 4;
      uint8_t OutChar;
      OutChar = BlockDecodeChar(FHT_Buffer, HadamardCodeSize);
     
   }
   }
  printf("Out: %s\n", OutString);
}
}


// Forward Fast Hadamard Transform
// Forward Fast Hadamard Transform
void FastHadamardTransform(int16_t *Data, uint16_t Len){  
void FastHadamardTransform(int16_t *Data){  
   int16_t Bit1, Bit2, NewBit1, NewBit2 = 0;
   int16_t Bit1, Bit2, NewBit1, NewBit2 = 0;
   for (uint16_t Step = 1; Step < Len; Step <<= 1) {
   for (uint16_t Step = 1; Step < HadamardCodeSize; Step <<= 1) {
       for (uint16_t Ptr = 0; Ptr < Len; Ptr += (Step << 1)) {  
       for (uint16_t Ptr = 0; Ptr < HadamardCodeSize; Ptr += (Step << 1)) {  
for (uint16_t Ptr2 = Ptr; Ptr2 - Ptr < Step; ++ Ptr2) {
for (uint16_t Ptr2 = Ptr; Ptr2 - Ptr < Step; ++ Ptr2) {
    Bit1      = Data[Ptr2];   
    Bit1      = Data[Ptr2];   
Line 95: Line 126:


// Inverse Fast Hadamard Transform
// Inverse Fast Hadamard Transform
void InverseFastHadamardTransform(int16_t *Data, uint16_t Len){
void InverseFastHadamardTransform(int16_t *Data){
   int16_t Bit1, Bit2, NewBit1, NewBit2 = 0;
   int16_t Bit1, Bit2, NewBit1, NewBit2 = 0;
   for (uint16_t Step = (Len >> 1); Step; Step >>= 1) {
   for (uint16_t Step = (HadamardCodeSize >> 1); Step; Step >>= 1) {
       for (uint16_t Ptr = 0; Ptr < Len; Ptr += (Step << 1)) {
       for (uint16_t Ptr = 0; Ptr < HadamardCodeSize; Ptr += (Step << 1)) {
for (uint16_t Ptr2 = Ptr; Ptr2 - Ptr < Step; ++ Ptr2) {
for (uint16_t Ptr2 = Ptr; Ptr2 - Ptr < Step; ++ Ptr2) {
    Bit1              = Data[Ptr2];
    Bit1              = Data[Ptr2];
Line 122: Line 153:


// genera block code FEC
// genera block code FEC
void BlockEncodeChar(int16_t *DataMatrix, uint8_t Input, uint16_t HadamardCodeSize){
void BlockEncodeChar(int16_t *DataMatrix, uint8_t Input){
   // pulisci buffer
   // pulisci buffer
   memset(DataMatrix, 0, HadamardCodeSize*sizeof(int16_t));
   memset(DataMatrix, 0, HadamardCodeSize*sizeof(int16_t));
Line 134: Line 165:
    
    
   // codifica
   // codifica
   InverseFastHadamardTransform(DataMatrix, HadamardCodeSize);   
   InverseFastHadamardTransform(DataMatrix);   
    
    
   // printbuf(FHT_Buffer,HadamardCodeSize,BitsPerCharacter);             
   // printbuf(FHT_Buffer,HadamardCodeSize,BitsPerInput);             
   //     
   //     
   // riduci a binario
   // riduci a binario
Line 145: Line 176:


// decodifica block code FEC
// decodifica block code FEC
uint8_t BlockDecodeChar(int16_t *DataMatrix, uint16_t HadamardCodeSize){
uint8_t BlockDecodeChar(int16_t *DataMatrix){
   for (uint16_t TimeBit = 0; TimeBit < HadamardCodeSize; TimeBit++){
   for (uint16_t TimeBit = 0; TimeBit < HadamardCodeSize; TimeBit++){
       if(DataMatrix[TimeBit] == 0) DataMatrix[TimeBit] = -1;
       if(DataMatrix[TimeBit] == 0) DataMatrix[TimeBit] = -1;
Line 151: Line 182:
    
    
   // decodifica
   // decodifica
   FastHadamardTransform(DataMatrix, HadamardCodeSize);
   FastHadamardTransform(DataMatrix);
    
    
  // printbuf(DataMatrix,HadamardCodeSize,BitsPerCharacter);                 
   // estrae dalla matrice
   // estrae dalla matrice
   int16_t Peak = 0;
   int16_t Peak = 0;
Line 174: Line 204:
}
}


// todo: rotazione posizione carattere, altrimenti un fischio distrugge un carattere
// interleaving
 
uint8_t InterleaveBlock(uint8_t *InData, int16_t *DataMatrix, uint8_t *ModulationSymbolsBuffer){
// impacca con alfabeto ortogonale a tempo
  uint8_t BitPos, SymbolOffset, BitOffset;
void OrthogonalPack(int16_t *DataMatrix, int16_t *ModulationSymbolsBuffer, uint8_t CharPosition, uint16_t HadamardCodeSize){
  for (uint8_t FECBlock = 0; FECBlock < HadamardCodesPerInterleaverBlock; FECBlock++){
  for (uint8_t TimeBit = 0; TimeBit < HadamardCodeSize; TimeBit++){  
     
      ModulationSymbolsBuffer[TimeBit] |= (DataMatrix[TimeBit] << CharPosition);
      BlockEncodeChar(DataMatrix, InData[FECBlock]);
      for (uint8_t FECBit = 0; FECBit < HadamardCodeSize; FECBit++){
BitPos = (FECBlock * HadamardCodeSize) + FECBit;
BitOffset = BitPos % BitsPerModulationSymbol;
SymbolOffset = ((FECBit * HadamardCodeSize) + FECBlock) % SymbolsPerInterleaverBlock;
ModulationSymbolsBuffer[SymbolOffset] |= (DataMatrix[FECBit] & 0b1) << BitOffset;
// printf("%u,%u,%u\n", SymbolOffset, BitOffset, BitPos);
      }
//      printf("\n in FEC: ");
//      printbuf(DataMatrix, HadamardCodeSize, 8);
   }
   }
}
}


// disimpacca con alfabeto ortogonale a tempo
// deinterleaving
void OrthogonalUnPack(int16_t *DataMatrix, int16_t *ModulationSymbolsBuffer, uint8_t CharPosition, uint16_t HadamardCodeSize){
void DeInterleaveBlock(uint8_t *OutData, int16_t *DataMatrix, uint8_t *ModulationSymbolsBuffer){
  for (uint8_t TimeBit = 0; TimeBit < HadamardCodeSize; TimeBit++){  
  uint8_t BitPos, SymbolOffset, BitOffset;
      DataMatrix[TimeBit] = (ModulationSymbolsBuffer[TimeBit] >> CharPosition) & 0b1;
  for (uint8_t FECBlock = 0; FECBlock < HadamardCodesPerInterleaverBlock; FECBlock++){
      // pulisci matrice
      memset(DataMatrix, 0, HadamardCodeSize*sizeof(int16_t));
      for (uint8_t FECBit = 0; FECBit < HadamardCodeSize; FECBit++){
BitPos = (FECBlock * HadamardCodeSize) + FECBit;
BitOffset= BitPos % BitsPerModulationSymbol;
SymbolOffset = ((FECBit * HadamardCodeSize) + FECBlock) % SymbolsPerInterleaverBlock;
DataMatrix[FECBit] |= ((ModulationSymbolsBuffer[SymbolOffset] & (0b1 << BitOffset)) >> BitOffset);
      }
//      printf("\n out FEC:\n");
//      printbuf(DataMatrix, HadamardCodeSize, 8);
     
      OutData[FECBlock] = BlockDecodeChar(DataMatrix);
   }
   }
}
}
</source>
</source>

Latest revision as of 10:21, 3 July 2012

trasformata che mappa un messagio di lunghezza n su una matrice di dimensione 2^(n-1), rileva 2^(n-2) errori e ne corregge 2^(n-3)

perche il codice sia lineare e' necessario che n sia una potenza di 2

due righe della matrice differiscono in n/2 posizioni

packing ortogonale ogni block code di un carattere viene associato a un bit del simbolo. in questo modo il carattere da trasmettere e' mappato nel domini odella frequenza, la sua rappresentazione come hadamard code nel dominio del tempo.

interleaving TODO

// Hadamard codec - gnugo@hacari.org - 06/2012

#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include <string.h>
#include <stdio.h>

uint8_t BitsPerModulationSymbol, 
  AlphabetSize, 
  BitsPerInput, 
  HadamardCodeSize, 
  SymbolsPerHadamardCode, 
  HadamardCodesPerInterleaverBlock,
  InterleaverBlockSize,
  SymbolsPerInterleaverBlock;

uint8_t BlockDecodeChar(int16_t *DataMatrix);
void BlockEncodeChar(int16_t *DataMatrix, uint8_t Input);
uint8_t InterleaveBlock(uint8_t *InData, int16_t *DataMatrix, uint8_t *ModulationSymbolsBuffer);

void main(void){  
   // alfabeto
   BitsPerModulationSymbol = 4; // bit rappresentati da un simbolo della modulazione
   AlphabetSize = (1 << BitsPerModulationSymbol); // dimensione alfabeto modulato
   
   // FEC
   BitsPerInput = 4; // dimensione singolo dato in ingresso
   HadamardCodeSize = 1 << (BitsPerInput - 1); // dimensione  del singolo codice hadamard in uscita 2^(n-1)
   SymbolsPerHadamardCode = (HadamardCodeSize/BitsPerModulationSymbol);
   
   // interleaver
   HadamardCodesPerInterleaverBlock =  32; // per arrivare ai 128 voluti da aes
   InterleaverBlockSize  = (HadamardCodesPerInterleaverBlock * HadamardCodeSize); // dimensione del blocco di interleaving
   SymbolsPerInterleaverBlock = (SymbolsPerHadamardCode * HadamardCodesPerInterleaverBlock);
   
   // buffer matrici 
   int16_t *FHT_Buffer;
   FHT_Buffer = malloc(HadamardCodeSize*sizeof(int16_t));
   
   // buffer modulazione 
   uint8_t *ModulationSymbolsBuffer;
   ModulationSymbolsBuffer = malloc(SymbolsPerInterleaverBlock*sizeof(uint8_t));
   
   // caratteri in ingresso
   unsigned char InString[16] = { 0 };
   strcpy(InString,"ciao mondo cane");  
   
   // dati input codice FEC
   uint8_t InData[32] = { 0 };
   
   // dati out codice FEC
   uint8_t OutData[32] = { 0 };
   
   // caratteri in uscita
   unsigned char OutString[16] = { 0 };

   // ##################### //
     
   printf("# HadamardCodeSize %u\n", HadamardCodeSize);  
   printf("# AlphabetSize %u\n", AlphabetSize);  
   printf("# HadamardCodesPerInterleaverBlock %u\n", HadamardCodesPerInterleaverBlock);
   printf("# InterleaverBlockSize %u\n", InterleaverBlockSize);  
   printf("# SymbolsPerInterleaverBlock %u\n", SymbolsPerInterleaverBlock);  
    
   printf("In: %s\n", InString);

   // formatta dati in ingresso, lsnibble first
   for (uint8_t TimeBit = 0; TimeBit < 16; TimeBit++){
      InData[TimeBit * 2] |= InString[TimeBit] & 0b1111;
      InData[(TimeBit * 2) + 1] |= (InString[TimeBit] >> 4) & 0b1111;
   }
   
   InterleaveBlock(InData, FHT_Buffer, ModulationSymbolsBuffer);

   // pulisci buffer
   memset(FHT_Buffer, 0, HadamardCodeSize*sizeof(int16_t));
   printbuf(ModulationSymbolsBuffer, SymbolsPerInterleaverBlock, 8);

   // disturbo
   ModulationSymbolsBuffer[2] = 42;
   ModulationSymbolsBuffer[6] = 42;
   ModulationSymbolsBuffer[12] = 42;
   ModulationSymbolsBuffer[11] = 42;
   
   // deinterleave
   DeInterleaveBlock(OutData, FHT_Buffer, ModulationSymbolsBuffer);
   
   // formatta dati in uscita
   for (uint8_t TimeBit = 0; TimeBit < 16; TimeBit++){
      OutString[TimeBit] |= (OutData[TimeBit * 2] & 0b1111);
      OutString[TimeBit] |= (OutData[(TimeBit * 2) + 1] & 0b1111) << 4;
   }
   printf("Out: %s\n", OutString);
}

// Forward Fast Hadamard Transform
void FastHadamardTransform(int16_t *Data){ 
   int16_t Bit1, Bit2, NewBit1, NewBit2 = 0;
   for (uint16_t Step = 1; Step < HadamardCodeSize; Step <<= 1) {
      for (uint16_t Ptr = 0; Ptr < HadamardCodeSize; Ptr += (Step << 1)) { 
	 for (uint16_t Ptr2 = Ptr; Ptr2 - Ptr < Step; ++ Ptr2) {
	    Bit1       = Data[Ptr2];  
	    Bit2       = Data[Ptr2+Step];
	    NewBit1    = Bit2; 
	    NewBit1   += Bit1;
	    NewBit2    = Bit2; 
	    NewBit2   -= Bit1;
	    Data[Ptr2] = NewBit1;
	    Data[Ptr2+Step]=NewBit2;
	 }
      }
   }
}	

// Inverse Fast Hadamard Transform
void InverseFastHadamardTransform(int16_t *Data){
   int16_t Bit1, Bit2, NewBit1, NewBit2 = 0;
   for (uint16_t Step = (HadamardCodeSize >> 1); Step; Step >>= 1) {
      for (uint16_t Ptr = 0; Ptr < HadamardCodeSize; Ptr += (Step << 1)) {
	 for (uint16_t Ptr2 = Ptr; Ptr2 - Ptr < Step; ++ Ptr2) {
	    Bit1               = Data[Ptr2];
	    Bit2               = Data[Ptr2 + Step];
	    NewBit1            = Bit1;
	    NewBit1            -= Bit2;
	    NewBit2            = Bit1;
	    NewBit2            += Bit2;
	    Data[Ptr2]         = NewBit1;
	    Data[Ptr2+Step] = NewBit2;
	 }
      }
   }
}

void printbuf(int16_t *Data, uint16_t Len, uint16_t Step){
   for(uint16_t i=0; i<Len; i++){
      if(i % (Step)== 0) printf("\n");
      printf("%d\t",Data[i]);
   }
   printf("\n");
}

// genera block code FEC
void BlockEncodeChar(int16_t *DataMatrix, uint8_t Input){
   // pulisci buffer
   memset(DataMatrix, 0, HadamardCodeSize*sizeof(int16_t));

   // inserimento carattere nella matrice, alti negativi, bassi positivi
   // il valore massimo del dato e' 2 * size(matrice)
   if(Input < (HadamardCodeSize * 2)){
      if (Input < HadamardCodeSize) DataMatrix[Input] = 1;
      else DataMatrix[Input - HadamardCodeSize] = -1;
   }
   
   // codifica
   InverseFastHadamardTransform(DataMatrix);  
   
   // printbuf(FHT_Buffer,HadamardCodeSize,BitsPerInput);            
   //    
   // riduci a binario
   for (uint16_t TimeBit = 0; TimeBit < HadamardCodeSize; TimeBit++){
      if(DataMatrix[TimeBit] < 0) DataMatrix[TimeBit] = 0;
   }
}

// decodifica block code FEC
uint8_t BlockDecodeChar(int16_t *DataMatrix){
   for (uint16_t TimeBit = 0; TimeBit < HadamardCodeSize; TimeBit++){
      if(DataMatrix[TimeBit] == 0) DataMatrix[TimeBit] = -1;
   }
   
   // decodifica
   FastHadamardTransform(DataMatrix);
   
   // estrae dalla matrice
   int16_t Peak = 0;
   uint8_t PeakPos = 0;
   uint16_t SqrSum = 0;
   for (uint8_t TimeBit = 0; TimeBit < HadamardCodeSize; TimeBit++){
      int16_t Signal = DataMatrix[TimeBit];
      SqrSum += Signal * Signal;
      if (fabs(Signal) > fabs(Peak)){
	 Peak = Signal;
	 PeakPos = TimeBit;
      }
   }
   
   uint8_t OutChar = PeakPos;
   if (Peak < 0) OutChar += HadamardCodeSize;
   SqrSum -= Peak * Peak;
   printf("OutChar %c (Peak %d, PeakPos %d, SqrSum %d)\n", OutChar, Peak, PeakPos, SqrSum);
   return OutChar;
}

// interleaving
uint8_t InterleaveBlock(uint8_t *InData, int16_t *DataMatrix, uint8_t *ModulationSymbolsBuffer){
   uint8_t BitPos, SymbolOffset, BitOffset;
   for (uint8_t FECBlock = 0; FECBlock < HadamardCodesPerInterleaverBlock; FECBlock++){
      
      BlockEncodeChar(DataMatrix, InData[FECBlock]);
      for (uint8_t FECBit = 0; FECBit < HadamardCodeSize; FECBit++){
	 BitPos = (FECBlock * HadamardCodeSize) + FECBit;
	 BitOffset = BitPos % BitsPerModulationSymbol;
	 SymbolOffset = ((FECBit * HadamardCodeSize) + FECBlock) % SymbolsPerInterleaverBlock;
	 ModulationSymbolsBuffer[SymbolOffset] |= (DataMatrix[FECBit] & 0b1) << BitOffset;
	 // printf("%u,%u,%u\n", SymbolOffset, BitOffset, BitPos);
      }
//      printf("\n in FEC: ");
//      printbuf(DataMatrix, HadamardCodeSize, 8);
   }
}

// deinterleaving
void DeInterleaveBlock(uint8_t *OutData, int16_t *DataMatrix, uint8_t *ModulationSymbolsBuffer){
   uint8_t BitPos, SymbolOffset, BitOffset;
   for (uint8_t FECBlock = 0; FECBlock < HadamardCodesPerInterleaverBlock; FECBlock++){
      // pulisci matrice
      memset(DataMatrix, 0, HadamardCodeSize*sizeof(int16_t));
      for (uint8_t FECBit = 0; FECBit < HadamardCodeSize; FECBit++){
	 BitPos = (FECBlock * HadamardCodeSize) + FECBit;
	 BitOffset= BitPos % BitsPerModulationSymbol;
	 SymbolOffset = ((FECBit * HadamardCodeSize) + FECBlock) % SymbolsPerInterleaverBlock;
	 DataMatrix[FECBit] |= ((ModulationSymbolsBuffer[SymbolOffset] & (0b1 << BitOffset)) >> BitOffset);
      }
//      printf("\n out FEC:\n");
//      printbuf(DataMatrix, HadamardCodeSize, 8);
      
      OutData[FECBlock] = BlockDecodeChar(DataMatrix);
   }
}