JiaoXianjun / BTLE

Bluetooth Low Energy (BLE) packet sniffer and transmitter for both standard and non standard (raw bit) based on Software Defined Radio (SDR).
http://sdr-x.github.io/
Apache License 2.0
733 stars 135 forks source link

To explain function demod_bits() #6

Open mutouyueliang opened 8 years ago

mutouyueliang commented 8 years ago

Hi Xianjun, Function demod_bits(a, num_bits, sample_per_symbol) implements the way how ADC samples is converted to bits.

1: Could you explain why the following code is able to determine the bit is 0 or 1:

if (I0*Q1 - I1*Q0) > 0
        bits(k) = 1; 

2: If sample_per_symbol is 4, just the first two samples are used to determine the bit value of the symbol as showed in function demo_bits(). What's the purpose of the other two samples? Will they be just discarded?

a232319779 commented 8 years ago

I'm still confusing about it. Anybody could explain how could it demodulate the bluetooth signal which modulated by GFSK?

Bushbaker27 commented 1 year ago

The given C code represents a function called demod_byte that takes an array of IQ samples (IQ_TYPE* rxp), the number of bytes to demodulate (int num_byte), and an output array to store the demodulated bytes (uint8_t *out_byte).

Let's break down the function and understand its functionality:

  1. Initialization:

    • Initialize variables i, j, I0, Q0, I1, Q1, bit_decision, and sample_idx.
    • i and j are loop counters used in the subsequent loops.
    • sample_idx keeps track of the current position in the rxp array.
  2. Byte Demodulation Loop:

    • Iterate over each byte to demodulate (num_byte times).
    • Set the output byte to 0 before processing a new byte (out_byte[i] = 0).
  3. Bit Demodulation Loop:

    • Iterate 8 times for each byte to demodulate (j<8).
    • Perform IQ demodulation by accessing IQ samples at specific positions in the rxp array.
      • I0 and Q0 store the real and imaginary parts of the current sample (rxp[sample_idx] and rxp[sample_idx+1]).
      • I1 and Q1 store the real and imaginary parts of the next sample (rxp[sample_idx+2] and rxp[sample_idx+3]).
    • Calculate the bit decision based on the demodulated IQ samples.
      • If (I0*Q1 - I1*Q0) is greater than 0, bit_decision is set to 1; otherwise, it is set to 0.
    • Store the bit decision in the appropriate position within the current byte.
      • The bit decision is left-shifted by j positions and combined with the existing bits using a bitwise OR operation (out_byte[i] = out_byte[i] | (bit_decision<<j)).
    • Update sample_idx to point to the next symbol in the rxp array by incrementing it by SAMPLE_PER_SYMBOL*2.
      • SAMPLE_PER_SYMBOL indicates the number of IQ samples per symbol, and the multiplication by 2 accounts for the real and imaginary parts.
  4. Return:

    • The function does not have a return value, as the demodulated bytes are stored in the out_byte array, which is passed by reference.

In summary, this code performs demodulation of IQ samples to recover binary data. It processes the IQ samples in chunks of four (I0, Q0, I1, Q1) and calculates a bit decision based on the difference between the products of consecutive samples. The bit decisions are combined to form bytes, which are then stored in the output array.