Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.71 KB | None | 0 0
  1. def czasProbkowania2(t,f):
  2. #funkcja do probkowania czasu
  3. #input:
  4. #t - czas, przez ktory probkujemy
  5. #f - czestotliwoac probkowania
  6. # dla t = 1 i f = 10 stworzy nam wartosci od 0 do 1 z krokiem co 0.1
  7. #output lista z momentami w ktorych probkujemy
  8.     iloscProbek = int(t / (1 / f))
  9.     time = []
  10.     time.append(0)
  11.     for x in range(iloscProbek):
  12.         time.append((1 / f) * (x + 1))
  13.     return time
  14.  
  15. def word_to_ascii(word):
  16.     """
  17.    :param word: word in ascii code numbered from 32 to 127
  18.    :return: string with concatenate binary values
  19.    """
  20.     txt = ''
  21.     for c in word:
  22.         txt += str(bin(ord(c))[2:].zfill(8))
  23.     return txt
  24.  
  25. def kluczowanie_amplitudy(binary, T, fn, N):
  26.     """
  27.    :param binary: string value with binary 8bits values
  28.    :param T: time list with subsequent frequencies
  29.    :param fn: frequency
  30.    :return: modulated signal list with elements
  31.    """
  32.     # parametry A1 i A2 przy czym A1 != A2 określają wielkość amplitudy
  33.     A1 = 5
  34.     A2 = 10
  35.     result = []
  36.     # dla każdej wartości z czasu
  37.     for t in T:
  38.         #print(t)
  39.         index = int(T.index(t)/N)
  40.         #print("index " +str(index))
  41.         if(int(binary[index]) == 0):
  42.             result.append(A1 * mt.sin(2*mt.pi * fn * index))
  43.         elif(int(binary[index]) == 1):
  44.             result.append(A2 * mt.sin(2*mt.pi * fn * index))
  45.  
  46.     return result
  47.  
  48.  
  49. def kluczowanie_czestotliwosci(binary, T, N, Tb):
  50.     """
  51.  
  52.    :param binary: string value with binary 8bits values
  53.    :param T: time list with subsequent frequencies
  54.    :param N: wielokrotność Tb
  55.    :param Tb: single bit processing time
  56.    :return: modulated signal list with elements
  57.    """
  58.  
  59.     fn1 = (N+1)/Tb
  60.     fn2 = (N+2)/Tb
  61.     result = []
  62.     # dla każdej wartości z ciagu znakowego
  63.     for t in T:
  64.         index = int(T.index(t) / N)
  65.         if (int(binary[index]) == 0):
  66.             result.append(mt.sin(2*mt.pi * fn1 * index))
  67.         elif (int(binary[index]) == 1):
  68.             result.append(mt.sin(2*mt.pi * fn2 * index))
  69.  
  70.     return result
  71.  
  72.  
  73. def kluczowanie_fazy(binary, T, fn):
  74.     """
  75.  
  76.    :param binary: string value with binary 8bits values
  77.    :param T: time list with subsequent frequencies
  78.    :param fn: frequency
  79.    :return: modulated signal list with elements
  80.    """
  81.     result = []
  82.     # dla każdej wartości z ciagu znakowego
  83.     for t in T:
  84.         index = int(T.index(t) / N)
  85.         if (int(binary[index]) == 0):
  86.             result.append(mt.sin(2*mt.pi * fn))
  87.         elif (int(binary[index]) == 1):
  88.             result.append(mt.sin(2*mt.pi * fn + mt.pi))
  89.  
  90.     return result
  91.  
  92. # string
  93. s = "KUBAW"
  94. # czas trwania sygnału w sekundach
  95. czas = 8
  96. # ilość bitów przetwarzanych na sekunde
  97. ilosc_bitow = len(s)*8
  98. # czas pojedynczego bitu
  99. Tb = czas/(ilosc_bitow)
  100. print("Tb " + str(Tb))
  101. # wielokrotność odwrotności Tb
  102. N=2
  103. # czestotliwosc
  104. fn = (N)/mt.pow(Tb, -1)
  105. print("fn " + str(fn))
  106.  
  107. binary = word_to_ascii(s)
  108. print(binary)
  109.  
  110. # mnożniki do generowania sygnału czasowego
  111. factor1 = 10000
  112. factor2 = 0.0001
  113. # zmienne tymczasowe
  114. tt = int(czas*factor1 + fn*factor1)
  115. diff = int(fn*factor1)
  116. # generowanie czasu
  117. T = [x*factor2 for x in range(0, tt, diff)]
  118. print(T)
  119.  
  120. # ------------- obliczanie sygnałów zmodulowanych
  121. klucz_1 = kluczowanie_amplitudy(binary, T, fn, N)
  122. plt.plot(T, klucz_1)
  123. plt.show()
  124.  
  125.  
  126. # dla pojedynczego czasu Tb
  127. TT = czasProbkowania(Tb, 20)
  128. TT.pop()
  129. print(len(TT))
  130. A1 = 2
  131. A2 = 4
  132. result = []
  133. fn = 20
  134. for t in TT:
  135.     if (int(binary[0]) == 0):
  136.         result.append(A1 * mt.sin(2 * mt.pi * fn * t))
  137.     elif (int(binary[0]) == 1):
  138.         result.append(A2 * mt.sin(2 * mt.pi * fn * t))
  139.  
  140. print(TT)
  141. print(result)
  142. plt.plot(TT, result)
  143. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement