alexusgracia

DSBM Raspberry

Oct 16th, 2020
488
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import RPi.GPIO as GPIO
  2. import time
  3. import numpy as np
  4. GPIO.setmode(GPIO.BOARD)
  5.  
  6. GPIO.setup(5, GPIO.IN)
  7.  
  8.  
  9.  
  10. def mesuresanteriors():
  11.     mesuresanteriors = [191, 195, 193, 190, 128, 127, 127, 126, 127, 126, 126, 127, 125, 125, 126, 125, 126, 125, 125, 125, 126, 125, 125, 112, 125, 125, 125, 125, 125, 125, 124, 124, 124, 125, 112, 125, 122, 124, 124, 124, 125, 123, 123, 124, 124, 125, 124, 124, 124, 124, 124, 124, 124, 123, 124, 124, 124, 112, 123, 124, 123, 122, 124, 123, 124, 123, 123, 123, 124, 123, 124, 123, 123, 123, 123, 123, 123, 123, 124, 123, 112, 123, 122, 124, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 122, 123, 123, 123, 123]
  12.     return mesuresanteriors
  13.  
  14.  
  15. def mostreja(mesures):
  16.     count = 0
  17.     GPIO.setup(5, GPIO.OUT)
  18.     GPIO.output(5, GPIO.LOW)
  19.     time.sleep(0.1)
  20.     GPIO.setup(5, GPIO.IN)
  21.     while(GPIO.input(5) == GPIO.LOW):
  22.         count +=1
  23.     mesures.append(count)
  24.     return
  25. try:
  26.     #Main
  27.     mesura = False
  28.     i = 0
  29.     mesures = []
  30.     if mesura:
  31.         while i<100:
  32.             mostreja(mesures)
  33.             i = i+1
  34.     else:
  35.         mesures = mesuresanteriors()
  36.  
  37.     npmesures = np.array(mesures)
  38.     print ('Median: ', np.median(npmesures))
  39.     print ('Desviacio: ', np.std(npmesures, axis = 0))
  40.     prob = npmesures/npmesures.sum()
  41.     shannon = -np.sum(prob*np.log2(prob))
  42.     print("Després de fer 100 mesures a una variable, l'entropia de Shannon ens mesura:", np.around(shannon))
  43.     print('Rang = Vmax - Vmin: ', 'Rang = 256-0', 'Rang = 256')
  44.     rang = 256
  45.     #print('Dada = 100 x (Vin-Vmin)/rang', 'Dada = ', 100 * (npmesures/rang))
  46.     print('bits = log2(rang): ', np.log2(rang))
  47.     print('bits correctes = log2(rang)-log2(sigma):', np.round(np.log2(rang))-np.log2(np.std(npmesures, axis = 0)))
  48. except KeyboardInterrupt:
  49.     pass
  50. finally:
  51.     GPIO.cleanup()
RAW Paste Data