Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Reads data from SPI.
- # Generates filter (coefficients) and applies them to data (signal)
- #
- # The setup:
- # - Read data through MISO GPIO pin.
- # - Other GPIO pins for SPI are grounded.
- # - A voltage regulator is needed to convert from Arduinos 5V pins to Pi's 3.3V pins
- from scipy import signal
- import numpy as np
- import spidev
- import matplotlib.pyplot as plt
- spi = spidev.SpiDev()
- spi.open(0, 0)
- ADC_data = []
- #t_data = []
- # def tid(data):
- # for i in range(len()):
- # t.append =
- ''' Write ADC data and sampletime to datafile.txt '''
- def datafile_write(input_data, runtime):
- print("Generating output file: datafile.txt")
- print("{0:>15}{1:>10.1f} samples".format("Samples:", len(input_data)))
- print("{0:>15}{1:>10.1f} us".format("Runtime:", runtime))
- interval = runtime / len(input_data)
- times = []
- current_time = 0
- # Generates CSV file with [ADC_data][sampletime]
- with open('datafile.txt', 'w') as datafile:
- for value in input_data:
- datafile.write(str(ADC_data * step))
- datafile.write("\t")
- datafile.write("{0:.3f}".format(current_time))
- datafile.write("\n")
- times.append(current_time)
- current_time += interval
- print("Datafile written")
- def datafile_read():
- values = []
- times = []
- with open('datafile.txt', 'r') as datafile:
- for line in datafile:
- read_data = datafile.readline().split("\t")
- values.append(read_data[0])
- times.append(read_data[1])
- return values, times
- ''' Calculate filter coefficients '''
- def filter_get_coefficients(N, fs, fc, filter_type):
- if filter_type == 'butter':
- b, a = signal.butter(N, Wn=fc / (fs / 2), btype='low')
- else:
- print("Incorrect filter type")
- ''' Apply filter '''
- def datafile_apply_filter_to_data(data, b, a):
- data = np.asarray(data, dtype=np.float64)
- y = signal.lfilter(b, a, data)
- return y
- ''' Graph for filtered signal '''
- def filtered_graph_generate(v, y, t):
- plt.clf()
- v = np.asarray(v, dtype=np.float64)
- plt.plot(t, v, 'b-', linewidth=1)
- plt.plot(t, y, 'g-', linewidth=0.8)
- plt.xlabel('Time [usec]')
- plt.grid()
- #plt.show()
- plt.savefig('filtered_data.png', dpi=500)
- ''' Plot filter response '''
- def filter_plot_response(b, a, fs, fc, title, x_scale):
- plt.clf()
- w, h = signal.freqz(b, a, worN=8000)
- plt.plot(0.5 * fs * w / np.pi, np.abs(h), 'b')
- plt.axvline(fc, color='k')
- plt.xlim(0, x_scale * 0.5 * fs)
- plt.title(title)
- plt.xlabel('Frequency [Hz]')
- plt.grid()
- #plt.show()
- plt.savefig('filter.png', dpi=500)
- def four_type_filter(fs, fc):
- b, a = signal.butter(N=5, Wn=fc / (fs / 2), btype='low')
- filter_plot_response(b, a, fc, fs, title="Butter LP Filter", x_scale=0.2)
- #plt.show()
- plt.clf()
- while true:
- ADC_data.append = spi.xfer2([int(SPIsend)])
- ''' fs = 30000, fc = 10000, order = 7, filter = Butterworth '''
- b, a = filter_get_coefficients(N=7, fs=30000, fc=10000, filter_type='butter')
- filter_plot_response(b, a, 30000, 10000, 'Butterworth LP Filter', 1)
- #print("b: {}".format(b))
- #print("a: {}".format(a))
- datafile_write(ADC_data, 10)
- v, t = datafile_read()
- y = datafile_apply_filter_to_data(v, b, a)
- filtered_graph_generate(v[1000:30000], y[1000:30000], t[100:10000])
- spi.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement