def __transmitSignalWithFrameSynchronization(expectedDataPosition=32 *
                                             __SYMBOL_LENGTH_IN_BITS,
                                             snr=None,
                                             offset=0,
                                             freqErr=0,
                                             phaseErr=0):
    modulator = Modulator(__CARRIER_FREQ, __SYMBOL_LENGTH_IN_BITS, __FI,
                          __SAMPLING_RATE, __NUM_OF_PERIODS_IN_SYMBOL)
    demodulator = Demodulator(__CARRIER_FREQ, __SYMBOL_LENGTH_IN_BITS, __FI,
                              __SAMPLING_RATE, __NUM_OF_PERIODS_IN_SYMBOL)
    channel = RadioChannel()
    frameSync = FrameSynchronization(modulator.modulate(__HEADER),
                                     __SYMBOL_LENGTH_IN_BITS)

    signal = modulator.modulate(__FRAME)
    transmittedSignal = channel.transmit(signal,
                                         snr=snr,
                                         signalOffset=offset,
                                         freqErr=freqErr,
                                         phaseErr=phaseErr)
    dataPosition = frameSync.synchronizeFrame(transmittedSignal)
    assert (dataPosition == expectedDataPosition + offset)

    demodulatedBits = demodulator.demodulate(transmittedSignal[dataPosition:])
    return demodulatedBits
Exemplo n.º 2
0
def __transmitSignalWithFrameSynchronization(expectedDataPosition=32 *
                                             __SYMBOL_LENGTH_IN_BITS,
                                             snr=None,
                                             offset=0,
                                             freqErr=0,
                                             phaseErr=0):
    modulator = Modulator(__CARRIER_FREQ, __SYMBOL_LENGTH_IN_BITS, __FI,
                          __SAMPLING_RATE)
    demodulator = Demodulator(__CARRIER_FREQ, __SYMBOL_LENGTH_IN_BITS, __FI,
                              __SAMPLING_RATE)
    channel = RadioChannel(__SAMPLING_RATE)
    frameSync = FrameSynchronization(modulator.modulate(__START_HEADER),
                                     modulator.modulate(__END_HEADER),
                                     __SYMBOL_LENGTH_IN_BITS, __SAMPLING_RATE)

    modulatedSignal = modulator.modulate(__FRAME)
    transmittedSignal = channel.transmit(modulatedSignal,
                                         snr=snr,
                                         signalOffset=offset,
                                         freqErr=freqErr,
                                         phaseErr=phaseErr)

    dataPosition = frameSync.synchronizeStartHeader(transmittedSignal)
    dataEndPosition = frameSync.synchronizeStopHeader(transmittedSignal)
    assert (dataPosition == expectedDataPosition + offset)

    transmittedSignal = frameSync.correctFreqAndPhase(
        transmittedSignal[dataPosition:dataEndPosition])
    demodulatedBits = demodulator.demodulate(transmittedSignal)
    return demodulatedBits
def __transmitSignal(samplingError, offset, snr, attenuation, freqErr, phaseErr):
    modulator = Modulator(__CARRIER_FREQ, __SYMBOL_LENGTH_IN_BITS, __FI, __SAMPLING_RATE)
    demodulator = Demodulator(__CARRIER_FREQ, __SYMBOL_LENGTH_IN_BITS, __FI, __SAMPLING_RATE)
    channel = RadioChannel(__SAMPLING_RATE)
    frameSync = FrameSynchronization(modulator.modulate(__HEADER), modulator.modulate(__END_HEADER),
                                     __SYMBOL_LENGTH_IN_BITS, __SAMPLING_RATE)
    timeRecover = TimingRecovery(__SYMBOL_LENGTH_IN_BITS)

    modulationStart = time.time()

    modulatedSignal = modulator.modulate(__FRAME)

    assert(time.time() - modulationStart < 1 / 25)

    transmittedSignal = channel.transmit(modulatedSignal, signalOffset=offset, adcSamplingErr=samplingError, snr=snr,
                                         channelAttenuation=attenuation, freqErr=freqErr, phaseErr=phaseErr)

    synchronizationStart = time.time()

    dataPosition = frameSync.synchronizeStartHeader(transmittedSignal)
    dataEndPosition = frameSync.synchronizeStopHeader(transmittedSignal[dataPosition:])
    transmittedSignal = frameSync.correctFreqAndPhase(transmittedSignal[dataPosition:dataPosition + dataEndPosition])
    transmittedSignal = timeRecover.synchronizeTiming(transmittedSignal)

    demodulatedBits = demodulator.demodulate(transmittedSignal)

    assert (time.time() - synchronizationStart < 1 / 25)
    return demodulatedBits
def __modulateAndDemodulate(snr=None, attenuation=1):
    modulator = Modulator(__CARRIER_FREQ, __SYMBOL_LENGTH_IN_BITS, __FI,
                          __SAMPLING_RATE, __NUM_OF_PERIODS_IN_SYMBOL)
    demodulator = Demodulator(__CARRIER_FREQ, __SYMBOL_LENGTH_IN_BITS, __FI,
                              __SAMPLING_RATE, __NUM_OF_PERIODS_IN_SYMBOL)
    channel = RadioChannel()

    signal = modulator.modulate(__BITS)
    transmittedSignal = channel.transmit(signal,
                                         snr,
                                         channelAttenuation=attenuation)
    demodulatedBits = demodulator.demodulate(transmittedSignal)
    return demodulatedBits
def shouldDemodulateMostOfInputBitsWithNoise():
    dem = Demodulator(__CARRIER_FREQ, __SYMBOL_LENGTH_IN_BITS, __FI, __SAMPLE_RATE)
    signal = __calcSignal()

    noise = np.random.normal(0, 1, int(len(signal))) * __calcSignalPower(signal) + 1j * np.random.normal(0, 1, int(len(signal))) * __calcSignalPower(signal)
    signal += noise
    demodulated = dem.demodulate(signal)
    assert(len(__INPUT_BITS) == len(demodulated))

    corruptedBits = 0
    for i in range(int(len(__INPUT_BITS))) :
        if demodulated[i] != __OUTPUT_BITS[i] :
            corruptedBits += 1
    assert(corruptedBits/int(len(__INPUT_BITS)) < 0.01)
def __transmitSignalWithTimingSynchronization(samplingErr):
    modulator = Modulator(__CARRIER_FREQ, __SYMBOL_LENGTH_IN_BITS, __FI,
                          __SAMPLING_RATE)
    demodulator = Demodulator(__CARRIER_FREQ, __SYMBOL_LENGTH_IN_BITS, __FI,
                              __SAMPLING_RATE)
    timeRecover = TimingRecovery(__SYMBOL_LENGTH_IN_BITS)
    channel = RadioChannel(__SAMPLING_RATE)

    signal = modulator.modulate(__BITS)
    transmittedSignal = channel.transmit(signal,
                                         adcSamplingErr=samplingErr,
                                         snr=10)
    transmittedSignal = timeRecover.synchronizeTiming(transmittedSignal)
    demodulatedBits = demodulator.demodulate(transmittedSignal)
    return demodulatedBits
def __transmitSignalWithFrameSynchronizationAndSamplingError(samplingError, offset, snr=None):
    modulator = Modulator(__CARRIER_FREQ, __SYMBOL_LENGTH_IN_BITS, __SAMPLING_RATE)
    demodulator = Demodulator(__CARRIER_FREQ, __SYMBOL_LENGTH_IN_BITS, __SAMPLING_RATE)
    channel = RadioChannel(__SAMPLING_RATE)
    frameSync = FrameSynchronization(modulator.modulate(__START_HEADER), modulator.modulate(__END_HEADER),
                                     __SYMBOL_LENGTH_IN_BITS, __SAMPLING_RATE)
    timeRecover = TimingRecovery(__SYMBOL_LENGTH_IN_BITS)

    modulatedSignal = modulator.modulate(__FRAME)
    transmittedSignal = channel.transmit(modulatedSignal, signalOffset=offset, adcSamplingErr=samplingError, snr=snr)
    dataPosition = frameSync.synchronizeStartHeader(transmittedSignal)
    dataEndPosition = frameSync.synchronizeStopHeader(transmittedSignal)
    transmittedSignal = transmittedSignal[dataPosition:dataEndPosition]
    transmittedSignal = timeRecover.synchronizeTiming(transmittedSignal)

    demodulatedBits = demodulator.demodulate(transmittedSignal)
    return demodulatedBits
def shouldRemoveFirstBitFromOtherSymbol():  #TODO: Camcore fix this shit
    modulator = Modulator(__CARRIER_FREQ, __SYMBOL_LENGTH_IN_BITS, __FI,
                          __SAMPLING_RATE, __NUM_OF_PERIODS_IN_SYMBOL)
    demodulator = Demodulator(__CARRIER_FREQ, __SYMBOL_LENGTH_IN_BITS, __FI,
                              __SAMPLING_RATE, __NUM_OF_PERIODS_IN_SYMBOL)
    timeRecover = TimingRecovery(__BUFFER_SIZE, __SYMBOL_LENGTH_IN_BITS)
    channel = RadioChannel()

    signal = modulator.modulate(__BITS)
    transmittedSignal = channel.transmit(signal, snr=3, signalOffset=2)
    recoveredSignal, rest = timeRecover.recoverTime(transmittedSignal)
    recoveredSignal.extend(rest)
    demodulatedBits = demodulator.demodulate(recoveredSignal)

    properBits = __BITS
    corruptedBits = 0
    for i in range(int(len(demodulatedBits))):
        if demodulatedBits[i] != properBits[i]:
            corruptedBits += 2
            if int(len(properBits)
                   ) <= i + 2 and demodulatedBits[i] == properBits[i + 2]:
                del properBits[i]
                del properBits[i + 1]
class Receiver:
    def __init__(self, sampleRate, carrierFreq, symbolLength, frameSize):
        self.sampleRate = sampleRate
        self.carrierFreq = carrierFreq
        self.symbolLength = symbolLength
        self.frameSizeInBits = frameSize * 8 / 2
        self.headerStart = [
            0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
            1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1,
            1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0
        ]
        self.headerEnd = [
            1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0,
            0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0,
            0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1,
            1, 0, 0, 1, 0, 0, 0, 0, 1, 1
        ]

        self.rtl = RtlSdr()
        self.rtl.sample_rate = self.sampleRate
        self.rtl.center_freq = self.carrierFreq

        self.demodulator = Demodulator(self.carrierFreq, self.symbolLength,
                                       self.sampleRate)
        self.modulator = Modulator(self.carrierFreq, self.symbolLength,
                                   self.sampleRate)
        self.frameSync = FrameSynchronization(
            self.modulator.modulate(self.headerStart),
            self.modulator.modulate(self.headerEnd), self.symbolLength,
            self.sampleRate)
        self.timeRecover = TimingRecovery(self.symbolLength)

        self.rtlSamples = Queue()
        self.frames = Queue()
        self.previousData = Queue(1)
        self.previousData.put([0])
        self.data = Queue()

        self.threads = []
        self.threads.append(threading.Thread(target=self.receive))
        self.threads.append(threading.Thread(target=self.findFrame))
        self.threads.append(threading.Thread(target=self.processData))
        self.threads.append(threading.Thread(target=self.playSound))

        for th in self.threads:
            th.start()

    def receive(self):
        def rtl_callback(samples, rtlsdr_obj):
            self.rtlSamples.put(samples)

        self.rtl.read_samples_async(rtl_callback, self.sampleRate / 10)

    def findFrame(self):
        currentData = self.rtlSamples.get()
        prevData = self.previousData.get()
        data = np.append(prevData, currentData)

        dataPosition = self.frameSync.synchronizeStartHeader(data)
        dataEndPosition = self.frameSync.synchronizeStopHeader(
            data[dataPosition:])

        if abs(dataEndPosition -
               self.frameSizeInBits) <= self.frameSizeInBits / 10:
            print("Frame found. Size = " + str(dataEndPosition))

            self.previousData.put(data[dataPosition + dataEndPosition:])
            self.frames.put(data[dataPosition:dataPosition + dataEndPosition])
        else:
            self.previousData.put(currentData)

    def processData(self):
        data = self.frames.get()

        data = self.frameSync.correctFreqAndPhase(data)
        data = self.timeRecover.synchronizeTiming(data)
        data = self.demodulator.demodulate(data)

        self.data.put(data)

    def playSound(self):
        dataToPlay = self.data.get()
        #TODO: write func
        return
def shouldDemodulateInputBits():
    dem = Demodulator(__CARRIER_FREQ, __SYMBOL_LENGTH_IN_BITS, __FI, __SAMPLE_RATE)
    signal = __calcSignal()
    assert(dem.demodulate(signal) == __OUTPUT_BITS)
Exemplo n.º 11
0
import numpy as np
from QPSK.Demodulator import Demodulator
from QPSK.Modulator import Modulator
from Transmission.Transmitter import Transmitter

__BITS = np.random.randint(2, size=200).tolist()

__CARRIER_FREQ = 2000000000
__NUM_OF_PERIODS_IN_SYMBOL = 2
__SYMBOL_LENGTH_IN_BITS = 32
__FI = 0
__SAMPLING_RATE = 3000000
# __SAMPLING_RATE = __CARRIER_FREQ * __SYMBOL_LENGTH_IN_BITS / __NUM_OF_PERIODS_IN_SYMBOL

modulator = Modulator(__CARRIER_FREQ, __SYMBOL_LENGTH_IN_BITS, __SAMPLING_RATE)
demodulator = Demodulator(__CARRIER_FREQ, __SYMBOL_LENGTH_IN_BITS,
                          __SAMPLING_RATE)
transmitter = Transmitter(__SAMPLING_RATE, __CARRIER_FREQ,
                          __SYMBOL_LENGTH_IN_BITS)

modulatedSig = modulator.modulate(__BITS)
transmitter.transmit(modulatedSig)
demodulatedBits = demodulator.demodulate(modulatedSig)
def __demodulateSignal(signal):
    demodulator = Demodulator(__CARRIER_FREQ, __SYMBOL_LENGTH_IN_BITS, __FI,
                              __SAMPLING_RATE)
    return demodulator.demodulate(signal)