Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. # Reads data from SPI.
  2. # Generates filter (coefficients) and applies them to data (signal)
  3. #
  4. # The setup:
  5. # - Read data through MISO GPIO pin.
  6. # - Other GPIO pins for SPI are grounded.
  7. # - A voltage regulator is needed to convert from Arduinos 5V pins to Pi's 3.3V pins
  8.  
  9.  
  10. from scipy import signal
  11. import numpy as np
  12. import spidev
  13. import matplotlib.pyplot as plt
  14.  
  15.  
  16. spi = spidev.SpiDev()
  17.  
  18. spi.open(0, 0)
  19.  
  20. ADC_data = []
  21.  
  22. #t_data = []
  23.  
  24. # def tid(data):
  25. # for i in range(len()):
  26. # t.append =
  27.  
  28. ''' Write ADC data and sampletime to datafile.txt '''
  29. def datafile_write(input_data, runtime):
  30. print("Generating output file: datafile.txt")
  31. print("{0:>15}{1:>10.1f} samples".format("Samples:", len(input_data)))
  32. print("{0:>15}{1:>10.1f} us".format("Runtime:", runtime))
  33. interval = runtime / len(input_data)
  34. times = []
  35. current_time = 0
  36.  
  37. # Generates CSV file with [ADC_data][sampletime]
  38. with open('datafile.txt', 'w') as datafile:
  39. for value in input_data:
  40. datafile.write(str(ADC_data * step))
  41. datafile.write("\t")
  42. datafile.write("{0:.3f}".format(current_time))
  43. datafile.write("\n")
  44. times.append(current_time)
  45. current_time += interval
  46. print("Datafile written")
  47.  
  48. def datafile_read():
  49. values = []
  50. times = []
  51. with open('datafile.txt', 'r') as datafile:
  52. for line in datafile:
  53. read_data = datafile.readline().split("\t")
  54. values.append(read_data[0])
  55. times.append(read_data[1])
  56. return values, times
  57.  
  58. ''' Calculate filter coefficients '''
  59. def filter_get_coefficients(N, fs, fc, filter_type):
  60. if filter_type == 'butter':
  61. b, a = signal.butter(N, Wn=fc / (fs / 2), btype='low')
  62. else:
  63. print("Incorrect filter type")
  64.  
  65. ''' Apply filter '''
  66. def datafile_apply_filter_to_data(data, b, a):
  67. data = np.asarray(data, dtype=np.float64)
  68. y = signal.lfilter(b, a, data)
  69. return y
  70.  
  71. ''' Graph for filtered signal '''
  72. def filtered_graph_generate(v, y, t):
  73. plt.clf()
  74. v = np.asarray(v, dtype=np.float64)
  75. plt.plot(t, v, 'b-', linewidth=1)
  76. plt.plot(t, y, 'g-', linewidth=0.8)
  77. plt.xlabel('Time [usec]')
  78. plt.grid()
  79. #plt.show()
  80. plt.savefig('filtered_data.png', dpi=500)
  81.  
  82. ''' Plot filter response '''
  83. def filter_plot_response(b, a, fs, fc, title, x_scale):
  84. plt.clf()
  85. w, h = signal.freqz(b, a, worN=8000)
  86. plt.plot(0.5 * fs * w / np.pi, np.abs(h), 'b')
  87. plt.axvline(fc, color='k')
  88. plt.xlim(0, x_scale * 0.5 * fs)
  89. plt.title(title)
  90. plt.xlabel('Frequency [Hz]')
  91. plt.grid()
  92. #plt.show()
  93. plt.savefig('filter.png', dpi=500)
  94.  
  95. def four_type_filter(fs, fc):
  96. b, a = signal.butter(N=5, Wn=fc / (fs / 2), btype='low')
  97. filter_plot_response(b, a, fc, fs, title="Butter LP Filter", x_scale=0.2)
  98. #plt.show()
  99. plt.clf()
  100.  
  101. while true:
  102. ADC_data.append = spi.xfer2([int(SPIsend)])
  103.  
  104. ''' fs = 30000, fc = 10000, order = 7, filter = Butterworth '''
  105. b, a = filter_get_coefficients(N=7, fs=30000, fc=10000, filter_type='butter')
  106.  
  107. filter_plot_response(b, a, 30000, 10000, 'Butterworth LP Filter', 1)
  108. #print("b: {}".format(b))
  109. #print("a: {}".format(a))
  110.  
  111. datafile_write(ADC_data, 10)
  112. v, t = datafile_read()
  113. y = datafile_apply_filter_to_data(v, b, a)
  114. filtered_graph_generate(v[1000:30000], y[1000:30000], t[100:10000])
  115.  
  116. spi.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement