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 __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 __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 __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 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
Exemplo n.º 9
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)
Exemplo n.º 10
0
class Transmitter:
    def __init__(self, sample_rate, carrier_freq, symbol_length):
        self.sampleRate = sample_rate
        self.carrierFreq = carrier_freq
        self.symbolLength = symbol_length
        self.modulator = Modulator(carrier_freq, self.symbolLength,
                                   self.sampleRate)
        self.modulatedStartHeader = self.modulator.modulate(headerStart)
        self.modulatedEndHeader = self.modulator.modulate(headerEnd)

        self.pluto = adi.Pluto()
        # self.pluto.tx_rf_bandwidth
        self.pluto.tx_lo = self.carrierFreq
        self.pluto.tx_enabled_channels = [0, 1]
        self.pluto.sample_rate = self.sampleRate

        self.recordedDataQueue = Queue()
        self.modulatedDataQueue = Queue()
        self.dataToTransmit = Queue()

        self.threads = []
        self.threads.append(
            threading.Thread(target=self.record(), name="micRecorder"))
        self.threads.append(
            threading.Thread(target=self.modulateData(), name="modulateData"))
        self.threads.append(
            threading.Thread(target=self.prepareDataToTransmit(),
                             name="prepareDataToTransmit"))
        self.threads.append(
            threading.Thread(target=self.transmit(), name="transmitter"))

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

    def record(self):
        def callback():
            self.recordedDataQueue.put(0)

        #TODO: writeFunc of record from mic
        data = ""
        self.recordedDataQueue.put(data)
        pass

    def modulateData(self):
        if self.recordedDataQueue.not_empty:
            data = self.recordedDataQueue.get()
            self.recordedDataQueue.mutex.release()
            modulatedData = self.modulator.modulate(data)
            self.modulatedDataQueue.put(modulatedData)

    def prepareDataToTransmit(self):
        if self.modulatedDataQueue.qsize() > 1024:
            data = self.modulatedDataQueue.get()
            preparedData = self.modulatedStartHeader + data + self.modulatedEndHeader
            self.dataToTransmit.put(preparedData)

    def transmit(self, data):
        self.pluto.tx(data)

    def transmit(self):
        if self.dataToTransmit.not_empty:
            data = self.dataToTransmit.get()
            self.pluto.tx_destroy_buffer()
            self.pluto.tx(data)
            self.dataToTransmit.mutex.release()
Exemplo n.º 11
0
__CARRIER_FREQ = 100
__NUM_OF_PERIODS_IN_SYMBOL = 2
__SYMBOL_LENGTH_IN_BITS = 32
__FI = 0
__SAMPLING_RATE = __CARRIER_FREQ * __SYMBOL_LENGTH_IN_BITS / __NUM_OF_PERIODS_IN_SYMBOL


def changeBitsToPolar(bits):
    resultBits = []
    for i in range(0, len(bits)):
        bit = bits[i]
        if bit == 0:
            bit = -1
        resultBits.append(bit)
    return resultBits


recorder = Recorder(chunk, sample_format, channels, fs, seconds, p, stream,
                    filename)
inputBits = recorder.record()

modulator = Modulator(__CARRIER_FREQ, __SYMBOL_LENGTH_IN_BITS, __FI,
                      __NUM_OF_PERIODS_IN_SYMBOL)
demodulator = Demodulator(__CARRIER_FREQ, __SYMBOL_LENGTH_IN_BITS, __FI,
                          __SAMPLING_RATE, __NUM_OF_PERIODS_IN_SYMBOL)

modulatedSig = modulator.modulate(inputBits)

demodulatedBits = demodulator.demodulate(modulatedSig)
sd.play(changeBitsToPolar(demodulatedBits), fs)
def __modulateSignal():
    modulator = Modulator(__CARRIER_FREQ, __SYMBOL_LENGTH_IN_BITS, __FI,
                          __SAMPLING_RATE)
    return modulator.modulate(__BITS)