Blame view
220222_final/GW/python/ttt.py
6.55 KB
35833671e
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
#!/usr/bin/python3 #import os """ Created on Tue Jan 11 2022 @author : ALKADRI Mohammad E-mail : mohammad.alkadri.95@gmail.com """ import threading import collections import time import socket import string import numpy as np from datetime import datetime def thread_client(): print("Server launched") a=1 while (a): sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) sock.connect(('127.0.0.1',4242)) data= sock.recv(1) data=data.decode() while (a): if 'q' in data: global arret print("Bye") time.sleep(1.6) arret=0 sock.close() a=0 print("Server done") print(datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')) def Data_RTL(): client = socket.socket(socket.AF_INET,socket.SOCK_STREAM) ip=socket.gethostbyname("127.0.0.1") port=1234 address=(ip,port) client.connect(address) global arret N=0 dd = collections.deque(maxlen=9770) while (arret): dd.append(client.recv(1024)) N+=1024 a=0 b=[]#np.zeros(10000000) i=0 for i in range(len(dd)): b[a:a+1023]=dd[i] a=a+1024 if ((len(b)%2)==1): b=b[0:-1] x=np.array(b) print(len(x)) #x.tofile("xxxxx.txt") x=x[0::2]+1j*x[1::2] x=x-127-127j; client.close() return x def PPS(x): # identification des fronts montants des 1-PPS sur gateway pps1=(np.absolute(x)>seuilpps).nonzero() dpps=np.diff(pps1,n=1,axis=1).reshape(-1) k=np.array((dpps>50).nonzero()).reshape(-1) ; # (fs*0.1)); % elimine les communications LoRa pps1=np.array(pps1).flatten() pps=np.array(pps1)[k.astype(int)]; """ k=(np.diff(pps,n=1,axis=0)>(fs*0.4)).nonzero() # elimine les fronts descendants print(len(k)) print(k) k=np.array(k).reshape(-1) if k[0]==0: k=k[1:]; # cas ou` acquisition commence dans un PPS haut """ print("pps = ",np.diff(pps,n=1,axis=0)) k=(np.diff(pps,n=1,axis=0)==300000).nonzero() k=np.array(k).reshape(-1) try: ppsgw=pps[k]; # indice des 1-PPS sur gw except: ppsgw=[1] if (ppsgw.size==0): ppsgw=[1] print("ppsgw = ", ppsgw) return ppsgw def Chirp_detect(x,t): cc=[] ddd=x[int((t+1)*1e6):int((t+2)*1e6)] cc=np.fft.fft(ddd) cc=np.array(cc) sssr=[] sssd=[] ccris=np.fromfile('ccris.bin',dtype=np.complex128) ccdec=np.fromfile('ccdec.bin',dtype=np.complex128) N=0#int((t-1)*1e6) sssr[N:N+len(ccris)-1]=cc[N:N+len(ccris)]*ccris sssr=np.array(sssr) chirp_detect_ris=np.fft.ifft(sssr) sssd[N:N+len(ccdec)-1]=cc[N:N+len(ccdec)]*ccdec sssd=np.array(sssd) chirp_detect_dec=np.fft.ifft(sssd) chirp_detect=np.absolute(chirp_detect_ris)+np.absolute(chirp_detect_dec); """ if (t==1): chirp_detect.tofile("ch1.txt") cc.tofile("c1.txt") if (t==2): chirp_detect.tofile("ch2.txt") cc.tofile("c2.txt") """ seuil=0.65*max(chirp_detect); print(seuil) k=np.array((chirp_detect>seuil).nonzero()).reshape(-1); try: debut=int(k[0]-int(d*fs)+(t-1)*1e6+2000000) # fin_gw1=k(end); % /!\ ATTENTION RISQUE D'AVOIR LES DEUX COMM except: debut=int((t-1)*1e6+2000000) # pour éviter l'erreur, k[0]=0 print(debut) return debut def Time(x,debut,ppsgw,t): global indice_pps xx=x[debut:debut+int(d*fs*longueur_chirps)]; # transmission gateway xx=np.fft.fft(xx) if t == 1: ccy=np.fromfile('ccrisy.bin',dtype=np.complex128) print("ccy1") else: ccy=np.fromfile('ccdecy.bin',dtype=np.complex128) print("ccy2") y=np.absolute(np.fft.ifft(xx * ccy)); pos1=np.array((y>(max(y)/2)).nonzero()).reshape(-1); #pos2=np.array(pos1>pos1[0]-200+6*1536) #pos22=pos2[0] try: pos11=pos1[0]; # premier chirp except: pos11 =1 yy=y[pos11:pos11+int(d*fs)] v=np.argmax(yy) print("1111111111111") print("pos11 ", pos11) print("np.max(y) ",np.max(y)) print("np.max(yy) ",np.max(yy)) print("v ",v) print("yy[v] ",yy[v]) if v !=1529 : v =v + 1/2*((yy[v-1]-yy[v+1])/(yy[v-1]+yy[v+1]-2*yy[v])) print("v ",v) t1=pos11+v print("t1 ", t1) debut=pos11+v-1-1+debut print(debut) pos22=pos11+6*1536 yy=y[pos22:pos22+int(d*fs)] v=np.argmax(yy) print("2222222222222") print("pos22 ", pos22) print("np.max(y) ",np.max(y)) print("np.max(yy) ",np.max(yy)) print("v ",v) print("yy[v] ",yy[v]) if v !=1529 : v =v + 1/2*((yy[v-1]-yy[v+1])/(yy[v-1]+yy[v+1]-2*yy[v])) print("v ",v) t2=pos22+v print("t2 ", t2) tg= t2-t1 print("tg ", tg) if t==1: if (ppsgw[0]<=debut): indice_pps=ppsgw[(np.array((ppsgw<debut).nonzero()).reshape(-1))[-1]] # indice_pps=ppsgw(find(ppsgw<debut_gw1)(end)); else: indice_pps=0 debut=debut-indice_pps # /!\ ecart premiere transmission return tg, debut, indice_pps def TGW(): x=Data_RTL() pps=PPS(x) debut1 = Chirp_detect(x,1) print(debut1) tg1, debut1, indice_pps= Time(x,debut1,pps,1) x[0:int(debut1+indice_pps+d*fs*800)]=0 print(pps) print(len(x)) print(debut1) debut2 = Chirp_detect(x,2) print(debut2) tg2, debut2, indice_pps= Time(x,debut2,pps,2) print(debut2) #Tgw = debut2-debut1 Tgw =[debut1,debut2-debut1] Tgw=np.array(Tgw) return Tgw def Send(Tgw): print("Server lanched") sock_T=socket.socket(socket.AF_INET,socket.SOCK_STREAM) sock_T.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) sock_T.bind(('127.0.0.1',2222)) sock_T.listen(1) conn,addr=sock_T.accept() with conn: print("connected from", addr) conn.send(Tgw) sock_T.close() while (1): fs=1.5e6 seuilpps=0.08*127*2; B=125e3; d=1/980; freq_int=1e4; longueur_chirps=8; threading.Thread(target=thread_client, args=()).start() arret = 1 Tgw = TGW() print("Tgw = ", Tgw) Send(Tgw) """ print("debut_gw1 =",debut_gw1) print("debut_gw1_vs_1pps = ", debut_gw1_vs_1pps) """ ############### FIN DE LA PREMIERE MESURE ################# """ debut_gw2_vs_1pps=debut_gw2-indice_pps # /!\ ecart deuxième transmission print("debut_gw2_vs_1pps = ", debut_gw2_vs_1pps) Tgw= debut_gw2_vs_1pps - debut_gw1_vs_1pps; print("fin du cycle") print(datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')) """ |