├── images ├── readme.txt ├── all.png ├── alpha.bmp ├── pins.bmp ├── pins2.bmp ├── 1alpha.bmp ├── 1chewing.bmp ├── pieeg.jpeg ├── youtube.jpg ├── Connection.bmp ├── Connection.jpeg ├── Collected_dataset.jpg └── 2.Graph_Gpio_D _1_5_4_spike.py ├── GUI ├── arxive │ ├── readme.txt │ └── 1.Send.py ├── readme.txt ├── 1.Graph_Gpio_D _1_6_3.py ├── 2.Graph_Gpio_D _1_5_4.py ├── 2.Graph_Gpio_D _1_5_4_OS.py └── Graph_Gpio_D _1_5_4_not_spike.py ├── Save_data ├── arxive │ ├── readme.txt │ ├── 1.Save_Data.py │ ├── pieeg16_os_save_data_1.py │ └── pieeg16_os_save_data_2.py ├── dataset_visualisation │ ├── readme.txt │ ├── 1.Data_Vis_Graph_sep.py │ ├── 2.Data_Vis_Graph_All_in_one.py │ └── 3. Vis_Alpha_Wavelet.py ├── Readme.txt ├── command_line │ ├── pieeg_to_csv.py │ └── utils.py ├── Save_data_OS.py └── 1.Save_Data.py ├── PiEEG_quick_start.pdf ├── Dataset ├── 3.Alpha_test.xlsx ├── 1.Chewing_Blinking.xlsx ├── 2.Chewing_Blinking.xlsx └── Readme.txt ├── ML_Application ├── 3.ML.py ├── Readme.MD ├── 2.Convert_to_Power_Alpha.py └── 1.Save_Data.py ├── license.txt └── README.md /images/readme.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /GUI/arxive/readme.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Save_data/arxive/readme.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Save_data/dataset_visualisation/readme.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /images/all.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pieeg-club/PiEEG-16/HEAD/images/all.png -------------------------------------------------------------------------------- /images/alpha.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pieeg-club/PiEEG-16/HEAD/images/alpha.bmp -------------------------------------------------------------------------------- /images/pins.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pieeg-club/PiEEG-16/HEAD/images/pins.bmp -------------------------------------------------------------------------------- /images/pins2.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pieeg-club/PiEEG-16/HEAD/images/pins2.bmp -------------------------------------------------------------------------------- /images/1alpha.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pieeg-club/PiEEG-16/HEAD/images/1alpha.bmp -------------------------------------------------------------------------------- /images/1chewing.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pieeg-club/PiEEG-16/HEAD/images/1chewing.bmp -------------------------------------------------------------------------------- /images/pieeg.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pieeg-club/PiEEG-16/HEAD/images/pieeg.jpeg -------------------------------------------------------------------------------- /images/youtube.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pieeg-club/PiEEG-16/HEAD/images/youtube.jpg -------------------------------------------------------------------------------- /PiEEG_quick_start.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pieeg-club/PiEEG-16/HEAD/PiEEG_quick_start.pdf -------------------------------------------------------------------------------- /images/Connection.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pieeg-club/PiEEG-16/HEAD/images/Connection.bmp -------------------------------------------------------------------------------- /images/Connection.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pieeg-club/PiEEG-16/HEAD/images/Connection.jpeg -------------------------------------------------------------------------------- /Dataset/3.Alpha_test.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pieeg-club/PiEEG-16/HEAD/Dataset/3.Alpha_test.xlsx -------------------------------------------------------------------------------- /images/Collected_dataset.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pieeg-club/PiEEG-16/HEAD/images/Collected_dataset.jpg -------------------------------------------------------------------------------- /Dataset/1.Chewing_Blinking.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pieeg-club/PiEEG-16/HEAD/Dataset/1.Chewing_Blinking.xlsx -------------------------------------------------------------------------------- /Dataset/2.Chewing_Blinking.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pieeg-club/PiEEG-16/HEAD/Dataset/2.Chewing_Blinking.xlsx -------------------------------------------------------------------------------- /GUI/readme.txt: -------------------------------------------------------------------------------- 1 | For Python 3.11.2 2 | 3 | pip 4 | gpiod 1.5.4 // for - 2.Graph_Gpio_D _1_5_4.py sudo pip3 install gpiod==1.5.4 5 | // or gpiod 1.6.3 for 1.Graph_Gpio_D _1_6_3.py 6 | matplotlib 3.9.2 7 | scipy 1.14.1 8 | spicy 0.16.0 9 | spidev 3.6 10 | -------------------------------------------------------------------------------- /Dataset/Readme.txt: -------------------------------------------------------------------------------- 1 | 2 | Dry Electrodes Ag/AgCl, without Gel 3 | Raw data, 250 samples per second 4 | 5 | Chewing and Blinking 4-3-2-1 times chewing and 4-3-2-1 times blinking (1.Chewing_Blinking.xlsx and 2.Chewing_Blinking.xlsx) 6 | 7 | Alpha data set - periodically eyes closed for 5 sec and open 5 sec (3.Alpha_test.xlsx) 8 | 9 | -------------------------------------------------------------------------------- /Save_data/Readme.txt: -------------------------------------------------------------------------------- 1 | 1.Save_Data.py - To save EEG data to an Excel file in real-time with Graph 2 | Save_data_OS.py is alternative if you have problem with gpiod library 3 | Visualization, not real-time 4 | 5 | 1.Data_Vis_Graph_sep.py - EEG graphs 6 | 2.Data_Vis_Graph_All_in_one.py - EEG graphs 7 | 3.Alpha_Wavelet.py - to see alpha rhythm test 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Save_data/dataset_visualisation/1.Data_Vis_Graph_sep.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | import matplotlib.pyplot as plt 3 | from scipy.signal import butter, filtfilt 4 | 5 | # Load the CSV file 6 | file_path = 'C:/Users/1.Chewing_Blinking.xlsx' # Update with your actual file path 7 | print("ok") 8 | data = pd.read_excel(file_path) 9 | 10 | print (data) 11 | # Define the band-pass filter 12 | def butter_bandpass(lowcut, highcut, fs, order=5): 13 | nyquist = 0.5 * fs 14 | low = lowcut / nyquist 15 | high = highcut / nyquist 16 | b, a = butter(order, [low, high], btype='band') 17 | return b, a 18 | 19 | def bandpass_filter(data, lowcut, highcut, fs, order=5): 20 | b, a = butter_bandpass(lowcut, highcut, fs, order=order) 21 | y = filtfilt(b, a, data) 22 | return y 23 | 24 | # Parameters for the band-pass filter 25 | lowcut = 8 # Low cutoff frequency (in Hz) 26 | highcut = 12 # High cutoff frequency (in Hz) 27 | fs = 250.0 # Sampling frequency (in Hz) 28 | 29 | # Apply the filter to each channel 30 | filtered_data = data.copy() 31 | for column in data.columns: 32 | filtered_data[column] = bandpass_filter(data[column], lowcut, highcut, fs) 33 | plt.figure(figsize=(15, 8)) 34 | 35 | 36 | plt.plot(data["data_1ch_test"], label=f'Original {column}', alpha=0.5) # replace the Ch 37 | plt.plot(filtered_data["data_1ch_test"], label=f'Original {column}', alpha=0.5) 38 | 39 | 40 | plt.title('Original') 41 | plt.xlabel('Sample Index') 42 | plt.ylabel('Amplitude') 43 | plt.legend() 44 | plt.show() 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /Save_data/dataset_visualisation/2.Data_Vis_Graph_All_in_one.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | import matplotlib.pyplot as plt 3 | from scipy.signal import butter, filtfilt 4 | 5 | # Load the CSV file 6 | file_path = 'C:/Users/3.Alpha_test.xlsx' # Update with your actual file path 7 | data = pd.read_excel(file_path) 8 | 9 | # Define the band-pass filter 10 | def butter_bandpass(lowcut, highcut, fs, order=5): 11 | nyquist = 0.5 * fs 12 | low = lowcut / nyquist 13 | high = highcut / nyquist 14 | b, a = butter(order, [low, high], btype='band') 15 | return b, a 16 | 17 | def bandpass_filter(data, lowcut, highcut, fs, order=5): 18 | b, a = butter_bandpass(lowcut, highcut, fs, order=order) 19 | y = filtfilt(b, a, data) 20 | return y 21 | 22 | # Parameters for the band-pass filter 23 | lowcut = 8 # Low cutoff frequency (in Hz) 24 | highcut = 12.0 # High cutoff frequency (in Hz) 25 | fs = 250.0 # Sampling frequency (in Hz) 26 | 27 | # Apply the filter to each channel 28 | filtered_data = data.copy() 29 | for column in data.columns: 30 | filtered_data[column] = bandpass_filter(data[column], lowcut, highcut, fs) 31 | 32 | # Plot the original and filtered data for each channel in a 4x4 grid 33 | fig, axs = plt.subplots(4, 4, figsize=(20, 15)) 34 | axs = axs.flatten() # Flatten the 2D array of axes to iterate easily 35 | 36 | for i, column in enumerate(data.columns): 37 | #axs[i].plot(data[column], label=f'Original {column}', alpha=0.5) 38 | axs[i].plot(filtered_data[column], label=f'Filtered {column}', linestyle='--') 39 | axs[i].set_title(f'Channel {column}') 40 | axs[i].set_xlabel('Sample Index') 41 | axs[i].set_ylabel('Amplitude') 42 | axs[i].legend() 43 | 44 | # Adjust layout to prevent overlap 45 | plt.tight_layout() 46 | plt.show() 47 | -------------------------------------------------------------------------------- /ML_Application/3.ML.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | import numpy as np 3 | from sklearn.model_selection import GroupKFold, cross_val_score 4 | from sklearn.preprocessing import StandardScaler 5 | from sklearn.ensemble import RandomForestClassifier 6 | from sklearn.impute import SimpleImputer 7 | from sklearn.metrics import accuracy_score 8 | 9 | # Load dataset 10 | file_path = "collected_alpha_power_results.csv" 11 | data = pd.read_csv(file_path) 12 | 13 | # Ensure "Dataset No." is treated as a string 14 | data["Dataset No."] = data["Dataset No."].astype(str) 15 | 16 | # Handle missing values (fill with column mean) 17 | imputer = SimpleImputer(strategy="mean") 18 | X = data.iloc[:, :-2] # Select only EEG features (excluding 'Stress' and 'Dataset No.') 19 | X = imputer.fit_transform(X) 20 | 21 | # Extract labels and groups 22 | y = data["Stress"] 23 | groups = data["Dataset No."] 24 | 25 | # Normalize EEG features for better performance 26 | scaler = StandardScaler() 27 | X_scaled = scaler.fit_transform(X) 28 | 29 | # Split data into train/test sets while keeping subjects together 30 | group_kfold = GroupKFold(n_splits=5) 31 | train_idx, test_idx = next(group_kfold.split(X_scaled, y, groups)) 32 | 33 | print (train_idx) 34 | print (test_idx) 35 | 36 | X_train, X_test = X_scaled[train_idx], X_scaled[test_idx] 37 | y_train, y_test = y.iloc[train_idx], y.iloc[test_idx] 38 | 39 | 40 | 41 | 42 | # Train Random Forest model 43 | clf = RandomForestClassifier(n_estimators=100, random_state=42) 44 | clf.fit(X_train, y_train) 45 | 46 | # Predict on test set 47 | y_pred = clf.predict(X_test) 48 | accuracy = accuracy_score(y_test, y_pred) 49 | print(f"Test Accuracy: {accuracy:.2f}") 50 | 51 | # Cross-validation 52 | cv_scores = cross_val_score(clf, X_scaled, y, cv=group_kfold, groups=groups, scoring="accuracy") 53 | print(f"Cross-Validation Accuracy: {np.mean(cv_scores):.2f} ± {np.std(cv_scores):.2f}") 54 | 55 | -------------------------------------------------------------------------------- /ML_Application/Readme.MD: -------------------------------------------------------------------------------- 1 | # How to Use Machine Learning to Stress Control via EEG 2 | 3 | If you want to create an EEG-based classifier for machine learning, here’s a short guide: 4 | 5 | #### 1. Collect EEG Datasets: 6 | 7 | Use Script [1.Save_Data.py](https://github.com/pieeg-club/PiEEG-16/blob/main/ML_Application/1.Save_Data.py) to collect raw EEG data. 8 | 9 | 10 | 11 | 12 | 13 | Data has the next Format in the Excel File (example of dataset) 14 | 15 |
16 |
17 |
17 |
18 |
32 |
33 |
39 |
40 |
47 |
48 |
52 |
53 |
59 |
60 |
64 |
65 |
69 |
70 |
78 |
79 |
80 |
81 |
82 |
83 | #### Citation
84 | https://pieeg.com/news/pieeg-16-is-availabe-in-the-market/
85 | arxive paper https://arxiv.org/abs/2409.07491
86 | Cite: Rakhmatulin, I. (2024). PiEEG-16 to Measure 16 EEG Channels with Raspberry Pi for Brain-Computer Interfaces and EEG devices. arXiv:2409.07491
87 |
88 | #### Contacts
89 | Full support in the [Forum](https://pieeg.com/forum-pieeg-low-cost-brain-computer-interface/)
90 |
91 | [LinkedIn](https://www.linkedin.com/company/96475004/admin/feed/posts/)
92 |
--------------------------------------------------------------------------------
/Save_data/command_line/utils.py:
--------------------------------------------------------------------------------
1 |
2 | # imports
3 | import spidev
4 | from RPi import GPIO
5 | GPIO.setwarnings(False)
6 | GPIO.setmode(GPIO.BOARD)
7 | import gpiod
8 |
9 |
10 | def get_voltage(output, a, data_check=0xFFFFFF, data_test=0x7FFFFF):
11 | voltage=(output[a]<<8) | output[a+1]
12 | voltage=(voltage<<8) | output[a+2]
13 | convert_voltage = voltage | data_test
14 | if convert_voltage==data_check:
15 | voltage = (voltage - 16777214)
16 | voltage = round(1000000*4.5*(voltage/16777215),2)
17 |
18 | return voltage
19 |
20 |
21 | def setup_pieeg16(gain=1):
22 | # Convert gain to bits
23 | gain = convert_gain(gain)
24 |
25 | # GPIO settings
26 | chip = gpiod.Chip("gpiochip4")
27 |
28 | cs_line = chip.get_line(19) # GPIO19
29 | cs_line.request(consumer="SPI_CS", type=gpiod.LINE_REQ_DIR_OUT)
30 | cs_line.set_value(1) # Set CS high initially
31 |
32 | # Initialize spidev
33 | spi_1 = spidev.SpiDev()
34 | spi_1.open(0,0)
35 | spi_1.max_speed_hz = 4000000
36 | spi_1.lsbfirst=False
37 | spi_1.mode=0b01
38 | spi_1.bits_per_word = 8
39 |
40 | spi_2 = spidev.SpiDev()
41 | spi_2.open(0,1)
42 | spi_2.max_speed_hz=4000000
43 | spi_2.lsbfirst=False
44 | spi_2.mode=0b01
45 | spi_2.bits_per_word = 8
46 |
47 | # Register Commands
48 | who_i_am=0x00
49 | config1=0x01
50 | config2=0X02
51 | config3=0X03
52 |
53 | reset=0x06
54 | stop=0x0A
55 | start=0x08
56 | sdatac=0x11
57 | rdatac=0x10
58 | wakeup=0x02
59 | rdata = 0x12
60 |
61 | ch1set=0x05
62 | ch2set=0x06
63 | ch3set=0x07
64 | ch4set=0x08
65 | ch5set=0x09
66 | ch6set=0x0A
67 | ch7set=0x0B
68 | ch8set=0x0C
69 |
70 | # device initialization and configuration - first 8 channels
71 | send_command (spi_1, wakeup)
72 | send_command (spi_1, stop)
73 | send_command (spi_1, reset)
74 | send_command (spi_1, sdatac)
75 |
76 | write_byte (spi_1, 0x14, 0x80) #GPIO 80
77 | write_byte (spi_1, config1, 0x96)
78 | write_byte (spi_1, config2, 0xD4)
79 | write_byte (spi_1, config3, 0xFF)
80 | write_byte (spi_1, 0x04, 0x00)
81 | write_byte (spi_1, 0x0D, 0x00)
82 | write_byte (spi_1, 0x0E, 0x00)
83 | write_byte (spi_1, 0x0F, 0x00)
84 | write_byte (spi_1, 0x10, 0x00)
85 | write_byte (spi_1, 0x11, 0x00)
86 | write_byte (spi_1, 0x15, 0x20)
87 |
88 | write_byte (spi_1, 0x17, 0x00)
89 | write_byte (spi_1, ch1set, gain)
90 | write_byte (spi_1, ch2set, gain)
91 | write_byte (spi_1, ch3set, gain)
92 | write_byte (spi_1, ch4set, gain)
93 | write_byte (spi_1, ch5set, gain)
94 | write_byte (spi_1, ch6set, gain)
95 | write_byte (spi_1, ch7set, gain)
96 | write_byte (spi_1, ch8set, gain)
97 |
98 | send_command (spi_1, rdatac)
99 | send_command (spi_1, start)
100 |
101 | # device initialization and configuration - last 8 channels
102 | send_command_2 (spi_2, cs_line, wakeup)
103 | send_command_2 (spi_2, cs_line, stop)
104 | send_command_2 (spi_2, cs_line, reset)
105 | send_command_2 (spi_2, cs_line, sdatac)
106 |
107 | write_byte_2 (spi_2, cs_line, 0x14, 0x80) #GPIO 80
108 | write_byte_2 (spi_2, cs_line, config1, 0x96)
109 | write_byte_2 (spi_2, cs_line, config2, 0xD4)
110 | write_byte_2 (spi_2, cs_line, config3, 0xFF)
111 | write_byte_2 (spi_2, cs_line, 0x04, 0x00)
112 | write_byte_2 (spi_2, cs_line, 0x0D, 0x00)
113 | write_byte_2 (spi_2, cs_line, 0x0E, 0x00)
114 | write_byte_2 (spi_2, cs_line, 0x0F, 0x00)
115 | write_byte_2 (spi_2, cs_line, 0x10, 0x00)
116 | write_byte_2 (spi_2, cs_line, 0x11, 0x00)
117 | write_byte_2 (spi_2, cs_line, 0x15, 0x20)
118 |
119 | write_byte_2 (spi_2, cs_line, 0x17, 0x00)
120 | write_byte_2 (spi_2, cs_line, ch1set, gain)
121 | write_byte_2 (spi_2, cs_line, ch2set, gain)
122 | write_byte_2 (spi_2, cs_line, ch3set, gain)
123 | write_byte_2 (spi_2, cs_line, ch4set, gain)
124 | write_byte_2 (spi_2, cs_line, ch5set, gain)
125 | write_byte_2 (spi_2, cs_line, ch6set, gain)
126 | write_byte_2 (spi_2, cs_line, ch7set, gain)
127 | write_byte_2 (spi_2, cs_line, ch8set, gain)
128 |
129 | send_command_2 (spi_2, cs_line, rdatac)
130 | send_command_2 (spi_2, cs_line, start)
131 |
132 | return spi_1, spi_2, cs_line
133 |
134 |
135 | # SPI Read/Write Functions
136 | def read_byte(spi, register):
137 | write=0x20
138 | register_write=write|register
139 | data = [register_write,0x00,register]
140 | spi.xfer(data)
141 |
142 |
143 | def send_command(spi, command):
144 | send_data = [command]
145 | spi.xfer(send_data)
146 |
147 |
148 | def write_byte(spi, register,data):
149 | write=0x40
150 | register_write=write|register
151 | data = [register_write,0x00,data]
152 | spi.xfer(data)
153 |
154 |
155 | def read_byte_2(spi_2, cs_line, register):
156 | write=0x20
157 | register_write=write|register
158 | data = [register_write,0x00,register]
159 | cs_line.set_value(0)
160 | spi_2.xfer(data)
161 | cs_line.set_value(1)
162 |
163 |
164 | def send_command_2(spi_2, cs_line, command):
165 | send_data = [command]
166 | cs_line.set_value(0)
167 | spi_2.xfer(send_data)
168 | cs_line.set_value(1)
169 |
170 |
171 | def write_byte_2(spi_2, cs_line, register, data):
172 | write=0x40
173 | register_write=write|register
174 | data = [register_write,0x00,data]
175 | cs_line.set_value(0)
176 | spi_2.xfer(data)
177 | cs_line.set_value(1)
178 |
179 |
180 | def convert_gain(value):
181 | # convert to bits
182 | if value == 1:
183 | return 0b000
184 | elif value == 2:
185 | return 0b001
186 | elif value == 4:
187 | return 0b010
188 | elif value == 6:
189 | return 0b011
190 | elif value == 8:
191 | return 0b100
192 | elif value == 12:
193 | return 0b101
194 | elif value == 24:
195 | return 0b110
196 | else:
197 | raise ValueError("Invalid gain value. Please use 1, 2, 4, 6, 8, 12, or 24.")
198 |
--------------------------------------------------------------------------------
/Save_data/Save_data_OS.py:
--------------------------------------------------------------------------------
1 | import spidev
2 | import time
3 | import gpiod
4 | import pandas as pd
5 | import sys
6 |
7 | button_pin_1 = 26 #13
8 | button_pin_2 = 13
9 | cs_pin = 19
10 | chip = gpiod.chip("gpiochip4")
11 | #chip = gpiod.chip("0")
12 | #cs_line = chip.get_line(19) # GPIO19
13 | cs_line = chip.get_line(cs_pin)
14 | cs_line_out = gpiod.line_request()
15 | #cs_line.request(consumer="SPI_CS", type=gpiod.LINE_REQ_DIR_OUT)
16 | cs_line_out.consumer = "SPI_CS"
17 | cs_line_out.request_type = gpiod.line_request.DIRECTION_OUTPUT
18 | cs_line.request(cs_line_out)
19 |
20 | cs_line.set_value(1) # Set CS high initially
21 |
22 | #button_line_1 = chip.get_line(button_pin_1)
23 | #button_line_1.request(consumer = "Button", type = gpiod.LINE_REQ_DIR_IN)
24 |
25 | line_1 = chip.get_line(button_pin_1)
26 |
27 | button_line_1 = gpiod.line_request()
28 | button_line_1.consumer = "Button"
29 | button_line_1.request_type = gpiod.line_request.DIRECTION_INPUT
30 | line_1.request(button_line_1)
31 |
32 | #button_line_2 = chip.get_line(button_pin_2)
33 | #button_line_2.request(consumer = "Button", type = gpiod.LINE_REQ_DIR_IN)
34 |
35 | spi = spidev.SpiDev()
36 |
37 | spi.open(0,0)
38 | spi.max_speed_hz = 1000000#600000
39 | spi.lsbfirst=False
40 | spi.mode=0b01
41 | spi.bits_per_word = 8
42 |
43 | spi_2 = spidev.SpiDev()
44 |
45 | spi_2.open(0,1)
46 | spi_2.max_speed_hz=1000000#600000
47 | spi_2.lsbfirst=False
48 | spi_2.mode=0b01
49 | spi_2.bits_per_word = 8
50 |
51 | who_i_am=0x00
52 | config1=0x01
53 | config2=0X02
54 | config3=0X03
55 |
56 | reset=0x06
57 | stop=0x0A
58 | start=0x08
59 | sdatac=0x11
60 | rdatac=0x10
61 | wakeup=0x02
62 | rdata = 0x12
63 |
64 | ch1set=0x05
65 | ch2set=0x06
66 | ch3set=0x07
67 | ch4set=0x08
68 | ch5set=0x09
69 | ch6set=0x0A
70 | ch7set=0x0B
71 | ch8set=0x0C
72 |
73 | data_test= 0x7FFFFF
74 | data_check=0xFFFFFF
75 |
76 | def read_byte(register):
77 | write=0x20
78 | register_write=write|register
79 | data = [register_write,0x00,register]
80 | read_reg=spi.xfer(data)
81 | print ("data", read_reg)
82 |
83 | def send_command(command):
84 | send_data = [command]
85 | com_reg=spi.xfer(send_data)
86 |
87 | def write_byte(register,data):
88 | write=0x40
89 | register_write=write|register
90 | data = [register_write,0x00,data]
91 | print (data)
92 | spi.xfer(data)
93 |
94 | def read_byte_2(register):
95 | write=0x20
96 | register_write=write|register
97 | data = [register_write,0x00,register]
98 | cs_line.set_value(0)
99 | read_reg=spi.xfer(data)
100 | cs_line.set_value(1)
101 | print ("data", read_reg)
102 |
103 | def send_command_2(command):
104 | send_data = [command]
105 | cs_line.set_value(0)
106 | spi_2.xfer(send_data)
107 | cs_line.set_value(1)
108 |
109 | def write_byte_2(register,data):
110 | write=0x40
111 | register_write=write|register
112 | data = [register_write,0x00,data]
113 | print (data)
114 |
115 | cs_line.set_value(0)
116 | spi_2.xfer(data)
117 | cs_line.set_value(1)
118 |
119 |
120 | send_command (wakeup)
121 | send_command (stop)
122 | send_command (reset)
123 | send_command (sdatac)
124 |
125 | write_byte (0x14, 0x80) #GPIO 80
126 | write_byte (config1, 0x96)
127 | write_byte (config2, 0xD4)
128 | write_byte (config3, 0xFF)
129 | write_byte (0x04, 0x00)
130 | write_byte (0x0D, 0x00)
131 | write_byte (0x0E, 0x00)
132 | write_byte (0x0F, 0x00)
133 | write_byte (0x10, 0x00)
134 | write_byte (0x11, 0x00)
135 | write_byte (0x15, 0x20)
136 | #
137 | write_byte (0x17, 0x00)
138 | write_byte (ch1set, 0x00)
139 | write_byte (ch2set, 0x00)
140 | write_byte (ch3set, 0x00)
141 | write_byte (ch4set, 0x00)
142 | write_byte (ch5set, 0x00)
143 | write_byte (ch6set, 0x00)
144 | write_byte (ch7set, 0x00)
145 | write_byte (ch8set, 0x00)
146 |
147 | send_command (rdatac)
148 | send_command (start)
149 |
150 | send_command_2 (wakeup)
151 | send_command_2 (stop)
152 | send_command_2 (reset)
153 | send_command_2 (sdatac)
154 |
155 | write_byte_2 (0x14, 0x80) #GPIO 80
156 | write_byte_2 (config1, 0x96)
157 | write_byte_2 (config2, 0xD4)
158 | write_byte_2 (config3, 0xFF)
159 | write_byte_2 (0x04, 0x00)
160 | write_byte_2 (0x0D, 0x00)
161 | write_byte_2 (0x0E, 0x00)
162 | write_byte_2 (0x0F, 0x00)
163 | write_byte_2 (0x10, 0x00)
164 | write_byte_2 (0x11, 0x00)
165 | write_byte_2 (0x15, 0x20)
166 | #
167 | write_byte_2 (0x17, 0x00)
168 | write_byte_2 (ch1set, 0x00)
169 | write_byte_2 (ch2set, 0x00)
170 | write_byte_2 (ch3set, 0x00)
171 | write_byte_2 (ch4set, 0x00)
172 | write_byte_2 (ch5set, 0x00)
173 | write_byte_2 (ch6set, 0x00)
174 | write_byte_2 (ch7set, 0x00)
175 | write_byte_2 (ch8set, 0x00)
176 |
177 | send_command_2 (rdatac)
178 | send_command_2 (start)
179 |
180 | DRDY=1
181 |
182 | result=[0]*27
183 | result_2=[0]*27
184 |
185 | data_1ch_test = []
186 | data_2ch_test = []
187 | data_3ch_test = []
188 | data_4ch_test = []
189 | data_5ch_test = []
190 | data_6ch_test = []
191 | data_7ch_test = []
192 | data_8ch_test = []
193 |
194 | data_9ch_test = []
195 | data_10ch_test = []
196 | data_11ch_test = []
197 | data_12ch_test = []
198 | data_13ch_test = []
199 | data_14ch_test = []
200 | data_15ch_test = []
201 | data_16ch_test = []
202 |
203 | test_DRDY = 5
204 | test_DRDY_2 = 5
205 |
206 | fps = 250
207 | highcut = 1
208 | lowcut = 10
209 | data_before_1 = data_before_2 = data_before_3 = data_before_4 = data_before_5 = data_before_6 = data_before_7 = data_before_8 = [0]*250
210 | data_before_9 = data_before_10 = data_before_11 = data_before_12 = data_before_13 = data_before_14 = data_before_15 = data_before_16 = [0]*250
211 |
212 | while 1:
213 |
214 |
215 | #print ("1", button_state)
216 | #print("2", button_state_2)
217 |
218 | #print ("ok3")
219 | button_state = line_1.get_value()
220 | #print (button_state)
221 | if button_state == 1:
222 | test_DRDY = 10
223 | if test_DRDY == 10 and button_state == 0:
224 | test_DRDY = 0
225 |
226 | output=spi.readbytes(27)
227 |
228 | cs_line.set_value(0)
229 | output_2=spi_2.readbytes(27)
230 | cs_line.set_value(1)
231 |
232 | # print (output[0],output[1],output[2])
233 | if output_2[0]==192 and output_2[1] == 0 and output_2[2] == 8:
234 | #print ("ok4")
235 | for a in range (3,25,3):
236 | voltage_1=(output[a]<<8)| output[a+1]
237 | voltage_1=(voltage_1<<8)| output[a+2]
238 | convert_voktage=voltage_1|data_test
239 | if convert_voktage==data_check:
240 | voltage_1_after_convert=(voltage_1-16777214)
241 | else:
242 | voltage_1_after_convert=voltage_1
243 | channel_num = (a/3)
244 |
245 | result[int (channel_num)]=round(1000000*4.5*(voltage_1_after_convert/16777215),2)
246 |
247 | data_1ch_test.append(result[1])
248 | data_2ch_test.append(result[2])
249 | data_3ch_test.append(result[3])
250 | data_4ch_test.append(result[4])
251 | data_5ch_test.append(result[5])
252 | data_6ch_test.append(result[6])
253 | data_7ch_test.append(result[7])
254 | data_8ch_test.append(result[8])
255 |
256 |
257 | for a in range (3,25,3):
258 | voltage_1=(output_2[a]<<8)| output_2[a+1]
259 | voltage_1=(voltage_1<<8)| output_2[a+2]
260 | convert_voktage=voltage_1|data_test
261 | if convert_voktage==data_check:
262 | voltage_1_after_convert=(voltage_1-16777214)
263 | else:
264 | voltage_1_after_convert=voltage_1
265 | channel_num = (a/3)
266 |
267 | result_2[int (channel_num)]=round(1000000*4.5*(voltage_1_after_convert/16777215),2)
268 |
269 | data_9ch_test.append(result_2[1])
270 | data_10ch_test.append(result_2[2])
271 | data_11ch_test.append(result_2[3])
272 | data_12ch_test.append(result_2[4])
273 | data_13ch_test.append(result_2[5])
274 | data_14ch_test.append(result_2[6])
275 | data_15ch_test.append(result_2[7])
276 | data_16ch_test.append(result_2[8])
277 |
278 | if len(data_9ch_test)==10000: # set lenght 250 it is 1 sec, 20000 = 80 sec
279 | data_dict = {
280 | 'data_1ch_test': data_1ch_test,
281 | 'data_2ch_test': data_2ch_test,
282 | 'data_3ch_test': data_3ch_test,
283 | 'data_4ch_test': data_4ch_test,
284 | 'data_5ch_test': data_5ch_test,
285 | 'data_6ch_test': data_6ch_test,
286 | 'data_7ch_test': data_7ch_test,
287 | 'data_8ch_test': data_8ch_test,
288 | 'data_9ch_test': data_9ch_test,
289 | 'data_10ch_test': data_10ch_test,
290 | 'data_11ch_test': data_11ch_test,
291 | 'data_12ch_test': data_12ch_test,
292 | 'data_13ch_test': data_13ch_test,
293 | 'data_14ch_test': data_14ch_test,
294 | 'data_15ch_test': data_15ch_test,
295 | 'data_16ch_test': data_16ch_test
296 | }
297 |
298 | df = pd.DataFrame(data_dict)
299 | df.to_excel("output3.xlsx", index=False)
300 | print (df)
301 | sys.exit()
302 |
303 |
304 | spi.close()
305 |
--------------------------------------------------------------------------------
/ML_Application/1.Save_Data.py:
--------------------------------------------------------------------------------
1 | import spidev
2 | import time
3 | import gpiod
4 | import pandas as pd
5 |
6 | button_pin_1 = 26 #13
7 | button_pin_2 = 13
8 | #chip = gpiod.Chip("gpiochip4")
9 | chip = gpiod.chip("0")
10 | cs_line = chip.get_line(19) # GPIO19
11 | cs_line.request(consumer="SPI_CS", type=gpiod.LINE_REQ_DIR_OUT)
12 | cs_line.set_value(1) # Set CS high initially
13 |
14 | button_line_1 = chip.get_line(button_pin_1)
15 | button_line_1.request(consumer = "Button", type = gpiod.LINE_REQ_DIR_IN)
16 |
17 |
18 | button_line_2 = chip.get_line(button_pin_2)
19 | button_line_2.request(consumer = "Button", type = gpiod.LINE_REQ_DIR_IN)
20 |
21 | spi = spidev.SpiDev()
22 |
23 | spi.open(0,0)
24 | spi.max_speed_hz = 4000000#600000
25 | spi.lsbfirst=False
26 | spi.mode=0b01
27 | spi.bits_per_word = 8
28 |
29 | spi_2 = spidev.SpiDev()
30 |
31 | spi_2.open(0,1)
32 | spi_2.max_speed_hz=4000000#600000
33 | spi_2.lsbfirst=False
34 | spi_2.mode=0b01
35 | spi_2.bits_per_word = 8
36 |
37 | who_i_am=0x00
38 | config1=0x01
39 | config2=0X02
40 | config3=0X03
41 |
42 | reset=0x06
43 | stop=0x0A
44 | start=0x08
45 | sdatac=0x11
46 | rdatac=0x10
47 | wakeup=0x02
48 | rdata = 0x12
49 |
50 | ch1set=0x05
51 | ch2set=0x06
52 | ch3set=0x07
53 | ch4set=0x08
54 | ch5set=0x09
55 | ch6set=0x0A
56 | ch7set=0x0B
57 | ch8set=0x0C
58 |
59 | data_test= 0x7FFFFF
60 | data_check=0xFFFFFF
61 |
62 | def read_byte(register):
63 | write=0x20
64 | register_write=write|register
65 | data = [register_write,0x00,register]
66 | read_reg=spi.xfer(data)
67 | print ("data", read_reg)
68 |
69 | def send_command(command):
70 | send_data = [command]
71 | com_reg=spi.xfer(send_data)
72 |
73 | def write_byte(register,data):
74 | write=0x40
75 | register_write=write|register
76 | data = [register_write,0x00,data]
77 | print (data)
78 | spi.xfer(data)
79 |
80 | def read_byte_2(register):
81 | write=0x20
82 | register_write=write|register
83 | data = [register_write,0x00,register]
84 | cs_line.set_value(0)
85 | read_reg=spi.xfer(data)
86 | cs_line.set_value(1)
87 | print ("data", read_reg)
88 |
89 | def send_command_2(command):
90 | send_data = [command]
91 | cs_line.set_value(0)
92 | spi_2.xfer(send_data)
93 | cs_line.set_value(1)
94 |
95 | def write_byte_2(register,data):
96 | write=0x40
97 | register_write=write|register
98 | data = [register_write,0x00,data]
99 | print (data)
100 |
101 | cs_line.set_value(0)
102 | spi_2.xfer(data)
103 | cs_line.set_value(1)
104 |
105 |
106 |
107 | send_command (wakeup)
108 | send_command (stop)
109 | send_command (reset)
110 | send_command (sdatac)
111 |
112 | write_byte (0x14, 0x80) #GPIO 80
113 | write_byte (config1, 0x96)
114 | write_byte (config2, 0xD4)
115 | write_byte (config3, 0xFF)
116 | write_byte (0x04, 0x00)
117 | write_byte (0x0D, 0x00)
118 | write_byte (0x0E, 0x00)
119 | write_byte (0x0F, 0x00)
120 | write_byte (0x10, 0x00)
121 | write_byte (0x11, 0x00)
122 | write_byte (0x15, 0x20)
123 | #
124 | write_byte (0x17, 0x00)
125 | write_byte (ch1set, 0x00)
126 | write_byte (ch2set, 0x00)
127 | write_byte (ch3set, 0x00)
128 | write_byte (ch4set, 0x00)
129 | write_byte (ch5set, 0x00)
130 | write_byte (ch6set, 0x00)
131 | write_byte (ch7set, 0x00)
132 | write_byte (ch8set, 0x00)
133 |
134 | send_command (rdatac)
135 | send_command (start)
136 |
137 |
138 | send_command_2 (wakeup)
139 | send_command_2 (stop)
140 | send_command_2 (reset)
141 | send_command_2 (sdatac)
142 |
143 | write_byte_2 (0x14, 0x80) #GPIO 80
144 | write_byte_2 (config1, 0x96)
145 | write_byte_2 (config2, 0xD4)
146 | write_byte_2 (config3, 0xFF)
147 | write_byte_2 (0x04, 0x00)
148 | write_byte_2 (0x0D, 0x00)
149 | write_byte_2 (0x0E, 0x00)
150 | write_byte_2 (0x0F, 0x00)
151 | write_byte_2 (0x10, 0x00)
152 | write_byte_2 (0x11, 0x00)
153 | write_byte_2 (0x15, 0x20)
154 | #
155 | write_byte_2 (0x17, 0x00)
156 | write_byte_2 (ch1set, 0x00)
157 | write_byte_2 (ch2set, 0x00)
158 | write_byte_2 (ch3set, 0x00)
159 | write_byte_2 (ch4set, 0x00)
160 | write_byte_2 (ch5set, 0x00)
161 | write_byte_2 (ch6set, 0x00)
162 | write_byte_2 (ch7set, 0x00)
163 | write_byte_2 (ch8set, 0x00)
164 |
165 | send_command_2 (rdatac)
166 | send_command_2 (start)
167 |
168 | DRDY=1
169 |
170 | result=[0]*27
171 | result_2=[0]*27
172 |
173 |
174 | data_1ch_test = []
175 | data_2ch_test = []
176 | data_3ch_test = []
177 | data_4ch_test = []
178 | data_5ch_test = []
179 | data_6ch_test = []
180 | data_7ch_test = []
181 | data_8ch_test = []
182 |
183 | data_9ch_test = []
184 | data_10ch_test = []
185 | data_11ch_test = []
186 | data_12ch_test = []
187 | data_13ch_test = []
188 | data_14ch_test = []
189 | data_15ch_test = []
190 | data_16ch_test = []
191 |
192 | axis_x=0
193 | y_minus_graph=100
194 | y_plus_graph=100
195 | x_minux_graph=5000
196 | x_plus_graph=250
197 | sample_len = 250
198 |
199 | fig, axis = plt.subplots(4, 4, figsize=(5, 5))
200 | plt.subplots_adjust(hspace=1)
201 | ch_name = 0
202 | ch_name_title = [1,5,2,6,3,7,4,8]
203 | axi = [(i, j) for i in range(4) for j in range(2)]
204 | for ax_row, ax_col in axi:
205 | axis[ax_row, ax_col].set_xlabel('Time')
206 | axis[ax_row, ax_col].set_ylabel('Amplitude')
207 | axis[ax_row, ax_col].set_title('Data after pass filter Ch-' + str(ch_name_title[ch_name]))
208 | ch_name = ch_name + 1
209 |
210 | test_DRDY = 5
211 | test_DRDY_2 = 5
212 | #1.2 Band-pass filter
213 | data_before = []
214 | data_after = []
215 | just_one_time = 0
216 | data_lenght_for_Filter = 2 # how much we read data for filter, all lenght [_____] + [_____] + [_____]
217 | read_data_lenght_one_time = 1 # for one time how much read [_____]
218 | sample_len = 250
219 | sample_lens = 250
220 | fps = 250
221 | highcut = 1
222 | lowcut = 10
223 | data_before_1 = data_before_2 = data_before_3 = data_before_4 = data_before_5 = data_before_6 = data_before_7 = data_before_8 = [0]*250
224 | data_before_9 = data_before_10 = data_before_11 = data_before_12 = data_before_13 = data_before_14 = data_before_15 = data_before_16 = [0]*250
225 |
226 | print (data_lenght_for_Filter*read_data_lenght_one_time-read_data_lenght_one_time)
227 |
228 | def butter_lowpass(cutoff, fs, order=5):
229 | nyq = 0.5 * fs
230 | normal_cutoff = cutoff / nyq
231 | b, a = signal.butter(order, normal_cutoff, btype='low', analog=False)
232 | return b, a
233 | def butter_lowpass_filter(data, cutoff, fs, order=5):
234 | b, a = butter_lowpass(cutoff, fs, order=order)
235 | y = signal.lfilter(b, a, data)
236 | return y
237 | def butter_highpass(cutoff, fs, order=3):
238 | nyq = 0.5 * fs
239 | normal_cutoff = cutoff / nyq
240 | b, a = signal.butter(order, normal_cutoff, btype='high', analog=False)
241 | return b, a
242 | def butter_highpass_filter(data, cutoff, fs, order=5):
243 | b, a = butter_highpass(cutoff, fs, order=order)
244 | y = signal.filtfilt(b, a, data)
245 | return y
246 |
247 | while 1:
248 |
249 | button_state = button_line_1.get_value()
250 | #print (button_state)
251 | if button_state == 1:
252 | test_DRDY = 10
253 | if test_DRDY == 10 and button_state == 0:
254 | test_DRDY = 0
255 |
256 | output=spi.readbytes(27)
257 |
258 | cs_line.set_value(0)
259 | output_2=spi_2.readbytes(27)
260 | cs_line.set_value(1)
261 |
262 | # print (output[0],output[1],output[2])
263 | if output_2[0]==192 and output_2[1] == 0 and output_2[2] == 8:
264 | #print ("ok4")
265 | for a in range (3,25,3):
266 | voltage_1=(output[a]<<8)| output[a+1]
267 | voltage_1=(voltage_1<<8)| output[a+2]
268 | convert_voktage=voltage_1|data_test
269 | if convert_voktage==data_check:
270 | voltage_1_after_convert=(voltage_1-16777214)
271 | else:
272 | voltage_1_after_convert=voltage_1
273 | channel_num = (a/3)
274 |
275 | result[int (channel_num)]=round(1000000*4.5*(voltage_1_after_convert/16777215),2)
276 |
277 | data_1ch_test.append(result[1])
278 | data_2ch_test.append(result[2])
279 | data_3ch_test.append(result[3])
280 | data_4ch_test.append(result[4])
281 | data_5ch_test.append(result[5])
282 | data_6ch_test.append(result[6])
283 | data_7ch_test.append(result[7])
284 | data_8ch_test.append(result[8])
285 |
286 |
287 | for a in range (3,25,3):
288 | voltage_1=(output_2[a]<<8)| output_2[a+1]
289 | voltage_1=(voltage_1<<8)| output_2[a+2]
290 | convert_voktage=voltage_1|data_test
291 | if convert_voktage==data_check:
292 | voltage_1_after_convert=(voltage_1-16777214)
293 | else:
294 | voltage_1_after_convert=voltage_1
295 | channel_num = (a/3)
296 |
297 | result_2[int (channel_num)]=round(1000000*4.5*(voltage_1_after_convert/16777215),2)
298 |
299 | data_9ch_test.append(result_2[1])
300 | data_10ch_test.append(result_2[2])
301 | data_11ch_test.append(result_2[3])
302 | data_12ch_test.append(result_2[4])
303 | data_13ch_test.append(result_2[5])
304 | data_14ch_test.append(result_2[6])
305 | data_15ch_test.append(result_2[7])
306 | data_16ch_test.append(result_2[8])
307 |
308 | if len(data_9ch_test)==20000: # set lenght 250 it is 1 sec, 20000 = 80 sec
309 | data_dict = {
310 | 'data_1ch_test': data_1ch_test,
311 | 'data_2ch_test': data_2ch_test,
312 | 'data_3ch_test': data_3ch_test,
313 | 'data_4ch_test': data_4ch_test,
314 | 'data_5ch_test': data_5ch_test,
315 | 'data_6ch_test': data_6ch_test,
316 | 'data_7ch_test': data_7ch_test,
317 | 'data_8ch_test': data_8ch_test,
318 | 'data_9ch_test': data_9ch_test,
319 | 'data_10ch_test': data_10ch_test,
320 | 'data_11ch_test': data_11ch_test,
321 | 'data_12ch_test': data_12ch_test,
322 | 'data_13ch_test': data_13ch_test,
323 | 'data_14ch_test': data_14ch_test,
324 | 'data_15ch_test': data_15ch_test,
325 | 'data_16ch_test': data_16ch_test
326 | }
327 |
328 | df = pd.DataFrame(data_dict)
329 | df.to_excel("output3.xlsx", index=False)
330 | print (df)
331 |
332 |
333 | spi.close()
334 |
--------------------------------------------------------------------------------
/Save_data/arxive/1.Save_Data.py:
--------------------------------------------------------------------------------
1 | import spidev
2 | import time
3 | from RPi import GPIO
4 | GPIO.setwarnings(False)
5 | GPIO.setmode(GPIO.BOARD)
6 | from gpiozero import LED,Button
7 | from matplotlib import pyplot as plt
8 | #sw1 = Button(26,pull_up=True)# 37
9 | #from gpiozero import LED,Button
10 | from scipy.ndimage import gaussian_filter1d
11 | from scipy import signal
12 | import gpiod
13 | from time import sleep
14 |
15 | button_pin_1 = 26 #13
16 | button_pin_2 = 13
17 | chip = gpiod.Chip("gpiochip4")
18 |
19 | cs_line = chip.get_line(19) # GPIO19
20 | cs_line.request(consumer="SPI_CS", type=gpiod.LINE_REQ_DIR_OUT)
21 | cs_line.set_value(1) # Set CS high initially
22 |
23 | button_line_1 = chip.get_line(button_pin_1)
24 | button_line_1.request(consumer = "Button", type = gpiod.LINE_REQ_DIR_IN)
25 |
26 |
27 | button_line_2 = chip.get_line(button_pin_2)
28 | button_line_2.request(consumer = "Button", type = gpiod.LINE_REQ_DIR_IN)
29 |
30 | spi = spidev.SpiDev()
31 |
32 | spi.open(0,0)
33 | spi.max_speed_hz = 4000000#600000
34 | spi.lsbfirst=False
35 | spi.mode=0b01
36 | spi.bits_per_word = 8
37 |
38 | spi_2 = spidev.SpiDev()
39 |
40 | spi_2.open(0,1)
41 | spi_2.max_speed_hz=4000000#600000
42 | spi_2.lsbfirst=False
43 | spi_2.mode=0b01
44 | spi_2.bits_per_word = 8
45 |
46 | who_i_am=0x00
47 | config1=0x01
48 | config2=0X02
49 | config3=0X03
50 |
51 | reset=0x06
52 | stop=0x0A
53 | start=0x08
54 | sdatac=0x11
55 | rdatac=0x10
56 | wakeup=0x02
57 | rdata = 0x12
58 |
59 | ch1set=0x05
60 | ch2set=0x06
61 | ch3set=0x07
62 | ch4set=0x08
63 | ch5set=0x09
64 | ch6set=0x0A
65 | ch7set=0x0B
66 | ch8set=0x0C
67 |
68 | data_test= 0x7FFFFF
69 | data_check=0xFFFFFF
70 |
71 | def read_byte(register):
72 | write=0x20
73 | register_write=write|register
74 | data = [register_write,0x00,register]
75 | read_reg=spi.xfer(data)
76 | print ("data", read_reg)
77 |
78 | def send_command(command):
79 | send_data = [command]
80 | com_reg=spi.xfer(send_data)
81 |
82 | def write_byte(register,data):
83 | write=0x40
84 | register_write=write|register
85 | data = [register_write,0x00,data]
86 | print (data)
87 | spi.xfer(data)
88 |
89 | def read_byte_2(register):
90 | write=0x20
91 | register_write=write|register
92 | data = [register_write,0x00,register]
93 | cs_line.set_value(0)
94 | read_reg=spi.xfer(data)
95 | cs_line.set_value(1)
96 | print ("data", read_reg)
97 |
98 | def send_command_2(command):
99 | send_data = [command]
100 | cs_line.set_value(0)
101 | spi_2.xfer(send_data)
102 | cs_line.set_value(1)
103 |
104 | def write_byte_2(register,data):
105 | write=0x40
106 | register_write=write|register
107 | data = [register_write,0x00,data]
108 | print (data)
109 |
110 | cs_line.set_value(0)
111 | spi_2.xfer(data)
112 | cs_line.set_value(1)
113 |
114 |
115 |
116 | send_command (wakeup)
117 | send_command (stop)
118 | send_command (reset)
119 | send_command (sdatac)
120 |
121 | write_byte (0x14, 0x80) #GPIO 80
122 | write_byte (config1, 0x96)
123 | write_byte (config2, 0xD4)
124 | write_byte (config3, 0xFF)
125 | write_byte (0x04, 0x00)
126 | write_byte (0x0D, 0x00)
127 | write_byte (0x0E, 0x00)
128 | write_byte (0x0F, 0x00)
129 | write_byte (0x10, 0x00)
130 | write_byte (0x11, 0x00)
131 | write_byte (0x15, 0x20)
132 | #
133 | write_byte (0x17, 0x00)
134 | write_byte (ch1set, 0x00)
135 | write_byte (ch2set, 0x00)
136 | write_byte (ch3set, 0x00)
137 | write_byte (ch4set, 0x00)
138 | write_byte (ch5set, 0x00)
139 | write_byte (ch6set, 0x00)
140 | write_byte (ch7set, 0x00)
141 | write_byte (ch8set, 0x00)
142 |
143 | send_command (rdatac)
144 | send_command (start)
145 |
146 |
147 | send_command_2 (wakeup)
148 | send_command_2 (stop)
149 | send_command_2 (reset)
150 | send_command_2 (sdatac)
151 |
152 | write_byte_2 (0x14, 0x80) #GPIO 80
153 | write_byte_2 (config1, 0x96)
154 | write_byte_2 (config2, 0xD4)
155 | write_byte_2 (config3, 0xFF)
156 | write_byte_2 (0x04, 0x00)
157 | write_byte_2 (0x0D, 0x00)
158 | write_byte_2 (0x0E, 0x00)
159 | write_byte_2 (0x0F, 0x00)
160 | write_byte_2 (0x10, 0x00)
161 | write_byte_2 (0x11, 0x00)
162 | write_byte_2 (0x15, 0x20)
163 | #
164 | write_byte_2 (0x17, 0x00)
165 | write_byte_2 (ch1set, 0x00)
166 | write_byte_2 (ch2set, 0x00)
167 | write_byte_2 (ch3set, 0x00)
168 | write_byte_2 (ch4set, 0x00)
169 | write_byte_2 (ch5set, 0x00)
170 | write_byte_2 (ch6set, 0x00)
171 | write_byte_2 (ch7set, 0x00)
172 | write_byte_2 (ch8set, 0x00)
173 |
174 | send_command_2 (rdatac)
175 | send_command_2 (start)
176 |
177 | DRDY=1
178 |
179 | result=[0]*27
180 | result_2=[0]*27
181 |
182 |
183 | data_1ch_test = []
184 | data_2ch_test = []
185 | data_3ch_test = []
186 | data_4ch_test = []
187 | data_5ch_test = []
188 | data_6ch_test = []
189 | data_7ch_test = []
190 | data_8ch_test = []
191 |
192 | data_9ch_test = []
193 | data_10ch_test = []
194 | data_11ch_test = []
195 | data_12ch_test = []
196 | data_13ch_test = []
197 | data_14ch_test = []
198 | data_15ch_test = []
199 | data_16ch_test = []
200 |
201 | axis_x=0
202 | y_minus_graph=100
203 | y_plus_graph=100
204 | x_minux_graph=5000
205 | x_plus_graph=250
206 | sample_len = 250
207 |
208 | fig, axis = plt.subplots(4, 4, figsize=(5, 5))
209 | plt.subplots_adjust(hspace=1)
210 | ch_name = 0
211 | ch_name_title = [1,5,2,6,3,7,4,8]
212 | axi = [(i, j) for i in range(4) for j in range(2)]
213 | for ax_row, ax_col in axi:
214 | axis[ax_row, ax_col].set_xlabel('Time')
215 | axis[ax_row, ax_col].set_ylabel('Amplitude')
216 | axis[ax_row, ax_col].set_title('Data after pass filter Ch-' + str(ch_name_title[ch_name]))
217 | ch_name = ch_name + 1
218 |
219 | test_DRDY = 5
220 | test_DRDY_2 = 5
221 | #1.2 Band-pass filter
222 | data_before = []
223 | data_after = []
224 | just_one_time = 0
225 | data_lenght_for_Filter = 2 # how much we read data for filter, all lenght [_____] + [_____] + [_____]
226 | read_data_lenght_one_time = 1 # for one time how much read [_____]
227 | sample_len = 250
228 | sample_lens = 250
229 | fps = 250
230 | highcut = 1
231 | lowcut = 10
232 | data_before_1 = data_before_2 = data_before_3 = data_before_4 = data_before_5 = data_before_6 = data_before_7 = data_before_8 = [0]*250
233 | data_before_9 = data_before_10 = data_before_11 = data_before_12 = data_before_13 = data_before_14 = data_before_15 = data_before_16 = [0]*250
234 |
235 | print (data_lenght_for_Filter*read_data_lenght_one_time-read_data_lenght_one_time)
236 |
237 | def butter_lowpass(cutoff, fs, order=5):
238 | nyq = 0.5 * fs
239 | normal_cutoff = cutoff / nyq
240 | b, a = signal.butter(order, normal_cutoff, btype='low', analog=False)
241 | return b, a
242 | def butter_lowpass_filter(data, cutoff, fs, order=5):
243 | b, a = butter_lowpass(cutoff, fs, order=order)
244 | y = signal.lfilter(b, a, data)
245 | return y
246 | def butter_highpass(cutoff, fs, order=3):
247 | nyq = 0.5 * fs
248 | normal_cutoff = cutoff / nyq
249 | b, a = signal.butter(order, normal_cutoff, btype='high', analog=False)
250 | return b, a
251 | def butter_highpass_filter(data, cutoff, fs, order=5):
252 | b, a = butter_highpass(cutoff, fs, order=order)
253 | y = signal.filtfilt(b, a, data)
254 | return y
255 |
256 | while 1:
257 |
258 |
259 | #print ("1", button_state)
260 | #print("2", button_state_2)
261 |
262 | #print ("ok3")
263 | button_state = button_line_1.get_value()
264 | #print (button_state)
265 | if button_state == 1:
266 | test_DRDY = 10
267 | if test_DRDY == 10 and button_state == 0:
268 | test_DRDY = 0
269 |
270 | output=spi.readbytes(27)
271 |
272 | cs_line.set_value(0)
273 | output_2=spi_2.readbytes(27)
274 | cs_line.set_value(1)
275 |
276 | # print (output[0],output[1],output[2])
277 | if output_2[0]==192 and output_2[1] == 0 and output_2[2] == 8:
278 | #print ("ok4")
279 | for a in range (3,25,3):
280 | voltage_1=(output[a]<<8)| output[a+1]
281 | voltage_1=(voltage_1<<8)| output[a+2]
282 | convert_voktage=voltage_1|data_test
283 | if convert_voktage==data_check:
284 | voltage_1_after_convert=(voltage_1-16777214)
285 | else:
286 | voltage_1_after_convert=voltage_1
287 | channel_num = (a/3)
288 |
289 | result[int (channel_num)]=round(1000000*4.5*(voltage_1_after_convert/16777215),2)
290 |
291 | data_1ch_test.append(result[1])
292 | data_2ch_test.append(result[2])
293 | data_3ch_test.append(result[3])
294 | data_4ch_test.append(result[4])
295 | data_5ch_test.append(result[5])
296 | data_6ch_test.append(result[6])
297 | data_7ch_test.append(result[7])
298 | data_8ch_test.append(result[8])
299 |
300 |
301 | for a in range (3,25,3):
302 | voltage_1=(output_2[a]<<8)| output_2[a+1]
303 | voltage_1=(voltage_1<<8)| output_2[a+2]
304 | convert_voktage=voltage_1|data_test
305 | if convert_voktage==data_check:
306 | voltage_1_after_convert=(voltage_1-16777214)
307 | else:
308 | voltage_1_after_convert=voltage_1
309 | channel_num = (a/3)
310 |
311 | result_2[int (channel_num)]=round(1000000*4.5*(voltage_1_after_convert/16777215),2)
312 |
313 | data_9ch_test.append(result_2[1])
314 | data_10ch_test.append(result_2[2])
315 | data_11ch_test.append(result_2[3])
316 | data_12ch_test.append(result_2[4])
317 | data_13ch_test.append(result_2[5])
318 | data_14ch_test.append(result_2[6])
319 | data_15ch_test.append(result_2[7])
320 | data_16ch_test.append(result_2[8])
321 |
322 |
323 |
324 |
325 | if len(data_9ch_test)==sample_len:
326 | data_dict = {
327 | 'data_1ch_test': data_1ch_test,
328 | 'data_2ch_test': data_2ch_test,
329 | 'data_3ch_test': data_3ch_test,
330 | 'data_4ch_test': data_4ch_test,
331 | 'data_5ch_test': data_5ch_test,
332 | 'data_6ch_test': data_6ch_test,
333 | 'data_7ch_test': data_7ch_test,
334 | 'data_8ch_test': data_8ch_test,
335 | 'data_9ch_test': data_9ch_test,
336 | 'data_10ch_test': data_10ch_test,
337 | 'data_11ch_test': data_11ch_test,
338 | 'data_12ch_test': data_12ch_test,
339 | 'data_13ch_test': data_13ch_test,
340 | 'data_14ch_test': data_14ch_test,
341 | 'data_15ch_test': data_15ch_test,
342 | 'data_16ch_test': data_16ch_test
343 | }
344 |
345 | df = pd.DataFrame(data_dict)
346 | df.to_excel("output3.xlsx", index=False)
347 | print (df)
348 |
349 |
350 | spi.close()
351 |
--------------------------------------------------------------------------------
/Save_data/1.Save_Data.py:
--------------------------------------------------------------------------------
1 | import spidev
2 | import time
3 | #from RPi import GPIO
4 | #GPIO.setwarnings(False)
5 | #GPIO.setmode(GPIO.BOARD)
6 | #from gpiozero import LED,Button
7 | #from matplotlib import pyplot as plt
8 | #sw1 = Button(26,pull_up=True)# 37
9 | #from gpiozero import LED,Button
10 | #from scipy.ndimage import gaussian_filter1d
11 | #from scipy import signal
12 | import gpiod
13 | #from time import sleep
14 | import pandas as pd
15 |
16 | button_pin_1 = 26 #13
17 | button_pin_2 = 13
18 | #chip = gpiod.Chip("gpiochip4")
19 | chip = gpiod.chip("0")
20 | cs_line = chip.get_line(19) # GPIO19
21 | cs_line.request(consumer="SPI_CS", type=gpiod.LINE_REQ_DIR_OUT)
22 | cs_line.set_value(1) # Set CS high initially
23 |
24 | button_line_1 = chip.get_line(button_pin_1)
25 | button_line_1.request(consumer = "Button", type = gpiod.LINE_REQ_DIR_IN)
26 |
27 |
28 | button_line_2 = chip.get_line(button_pin_2)
29 | button_line_2.request(consumer = "Button", type = gpiod.LINE_REQ_DIR_IN)
30 |
31 | spi = spidev.SpiDev()
32 |
33 | spi.open(0,0)
34 | spi.max_speed_hz = 4000000#600000
35 | spi.lsbfirst=False
36 | spi.mode=0b01
37 | spi.bits_per_word = 8
38 |
39 | spi_2 = spidev.SpiDev()
40 |
41 | spi_2.open(0,1)
42 | spi_2.max_speed_hz=4000000#600000
43 | spi_2.lsbfirst=False
44 | spi_2.mode=0b01
45 | spi_2.bits_per_word = 8
46 |
47 | who_i_am=0x00
48 | config1=0x01
49 | config2=0X02
50 | config3=0X03
51 |
52 | reset=0x06
53 | stop=0x0A
54 | start=0x08
55 | sdatac=0x11
56 | rdatac=0x10
57 | wakeup=0x02
58 | rdata = 0x12
59 |
60 | ch1set=0x05
61 | ch2set=0x06
62 | ch3set=0x07
63 | ch4set=0x08
64 | ch5set=0x09
65 | ch6set=0x0A
66 | ch7set=0x0B
67 | ch8set=0x0C
68 |
69 | data_test= 0x7FFFFF
70 | data_check=0xFFFFFF
71 |
72 | def read_byte(register):
73 | write=0x20
74 | register_write=write|register
75 | data = [register_write,0x00,register]
76 | read_reg=spi.xfer(data)
77 | print ("data", read_reg)
78 |
79 | def send_command(command):
80 | send_data = [command]
81 | com_reg=spi.xfer(send_data)
82 |
83 | def write_byte(register,data):
84 | write=0x40
85 | register_write=write|register
86 | data = [register_write,0x00,data]
87 | print (data)
88 | spi.xfer(data)
89 |
90 | def read_byte_2(register):
91 | write=0x20
92 | register_write=write|register
93 | data = [register_write,0x00,register]
94 | cs_line.set_value(0)
95 | read_reg=spi.xfer(data)
96 | cs_line.set_value(1)
97 | print ("data", read_reg)
98 |
99 | def send_command_2(command):
100 | send_data = [command]
101 | cs_line.set_value(0)
102 | spi_2.xfer(send_data)
103 | cs_line.set_value(1)
104 |
105 | def write_byte_2(register,data):
106 | write=0x40
107 | register_write=write|register
108 | data = [register_write,0x00,data]
109 | print (data)
110 |
111 | cs_line.set_value(0)
112 | spi_2.xfer(data)
113 | cs_line.set_value(1)
114 |
115 |
116 |
117 | send_command (wakeup)
118 | send_command (stop)
119 | send_command (reset)
120 | send_command (sdatac)
121 |
122 | write_byte (0x14, 0x80) #GPIO 80
123 | write_byte (config1, 0x96)
124 | write_byte (config2, 0xD4)
125 | write_byte (config3, 0xFF)
126 | write_byte (0x04, 0x00)
127 | write_byte (0x0D, 0x00)
128 | write_byte (0x0E, 0x00)
129 | write_byte (0x0F, 0x00)
130 | write_byte (0x10, 0x00)
131 | write_byte (0x11, 0x00)
132 | write_byte (0x15, 0x20)
133 | #
134 | write_byte (0x17, 0x00)
135 | write_byte (ch1set, 0x00)
136 | write_byte (ch2set, 0x00)
137 | write_byte (ch3set, 0x00)
138 | write_byte (ch4set, 0x00)
139 | write_byte (ch5set, 0x00)
140 | write_byte (ch6set, 0x00)
141 | write_byte (ch7set, 0x00)
142 | write_byte (ch8set, 0x00)
143 |
144 | send_command (rdatac)
145 | send_command (start)
146 |
147 |
148 | send_command_2 (wakeup)
149 | send_command_2 (stop)
150 | send_command_2 (reset)
151 | send_command_2 (sdatac)
152 |
153 | write_byte_2 (0x14, 0x80) #GPIO 80
154 | write_byte_2 (config1, 0x96)
155 | write_byte_2 (config2, 0xD4)
156 | write_byte_2 (config3, 0xFF)
157 | write_byte_2 (0x04, 0x00)
158 | write_byte_2 (0x0D, 0x00)
159 | write_byte_2 (0x0E, 0x00)
160 | write_byte_2 (0x0F, 0x00)
161 | write_byte_2 (0x10, 0x00)
162 | write_byte_2 (0x11, 0x00)
163 | write_byte_2 (0x15, 0x20)
164 | #
165 | write_byte_2 (0x17, 0x00)
166 | write_byte_2 (ch1set, 0x00)
167 | write_byte_2 (ch2set, 0x00)
168 | write_byte_2 (ch3set, 0x00)
169 | write_byte_2 (ch4set, 0x00)
170 | write_byte_2 (ch5set, 0x00)
171 | write_byte_2 (ch6set, 0x00)
172 | write_byte_2 (ch7set, 0x00)
173 | write_byte_2 (ch8set, 0x00)
174 |
175 | send_command_2 (rdatac)
176 | send_command_2 (start)
177 |
178 | DRDY=1
179 |
180 | result=[0]*27
181 | result_2=[0]*27
182 |
183 |
184 | data_1ch_test = []
185 | data_2ch_test = []
186 | data_3ch_test = []
187 | data_4ch_test = []
188 | data_5ch_test = []
189 | data_6ch_test = []
190 | data_7ch_test = []
191 | data_8ch_test = []
192 |
193 | data_9ch_test = []
194 | data_10ch_test = []
195 | data_11ch_test = []
196 | data_12ch_test = []
197 | data_13ch_test = []
198 | data_14ch_test = []
199 | data_15ch_test = []
200 | data_16ch_test = []
201 |
202 | axis_x=0
203 | y_minus_graph=100
204 | y_plus_graph=100
205 | x_minux_graph=5000
206 | x_plus_graph=250
207 | sample_len = 250
208 |
209 | fig, axis = plt.subplots(4, 4, figsize=(5, 5))
210 | plt.subplots_adjust(hspace=1)
211 | ch_name = 0
212 | ch_name_title = [1,5,2,6,3,7,4,8]
213 | axi = [(i, j) for i in range(4) for j in range(2)]
214 | for ax_row, ax_col in axi:
215 | axis[ax_row, ax_col].set_xlabel('Time')
216 | axis[ax_row, ax_col].set_ylabel('Amplitude')
217 | axis[ax_row, ax_col].set_title('Data after pass filter Ch-' + str(ch_name_title[ch_name]))
218 | ch_name = ch_name + 1
219 |
220 | test_DRDY = 5
221 | test_DRDY_2 = 5
222 | #1.2 Band-pass filter
223 | data_before = []
224 | data_after = []
225 | just_one_time = 0
226 | data_lenght_for_Filter = 2 # how much we read data for filter, all lenght [_____] + [_____] + [_____]
227 | read_data_lenght_one_time = 1 # for one time how much read [_____]
228 | sample_len = 250
229 | sample_lens = 250
230 | fps = 250
231 | highcut = 1
232 | lowcut = 10
233 | data_before_1 = data_before_2 = data_before_3 = data_before_4 = data_before_5 = data_before_6 = data_before_7 = data_before_8 = [0]*250
234 | data_before_9 = data_before_10 = data_before_11 = data_before_12 = data_before_13 = data_before_14 = data_before_15 = data_before_16 = [0]*250
235 |
236 | print (data_lenght_for_Filter*read_data_lenght_one_time-read_data_lenght_one_time)
237 |
238 | def butter_lowpass(cutoff, fs, order=5):
239 | nyq = 0.5 * fs
240 | normal_cutoff = cutoff / nyq
241 | b, a = signal.butter(order, normal_cutoff, btype='low', analog=False)
242 | return b, a
243 | def butter_lowpass_filter(data, cutoff, fs, order=5):
244 | b, a = butter_lowpass(cutoff, fs, order=order)
245 | y = signal.lfilter(b, a, data)
246 | return y
247 | def butter_highpass(cutoff, fs, order=3):
248 | nyq = 0.5 * fs
249 | normal_cutoff = cutoff / nyq
250 | b, a = signal.butter(order, normal_cutoff, btype='high', analog=False)
251 | return b, a
252 | def butter_highpass_filter(data, cutoff, fs, order=5):
253 | b, a = butter_highpass(cutoff, fs, order=order)
254 | y = signal.filtfilt(b, a, data)
255 | return y
256 |
257 | while 1:
258 |
259 |
260 | #print ("1", button_state)
261 | #print("2", button_state_2)
262 |
263 | #print ("ok3")
264 | button_state = button_line_1.get_value()
265 | #print (button_state)
266 | if button_state == 1:
267 | test_DRDY = 10
268 | if test_DRDY == 10 and button_state == 0:
269 | test_DRDY = 0
270 |
271 | output=spi.readbytes(27)
272 |
273 | cs_line.set_value(0)
274 | output_2=spi_2.readbytes(27)
275 | cs_line.set_value(1)
276 |
277 | # print (output[0],output[1],output[2])
278 | if output_2[0]==192 and output_2[1] == 0 and output_2[2] == 8:
279 | #print ("ok4")
280 | for a in range (3,25,3):
281 | voltage_1=(output[a]<<8)| output[a+1]
282 | voltage_1=(voltage_1<<8)| output[a+2]
283 | convert_voktage=voltage_1|data_test
284 | if convert_voktage==data_check:
285 | voltage_1_after_convert=(voltage_1-16777214)
286 | else:
287 | voltage_1_after_convert=voltage_1
288 | channel_num = (a/3)
289 |
290 | result[int (channel_num)]=round(1000000*4.5*(voltage_1_after_convert/16777215),2)
291 |
292 | data_1ch_test.append(result[1])
293 | data_2ch_test.append(result[2])
294 | data_3ch_test.append(result[3])
295 | data_4ch_test.append(result[4])
296 | data_5ch_test.append(result[5])
297 | data_6ch_test.append(result[6])
298 | data_7ch_test.append(result[7])
299 | data_8ch_test.append(result[8])
300 |
301 |
302 | for a in range (3,25,3):
303 | voltage_1=(output_2[a]<<8)| output_2[a+1]
304 | voltage_1=(voltage_1<<8)| output_2[a+2]
305 | convert_voktage=voltage_1|data_test
306 | if convert_voktage==data_check:
307 | voltage_1_after_convert=(voltage_1-16777214)
308 | else:
309 | voltage_1_after_convert=voltage_1
310 | channel_num = (a/3)
311 |
312 | result_2[int (channel_num)]=round(1000000*4.5*(voltage_1_after_convert/16777215),2)
313 |
314 | data_9ch_test.append(result_2[1])
315 | data_10ch_test.append(result_2[2])
316 | data_11ch_test.append(result_2[3])
317 | data_12ch_test.append(result_2[4])
318 | data_13ch_test.append(result_2[5])
319 | data_14ch_test.append(result_2[6])
320 | data_15ch_test.append(result_2[7])
321 | data_16ch_test.append(result_2[8])
322 |
323 | if len(data_9ch_test)==20000: # set lenght 250 it is 1 sec, 20000 = 80 sec
324 | data_dict = {
325 | 'data_1ch_test': data_1ch_test,
326 | 'data_2ch_test': data_2ch_test,
327 | 'data_3ch_test': data_3ch_test,
328 | 'data_4ch_test': data_4ch_test,
329 | 'data_5ch_test': data_5ch_test,
330 | 'data_6ch_test': data_6ch_test,
331 | 'data_7ch_test': data_7ch_test,
332 | 'data_8ch_test': data_8ch_test,
333 | 'data_9ch_test': data_9ch_test,
334 | 'data_10ch_test': data_10ch_test,
335 | 'data_11ch_test': data_11ch_test,
336 | 'data_12ch_test': data_12ch_test,
337 | 'data_13ch_test': data_13ch_test,
338 | 'data_14ch_test': data_14ch_test,
339 | 'data_15ch_test': data_15ch_test,
340 | 'data_16ch_test': data_16ch_test
341 | }
342 |
343 | df = pd.DataFrame(data_dict)
344 | df.to_excel("output3.xlsx", index=False)
345 | print (df)
346 |
347 |
348 | spi.close()
349 |
--------------------------------------------------------------------------------
/Save_data/arxive/pieeg16_os_save_data_1.py:
--------------------------------------------------------------------------------
1 | import spidev
2 | import time
3 | #from RPi import GPIO
4 | from matplotlib import pyplot as plt
5 | from scipy.ndimage import gaussian_filter1d
6 | from scipy import signal
7 | import gpiod
8 |
9 | #GPIO.setwarnings(False)
10 | #GPIO.setmode(GPIO.BOARD)
11 |
12 | button_pin_1 = 26 #13
13 | button_pin_2 = 13
14 | cs_pin = 19
15 | #chip = gpiod.Chip("gpiochip4")
16 | # chip = gpiod.chip("/dev/gpiochip4")
17 | chip = gpiod.chip("0")
18 | #cs_line = chip.get_line(19) # GPIO19
19 | #cs_line.request(consumer="SPI_CS", type=gpiod.LINE_REQ_DIR_OUT)
20 | cs_line = chip.get_line(cs_pin)
21 | cs_line_out = gpiod.line_request()
22 | cs_line_out.consumer = "SPI_CS"
23 | cs_line_out.request_type = gpiod.line_request.DIRECTION_OUTPUT
24 | cs_line.request(cs_line_out)
25 |
26 | #cs_line.request(consumer="SPI_CS", type=gpiod.line_request.DIRECTION_OUTPUT)
27 | cs_line.set_value(1) # Set CS high initially
28 |
29 |
30 | #button_line_1 = chip.get_line(button_pin_1)
31 | #button_line_1.request(consumer = "Button", type = gpiod.LINE_REQ_DIR_IN)
32 |
33 | line_1 = chip.get_line(button_pin_1)
34 |
35 | #line_2 = chip.get_line(button_pin_2)
36 |
37 | button_line_1 = gpiod.line_request()
38 | button_line_1.consumer = "Button"
39 | button_line_1.request_type = gpiod.line_request.DIRECTION_INPUT
40 | line_1.request(button_line_1)
41 |
42 | #button_line_2 = chip.get_line(button_pin_2)
43 | #button_line_2.request(consumer = "Button", type = gpiod.LINE_REQ_DIR_IN)
44 | #button_line_2 = gpiod.line_request()
45 | #button_line_2.consumer = "Button"
46 | #button_line_2.request_type = gpiod.line_request.DIRECTION_INPUT
47 | #line_2.request(button_line_2)
48 |
49 | spi = spidev.SpiDev()
50 | spi.open(0,0)
51 | spi.max_speed_hz = 4000000#600000
52 | spi.lsbfirst=False
53 | spi.mode=0b01
54 | spi.bits_per_word = 8
55 |
56 | spi_2 = spidev.SpiDev()
57 | spi_2.open(0,1)
58 | spi_2.max_speed_hz=4000000#600000
59 | spi_2.lsbfirst=False
60 | spi_2.mode=0b01
61 | spi_2.bits_per_word = 8
62 |
63 | who_i_am=0x00
64 | config1=0x01
65 | config2=0X02
66 | config3=0X03
67 |
68 | reset=0x06
69 | stop=0x0A
70 | start=0x08
71 | sdatac=0x11
72 | rdatac=0x10
73 | wakeup=0x02
74 | rdata = 0x12
75 |
76 | ch1set=0x05
77 | ch2set=0x06
78 | ch3set=0x07
79 | ch4set=0x08
80 | ch5set=0x09
81 | ch6set=0x0A
82 | ch7set=0x0B
83 | ch8set=0x0C
84 |
85 | data_test= 0x7FFFFF
86 | data_check=0xFFFFFF
87 |
88 | def read_byte(register):
89 | write=0x20
90 | register_write=write|register
91 | data = [register_write,0x00,register]
92 | read_reg=spi.xfer(data)
93 | print ("data", read_reg)
94 |
95 | def send_command(command):
96 | send_data = [command]
97 | com_reg=spi.xfer(send_data)
98 |
99 | def write_byte(register,data):
100 | write=0x40
101 | register_write=write|register
102 | data = [register_write,0x00,data]
103 | print (data)
104 | spi.xfer(data)
105 |
106 | def read_byte_2(register):
107 | write=0x20
108 | register_write=write|register
109 | data = [register_write,0x00,register]
110 | cs_line.set_value(0)
111 | read_reg=spi.xfer(data)
112 | cs_line.set_value(1)
113 | print ("data", read_reg)
114 |
115 | def send_command_2(command):
116 | send_data = [command]
117 | cs_line.set_value(0)
118 | spi_2.xfer(send_data)
119 | cs_line.set_value(1)
120 |
121 | def write_byte_2(register,data):
122 | write=0x40
123 | register_write=write|register
124 | data = [register_write,0x00,data]
125 | print (data)
126 |
127 | cs_line.set_value(0)
128 | spi_2.xfer(data)
129 | cs_line.set_value(1)
130 |
131 |
132 |
133 | send_command (wakeup)
134 | send_command (stop)
135 | send_command (reset)
136 | send_command (sdatac)
137 |
138 | write_byte (0x14, 0x80) #GPIO 80
139 | write_byte (config1, 0x96)
140 | write_byte (config2, 0xD4)
141 | write_byte (config3, 0xFF)
142 | write_byte (0x04, 0x00)
143 | write_byte (0x0D, 0x00)
144 | write_byte (0x0E, 0x00)
145 | write_byte (0x0F, 0x00)
146 | write_byte (0x10, 0x00)
147 | write_byte (0x11, 0x00)
148 | write_byte (0x15, 0x20)
149 | #
150 | write_byte (0x17, 0x00)
151 | write_byte (ch1set, 0x00)
152 | write_byte (ch2set, 0x00)
153 | write_byte (ch3set, 0x00)
154 | write_byte (ch4set, 0x00)
155 | write_byte (ch5set, 0x00)
156 | write_byte (ch6set, 0x00)
157 | write_byte (ch7set, 0x00)
158 | write_byte (ch8set, 0x00)
159 |
160 | send_command (rdatac)
161 | send_command (start)
162 |
163 |
164 | send_command_2 (wakeup)
165 | send_command_2 (stop)
166 | send_command_2 (reset)
167 | send_command_2 (sdatac)
168 |
169 | write_byte_2 (0x14, 0x80) #GPIO 80
170 | write_byte_2 (config1, 0x96)
171 | write_byte_2 (config2, 0xD4)
172 | write_byte_2 (config3, 0xFF)
173 | write_byte_2 (0x04, 0x00)
174 | write_byte_2 (0x0D, 0x00)
175 | write_byte_2 (0x0E, 0x00)
176 | write_byte_2 (0x0F, 0x00)
177 | write_byte_2 (0x10, 0x00)
178 | write_byte_2 (0x11, 0x00)
179 | write_byte_2 (0x15, 0x20)
180 | #
181 | write_byte_2 (0x17, 0x00)
182 | write_byte_2 (ch1set, 0x00)
183 | write_byte_2 (ch2set, 0x00)
184 | write_byte_2 (ch3set, 0x00)
185 | write_byte_2 (ch4set, 0x00)
186 | write_byte_2 (ch5set, 0x00)
187 | write_byte_2 (ch6set, 0x00)
188 | write_byte_2 (ch7set, 0x00)
189 | write_byte_2 (ch8set, 0x00)
190 |
191 | send_command_2 (rdatac)
192 | send_command_2 (start)
193 |
194 | DRDY=1
195 |
196 | result=[0]*27
197 | result_2=[0]*27
198 |
199 |
200 | data_1ch_test = []
201 | data_2ch_test = []
202 | data_3ch_test = []
203 | data_4ch_test = []
204 | data_5ch_test = []
205 | data_6ch_test = []
206 | data_7ch_test = []
207 | data_8ch_test = []
208 |
209 | data_9ch_test = []
210 | data_10ch_test = []
211 | data_11ch_test = []
212 | data_12ch_test = []
213 | data_13ch_test = []
214 | data_14ch_test = []
215 | data_15ch_test = []
216 | data_16ch_test = []
217 |
218 | axis_x=0
219 | y_minus_graph=100
220 | y_plus_graph=100
221 | x_minux_graph=5000
222 | x_plus_graph=250
223 | sample_len = 250
224 |
225 | fig, axis = plt.subplots(4, 4, figsize=(5, 5))
226 | plt.subplots_adjust(hspace=1)
227 | ch_name = 0
228 | ch_name_title = [1,5,2,6,3,7,4,8]
229 | axi = [(i, j) for i in range(4) for j in range(2)]
230 | for ax_row, ax_col in axi:
231 | axis[ax_row, ax_col].set_xlabel('Time')
232 | axis[ax_row, ax_col].set_ylabel('Amplitude')
233 | axis[ax_row, ax_col].set_title('Data after pass filter Ch-' + str(ch_name_title[ch_name]))
234 | ch_name = ch_name + 1
235 |
236 | test_DRDY = 5
237 | test_DRDY_2 = 5
238 | #1.2 Band-pass filter
239 | data_before = []
240 | data_after = []
241 | just_one_time = 0
242 | data_lenght_for_Filter = 2 # how much we read data for filter, all lenght [_____] + [_____] + [_____]
243 | read_data_lenght_one_time = 1 # for one time how much read [_____]
244 | sample_len = 250
245 | sample_lens = 250
246 | fps = 250
247 | highcut = 1
248 | lowcut = 10
249 | data_before_1 = data_before_2 = data_before_3 = data_before_4 = data_before_5 = data_before_6 = data_before_7 = data_before_8 = [0]*250
250 | data_before_9 = data_before_10 = data_before_11 = data_before_12 = data_before_13 = data_before_14 = data_before_15 = data_before_16 = [0]*250
251 |
252 | print (data_lenght_for_Filter*read_data_lenght_one_time-read_data_lenght_one_time)
253 |
254 | def butter_lowpass(cutoff, fs, order=5):
255 | nyq = 0.5 * fs
256 | normal_cutoff = cutoff / nyq
257 | b, a = signal.butter(order, normal_cutoff, btype='low', analog=False)
258 | return b, a
259 | def butter_lowpass_filter(data, cutoff, fs, order=5):
260 | b, a = butter_lowpass(cutoff, fs, order=order)
261 | y = signal.lfilter(b, a, data)
262 | return y
263 | def butter_highpass(cutoff, fs, order=3):
264 | nyq = 0.5 * fs
265 | normal_cutoff = cutoff / nyq
266 | b, a = signal.butter(order, normal_cutoff, btype='high', analog=False)
267 | return b, a
268 | def butter_highpass_filter(data, cutoff, fs, order=5):
269 | b, a = butter_highpass(cutoff, fs, order=order)
270 | y = signal.filtfilt(b, a, data)
271 | return y
272 |
273 | while 1:
274 |
275 |
276 | #print ("1", button_state)
277 | #print("2", button_state_2)
278 |
279 | #print ("ok3")
280 | button_state = line_1.get_value()
281 | #print (button_state)
282 | if button_state == 1:
283 | test_DRDY = 10
284 | if test_DRDY == 10 and button_state == 0:
285 | test_DRDY = 0
286 |
287 | output=spi.readbytes(27)
288 |
289 | cs_line.set_value(0)
290 | output_2=spi_2.readbytes(27)
291 | cs_line.set_value(1)
292 |
293 | # print (output[0],output[1],output[2])
294 | if output_2[0]==192 and output_2[1] == 0 and output_2[2] == 8:
295 | #print ("ok4")
296 | for a in range (3,25,3):
297 | voltage_1=(output[a]<<8)| output[a+1]
298 | voltage_1=(voltage_1<<8)| output[a+2]
299 | convert_voktage=voltage_1|data_test
300 | if convert_voktage==data_check:
301 | voltage_1_after_convert=(voltage_1-16777214)
302 | else:
303 | voltage_1_after_convert=voltage_1
304 | channel_num = (a/3)
305 |
306 | result[int (channel_num)]=round(1000000*4.5*(voltage_1_after_convert/16777215),2)
307 |
308 | data_1ch_test.append(result[1])
309 | data_2ch_test.append(result[2])
310 | data_3ch_test.append(result[3])
311 | data_4ch_test.append(result[4])
312 | data_5ch_test.append(result[5])
313 | data_6ch_test.append(result[6])
314 | data_7ch_test.append(result[7])
315 | data_8ch_test.append(result[8])
316 |
317 |
318 | for a in range (3,25,3):
319 | voltage_1=(output_2[a]<<8)| output_2[a+1]
320 | voltage_1=(voltage_1<<8)| output_2[a+2]
321 | convert_voktage=voltage_1|data_test
322 | if convert_voktage==data_check:
323 | voltage_1_after_convert=(voltage_1-16777214)
324 | else:
325 | voltage_1_after_convert=voltage_1
326 | channel_num = (a/3)
327 |
328 | result_2[int (channel_num)]=round(1000000*4.5*(voltage_1_after_convert/16777215),2)
329 |
330 | data_9ch_test.append(result_2[1])
331 | data_10ch_test.append(result_2[2])
332 | data_11ch_test.append(result_2[3])
333 | data_12ch_test.append(result_2[4])
334 | data_13ch_test.append(result_2[5])
335 | data_14ch_test.append(result_2[6])
336 | data_15ch_test.append(result_2[7])
337 | data_16ch_test.append(result_2[8])
338 |
339 |
340 |
341 |
342 | if len(data_9ch_test)==2000:
343 |
344 | data_dict = {
345 | 'data_1ch_test': data_1ch_test,
346 | 'data_2ch_test': data_2ch_test,
347 | 'data_3ch_test': data_3ch_test,
348 | 'data_4ch_test': data_4ch_test,
349 | 'data_5ch_test': data_5ch_test,
350 | 'data_6ch_test': data_6ch_test,
351 | 'data_7ch_test': data_7ch_test,
352 | 'data_8ch_test': data_8ch_test,
353 | 'data_9ch_test': data_9ch_test,
354 | 'data_10ch_test': data_10ch_test,
355 | 'data_11ch_test': data_11ch_test,
356 | 'data_12ch_test': data_12ch_test,
357 | 'data_13ch_test': data_13ch_test,
358 | 'data_14ch_test': data_14ch_test,
359 | 'data_15ch_test': data_15ch_test,
360 | 'data_16ch_test': data_16ch_test
361 | }
362 |
363 | df = pd.DataFrame(data_dict)
364 | df.to_excel("output3.xlsx", index=False)
365 | print (df)
366 |
367 |
368 | spi.close()
369 |
--------------------------------------------------------------------------------
/Save_data/arxive/pieeg16_os_save_data_2.py:
--------------------------------------------------------------------------------
1 | import spidev
2 | import time
3 | #from RPi import GPIO
4 | from matplotlib import pyplot as plt
5 | from scipy.ndimage import gaussian_filter1d
6 | from scipy import signal
7 | import gpiod
8 | import pandas as pd
9 |
10 | #GPIO.setwarnings(False)
11 | #GPIO.setmode(GPIO.BOARD)
12 |
13 | button_pin_1 = 26 #13
14 | button_pin_2 = 13
15 | cs_pin = 19
16 | #chip = gpiod.Chip("gpiochip4")
17 | print ("ok1")
18 | chip = gpiod.chip("/dev/gpiochip4")
19 | print ("ok2")
20 | #cs_line = chip.get_line(19) # GPIO19
21 | print ("ok3")
22 | #cs_line.request(consumer="SPI_CS", type=gpiod.LINE_REQ_DIR_OUT)
23 |
24 | print("ok4")
25 | cs_line = chip.get_line(cs_pin)
26 | print ("ok5")
27 | cs_line_out = gpiod.line_request()
28 | print ("ok6")
29 | cs_line_out.consumer = "SPI_CS"
30 | print ("ok7")
31 | cs_line_out.request_type = gpiod.line_request.DIRECTION_OUTPUT
32 | print ("ok8")
33 | cs_line.request(cs_line_out)
34 |
35 | #cs_line.request(consumer="SPI_CS", type=gpiod.line_request.DIRECTION_OUTPUT)
36 | print ("ok4")
37 | cs_line.set_value(1) # Set CS high initially
38 | print ("ok5")
39 |
40 | #button_line_1 = chip.get_line(button_pin_1)
41 | #button_line_1.request(consumer = "Button", type = gpiod.LINE_REQ_DIR_IN)
42 | line_1 = chip.get_line(button_pin_1)
43 | line_2 = chip.get_line(button_pin_2)
44 |
45 | button_line_1 = gpiod.line_request()
46 | button_line_1.consumer = "Button"
47 | button_line_1.request_type = gpiod.line_request.DIRECTION_INPUT
48 | line_1.request(button_line_1)
49 |
50 | #button_line_2 = chip.get_line(button_pin_2)
51 | #button_line_2.request(consumer = "Button", type = gpiod.LINE_REQ_DIR_IN)
52 | button_line_2 = gpiod.line_request()
53 | button_line_2.consumer = "Button"
54 | button_line_2.request_type = gpiod.line_request.DIRECTION_INPUT
55 | line_2.request(button_line_2)
56 |
57 | spi = spidev.SpiDev()
58 | spi.open(0,0)
59 | spi.max_speed_hz = 1000000 #600000 spi.max_speed_hz
60 | spi.lsbfirst=False
61 | spi.mode=0b01
62 | spi.bits_per_word = 8
63 |
64 | spi_2 = spidev.SpiDev()
65 | spi_2.open(0,1)
66 | spi_2.max_speed_hz=1000000 #600000
67 | spi_2.lsbfirst=False
68 | spi_2.mode=0b01
69 | spi_2.bits_per_word = 8
70 |
71 | who_i_am=0x00
72 | config1=0x01
73 | config2=0X02
74 | config3=0X03
75 |
76 | reset=0x06
77 | stop=0x0A
78 | start=0x08
79 | sdatac=0x11
80 | rdatac=0x10
81 | wakeup=0x02
82 | rdata = 0x12
83 |
84 | ch1set=0x05
85 | ch2set=0x06
86 | ch3set=0x07
87 | ch4set=0x08
88 | ch5set=0x09
89 | ch6set=0x0A
90 | ch7set=0x0B
91 | ch8set=0x0C
92 |
93 | data_test= 0x7FFFFF
94 | data_check=0xFFFFFF
95 |
96 | def read_byte(register):
97 | write=0x20
98 | register_write=write|register
99 | data = [register_write,0x00,register]
100 | read_reg=spi.xfer(data)
101 | print ("data", read_reg)
102 |
103 | def send_command(command):
104 | send_data = [command]
105 | com_reg=spi.xfer(send_data)
106 |
107 | def write_byte(register,data):
108 | write=0x40
109 | register_write=write|register
110 | data = [register_write,0x00,data]
111 | print (data)
112 | spi.xfer(data)
113 |
114 | def read_byte_2(register):
115 | write=0x20
116 | register_write=write|register
117 | data = [register_write,0x00,register]
118 | cs_line.set_value(0)
119 | read_reg=spi.xfer(data)
120 | cs_line.set_value(1)
121 | print ("data", read_reg)
122 |
123 | def send_command_2(command):
124 | send_data = [command]
125 | cs_line.set_value(0)
126 | spi_2.xfer(send_data)
127 | cs_line.set_value(1)
128 |
129 | def write_byte_2(register,data):
130 | write=0x40
131 | register_write=write|register
132 | data = [register_write,0x00,data]
133 | print (data)
134 |
135 | cs_line.set_value(0)
136 | spi_2.xfer(data)
137 | cs_line.set_value(1)
138 |
139 |
140 |
141 | send_command (wakeup)
142 | send_command (stop)
143 | send_command (reset)
144 | send_command (sdatac)
145 |
146 | write_byte (0x14, 0x80) #GPIO 80
147 | write_byte (config1, 0x96)
148 | write_byte (config2, 0xD4)
149 | write_byte (config3, 0xFF)
150 | write_byte (0x04, 0x00)
151 | write_byte (0x0D, 0x00)
152 | write_byte (0x0E, 0x00)
153 | write_byte (0x0F, 0x00)
154 | write_byte (0x10, 0x00)
155 | write_byte (0x11, 0x00)
156 | write_byte (0x15, 0x20)
157 | #
158 | write_byte (0x17, 0x00)
159 | write_byte (ch1set, 0x01)
160 | write_byte (ch2set, 0x01)
161 | write_byte (ch3set, 0x01)
162 | write_byte (ch4set, 0x01)
163 | write_byte (ch5set, 0x01)
164 | write_byte (ch6set, 0x01)
165 | write_byte (ch7set, 0x01)
166 | write_byte (ch8set, 0x01)
167 |
168 | send_command (rdatac)
169 | send_command (start)
170 |
171 |
172 | send_command_2 (wakeup)
173 | send_command_2 (stop)
174 | send_command_2 (reset)
175 | send_command_2 (sdatac)
176 |
177 | write_byte_2 (0x14, 0x80) #GPIO 80
178 | write_byte_2 (config1, 0x96)
179 | write_byte_2 (config2, 0xD4)
180 | write_byte_2 (config3, 0xFF)
181 | write_byte_2 (0x04, 0x00)
182 | write_byte_2 (0x0D, 0x00)
183 | write_byte_2 (0x0E, 0x00)
184 | write_byte_2 (0x0F, 0x00)
185 | write_byte_2 (0x10, 0x00)
186 | write_byte_2 (0x11, 0x00)
187 | write_byte_2 (0x15, 0x20)
188 | #
189 | write_byte_2 (0x17, 0x00)
190 | write_byte_2 (ch1set, 0x01)
191 | write_byte_2 (ch2set, 0x01)
192 | write_byte_2 (ch3set, 0x01)
193 | write_byte_2 (ch4set, 0x01)
194 | write_byte_2 (ch5set, 0x01)
195 | write_byte_2 (ch6set, 0x01)
196 | write_byte_2 (ch7set, 0x01)
197 | write_byte_2 (ch8set, 0x01)
198 |
199 | send_command_2 (rdatac)
200 | send_command_2 (start)
201 |
202 | DRDY=1
203 |
204 | result=[0]*27
205 | result_2=[0]*27
206 |
207 |
208 | data_1ch_test = []
209 | data_2ch_test = []
210 | data_3ch_test = []
211 | data_4ch_test = []
212 | data_5ch_test = []
213 | data_6ch_test = []
214 | data_7ch_test = []
215 | data_8ch_test = []
216 |
217 | data_9ch_test = []
218 | data_10ch_test = []
219 | data_11ch_test = []
220 | data_12ch_test = []
221 | data_13ch_test = []
222 | data_14ch_test = []
223 | data_15ch_test = []
224 | data_16ch_test = []
225 |
226 | axis_x=0
227 | y_minus_graph=100
228 | y_plus_graph=100
229 | x_minux_graph=5000
230 | x_plus_graph=250
231 | sample_len = 250
232 |
233 | fig, axis = plt.subplots(4, 4, figsize=(5, 5))
234 | plt.subplots_adjust(hspace=1)
235 | ch_name = 0
236 | ch_name_title = [1,5,2,6,3,7,4,8]
237 | axi = [(i, j) for i in range(4) for j in range(2)]
238 | for ax_row, ax_col in axi:
239 | axis[ax_row, ax_col].set_xlabel('Time')
240 | axis[ax_row, ax_col].set_ylabel('Amplitude')
241 | axis[ax_row, ax_col].set_title('Data after pass filter Ch-' + str(ch_name_title[ch_name]))
242 | ch_name = ch_name + 1
243 |
244 | test_DRDY = 5
245 | test_DRDY_2 = 5
246 | #1.2 Band-pass filter
247 | data_before = []
248 | data_after = []
249 | just_one_time = 0
250 | data_lenght_for_Filter = 2 # how much we read data for filter, all lenght [_____] + [_____] + [_____]
251 | read_data_lenght_one_time = 1 # for one time how much read [_____]
252 | sample_len = 250
253 | sample_lens = 250
254 | fps = 250
255 | highcut = 1
256 | lowcut = 10
257 | data_before_1 = data_before_2 = data_before_3 = data_before_4 = data_before_5 = data_before_6 = data_before_7 = data_before_8 = [0]*250
258 | data_before_9 = data_before_10 = data_before_11 = data_before_12 = data_before_13 = data_before_14 = data_before_15 = data_before_16 = [0]*250
259 |
260 | print (data_lenght_for_Filter*read_data_lenght_one_time-read_data_lenght_one_time)
261 |
262 | def butter_lowpass(cutoff, fs, order=5):
263 | nyq = 0.5 * fs
264 | normal_cutoff = cutoff / nyq
265 | b, a = signal.butter(order, normal_cutoff, btype='low', analog=False)
266 | return b, a
267 | def butter_lowpass_filter(data, cutoff, fs, order=5):
268 | b, a = butter_lowpass(cutoff, fs, order=order)
269 | y = signal.lfilter(b, a, data)
270 | return y
271 | def butter_highpass(cutoff, fs, order=3):
272 | nyq = 0.5 * fs
273 | normal_cutoff = cutoff / nyq
274 | b, a = signal.butter(order, normal_cutoff, btype='high', analog=False)
275 | return b, a
276 | def butter_highpass_filter(data, cutoff, fs, order=5):
277 | b, a = butter_highpass(cutoff, fs, order=order)
278 | y = signal.filtfilt(b, a, data)
279 | return y
280 |
281 | while 1:
282 |
283 | #print ("1", button_state)
284 | #print("2", button_state_2)
285 |
286 | #print ("ok3")
287 | button_state = line_1.get_value()
288 | button_state_2 = line_2.get_value()
289 | #print (button_state)
290 |
291 | if button_state == 1:
292 | test_DRDY = 10
293 |
294 | if test_DRDY == 10 and button_state == 0:
295 | test_DRDY = 0
296 | #time.sleep(0.001)
297 | #time.sleep(0.000001)
298 | output=spi.readbytes(27)
299 |
300 | cs_line.set_value(0)
301 | #time.sleep(0.001)
302 | output_2=spi_2.readbytes(27)
303 | cs_line.set_value(1)
304 |
305 | # print (output[0],output[1],output[2])
306 |
307 | if output_2[0]==192 and output_2[1] == 0 and output_2[2] == 8 and output[0]==192 and output[1] == 0 and output[2]==8:
308 | #print ("ok4")
309 | for a in range (3,25,3):
310 | voltage_1=(output[a]<<8)| output[a+1]
311 | voltage_1=(voltage_1<<8)| output[a+2]
312 | convert_voktage=voltage_1|data_test
313 | if convert_voktage==data_check:
314 | voltage_1_after_convert=(voltage_1-16777214)
315 | else:
316 | voltage_1_after_convert=voltage_1
317 | channel_num = (a/3)
318 |
319 | result[int (channel_num)]=round(1000000*4.5*(voltage_1_after_convert/16777215),2)
320 |
321 | data_1ch_test.append(result[1])
322 | data_2ch_test.append(result[2])
323 | data_3ch_test.append(result[3])
324 | data_4ch_test.append(result[4])
325 | data_5ch_test.append(result[5])
326 | data_6ch_test.append(result[6])
327 | data_7ch_test.append(result[7])
328 | data_8ch_test.append(result[8])
329 |
330 |
331 | for a in range (3,25,3):
332 | voltage_1=(output_2[a]<<8)| output_2[a+1]
333 | voltage_1=(voltage_1<<8)| output_2[a+2]
334 | convert_voktage=voltage_1|data_test
335 | if convert_voktage==data_check:
336 | voltage_1_after_convert=(voltage_1-16777214)
337 | else:
338 | voltage_1_after_convert=voltage_1
339 | channel_num = (a/3)
340 |
341 | result_2[int (channel_num)]=round(1000000*4.5*(voltage_1_after_convert/16777215),2)
342 |
343 | data_9ch_test.append(result_2[1])
344 | data_10ch_test.append(result_2[2])
345 | data_11ch_test.append(result_2[3])
346 | data_12ch_test.append(result_2[4])
347 | data_13ch_test.append(result_2[5])
348 | data_14ch_test.append(result_2[6])
349 | data_15ch_test.append(result_2[7])
350 | data_16ch_test.append(result_2[8])
351 |
352 |
353 |
354 |
355 | if len(data_9ch_test)==2000:
356 | if len(data_9ch_test)==2000:
357 |
358 | data_dict = {
359 | 'data_1ch_test': data_1ch_test,
360 | 'data_2ch_test': data_2ch_test,
361 | 'data_3ch_test': data_3ch_test,
362 | 'data_4ch_test': data_4ch_test,
363 | 'data_5ch_test': data_5ch_test,
364 | 'data_6ch_test': data_6ch_test,
365 | 'data_7ch_test': data_7ch_test,
366 | 'data_8ch_test': data_8ch_test,
367 | 'data_9ch_test': data_9ch_test,
368 | 'data_10ch_test': data_10ch_test,
369 | 'data_11ch_test': data_11ch_test,
370 | 'data_12ch_test': data_12ch_test,
371 | 'data_13ch_test': data_13ch_test,
372 | 'data_14ch_test': data_14ch_test,
373 | 'data_15ch_test': data_15ch_test,
374 | 'data_16ch_test': data_16ch_test
375 | }
376 |
377 | df = pd.DataFrame(data_dict)
378 | df.to_excel("pieeg_datset.xlsx", index=False)
379 | print (df)
380 |
381 |
382 | spi.close()
383 |
--------------------------------------------------------------------------------
/GUI/1.Graph_Gpio_D _1_6_3.py:
--------------------------------------------------------------------------------
1 | import spidev
2 | import time
3 | from RPi import GPIO
4 | GPIO.setwarnings(False)
5 | GPIO.setmode(GPIO.BOARD)
6 | from gpiozero import LED,Button
7 | from matplotlib import pyplot as plt
8 | #sw1 = Button(26,pull_up=True)# 37
9 | #from gpiozero import LED,Button
10 | from scipy.ndimage import gaussian_filter1d
11 | from scipy import signal
12 | import gpiod
13 | from time import sleep
14 |
15 | button_pin_1 = 26 #13
16 | button_pin_2 = 13
17 | chip = gpiod.Chip("gpiochip4")
18 |
19 | cs_line = chip.get_line(19) # GPIO19
20 | cs_line.request(consumer="SPI_CS", type=gpiod.LINE_REQ_DIR_OUT)
21 | cs_line.set_value(1) # Set CS high initially
22 |
23 | button_line_1 = chip.get_line(button_pin_1)
24 | button_line_1.request(consumer = "Button", type = gpiod.LINE_REQ_DIR_IN)
25 |
26 |
27 | button_line_2 = chip.get_line(button_pin_2)
28 | button_line_2.request(consumer = "Button", type = gpiod.LINE_REQ_DIR_IN)
29 |
30 | spi = spidev.SpiDev()
31 |
32 | spi.open(0,0)
33 | spi.max_speed_hz = 4000000#600000
34 | spi.lsbfirst=False
35 | spi.mode=0b01
36 | spi.bits_per_word = 8
37 |
38 | spi_2 = spidev.SpiDev()
39 |
40 | spi_2.open(0,1)
41 | spi_2.max_speed_hz=4000000#600000
42 | spi_2.lsbfirst=False
43 | spi_2.mode=0b01
44 | spi_2.bits_per_word = 8
45 |
46 | who_i_am=0x00
47 | config1=0x01
48 | config2=0X02
49 | config3=0X03
50 |
51 | reset=0x06
52 | stop=0x0A
53 | start=0x08
54 | sdatac=0x11
55 | rdatac=0x10
56 | wakeup=0x02
57 | rdata = 0x12
58 |
59 | ch1set=0x05
60 | ch2set=0x06
61 | ch3set=0x07
62 | ch4set=0x08
63 | ch5set=0x09
64 | ch6set=0x0A
65 | ch7set=0x0B
66 | ch8set=0x0C
67 |
68 | data_test= 0x7FFFFF
69 | data_check=0xFFFFFF
70 |
71 | def read_byte(register):
72 | write=0x20
73 | register_write=write|register
74 | data = [register_write,0x00,register]
75 | read_reg=spi.xfer(data)
76 | print ("data", read_reg)
77 |
78 | def send_command(command):
79 | send_data = [command]
80 | com_reg=spi.xfer(send_data)
81 |
82 | def write_byte(register,data):
83 | write=0x40
84 | register_write=write|register
85 | data = [register_write,0x00,data]
86 | print (data)
87 | spi.xfer(data)
88 |
89 | def read_byte_2(register):
90 | write=0x20
91 | register_write=write|register
92 | data = [register_write,0x00,register]
93 | cs_line.set_value(0)
94 | read_reg=spi.xfer(data)
95 | cs_line.set_value(1)
96 | print ("data", read_reg)
97 |
98 | def send_command_2(command):
99 | send_data = [command]
100 | cs_line.set_value(0)
101 | spi_2.xfer(send_data)
102 | cs_line.set_value(1)
103 |
104 | def write_byte_2(register,data):
105 | write=0x40
106 | register_write=write|register
107 | data = [register_write,0x00,data]
108 | print (data)
109 |
110 | cs_line.set_value(0)
111 | spi_2.xfer(data)
112 | cs_line.set_value(1)
113 |
114 |
115 |
116 | send_command (wakeup)
117 | send_command (stop)
118 | send_command (reset)
119 | send_command (sdatac)
120 |
121 | write_byte (0x14, 0x80) #GPIO 80
122 | write_byte (config1, 0x96)
123 | write_byte (config2, 0xD4)
124 | write_byte (config3, 0xFF)
125 | write_byte (0x04, 0x00)
126 | write_byte (0x0D, 0x00)
127 | write_byte (0x0E, 0x00)
128 | write_byte (0x0F, 0x00)
129 | write_byte (0x10, 0x00)
130 | write_byte (0x11, 0x00)
131 | write_byte (0x15, 0x20)
132 | #
133 | write_byte (0x17, 0x00)
134 | write_byte (ch1set, 0x00)
135 | write_byte (ch2set, 0x00)
136 | write_byte (ch3set, 0x00)
137 | write_byte (ch4set, 0x00)
138 | write_byte (ch5set, 0x00)
139 | write_byte (ch6set, 0x00)
140 | write_byte (ch7set, 0x00)
141 | write_byte (ch8set, 0x00)
142 |
143 | send_command (rdatac)
144 | send_command (start)
145 |
146 |
147 | send_command_2 (wakeup)
148 | send_command_2 (stop)
149 | send_command_2 (reset)
150 | send_command_2 (sdatac)
151 |
152 | write_byte_2 (0x14, 0x80) #GPIO 80
153 | write_byte_2 (config1, 0x96)
154 | write_byte_2 (config2, 0xD4)
155 | write_byte_2 (config3, 0xFF)
156 | write_byte_2 (0x04, 0x00)
157 | write_byte_2 (0x0D, 0x00)
158 | write_byte_2 (0x0E, 0x00)
159 | write_byte_2 (0x0F, 0x00)
160 | write_byte_2 (0x10, 0x00)
161 | write_byte_2 (0x11, 0x00)
162 | write_byte_2 (0x15, 0x20)
163 | #
164 | write_byte_2 (0x17, 0x00)
165 | write_byte_2 (ch1set, 0x00)
166 | write_byte_2 (ch2set, 0x00)
167 | write_byte_2 (ch3set, 0x00)
168 | write_byte_2 (ch4set, 0x00)
169 | write_byte_2 (ch5set, 0x00)
170 | write_byte_2 (ch6set, 0x00)
171 | write_byte_2 (ch7set, 0x00)
172 | write_byte_2 (ch8set, 0x00)
173 |
174 | send_command_2 (rdatac)
175 | send_command_2 (start)
176 |
177 | DRDY=1
178 |
179 | result=[0]*27
180 | result_2=[0]*27
181 |
182 |
183 | data_1ch_test = []
184 | data_2ch_test = []
185 | data_3ch_test = []
186 | data_4ch_test = []
187 | data_5ch_test = []
188 | data_6ch_test = []
189 | data_7ch_test = []
190 | data_8ch_test = []
191 |
192 | data_9ch_test = []
193 | data_10ch_test = []
194 | data_11ch_test = []
195 | data_12ch_test = []
196 | data_13ch_test = []
197 | data_14ch_test = []
198 | data_15ch_test = []
199 | data_16ch_test = []
200 |
201 | axis_x=0
202 | y_minus_graph=100
203 | y_plus_graph=100
204 | x_minux_graph=5000
205 | x_plus_graph=250
206 | sample_len = 250
207 |
208 | fig, axis = plt.subplots(4, 4, figsize=(5, 5))
209 | plt.subplots_adjust(hspace=1)
210 | ch_name = 0
211 | ch_name_title = [1,5,2,6,3,7,4,8]
212 | axi = [(i, j) for i in range(4) for j in range(2)]
213 | for ax_row, ax_col in axi:
214 | axis[ax_row, ax_col].set_xlabel('Time')
215 | axis[ax_row, ax_col].set_ylabel('Amplitude')
216 | axis[ax_row, ax_col].set_title('Data after pass filter Ch-' + str(ch_name_title[ch_name]))
217 | ch_name = ch_name + 1
218 |
219 | test_DRDY = 5
220 | test_DRDY_2 = 5
221 | #1.2 Band-pass filter
222 | data_before = []
223 | data_after = []
224 | just_one_time = 0
225 | data_lenght_for_Filter = 2 # how much we read data for filter, all lenght [_____] + [_____] + [_____]
226 | read_data_lenght_one_time = 1 # for one time how much read [_____]
227 | sample_len = 250
228 | sample_lens = 250
229 | fps = 250
230 | highcut = 1
231 | lowcut = 10
232 | data_before_1 = data_before_2 = data_before_3 = data_before_4 = data_before_5 = data_before_6 = data_before_7 = data_before_8 = [0]*250
233 | data_before_9 = data_before_10 = data_before_11 = data_before_12 = data_before_13 = data_before_14 = data_before_15 = data_before_16 = [0]*250
234 |
235 | print (data_lenght_for_Filter*read_data_lenght_one_time-read_data_lenght_one_time)
236 |
237 | def butter_lowpass(cutoff, fs, order=5):
238 | nyq = 0.5 * fs
239 | normal_cutoff = cutoff / nyq
240 | b, a = signal.butter(order, normal_cutoff, btype='low', analog=False)
241 | return b, a
242 | def butter_lowpass_filter(data, cutoff, fs, order=5):
243 | b, a = butter_lowpass(cutoff, fs, order=order)
244 | y = signal.lfilter(b, a, data)
245 | return y
246 | def butter_highpass(cutoff, fs, order=3):
247 | nyq = 0.5 * fs
248 | normal_cutoff = cutoff / nyq
249 | b, a = signal.butter(order, normal_cutoff, btype='high', analog=False)
250 | return b, a
251 | def butter_highpass_filter(data, cutoff, fs, order=5):
252 | b, a = butter_highpass(cutoff, fs, order=order)
253 | y = signal.filtfilt(b, a, data)
254 | return y
255 |
256 | while 1:
257 |
258 |
259 | #print ("1", button_state)
260 | #print("2", button_state_2)
261 |
262 | #print ("ok3")
263 | button_state = button_line_1.get_value()
264 | #print (button_state)
265 | if button_state == 1:
266 | test_DRDY = 10
267 | if test_DRDY == 10 and button_state == 0:
268 | test_DRDY = 0
269 |
270 | output=spi.readbytes(27)
271 |
272 | cs_line.set_value(0)
273 | output_2=spi_2.readbytes(27)
274 | cs_line.set_value(1)
275 |
276 | # print (output[0],output[1],output[2])
277 | if output_2[0]==192 and output_2[1] == 0 and output_2[2] == 8:
278 | #print ("ok4")
279 | for a in range (3,25,3):
280 | voltage_1=(output[a]<<8)| output[a+1]
281 | voltage_1=(voltage_1<<8)| output[a+2]
282 | convert_voktage=voltage_1|data_test
283 | if convert_voktage==data_check:
284 | voltage_1_after_convert=(voltage_1-16777214)
285 | else:
286 | voltage_1_after_convert=voltage_1
287 | channel_num = (a/3)
288 |
289 | result[int (channel_num)]=round(1000000*4.5*(voltage_1_after_convert/16777215),2)
290 |
291 | data_1ch_test.append(result[1])
292 | data_2ch_test.append(result[2])
293 | data_3ch_test.append(result[3])
294 | data_4ch_test.append(result[4])
295 | data_5ch_test.append(result[5])
296 | data_6ch_test.append(result[6])
297 | data_7ch_test.append(result[7])
298 | data_8ch_test.append(result[8])
299 |
300 |
301 | for a in range (3,25,3):
302 | voltage_1=(output_2[a]<<8)| output_2[a+1]
303 | voltage_1=(voltage_1<<8)| output_2[a+2]
304 | convert_voktage=voltage_1|data_test
305 | if convert_voktage==data_check:
306 | voltage_1_after_convert=(voltage_1-16777214)
307 | else:
308 | voltage_1_after_convert=voltage_1
309 | channel_num = (a/3)
310 |
311 | result_2[int (channel_num)]=round(1000000*4.5*(voltage_1_after_convert/16777215),2)
312 |
313 | data_9ch_test.append(result_2[1])
314 | data_10ch_test.append(result_2[2])
315 | data_11ch_test.append(result_2[3])
316 | data_12ch_test.append(result_2[4])
317 | data_13ch_test.append(result_2[5])
318 | data_14ch_test.append(result_2[6])
319 | data_15ch_test.append(result_2[7])
320 | data_16ch_test.append(result_2[8])
321 |
322 |
323 |
324 |
325 | if len(data_9ch_test)==sample_len:
326 |
327 |
328 | data_after_1 = data_1ch_test
329 | dataset_1 = data_before_1 + data_after_1
330 | data_before_1 = dataset_1[250:]
331 | data_for_graph_1 = dataset_1
332 |
333 | data_filt_numpy_high_1 = butter_highpass_filter(data_for_graph_1, highcut, fps)
334 | data_for_graph_1 = butter_lowpass_filter(data_filt_numpy_high_1, lowcut, fps)
335 |
336 | axis[0,0].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_1[250:], color = '#0a0b0c')
337 | axis[0,0].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_1[50]-y_minus_graph, data_for_graph_1[150]+y_plus_graph])
338 |
339 | # 2
340 | data_after_2 = data_2ch_test
341 | dataset_2 = data_before_2 + data_after_2
342 | data_before_2 = dataset_2[250:]
343 | data_for_graph_2 = dataset_2
344 |
345 | data_filt_numpy_high_2 = butter_highpass_filter(data_for_graph_2, highcut, fps)
346 | data_for_graph_2 = butter_lowpass_filter(data_filt_numpy_high_2, lowcut, fps)
347 |
348 | axis[1,0].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_2[250:], color = '#0a0b0c')
349 | axis[1,0].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_2[50]-y_minus_graph, data_for_graph_2[150]+y_plus_graph])
350 |
351 | # 3
352 | data_after_3 = data_3ch_test
353 | dataset_3 = data_before_3 + data_after_3
354 | data_before_3 = dataset_3[250:]
355 | data_for_graph_3 = dataset_3
356 |
357 | data_filt_numpy_high_3 = butter_highpass_filter(data_for_graph_3, highcut, fps)
358 | data_for_graph_3 = butter_lowpass_filter(data_filt_numpy_high_3, lowcut, fps)
359 |
360 | axis[2,0].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_3[250:], color = '#0a0b0c')
361 | axis[2,0].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_3[50]-y_minus_graph, data_for_graph_3[150]+y_plus_graph])
362 |
363 | # 4
364 | data_after_4 = data_4ch_test
365 | dataset_4 = data_before_4 + data_after_4
366 | data_before_4 = dataset_4[250:]
367 | data_for_graph_4 = dataset_4
368 |
369 | data_filt_numpy_high_4 = butter_highpass_filter(data_for_graph_4, highcut, fps)
370 | data_for_graph_4 = butter_lowpass_filter(data_filt_numpy_high_4, lowcut, fps)
371 |
372 | axis[3,0].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_4[250:], color = '#0a0b0c')
373 | axis[3,0].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_4[50]-y_minus_graph, data_for_graph_4[150]+y_plus_graph])
374 |
375 | #5
376 | data_after_5 = data_5ch_test
377 | dataset_5 = data_before_5 + data_after_5
378 | data_before_5 = dataset_5[250:]
379 | data_for_graph_5 = dataset_5
380 |
381 | data_filt_numpy_high_5 = butter_highpass_filter(data_for_graph_5, highcut, fps)
382 | data_for_graph_5 = butter_lowpass_filter(data_filt_numpy_high_5, lowcut, fps)
383 |
384 | axis[0,1].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_5[250:], color = '#0a0b0c')
385 | axis[0,1].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_5[50]-y_minus_graph, data_for_graph_5[150]+y_plus_graph])
386 |
387 | #6
388 | data_after_6 = data_6ch_test
389 | dataset_6 = data_before_6 + data_after_6
390 | data_before_6 = dataset_6[250:]
391 | data_for_graph_6 = dataset_6
392 |
393 | data_filt_numpy_high_6 = butter_highpass_filter(data_for_graph_6, highcut, fps)
394 | data_for_graph_6 = butter_lowpass_filter(data_filt_numpy_high_6, lowcut, fps)
395 |
396 | axis[1,1].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_6[250:], color = '#0a0b0c')
397 | axis[1,1].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_6[50]-y_minus_graph, data_for_graph_6[150]+y_plus_graph])
398 |
399 | #7
400 | data_after_7 = data_7ch_test
401 | dataset_7 = data_before_7 + data_after_7
402 | data_before_7 = dataset_7[250:]
403 | data_for_graph_7 = dataset_7
404 |
405 | data_filt_numpy_high_7 = butter_highpass_filter(data_for_graph_7, highcut, fps)
406 | data_for_graph_7 = butter_lowpass_filter(data_filt_numpy_high_7, lowcut, fps)
407 |
408 | axis[2,1].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_7[250:], color = '#0a0b0c')
409 | axis[2,1].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_7[50]-y_minus_graph, data_for_graph_7[150]+y_plus_graph])
410 |
411 | #8
412 | data_after_8 = data_8ch_test
413 | dataset_8 = data_before_8 + data_after_8
414 | data_before_8 = dataset_8[250:]
415 | data_for_graph_8 = dataset_8
416 |
417 | data_filt_numpy_high_8 = butter_highpass_filter(data_for_graph_8, highcut, fps)
418 | data_for_graph_8 = butter_lowpass_filter(data_filt_numpy_high_8, lowcut, fps)
419 |
420 | axis[3,1].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_8[250:], color = '#0a0b0c')
421 | axis[3,1].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_8[50]-y_minus_graph, data_for_graph_8[150]+y_plus_graph])
422 |
423 | # 9
424 | data_after_9 = data_9ch_test
425 | dataset_9 = data_before_9 + data_after_9
426 | data_before_9 = dataset_9[250:]
427 | data_for_graph_9 = dataset_9
428 |
429 | data_filt_numpy_high_9 = butter_highpass_filter(data_for_graph_9, highcut, fps)
430 | data_for_graph_9 = butter_lowpass_filter(data_filt_numpy_high_9, lowcut, fps)
431 |
432 | axis[0,2].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_9[250:], color = '#0a0b0c')
433 | axis[0,2].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_9[50]-y_minus_graph, data_for_graph_9[150]+y_plus_graph])
434 |
435 | # 10
436 | data_after_10 = data_10ch_test
437 | dataset_10 = data_before_10 + data_after_10
438 | data_before_10 = dataset_10[250:]
439 | data_for_graph_10 = dataset_10
440 |
441 | data_filt_numpy_high_10 = butter_highpass_filter(data_for_graph_10, highcut, fps)
442 | data_for_graph_10 = butter_lowpass_filter(data_filt_numpy_high_10, lowcut, fps)
443 |
444 | axis[1,2].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_10[250:], color = '#0a0b0c')
445 | axis[1,2].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_10[50]-y_minus_graph, data_for_graph_10[150]+y_plus_graph])
446 |
447 | # 11
448 | data_after_11 = data_11ch_test
449 | dataset_11 = data_before_11 + data_after_11
450 | data_before_11 = dataset_11[250:]
451 | data_for_graph_11 = dataset_11
452 |
453 | data_filt_numpy_high_11 = butter_highpass_filter(data_for_graph_11, highcut, fps)
454 | data_for_graph_11 = butter_lowpass_filter(data_filt_numpy_high_11, lowcut, fps)
455 |
456 | axis[2,2].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_11[250:], color = '#0a0b0c')
457 | axis[2,2].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_11[50]-y_minus_graph, data_for_graph_11[150]+y_plus_graph])
458 |
459 | # 12
460 | data_after_12 = data_12ch_test
461 | dataset_12 = data_before_12 + data_after_12
462 | data_before_12 = dataset_12[250:]
463 | data_for_graph_12 = dataset_12
464 |
465 | data_filt_numpy_high_12 = butter_highpass_filter(data_for_graph_12, highcut, fps)
466 | data_for_graph_12 = butter_lowpass_filter(data_filt_numpy_high_12, lowcut, fps)
467 |
468 | axis[3,2].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_12[250:], color = '#0a0b0c')
469 | axis[3,2].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_12[50]-y_minus_graph, data_for_graph_12[150]+y_plus_graph])
470 |
471 | # 13
472 | data_after_13 = data_13ch_test
473 | dataset_13 = data_before_13 + data_after_13
474 | data_before_13 = dataset_13[250:]
475 | data_for_graph_13 = dataset_13
476 |
477 | data_filt_numpy_high_13 = butter_highpass_filter(data_for_graph_13, highcut, fps)
478 | data_for_graph_13 = butter_lowpass_filter(data_filt_numpy_high_13, lowcut, fps)
479 |
480 | axis[0,3].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_13[250:], color = '#0a0b0c')
481 | axis[0,3].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_13[50]-y_minus_graph, data_for_graph_13[150]+y_plus_graph])
482 |
483 | # 14
484 | data_after_14 = data_14ch_test
485 | dataset_14 = data_before_14 + data_after_14
486 | data_before_14 = dataset_14[250:]
487 | data_for_graph_14 = dataset_14
488 |
489 | data_filt_numpy_high_14 = butter_highpass_filter(data_for_graph_14, highcut, fps)
490 | data_for_graph_14 = butter_lowpass_filter(data_filt_numpy_high_14, lowcut, fps)
491 |
492 | axis[1,3].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_14[250:], color = '#0a0b0c')
493 | axis[1,3].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_14[50]-y_minus_graph, data_for_graph_14[150]+y_plus_graph])
494 |
495 | # 15
496 | data_after_15 = data_15ch_test
497 | dataset_15 = data_before_15 + data_after_15
498 | data_before_15 = dataset_15[250:]
499 | data_for_graph_15 = dataset_15
500 |
501 | data_filt_numpy_high_15 = butter_highpass_filter(data_for_graph_15, highcut, fps)
502 | data_for_graph_15 = butter_lowpass_filter(data_filt_numpy_high_15, lowcut, fps)
503 |
504 | axis[2,3].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_15[250:], color = '#0a0b0c')
505 | axis[2,3].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_15[50]-y_minus_graph, data_for_graph_15[150]+y_plus_graph])
506 |
507 | # 16
508 | data_after_16 = data_16ch_test
509 | dataset_16 = data_before_16 + data_after_16
510 | data_before_16 = dataset_16[250:]
511 | data_for_graph_16 = dataset_16
512 |
513 | data_filt_numpy_high_16 = butter_highpass_filter(data_for_graph_16, highcut, fps)
514 | data_for_graph_16 = butter_lowpass_filter(data_filt_numpy_high_16, lowcut, fps)
515 |
516 | axis[3,3].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_16[250:], color = '#0a0b0c')
517 | axis[3,3].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_16[50]-y_minus_graph, data_for_graph_16[150]+y_plus_graph])
518 |
519 |
520 | plt.pause(0.0000000000001)
521 |
522 | axis_x=axis_x+sample_lens
523 | data_1ch_test = []
524 | data_2ch_test = []
525 | data_3ch_test = []
526 | data_4ch_test = []
527 | data_5ch_test = []
528 | data_6ch_test = []
529 | data_7ch_test = []
530 | data_8ch_test = []
531 | data_9ch_test = []
532 | data_10ch_test = []
533 | data_11ch_test = []
534 | data_12ch_test = []
535 | data_13ch_test = []
536 | data_14ch_test = []
537 | data_15ch_test = []
538 | data_16ch_test = []
539 |
540 |
541 | spi.close()
542 |
543 |
544 |
545 |
546 |
547 |
548 |
--------------------------------------------------------------------------------
/GUI/2.Graph_Gpio_D _1_5_4.py:
--------------------------------------------------------------------------------
1 | import spidev
2 | import time
3 | #from RPi import GPIO
4 | from matplotlib import pyplot as plt
5 | from scipy.ndimage import gaussian_filter1d
6 | from scipy import signal
7 | import gpiod
8 |
9 | #GPIO.setwarnings(False)
10 | #GPIO.setmode(GPIO.BOARD)
11 |
12 | button_pin_1 = 26 #13
13 | button_pin_2 = 13
14 | cs_pin = 19
15 | #chip = gpiod.Chip("gpiochip4")
16 | # chip = gpiod.chip("/dev/gpiochip4")
17 | chip = gpiod.chip("0")
18 | #cs_line = chip.get_line(19) # GPIO19
19 | #cs_line.request(consumer="SPI_CS", type=gpiod.LINE_REQ_DIR_OUT)
20 | cs_line = chip.get_line(cs_pin)
21 | cs_line_out = gpiod.line_request()
22 | cs_line_out.consumer = "SPI_CS"
23 | cs_line_out.request_type = gpiod.line_request.DIRECTION_OUTPUT
24 | cs_line.request(cs_line_out)
25 |
26 | #cs_line.request(consumer="SPI_CS", type=gpiod.line_request.DIRECTION_OUTPUT)
27 | cs_line.set_value(1) # Set CS high initially
28 |
29 |
30 | #button_line_1 = chip.get_line(button_pin_1)
31 | #button_line_1.request(consumer = "Button", type = gpiod.LINE_REQ_DIR_IN)
32 |
33 | line_1 = chip.get_line(button_pin_1)
34 |
35 | #line_2 = chip.get_line(button_pin_2)
36 |
37 | button_line_1 = gpiod.line_request()
38 | button_line_1.consumer = "Button"
39 | button_line_1.request_type = gpiod.line_request.DIRECTION_INPUT
40 | line_1.request(button_line_1)
41 |
42 | #button_line_2 = chip.get_line(button_pin_2)
43 | #button_line_2.request(consumer = "Button", type = gpiod.LINE_REQ_DIR_IN)
44 | #button_line_2 = gpiod.line_request()
45 | #button_line_2.consumer = "Button"
46 | #button_line_2.request_type = gpiod.line_request.DIRECTION_INPUT
47 | #line_2.request(button_line_2)
48 |
49 | spi = spidev.SpiDev()
50 | spi.open(0,0)
51 | spi.max_speed_hz = 4000000#600000
52 | spi.lsbfirst=False
53 | spi.mode=0b01
54 | spi.bits_per_word = 8
55 |
56 | spi_2 = spidev.SpiDev()
57 | spi_2.open(0,1)
58 | spi_2.max_speed_hz=4000000#600000
59 | spi_2.lsbfirst=False
60 | spi_2.mode=0b01
61 | spi_2.bits_per_word = 8
62 |
63 | who_i_am=0x00
64 | config1=0x01
65 | config2=0X02
66 | config3=0X03
67 |
68 | reset=0x06
69 | stop=0x0A
70 | start=0x08
71 | sdatac=0x11
72 | rdatac=0x10
73 | wakeup=0x02
74 | rdata = 0x12
75 |
76 | ch1set=0x05
77 | ch2set=0x06
78 | ch3set=0x07
79 | ch4set=0x08
80 | ch5set=0x09
81 | ch6set=0x0A
82 | ch7set=0x0B
83 | ch8set=0x0C
84 |
85 | data_test= 0x7FFFFF
86 | data_check=0xFFFFFF
87 |
88 | def read_byte(register):
89 | write=0x20
90 | register_write=write|register
91 | data = [register_write,0x00,register]
92 | read_reg=spi.xfer(data)
93 | print ("data", read_reg)
94 |
95 | def send_command(command):
96 | send_data = [command]
97 | com_reg=spi.xfer(send_data)
98 |
99 | def write_byte(register,data):
100 | write=0x40
101 | register_write=write|register
102 | data = [register_write,0x00,data]
103 | print (data)
104 | spi.xfer(data)
105 |
106 | def read_byte_2(register):
107 | write=0x20
108 | register_write=write|register
109 | data = [register_write,0x00,register]
110 | cs_line.set_value(0)
111 | read_reg=spi.xfer(data)
112 | cs_line.set_value(1)
113 | print ("data", read_reg)
114 |
115 | def send_command_2(command):
116 | send_data = [command]
117 | cs_line.set_value(0)
118 | spi_2.xfer(send_data)
119 | cs_line.set_value(1)
120 |
121 | def write_byte_2(register,data):
122 | write=0x40
123 | register_write=write|register
124 | data = [register_write,0x00,data]
125 | print (data)
126 |
127 | cs_line.set_value(0)
128 | spi_2.xfer(data)
129 | cs_line.set_value(1)
130 |
131 |
132 |
133 | send_command (wakeup)
134 | send_command (stop)
135 | send_command (reset)
136 | send_command (sdatac)
137 |
138 | write_byte (0x14, 0x80) #GPIO 80
139 | write_byte (config1, 0x96)
140 | write_byte (config2, 0xD4)
141 | write_byte (config3, 0xFF)
142 | write_byte (0x04, 0x00)
143 | write_byte (0x0D, 0x00)
144 | write_byte (0x0E, 0x00)
145 | write_byte (0x0F, 0x00)
146 | write_byte (0x10, 0x00)
147 | write_byte (0x11, 0x00)
148 | write_byte (0x15, 0x20)
149 | #
150 | write_byte (0x17, 0x00)
151 | write_byte (ch1set, 0x00)
152 | write_byte (ch2set, 0x00)
153 | write_byte (ch3set, 0x00)
154 | write_byte (ch4set, 0x00)
155 | write_byte (ch5set, 0x00)
156 | write_byte (ch6set, 0x00)
157 | write_byte (ch7set, 0x00)
158 | write_byte (ch8set, 0x00)
159 |
160 | send_command (rdatac)
161 | send_command (start)
162 |
163 |
164 | send_command_2 (wakeup)
165 | send_command_2 (stop)
166 | send_command_2 (reset)
167 | send_command_2 (sdatac)
168 |
169 | write_byte_2 (0x14, 0x80) #GPIO 80
170 | write_byte_2 (config1, 0x96)
171 | write_byte_2 (config2, 0xD4)
172 | write_byte_2 (config3, 0xFF)
173 | write_byte_2 (0x04, 0x00)
174 | write_byte_2 (0x0D, 0x00)
175 | write_byte_2 (0x0E, 0x00)
176 | write_byte_2 (0x0F, 0x00)
177 | write_byte_2 (0x10, 0x00)
178 | write_byte_2 (0x11, 0x00)
179 | write_byte_2 (0x15, 0x20)
180 | #
181 | write_byte_2 (0x17, 0x00)
182 | write_byte_2 (ch1set, 0x00)
183 | write_byte_2 (ch2set, 0x00)
184 | write_byte_2 (ch3set, 0x00)
185 | write_byte_2 (ch4set, 0x00)
186 | write_byte_2 (ch5set, 0x00)
187 | write_byte_2 (ch6set, 0x00)
188 | write_byte_2 (ch7set, 0x00)
189 | write_byte_2 (ch8set, 0x00)
190 |
191 | send_command_2 (rdatac)
192 | send_command_2 (start)
193 |
194 | DRDY=1
195 |
196 | result=[0]*27
197 | result_2=[0]*27
198 |
199 |
200 | data_1ch_test = []
201 | data_2ch_test = []
202 | data_3ch_test = []
203 | data_4ch_test = []
204 | data_5ch_test = []
205 | data_6ch_test = []
206 | data_7ch_test = []
207 | data_8ch_test = []
208 |
209 | data_9ch_test = []
210 | data_10ch_test = []
211 | data_11ch_test = []
212 | data_12ch_test = []
213 | data_13ch_test = []
214 | data_14ch_test = []
215 | data_15ch_test = []
216 | data_16ch_test = []
217 |
218 | axis_x=0
219 | y_minus_graph=100
220 | y_plus_graph=100
221 | x_minux_graph=5000
222 | x_plus_graph=250
223 | sample_len = 250
224 |
225 | fig, axis = plt.subplots(4, 4, figsize=(5, 5))
226 | plt.subplots_adjust(hspace=1)
227 | ch_name = 0
228 | ch_name_title = [1,5,2,6,3,7,4,8]
229 | axi = [(i, j) for i in range(4) for j in range(2)]
230 | for ax_row, ax_col in axi:
231 | axis[ax_row, ax_col].set_xlabel('Time')
232 | axis[ax_row, ax_col].set_ylabel('Amplitude')
233 | axis[ax_row, ax_col].set_title('Data after pass filter Ch-' + str(ch_name_title[ch_name]))
234 | ch_name = ch_name + 1
235 |
236 | test_DRDY = 5
237 | test_DRDY_2 = 5
238 | #1.2 Band-pass filter
239 | data_before = []
240 | data_after = []
241 | just_one_time = 0
242 | data_lenght_for_Filter = 2 # how much we read data for filter, all lenght [_____] + [_____] + [_____]
243 | read_data_lenght_one_time = 1 # for one time how much read [_____]
244 | sample_len = 250
245 | sample_lens = 250
246 | fps = 250
247 | highcut = 1
248 | lowcut = 10
249 | data_before_1 = data_before_2 = data_before_3 = data_before_4 = data_before_5 = data_before_6 = data_before_7 = data_before_8 = [0]*250
250 | data_before_9 = data_before_10 = data_before_11 = data_before_12 = data_before_13 = data_before_14 = data_before_15 = data_before_16 = [0]*250
251 |
252 | print (data_lenght_for_Filter*read_data_lenght_one_time-read_data_lenght_one_time)
253 |
254 | def butter_lowpass(cutoff, fs, order=5):
255 | nyq = 0.5 * fs
256 | normal_cutoff = cutoff / nyq
257 | b, a = signal.butter(order, normal_cutoff, btype='low', analog=False)
258 | return b, a
259 | def butter_lowpass_filter(data, cutoff, fs, order=5):
260 | b, a = butter_lowpass(cutoff, fs, order=order)
261 | y = signal.lfilter(b, a, data)
262 | return y
263 | def butter_highpass(cutoff, fs, order=3):
264 | nyq = 0.5 * fs
265 | normal_cutoff = cutoff / nyq
266 | b, a = signal.butter(order, normal_cutoff, btype='high', analog=False)
267 | return b, a
268 | def butter_highpass_filter(data, cutoff, fs, order=5):
269 | b, a = butter_highpass(cutoff, fs, order=order)
270 | y = signal.filtfilt(b, a, data)
271 | return y
272 |
273 | while 1:
274 |
275 |
276 | #print ("1", button_state)
277 | #print("2", button_state_2)
278 |
279 | #print ("ok3")
280 | button_state = line_1.get_value()
281 | #print (button_state)
282 | if button_state == 1:
283 | test_DRDY = 10
284 | if test_DRDY == 10 and button_state == 0:
285 | test_DRDY = 0
286 |
287 | output=spi.readbytes(27)
288 |
289 | cs_line.set_value(0)
290 | output_2=spi_2.readbytes(27)
291 | cs_line.set_value(1)
292 |
293 | # print (output[0],output[1],output[2])
294 | if output_2[0]==192 and output_2[1] == 0 and output_2[2] == 8:
295 | #print ("ok4")
296 | for a in range (3,25,3):
297 | voltage_1=(output[a]<<8)| output[a+1]
298 | voltage_1=(voltage_1<<8)| output[a+2]
299 | convert_voktage=voltage_1|data_test
300 | if convert_voktage==data_check:
301 | voltage_1_after_convert=(voltage_1-16777214)
302 | else:
303 | voltage_1_after_convert=voltage_1
304 | channel_num = (a/3)
305 |
306 | result[int (channel_num)]=round(1000000*4.5*(voltage_1_after_convert/16777215),2)
307 |
308 | data_1ch_test.append(result[1])
309 | data_2ch_test.append(result[2])
310 | data_3ch_test.append(result[3])
311 | data_4ch_test.append(result[4])
312 | data_5ch_test.append(result[5])
313 | data_6ch_test.append(result[6])
314 | data_7ch_test.append(result[7])
315 | data_8ch_test.append(result[8])
316 |
317 |
318 | for a in range (3,25,3):
319 | voltage_1=(output_2[a]<<8)| output_2[a+1]
320 | voltage_1=(voltage_1<<8)| output_2[a+2]
321 | convert_voktage=voltage_1|data_test
322 | if convert_voktage==data_check:
323 | voltage_1_after_convert=(voltage_1-16777214)
324 | else:
325 | voltage_1_after_convert=voltage_1
326 | channel_num = (a/3)
327 |
328 | result_2[int (channel_num)]=round(1000000*4.5*(voltage_1_after_convert/16777215),2)
329 |
330 | data_9ch_test.append(result_2[1])
331 | data_10ch_test.append(result_2[2])
332 | data_11ch_test.append(result_2[3])
333 | data_12ch_test.append(result_2[4])
334 | data_13ch_test.append(result_2[5])
335 | data_14ch_test.append(result_2[6])
336 | data_15ch_test.append(result_2[7])
337 | data_16ch_test.append(result_2[8])
338 |
339 |
340 |
341 |
342 | if len(data_9ch_test)==sample_len:
343 |
344 |
345 | data_after_1 = data_1ch_test
346 | dataset_1 = data_before_1 + data_after_1
347 | data_before_1 = dataset_1[250:]
348 | data_for_graph_1 = dataset_1
349 |
350 | data_filt_numpy_high_1 = butter_highpass_filter(data_for_graph_1, highcut, fps)
351 | data_for_graph_1 = butter_lowpass_filter(data_filt_numpy_high_1, lowcut, fps)
352 |
353 | axis[0,0].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_1[250:], color = '#0a0b0c')
354 | axis[0,0].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_1[50]-y_minus_graph, data_for_graph_1[150]+y_plus_graph])
355 |
356 | # 2
357 | data_after_2 = data_2ch_test
358 | dataset_2 = data_before_2 + data_after_2
359 | data_before_2 = dataset_2[250:]
360 | data_for_graph_2 = dataset_2
361 |
362 | data_filt_numpy_high_2 = butter_highpass_filter(data_for_graph_2, highcut, fps)
363 | data_for_graph_2 = butter_lowpass_filter(data_filt_numpy_high_2, lowcut, fps)
364 |
365 | axis[1,0].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_2[250:], color = '#0a0b0c')
366 | axis[1,0].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_2[50]-y_minus_graph, data_for_graph_2[150]+y_plus_graph])
367 |
368 | # 3
369 | data_after_3 = data_3ch_test
370 | dataset_3 = data_before_3 + data_after_3
371 | data_before_3 = dataset_3[250:]
372 | data_for_graph_3 = dataset_3
373 |
374 | data_filt_numpy_high_3 = butter_highpass_filter(data_for_graph_3, highcut, fps)
375 | data_for_graph_3 = butter_lowpass_filter(data_filt_numpy_high_3, lowcut, fps)
376 |
377 | axis[2,0].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_3[250:], color = '#0a0b0c')
378 | axis[2,0].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_3[50]-y_minus_graph, data_for_graph_3[150]+y_plus_graph])
379 |
380 | # 4
381 | data_after_4 = data_4ch_test
382 | dataset_4 = data_before_4 + data_after_4
383 | data_before_4 = dataset_4[250:]
384 | data_for_graph_4 = dataset_4
385 |
386 | data_filt_numpy_high_4 = butter_highpass_filter(data_for_graph_4, highcut, fps)
387 | data_for_graph_4 = butter_lowpass_filter(data_filt_numpy_high_4, lowcut, fps)
388 |
389 | axis[3,0].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_4[250:], color = '#0a0b0c')
390 | axis[3,0].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_4[50]-y_minus_graph, data_for_graph_4[150]+y_plus_graph])
391 |
392 | #5
393 | data_after_5 = data_5ch_test
394 | dataset_5 = data_before_5 + data_after_5
395 | data_before_5 = dataset_5[250:]
396 | data_for_graph_5 = dataset_5
397 |
398 | data_filt_numpy_high_5 = butter_highpass_filter(data_for_graph_5, highcut, fps)
399 | data_for_graph_5 = butter_lowpass_filter(data_filt_numpy_high_5, lowcut, fps)
400 |
401 | axis[0,1].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_5[250:], color = '#0a0b0c')
402 | axis[0,1].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_5[50]-y_minus_graph, data_for_graph_5[150]+y_plus_graph])
403 |
404 | #6
405 | data_after_6 = data_6ch_test
406 | dataset_6 = data_before_6 + data_after_6
407 | data_before_6 = dataset_6[250:]
408 | data_for_graph_6 = dataset_6
409 |
410 | data_filt_numpy_high_6 = butter_highpass_filter(data_for_graph_6, highcut, fps)
411 | data_for_graph_6 = butter_lowpass_filter(data_filt_numpy_high_6, lowcut, fps)
412 |
413 | axis[1,1].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_6[250:], color = '#0a0b0c')
414 | axis[1,1].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_6[50]-y_minus_graph, data_for_graph_6[150]+y_plus_graph])
415 |
416 | #7
417 | data_after_7 = data_7ch_test
418 | dataset_7 = data_before_7 + data_after_7
419 | data_before_7 = dataset_7[250:]
420 | data_for_graph_7 = dataset_7
421 |
422 | data_filt_numpy_high_7 = butter_highpass_filter(data_for_graph_7, highcut, fps)
423 | data_for_graph_7 = butter_lowpass_filter(data_filt_numpy_high_7, lowcut, fps)
424 |
425 | axis[2,1].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_7[250:], color = '#0a0b0c')
426 | axis[2,1].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_7[50]-y_minus_graph, data_for_graph_1[150]+y_plus_graph])
427 |
428 | #8
429 | data_after_8 = data_8ch_test
430 | dataset_8 = data_before_8 + data_after_8
431 | data_before_8 = dataset_8[250:]
432 | data_for_graph_8 = dataset_8
433 |
434 | data_filt_numpy_high_8 = butter_highpass_filter(data_for_graph_8, highcut, fps)
435 | data_for_graph_8 = butter_lowpass_filter(data_filt_numpy_high_8, lowcut, fps)
436 |
437 | axis[3,1].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_8[250:], color = '#0a0b0c')
438 | axis[3,1].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_8[50]-y_minus_graph, data_for_graph_8[150]+y_plus_graph])
439 |
440 | # 9
441 | data_after_9 = data_9ch_test
442 | dataset_9 = data_before_9 + data_after_9
443 | data_before_9 = dataset_9[250:]
444 | data_for_graph_9 = dataset_9
445 |
446 | data_filt_numpy_high_9 = butter_highpass_filter(data_for_graph_9, highcut, fps)
447 | data_for_graph_9 = butter_lowpass_filter(data_filt_numpy_high_9, lowcut, fps)
448 |
449 | axis[0,2].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_9[250:], color = '#0a0b0c')
450 | axis[0,2].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_9[50]-y_minus_graph, data_for_graph_9[150]+y_plus_graph])
451 |
452 | # 10
453 | data_after_10 = data_10ch_test
454 | dataset_10 = data_before_10 + data_after_10
455 | data_before_10 = dataset_10[250:]
456 | data_for_graph_10 = dataset_10
457 |
458 | data_filt_numpy_high_10 = butter_highpass_filter(data_for_graph_10, highcut, fps)
459 | data_for_graph_10 = butter_lowpass_filter(data_filt_numpy_high_10, lowcut, fps)
460 |
461 | axis[1,2].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_10[250:], color = '#0a0b0c')
462 | axis[1,2].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_10[50]-y_minus_graph, data_for_graph_10[150]+y_plus_graph])
463 |
464 | # 11
465 | data_after_11 = data_11ch_test
466 | dataset_11 = data_before_11 + data_after_11
467 | data_before_11 = dataset_11[250:]
468 | data_for_graph_11 = dataset_11
469 |
470 | data_filt_numpy_high_11 = butter_highpass_filter(data_for_graph_11, highcut, fps)
471 | data_for_graph_11 = butter_lowpass_filter(data_filt_numpy_high_11, lowcut, fps)
472 |
473 | axis[2,2].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_11[250:], color = '#0a0b0c')
474 | axis[2,2].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_11[50]-y_minus_graph, data_for_graph_11[150]+y_plus_graph])
475 |
476 | # 12
477 | data_after_12 = data_12ch_test
478 | dataset_12 = data_before_12 + data_after_12
479 | data_before_12 = dataset_12[250:]
480 | data_for_graph_12 = dataset_12
481 |
482 | data_filt_numpy_high_12 = butter_highpass_filter(data_for_graph_12, highcut, fps)
483 | data_for_graph_12 = butter_lowpass_filter(data_filt_numpy_high_12, lowcut, fps)
484 |
485 | axis[3,2].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_12[250:], color = '#0a0b0c')
486 | axis[3,2].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_12[50]-y_minus_graph, data_for_graph_12[150]+y_plus_graph])
487 |
488 | # 13
489 | data_after_13 = data_13ch_test
490 | dataset_13 = data_before_13 + data_after_13
491 | data_before_13 = dataset_13[250:]
492 | data_for_graph_13 = dataset_13
493 |
494 | data_filt_numpy_high_13 = butter_highpass_filter(data_for_graph_13, highcut, fps)
495 | data_for_graph_13 = butter_lowpass_filter(data_filt_numpy_high_13, lowcut, fps)
496 |
497 | axis[0,3].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_13[250:], color = '#0a0b0c')
498 | axis[0,3].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_13[50]-y_minus_graph, data_for_graph_13[150]+y_plus_graph])
499 |
500 | # 14
501 | data_after_14 = data_14ch_test
502 | dataset_14 = data_before_14 + data_after_14
503 | data_before_14 = dataset_14[250:]
504 | data_for_graph_14 = dataset_14
505 |
506 | data_filt_numpy_high_14 = butter_highpass_filter(data_for_graph_14, highcut, fps)
507 | data_for_graph_14 = butter_lowpass_filter(data_filt_numpy_high_14, lowcut, fps)
508 |
509 | axis[1,3].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_14[250:], color = '#0a0b0c')
510 | axis[1,3].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_14[50]-y_minus_graph, data_for_graph_14[150]+y_plus_graph])
511 |
512 | # 15
513 | data_after_15 = data_15ch_test
514 | dataset_15 = data_before_15 + data_after_15
515 | data_before_15 = dataset_15[250:]
516 | data_for_graph_15 = dataset_15
517 |
518 | data_filt_numpy_high_15 = butter_highpass_filter(data_for_graph_15, highcut, fps)
519 | data_for_graph_15 = butter_lowpass_filter(data_filt_numpy_high_15, lowcut, fps)
520 |
521 | axis[2,3].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_15[250:], color = '#0a0b0c')
522 | axis[2,3].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_15[50]-y_minus_graph, data_for_graph_15[150]+y_plus_graph])
523 |
524 | # 16
525 | data_after_16 = data_16ch_test
526 | dataset_16 = data_before_16 + data_after_16
527 | data_before_16 = dataset_16[250:]
528 | data_for_graph_16 = dataset_16
529 |
530 | data_filt_numpy_high_16 = butter_highpass_filter(data_for_graph_16, highcut, fps)
531 | data_for_graph_16 = butter_lowpass_filter(data_filt_numpy_high_16, lowcut, fps)
532 |
533 | axis[3,3].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_16[250:], color = '#0a0b0c')
534 | axis[3,3].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_16[50]-y_minus_graph, data_for_graph_16[150]+y_plus_graph])
535 |
536 |
537 | plt.pause(0.0000000000001)
538 |
539 | axis_x=axis_x+sample_lens
540 | data_1ch_test = []
541 | data_2ch_test = []
542 | data_3ch_test = []
543 | data_4ch_test = []
544 | data_5ch_test = []
545 | data_6ch_test = []
546 | data_7ch_test = []
547 | data_8ch_test = []
548 | data_9ch_test = []
549 | data_10ch_test = []
550 | data_11ch_test = []
551 | data_12ch_test = []
552 | data_13ch_test = []
553 | data_14ch_test = []
554 | data_15ch_test = []
555 | data_16ch_test = []
556 |
557 |
558 | spi.close()
559 |
--------------------------------------------------------------------------------
/GUI/2.Graph_Gpio_D _1_5_4_OS.py:
--------------------------------------------------------------------------------
1 | import spidev
2 | import time
3 | #from RPi import GPIO
4 | from matplotlib import pyplot as plt
5 | from scipy.ndimage import gaussian_filter1d
6 | from scipy import signal
7 | import gpiod
8 |
9 | #GPIO.setwarnings(False)
10 | #GPIO.setmode(GPIO.BOARD)
11 |
12 | button_pin_1 = 26 #13
13 | button_pin_2 = 13
14 | cs_pin = 19
15 | #chip = gpiod.Chip("gpiochip4")
16 |
17 | chip = gpiod.chip("/dev/gpiochip4")
18 |
19 | #cs_line = chip.get_line(19) # GPIO19
20 |
21 | #cs_line.request(consumer="SPI_CS", type=gpiod.LINE_REQ_DIR_OUT)
22 |
23 |
24 | cs_line = chip.get_line(cs_pin)
25 |
26 | cs_line_out = gpiod.line_request()
27 |
28 | cs_line_out.consumer = "SPI_CS"
29 |
30 | cs_line_out.request_type = gpiod.line_request.DIRECTION_OUTPUT
31 |
32 | cs_line.request(cs_line_out)
33 |
34 | #cs_line.request(consumer="SPI_CS", type=gpiod.line_request.DIRECTION_OUTPUT)
35 |
36 | cs_line.set_value(1) # Set CS high initially
37 |
38 |
39 |
40 | #button_line_1 = chip.get_line(button_pin_1)
41 | #button_line_1.request(consumer = "Button", type = gpiod.LINE_REQ_DIR_IN)
42 | line_1 = chip.get_line(button_pin_1)
43 | line_2 = chip.get_line(button_pin_2)
44 |
45 | button_line_1 = gpiod.line_request()
46 | button_line_1.consumer = "Button"
47 | button_line_1.request_type = gpiod.line_request.DIRECTION_INPUT
48 | line_1.request(button_line_1)
49 |
50 | #button_line_2 = chip.get_line(button_pin_2)
51 | #button_line_2.request(consumer = "Button", type = gpiod.LINE_REQ_DIR_IN)
52 | #button_line_2 = gpiod.line_request()
53 | #button_line_2.consumer = "Button"
54 | #button_line_2.request_type = gpiod.line_request.DIRECTION_INPUT
55 | #line_2.request(button_line_2)
56 |
57 | spi = spidev.SpiDev()
58 | spi.open(0,0)
59 | spi.max_speed_hz = 1000000#4000000
60 | spi.lsbfirst=False
61 | spi.mode=0b01
62 | spi.bits_per_word = 8
63 |
64 | spi_2 = spidev.SpiDev()
65 | spi_2.open(0,1)
66 | spi_2.max_speed_hz= 1000000#400000
67 | spi_2.lsbfirst=False
68 | spi_2.mode=0b01
69 | spi_2.bits_per_word = 8
70 |
71 | who_i_am=0x00
72 | config1=0x01
73 | config2=0X02
74 | config3=0X03
75 |
76 | reset=0x06
77 | stop=0x0A
78 | start=0x08
79 | sdatac=0x11
80 | rdatac=0x10
81 | wakeup=0x02
82 | rdata = 0x12
83 |
84 | ch1set=0x05
85 | ch2set=0x06
86 | ch3set=0x07
87 | ch4set=0x08
88 | ch5set=0x09
89 | ch6set=0x0A
90 | ch7set=0x0B
91 | ch8set=0x0C
92 |
93 | data_test= 0x7FFFFF
94 | data_check=0xFFFFFF
95 |
96 | def read_byte(register):
97 | write=0x20
98 | register_write=write|register
99 | data = [register_write,0x00,register]
100 | read_reg=spi.xfer(data)
101 | print ("data", read_reg)
102 |
103 | def send_command(command):
104 | send_data = [command]
105 | com_reg=spi.xfer(send_data)
106 |
107 | def write_byte(register,data):
108 | write=0x40
109 | register_write=write|register
110 | data = [register_write,0x00,data]
111 | print (data)
112 | spi.xfer(data)
113 |
114 | def read_byte_2(register):
115 | write=0x20
116 | register_write=write|register
117 | data = [register_write,0x00,register]
118 | cs_line.set_value(0)
119 | read_reg=spi.xfer(data)
120 | cs_line.set_value(1)
121 | print ("data", read_reg)
122 |
123 | def send_command_2(command):
124 | send_data = [command]
125 | cs_line.set_value(0)
126 | spi_2.xfer(send_data)
127 | cs_line.set_value(1)
128 |
129 | def write_byte_2(register,data):
130 | write=0x40
131 | register_write=write|register
132 | data = [register_write,0x00,data]
133 | print (data)
134 |
135 | cs_line.set_value(0)
136 | spi_2.xfer(data)
137 | cs_line.set_value(1)
138 |
139 |
140 |
141 | send_command (wakeup)
142 | send_command (stop)
143 | send_command (reset)
144 | send_command (sdatac)
145 |
146 | write_byte (0x14, 0x80) #GPIO 80
147 | write_byte (config1, 0x96)
148 | write_byte (config2, 0xD4)
149 | write_byte (config3, 0xFF)
150 | write_byte (0x04, 0x00)
151 | write_byte (0x0D, 0x00)
152 | write_byte (0x0E, 0x00)
153 | write_byte (0x0F, 0x00)
154 | write_byte (0x10, 0x00)
155 | write_byte (0x11, 0x00)
156 | write_byte (0x15, 0x20)
157 | #
158 | write_byte (0x17, 0x00)
159 | write_byte (ch1set, 0x00)
160 | write_byte (ch2set, 0x00)
161 | write_byte (ch3set, 0x00)
162 | write_byte (ch4set, 0x00)
163 | write_byte (ch5set, 0x00)
164 | write_byte (ch6set, 0x00)
165 | write_byte (ch7set, 0x00)
166 | write_byte (ch8set, 0x00)
167 |
168 | send_command (rdatac)
169 | send_command (start)
170 |
171 |
172 | send_command_2 (wakeup)
173 | send_command_2 (stop)
174 | send_command_2 (reset)
175 | send_command_2 (sdatac)
176 |
177 | write_byte_2 (0x14, 0x80) #GPIO 80
178 | write_byte_2 (config1, 0x96)
179 | write_byte_2 (config2, 0xD4)
180 | write_byte_2 (config3, 0xFF)
181 | write_byte_2 (0x04, 0x00)
182 | write_byte_2 (0x0D, 0x00)
183 | write_byte_2 (0x0E, 0x00)
184 | write_byte_2 (0x0F, 0x00)
185 | write_byte_2 (0x10, 0x00)
186 | write_byte_2 (0x11, 0x00)
187 | write_byte_2 (0x15, 0x20)
188 | #
189 | write_byte_2 (0x17, 0x00)
190 | write_byte_2 (ch1set, 0x00)
191 | write_byte_2 (ch2set, 0x00)
192 | write_byte_2 (ch3set, 0x00)
193 | write_byte_2 (ch4set, 0x00)
194 | write_byte_2 (ch5set, 0x00)
195 | write_byte_2 (ch6set, 0x00)
196 | write_byte_2 (ch7set, 0x00)
197 | write_byte_2 (ch8set, 0x00)
198 |
199 | send_command_2 (rdatac)
200 | send_command_2 (start)
201 |
202 | DRDY=1
203 |
204 | result=[0]*27
205 | result_2=[0]*27
206 |
207 |
208 | data_1ch_test = []
209 | data_2ch_test = []
210 | data_3ch_test = []
211 | data_4ch_test = []
212 | data_5ch_test = []
213 | data_6ch_test = []
214 | data_7ch_test = []
215 | data_8ch_test = []
216 |
217 | data_9ch_test = []
218 | data_10ch_test = []
219 | data_11ch_test = []
220 | data_12ch_test = []
221 | data_13ch_test = []
222 | data_14ch_test = []
223 | data_15ch_test = []
224 | data_16ch_test = []
225 |
226 | axis_x=0
227 | y_minus_graph=100
228 | y_plus_graph=100
229 | x_minux_graph=5000
230 | x_plus_graph=250
231 | sample_len = 250
232 |
233 | fig, axis = plt.subplots(4, 4, figsize=(5, 5))
234 | plt.subplots_adjust(hspace=1)
235 | ch_name = 0
236 | ch_name_title = [1,5,2,6,3,7,4,8]
237 | axi = [(i, j) for i in range(4) for j in range(2)]
238 | for ax_row, ax_col in axi:
239 | axis[ax_row, ax_col].set_xlabel('Time')
240 | axis[ax_row, ax_col].set_ylabel('Amplitude')
241 | axis[ax_row, ax_col].set_title('Data after pass filter Ch-' + str(ch_name_title[ch_name]))
242 | ch_name = ch_name + 1
243 |
244 | test_DRDY = 5
245 | test_DRDY_2 = 5
246 | #1.2 Band-pass filter
247 | data_before = []
248 | data_after = []
249 | just_one_time = 0
250 | data_lenght_for_Filter = 2 # how much we read data for filter, all lenght [_____] + [_____] + [_____]
251 | read_data_lenght_one_time = 1 # for one time how much read [_____]
252 | sample_len = 250
253 | sample_lens = 250
254 | fps = 250
255 | highcut = 1
256 | lowcut = 10
257 | data_before_1 = data_before_2 = data_before_3 = data_before_4 = data_before_5 = data_before_6 = data_before_7 = data_before_8 = [0]*250
258 | data_before_9 = data_before_10 = data_before_11 = data_before_12 = data_before_13 = data_before_14 = data_before_15 = data_before_16 = [0]*250
259 |
260 | print (data_lenght_for_Filter*read_data_lenght_one_time-read_data_lenght_one_time)
261 |
262 | def butter_lowpass(cutoff, fs, order=5):
263 | nyq = 0.5 * fs
264 | normal_cutoff = cutoff / nyq
265 | b, a = signal.butter(order, normal_cutoff, btype='low', analog=False)
266 | return b, a
267 | def butter_lowpass_filter(data, cutoff, fs, order=5):
268 | b, a = butter_lowpass(cutoff, fs, order=order)
269 | y = signal.lfilter(b, a, data)
270 | return y
271 | def butter_highpass(cutoff, fs, order=3):
272 | nyq = 0.5 * fs
273 | normal_cutoff = cutoff / nyq
274 | b, a = signal.butter(order, normal_cutoff, btype='high', analog=False)
275 | return b, a
276 | def butter_highpass_filter(data, cutoff, fs, order=5):
277 | b, a = butter_highpass(cutoff, fs, order=order)
278 | y = signal.filtfilt(b, a, data)
279 | return y
280 |
281 | while 1:
282 |
283 |
284 | #print ("1", button_state)
285 | #print("2", button_state_2)
286 |
287 | #print ("ok3")
288 | button_state = line_1.get_value()
289 | #print (button_state)
290 | if button_state == 1:
291 | test_DRDY = 10
292 | if test_DRDY == 10 and button_state == 0:
293 | test_DRDY = 0
294 |
295 | output=spi.readbytes(27)
296 |
297 | cs_line.set_value(0)
298 | output_2=spi_2.readbytes(27)
299 | cs_line.set_value(1)
300 |
301 | # print (output[0],output[1],output[2])
302 | if output_2[0]==192 and output_2[1] == 0 and output_2[2] == 8:
303 | #print ("ok4")
304 | for a in range (3,25,3):
305 | voltage_1=(output[a]<<8)| output[a+1]
306 | voltage_1=(voltage_1<<8)| output[a+2]
307 | convert_voktage=voltage_1|data_test
308 | if convert_voktage==data_check:
309 | voltage_1_after_convert=(voltage_1-16777214)
310 | else:
311 | voltage_1_after_convert=voltage_1
312 | channel_num = (a/3)
313 |
314 | result[int (channel_num)]=round(1000000*4.5*(voltage_1_after_convert/16777215),2)
315 |
316 | data_1ch_test.append(result[1])
317 | data_2ch_test.append(result[2])
318 | data_3ch_test.append(result[3])
319 | data_4ch_test.append(result[4])
320 | data_5ch_test.append(result[5])
321 | data_6ch_test.append(result[6])
322 | data_7ch_test.append(result[7])
323 | data_8ch_test.append(result[8])
324 |
325 |
326 | for a in range (3,25,3):
327 | voltage_1=(output_2[a]<<8)| output_2[a+1]
328 | voltage_1=(voltage_1<<8)| output_2[a+2]
329 | convert_voktage=voltage_1|data_test
330 | if convert_voktage==data_check:
331 | voltage_1_after_convert=(voltage_1-16777214)
332 | else:
333 | voltage_1_after_convert=voltage_1
334 | channel_num = (a/3)
335 |
336 | result_2[int (channel_num)]=round(1000000*4.5*(voltage_1_after_convert/16777215),2)
337 |
338 | data_9ch_test.append(result_2[1])
339 | data_10ch_test.append(result_2[2])
340 | data_11ch_test.append(result_2[3])
341 | data_12ch_test.append(result_2[4])
342 | data_13ch_test.append(result_2[5])
343 | data_14ch_test.append(result_2[6])
344 | data_15ch_test.append(result_2[7])
345 | data_16ch_test.append(result_2[8])
346 |
347 |
348 |
349 |
350 | if len(data_9ch_test)==sample_len:
351 |
352 |
353 | data_after_1 = data_1ch_test
354 | dataset_1 = data_before_1 + data_after_1
355 | data_before_1 = dataset_1[250:]
356 | data_for_graph_1 = dataset_1
357 |
358 | data_filt_numpy_high_1 = butter_highpass_filter(data_for_graph_1, highcut, fps)
359 | data_for_graph_1 = butter_lowpass_filter(data_filt_numpy_high_1, lowcut, fps)
360 |
361 | axis[0,0].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_1[250:], color = '#0a0b0c')
362 | axis[0,0].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_1[50]-y_minus_graph, data_for_graph_1[150]+y_plus_graph])
363 |
364 | # 2
365 | data_after_2 = data_2ch_test
366 | dataset_2 = data_before_2 + data_after_2
367 | data_before_2 = dataset_2[250:]
368 | data_for_graph_2 = dataset_2
369 |
370 | data_filt_numpy_high_2 = butter_highpass_filter(data_for_graph_2, highcut, fps)
371 | data_for_graph_2 = butter_lowpass_filter(data_filt_numpy_high_2, lowcut, fps)
372 |
373 | axis[1,0].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_2[250:], color = '#0a0b0c')
374 | axis[1,0].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_2[50]-y_minus_graph, data_for_graph_2[150]+y_plus_graph])
375 |
376 | # 3
377 | data_after_3 = data_3ch_test
378 | dataset_3 = data_before_3 + data_after_3
379 | data_before_3 = dataset_3[250:]
380 | data_for_graph_3 = dataset_3
381 |
382 | data_filt_numpy_high_3 = butter_highpass_filter(data_for_graph_3, highcut, fps)
383 | data_for_graph_3 = butter_lowpass_filter(data_filt_numpy_high_3, lowcut, fps)
384 |
385 | axis[2,0].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_3[250:], color = '#0a0b0c')
386 | axis[2,0].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_3[50]-y_minus_graph, data_for_graph_3[150]+y_plus_graph])
387 |
388 | # 4
389 | data_after_4 = data_4ch_test
390 | dataset_4 = data_before_4 + data_after_4
391 | data_before_4 = dataset_4[250:]
392 | data_for_graph_4 = dataset_4
393 |
394 | data_filt_numpy_high_4 = butter_highpass_filter(data_for_graph_4, highcut, fps)
395 | data_for_graph_4 = butter_lowpass_filter(data_filt_numpy_high_4, lowcut, fps)
396 |
397 | axis[3,0].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_4[250:], color = '#0a0b0c')
398 | axis[3,0].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_4[50]-y_minus_graph, data_for_graph_4[150]+y_plus_graph])
399 |
400 | #5
401 | data_after_5 = data_5ch_test
402 | dataset_5 = data_before_5 + data_after_5
403 | data_before_5 = dataset_5[250:]
404 | data_for_graph_5 = dataset_5
405 |
406 | data_filt_numpy_high_5 = butter_highpass_filter(data_for_graph_5, highcut, fps)
407 | data_for_graph_5 = butter_lowpass_filter(data_filt_numpy_high_5, lowcut, fps)
408 |
409 | axis[0,1].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_5[250:], color = '#0a0b0c')
410 | axis[0,1].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_5[50]-y_minus_graph, data_for_graph_5[150]+y_plus_graph])
411 |
412 | #6
413 | data_after_6 = data_6ch_test
414 | dataset_6 = data_before_6 + data_after_6
415 | data_before_6 = dataset_6[250:]
416 | data_for_graph_6 = dataset_6
417 |
418 | data_filt_numpy_high_6 = butter_highpass_filter(data_for_graph_6, highcut, fps)
419 | data_for_graph_6 = butter_lowpass_filter(data_filt_numpy_high_6, lowcut, fps)
420 |
421 | axis[1,1].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_6[250:], color = '#0a0b0c')
422 | axis[1,1].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_6[50]-y_minus_graph, data_for_graph_6[150]+y_plus_graph])
423 |
424 | #7
425 | data_after_7 = data_7ch_test
426 | dataset_7 = data_before_7 + data_after_7
427 | data_before_7 = dataset_7[250:]
428 | data_for_graph_7 = dataset_7
429 |
430 | data_filt_numpy_high_7 = butter_highpass_filter(data_for_graph_7, highcut, fps)
431 | data_for_graph_7 = butter_lowpass_filter(data_filt_numpy_high_7, lowcut, fps)
432 |
433 | axis[2,1].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_7[250:], color = '#0a0b0c')
434 | axis[2,1].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_7[50]-y_minus_graph, data_for_graph_1[150]+y_plus_graph])
435 |
436 | #8
437 | data_after_8 = data_8ch_test
438 | dataset_8 = data_before_8 + data_after_8
439 | data_before_8 = dataset_8[250:]
440 | data_for_graph_8 = dataset_8
441 |
442 | data_filt_numpy_high_8 = butter_highpass_filter(data_for_graph_8, highcut, fps)
443 | data_for_graph_8 = butter_lowpass_filter(data_filt_numpy_high_8, lowcut, fps)
444 |
445 | axis[3,1].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_8[250:], color = '#0a0b0c')
446 | axis[3,1].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_8[50]-y_minus_graph, data_for_graph_8[150]+y_plus_graph])
447 |
448 | # 9
449 | data_after_9 = data_9ch_test
450 | dataset_9 = data_before_9 + data_after_9
451 | data_before_9 = dataset_9[250:]
452 | data_for_graph_9 = dataset_9
453 |
454 | data_filt_numpy_high_9 = butter_highpass_filter(data_for_graph_9, highcut, fps)
455 | data_for_graph_9 = butter_lowpass_filter(data_filt_numpy_high_9, lowcut, fps)
456 |
457 | axis[0,2].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_9[250:], color = '#0a0b0c')
458 | axis[0,2].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_9[50]-y_minus_graph, data_for_graph_9[150]+y_plus_graph])
459 |
460 | # 10
461 | data_after_10 = data_10ch_test
462 | dataset_10 = data_before_10 + data_after_10
463 | data_before_10 = dataset_10[250:]
464 | data_for_graph_10 = dataset_10
465 |
466 | data_filt_numpy_high_10 = butter_highpass_filter(data_for_graph_10, highcut, fps)
467 | data_for_graph_10 = butter_lowpass_filter(data_filt_numpy_high_10, lowcut, fps)
468 |
469 | axis[1,2].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_10[250:], color = '#0a0b0c')
470 | axis[1,2].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_10[50]-y_minus_graph, data_for_graph_10[150]+y_plus_graph])
471 |
472 | # 11
473 | data_after_11 = data_11ch_test
474 | dataset_11 = data_before_11 + data_after_11
475 | data_before_11 = dataset_11[250:]
476 | data_for_graph_11 = dataset_11
477 |
478 | data_filt_numpy_high_11 = butter_highpass_filter(data_for_graph_11, highcut, fps)
479 | data_for_graph_11 = butter_lowpass_filter(data_filt_numpy_high_11, lowcut, fps)
480 |
481 | axis[2,2].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_11[250:], color = '#0a0b0c')
482 | axis[2,2].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_11[50]-y_minus_graph, data_for_graph_11[150]+y_plus_graph])
483 |
484 | # 12
485 | data_after_12 = data_12ch_test
486 | dataset_12 = data_before_12 + data_after_12
487 | data_before_12 = dataset_12[250:]
488 | data_for_graph_12 = dataset_12
489 |
490 | data_filt_numpy_high_12 = butter_highpass_filter(data_for_graph_12, highcut, fps)
491 | data_for_graph_12 = butter_lowpass_filter(data_filt_numpy_high_12, lowcut, fps)
492 |
493 | axis[3,2].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_12[250:], color = '#0a0b0c')
494 | axis[3,2].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_12[50]-y_minus_graph, data_for_graph_12[150]+y_plus_graph])
495 |
496 | # 13
497 | data_after_13 = data_13ch_test
498 | dataset_13 = data_before_13 + data_after_13
499 | data_before_13 = dataset_13[250:]
500 | data_for_graph_13 = dataset_13
501 |
502 | data_filt_numpy_high_13 = butter_highpass_filter(data_for_graph_13, highcut, fps)
503 | data_for_graph_13 = butter_lowpass_filter(data_filt_numpy_high_13, lowcut, fps)
504 |
505 | axis[0,3].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_13[250:], color = '#0a0b0c')
506 | axis[0,3].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_13[50]-y_minus_graph, data_for_graph_13[150]+y_plus_graph])
507 |
508 | # 14
509 | data_after_14 = data_14ch_test
510 | dataset_14 = data_before_14 + data_after_14
511 | data_before_14 = dataset_14[250:]
512 | data_for_graph_14 = dataset_14
513 |
514 | data_filt_numpy_high_14 = butter_highpass_filter(data_for_graph_14, highcut, fps)
515 | data_for_graph_14 = butter_lowpass_filter(data_filt_numpy_high_14, lowcut, fps)
516 |
517 | axis[1,3].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_14[250:], color = '#0a0b0c')
518 | axis[1,3].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_14[50]-y_minus_graph, data_for_graph_14[150]+y_plus_graph])
519 |
520 | # 15
521 | data_after_15 = data_15ch_test
522 | dataset_15 = data_before_15 + data_after_15
523 | data_before_15 = dataset_15[250:]
524 | data_for_graph_15 = dataset_15
525 |
526 | data_filt_numpy_high_15 = butter_highpass_filter(data_for_graph_15, highcut, fps)
527 | data_for_graph_15 = butter_lowpass_filter(data_filt_numpy_high_15, lowcut, fps)
528 |
529 | axis[2,3].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_15[250:], color = '#0a0b0c')
530 | axis[2,3].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_15[50]-y_minus_graph, data_for_graph_15[150]+y_plus_graph])
531 |
532 | # 16
533 | data_after_16 = data_16ch_test
534 | dataset_16 = data_before_16 + data_after_16
535 | data_before_16 = dataset_16[250:]
536 | data_for_graph_16 = dataset_16
537 |
538 | data_filt_numpy_high_16 = butter_highpass_filter(data_for_graph_16, highcut, fps)
539 | data_for_graph_16 = butter_lowpass_filter(data_filt_numpy_high_16, lowcut, fps)
540 |
541 | axis[3,3].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_16[250:], color = '#0a0b0c')
542 | axis[3,3].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_16[50]-y_minus_graph, data_for_graph_16[150]+y_plus_graph])
543 |
544 |
545 | plt.pause(0.0000000000001)
546 |
547 | axis_x=axis_x+sample_lens
548 | data_1ch_test = []
549 | data_2ch_test = []
550 | data_3ch_test = []
551 | data_4ch_test = []
552 | data_5ch_test = []
553 | data_6ch_test = []
554 | data_7ch_test = []
555 | data_8ch_test = []
556 | data_9ch_test = []
557 | data_10ch_test = []
558 | data_11ch_test = []
559 | data_12ch_test = []
560 | data_13ch_test = []
561 | data_14ch_test = []
562 | data_15ch_test = []
563 | data_16ch_test = []
564 |
565 |
566 | spi.close()
567 |
--------------------------------------------------------------------------------
/images/2.Graph_Gpio_D _1_5_4_spike.py:
--------------------------------------------------------------------------------
1 | import spidev
2 | import time
3 | #from RPi import GPIO
4 | from matplotlib import pyplot as plt
5 | from scipy.ndimage import gaussian_filter1d
6 | from scipy import signal
7 | import gpiod
8 |
9 | #GPIO.setwarnings(False)
10 | #GPIO.setmode(GPIO.BOARD)
11 |
12 | button_pin_1 = 26 #13
13 | button_pin_2 = 13
14 | cs_pin = 19
15 | #chip = gpiod.Chip("gpiochip4")
16 | # chip = gpiod.chip("/dev/gpiochip4")
17 | chip = gpiod.chip("0")
18 | #cs_line = chip.get_line(19) # GPIO19
19 | #cs_line.request(consumer="SPI_CS", type=gpiod.LINE_REQ_DIR_OUT)
20 | cs_line = chip.get_line(cs_pin)
21 | cs_line_out = gpiod.line_request()
22 | cs_line_out.consumer = "SPI_CS"
23 | cs_line_out.request_type = gpiod.line_request.DIRECTION_OUTPUT
24 | cs_line.request(cs_line_out)
25 |
26 | #cs_line.request(consumer="SPI_CS", type=gpiod.line_request.DIRECTION_OUTPUT)
27 | cs_line.set_value(1) # Set CS high initially
28 |
29 |
30 | #button_line_1 = chip.get_line(button_pin_1)
31 | #button_line_1.request(consumer = "Button", type = gpiod.LINE_REQ_DIR_IN)
32 |
33 | line_1 = chip.get_line(button_pin_1)
34 |
35 | #line_2 = chip.get_line(button_pin_2)
36 |
37 | button_line_1 = gpiod.line_request()
38 | button_line_1.consumer = "Button"
39 | button_line_1.request_type = gpiod.line_request.DIRECTION_INPUT
40 | line_1.request(button_line_1)
41 |
42 | #button_line_2 = chip.get_line(button_pin_2)
43 | #button_line_2.request(consumer = "Button", type = gpiod.LINE_REQ_DIR_IN)
44 | #button_line_2 = gpiod.line_request()
45 | #button_line_2.consumer = "Button"
46 | #button_line_2.request_type = gpiod.line_request.DIRECTION_INPUT
47 | #line_2.request(button_line_2)
48 |
49 | spi = spidev.SpiDev()
50 | spi.open(0,0)
51 | spi.max_speed_hz = 4000000#600000
52 | spi.lsbfirst=False
53 | spi.mode=0b01
54 | spi.bits_per_word = 8
55 |
56 | spi_2 = spidev.SpiDev()
57 | spi_2.open(0,1)
58 | spi_2.max_speed_hz=4000000#600000
59 | spi_2.lsbfirst=False
60 | spi_2.mode=0b01
61 | spi_2.bits_per_word = 8
62 |
63 | who_i_am=0x00
64 | config1=0x01
65 | config2=0X02
66 | config3=0X03
67 |
68 | reset=0x06
69 | stop=0x0A
70 | start=0x08
71 | sdatac=0x11
72 | rdatac=0x10
73 | wakeup=0x02
74 | rdata = 0x12
75 |
76 | ch1set=0x05
77 | ch2set=0x06
78 | ch3set=0x07
79 | ch4set=0x08
80 | ch5set=0x09
81 | ch6set=0x0A
82 | ch7set=0x0B
83 | ch8set=0x0C
84 |
85 | data_test= 0x7FFFFF
86 | data_check=0xFFFFFF
87 |
88 | def read_byte(register):
89 | write=0x20
90 | register_write=write|register
91 | data = [register_write,0x00,register]
92 | read_reg=spi.xfer(data)
93 | print ("data", read_reg)
94 |
95 | def send_command(command):
96 | send_data = [command]
97 | com_reg=spi.xfer(send_data)
98 |
99 | def write_byte(register,data):
100 | write=0x40
101 | register_write=write|register
102 | data = [register_write,0x00,data]
103 | print (data)
104 | spi.xfer(data)
105 |
106 | def read_byte_2(register):
107 | write=0x20
108 | register_write=write|register
109 | data = [register_write,0x00,register]
110 | cs_line.set_value(0)
111 | read_reg=spi.xfer(data)
112 | cs_line.set_value(1)
113 | print ("data", read_reg)
114 |
115 | def send_command_2(command):
116 | send_data = [command]
117 | cs_line.set_value(0)
118 | spi_2.xfer(send_data)
119 | cs_line.set_value(1)
120 |
121 | def write_byte_2(register,data):
122 | write=0x40
123 | register_write=write|register
124 | data = [register_write,0x00,data]
125 | print (data)
126 |
127 | cs_line.set_value(0)
128 | spi_2.xfer(data)
129 | cs_line.set_value(1)
130 |
131 |
132 |
133 | send_command (wakeup)
134 | send_command (stop)
135 | send_command (reset)
136 | send_command (sdatac)
137 |
138 | write_byte (0x14, 0x80) #GPIO 80
139 | write_byte (config1, 0x96)
140 | write_byte (config2, 0xD4)
141 | write_byte (config3, 0xFF)
142 | write_byte (0x04, 0x00)
143 | write_byte (0x0D, 0x00)
144 | write_byte (0x0E, 0x00)
145 | write_byte (0x0F, 0x00)
146 | write_byte (0x10, 0x00)
147 | write_byte (0x11, 0x00)
148 | write_byte (0x15, 0x20)
149 | #
150 | write_byte (0x17, 0x00)
151 | write_byte (ch1set, 0x00)
152 | write_byte (ch2set, 0x00)
153 | write_byte (ch3set, 0x00)
154 | write_byte (ch4set, 0x00)
155 | write_byte (ch5set, 0x00)
156 | write_byte (ch6set, 0x00)
157 | write_byte (ch7set, 0x00)
158 | write_byte (ch8set, 0x00)
159 |
160 | send_command (rdatac)
161 | send_command (start)
162 |
163 |
164 | send_command_2 (wakeup)
165 | send_command_2 (stop)
166 | send_command_2 (reset)
167 | send_command_2 (sdatac)
168 |
169 | write_byte_2 (0x14, 0x80) #GPIO 80
170 | write_byte_2 (config1, 0x96)
171 | write_byte_2 (config2, 0xD4)
172 | write_byte_2 (config3, 0xFF)
173 | write_byte_2 (0x04, 0x00)
174 | write_byte_2 (0x0D, 0x00)
175 | write_byte_2 (0x0E, 0x00)
176 | write_byte_2 (0x0F, 0x00)
177 | write_byte_2 (0x10, 0x00)
178 | write_byte_2 (0x11, 0x00)
179 | write_byte_2 (0x15, 0x20)
180 | #
181 | write_byte_2 (0x17, 0x00)
182 | write_byte_2 (ch1set, 0x00)
183 | write_byte_2 (ch2set, 0x00)
184 | write_byte_2 (ch3set, 0x00)
185 | write_byte_2 (ch4set, 0x00)
186 | write_byte_2 (ch5set, 0x00)
187 | write_byte_2 (ch6set, 0x00)
188 | write_byte_2 (ch7set, 0x00)
189 | write_byte_2 (ch8set, 0x00)
190 |
191 | send_command_2 (rdatac)
192 | send_command_2 (start)
193 |
194 | DRDY=1
195 |
196 | result=[0]*27
197 | result_2=[0]*27
198 |
199 |
200 | data_1ch_test = []
201 | data_2ch_test = []
202 | data_3ch_test = []
203 | data_4ch_test = []
204 | data_5ch_test = []
205 | data_6ch_test = []
206 | data_7ch_test = []
207 | data_8ch_test = []
208 |
209 | data_9ch_test = []
210 | data_10ch_test = []
211 | data_11ch_test = []
212 | data_12ch_test = []
213 | data_13ch_test = []
214 | data_14ch_test = []
215 | data_15ch_test = []
216 | data_16ch_test = []
217 |
218 | axis_x=0
219 | y_minus_graph=100
220 | y_plus_graph=100
221 | x_minux_graph=5000
222 | x_plus_graph=250
223 | sample_len = 250
224 |
225 | fig, axis = plt.subplots(4, 4, figsize=(5, 5))
226 | plt.subplots_adjust(hspace=1)
227 | ch_name = 0
228 | ch_name_title = [1,5,2,6,3,7,4,8]
229 | axi = [(i, j) for i in range(4) for j in range(2)]
230 | for ax_row, ax_col in axi:
231 | axis[ax_row, ax_col].set_xlabel('Time')
232 | axis[ax_row, ax_col].set_ylabel('Amplitude')
233 | axis[ax_row, ax_col].set_title('Data after pass filter Ch-' + str(ch_name_title[ch_name]))
234 | ch_name = ch_name + 1
235 |
236 | test_DRDY = 5
237 | test_DRDY_2 = 5
238 | #1.2 Band-pass filter
239 | data_before = []
240 | data_after = []
241 | just_one_time = 0
242 | data_lenght_for_Filter = 2 # how much we read data for filter, all lenght [_____] + [_____] + [_____]
243 | read_data_lenght_one_time = 1 # for one time how much read [_____]
244 | sample_len = 250
245 | sample_lens = 250
246 | fps = 250
247 | highcut = 1
248 | lowcut = 10
249 | data_before_1 = data_before_2 = data_before_3 = data_before_4 = data_before_5 = data_before_6 = data_before_7 = data_before_8 = [0]*250
250 | data_before_9 = data_before_10 = data_before_11 = data_before_12 = data_before_13 = data_before_14 = data_before_15 = data_before_16 = [0]*250
251 |
252 | print (data_lenght_for_Filter*read_data_lenght_one_time-read_data_lenght_one_time)
253 |
254 | def butter_lowpass(cutoff, fs, order=5):
255 | nyq = 0.5 * fs
256 | normal_cutoff = cutoff / nyq
257 | b, a = signal.butter(order, normal_cutoff, btype='low', analog=False)
258 | return b, a
259 | def butter_lowpass_filter(data, cutoff, fs, order=5):
260 | b, a = butter_lowpass(cutoff, fs, order=order)
261 | y = signal.lfilter(b, a, data)
262 | return y
263 | def butter_highpass(cutoff, fs, order=3):
264 | nyq = 0.5 * fs
265 | normal_cutoff = cutoff / nyq
266 | b, a = signal.butter(order, normal_cutoff, btype='high', analog=False)
267 | return b, a
268 | def butter_highpass_filter(data, cutoff, fs, order=5):
269 | b, a = butter_highpass(cutoff, fs, order=order)
270 | y = signal.filtfilt(b, a, data)
271 | return y
272 |
273 | last_valid_value = None
274 | counter = 0
275 |
276 | def _to_signed_24bit(msb: int, middle: int, lsb: int) -> int:
277 | # Combine the bytes into a 24-bit integer
278 | combined = (msb << 16) | (middle << 8) | lsb
279 |
280 | # Check the sign bit (bit 7 of the MSB)
281 | if (msb & 0x80) != 0:
282 | # Convert to negative 24-bit signed integer
283 | combined -= 1 << 24
284 |
285 | return combined
286 |
287 | def the_input_is_valid(input_list):
288 | global last_valid_value, counter
289 | msb = input_list[24]
290 | middle = input_list[25]
291 | lsb = input_list[26]
292 |
293 | current_value = _to_signed_24bit(msb, middle, lsb)
294 |
295 | if last_valid_value is None:
296 | _last_valid_value = current_value
297 | return True
298 |
299 | difference = abs(current_value - last_valid_value)
300 | if difference > 3925:
301 | print(f'Corrupted data detected _counter: {counter}')
302 | counter += 1
303 | return False
304 |
305 | _last_valid_value = current_value
306 | return True
307 |
308 | while 1:
309 |
310 |
311 | #print ("1", button_state)
312 | #print("2", button_state_2)
313 |
314 | #print ("ok3")
315 | button_state = line_1.get_value()
316 | #print (button_state)
317 | if button_state == 1:
318 | test_DRDY = 10
319 | if test_DRDY == 10 and button_state == 0:
320 | test_DRDY = 0
321 |
322 | output=spi.readbytes(27)
323 |
324 | cs_line.set_value(0)
325 | output_2=spi_2.readbytes(27)
326 | cs_line.set_value(1)
327 |
328 | if not the_input_is_valid(output_2):
329 | continue
330 |
331 | # print (output[0],output[1],output[2])
332 | if output_2[0]==192 and output_2[1] == 0 and output_2[2] == 8:
333 | #print ("ok4")
334 | for a in range (3,25,3):
335 | voltage_1=(output[a]<<8)| output[a+1]
336 | voltage_1=(voltage_1<<8)| output[a+2]
337 | convert_voktage=voltage_1|data_test
338 | if convert_voktage==data_check:
339 | voltage_1_after_convert=(voltage_1-16777214)
340 | else:
341 | voltage_1_after_convert=voltage_1
342 | channel_num = (a/3)
343 |
344 | result[int (channel_num)]=round(1000000*4.5*(voltage_1_after_convert/16777215),2)
345 |
346 | data_1ch_test.append(result[1])
347 | data_2ch_test.append(result[2])
348 | data_3ch_test.append(result[3])
349 | data_4ch_test.append(result[4])
350 | data_5ch_test.append(result[5])
351 | data_6ch_test.append(result[6])
352 | data_7ch_test.append(result[7])
353 | data_8ch_test.append(result[8])
354 |
355 |
356 | for a in range (3,25,3):
357 | voltage_1=(output_2[a]<<8)| output_2[a+1]
358 | voltage_1=(voltage_1<<8)| output_2[a+2]
359 | convert_voktage=voltage_1|data_test
360 | if convert_voktage==data_check:
361 | voltage_1_after_convert=(voltage_1-16777214)
362 | else:
363 | voltage_1_after_convert=voltage_1
364 | channel_num = (a/3)
365 |
366 | result_2[int (channel_num)]=round(1000000*4.5*(voltage_1_after_convert/16777215),2)
367 |
368 | data_9ch_test.append(result_2[1])
369 | data_10ch_test.append(result_2[2])
370 | data_11ch_test.append(result_2[3])
371 | data_12ch_test.append(result_2[4])
372 | data_13ch_test.append(result_2[5])
373 | data_14ch_test.append(result_2[6])
374 | data_15ch_test.append(result_2[7])
375 | data_16ch_test.append(result_2[8])
376 |
377 |
378 |
379 |
380 | if len(data_9ch_test)==sample_len:
381 |
382 |
383 | data_after_1 = data_1ch_test
384 | dataset_1 = data_before_1 + data_after_1
385 | data_before_1 = dataset_1[250:]
386 | data_for_graph_1 = dataset_1
387 |
388 | data_filt_numpy_high_1 = butter_highpass_filter(data_for_graph_1, highcut, fps)
389 | data_for_graph_1 = butter_lowpass_filter(data_filt_numpy_high_1, lowcut, fps)
390 |
391 | axis[0,0].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_1[250:], color = '#0a0b0c')
392 | axis[0,0].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_1[50]-y_minus_graph, data_for_graph_1[150]+y_plus_graph])
393 |
394 | # 2
395 | data_after_2 = data_2ch_test
396 | dataset_2 = data_before_2 + data_after_2
397 | data_before_2 = dataset_2[250:]
398 | data_for_graph_2 = dataset_2
399 |
400 | data_filt_numpy_high_2 = butter_highpass_filter(data_for_graph_2, highcut, fps)
401 | data_for_graph_2 = butter_lowpass_filter(data_filt_numpy_high_2, lowcut, fps)
402 |
403 | axis[1,0].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_2[250:], color = '#0a0b0c')
404 | axis[1,0].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_2[50]-y_minus_graph, data_for_graph_2[150]+y_plus_graph])
405 |
406 | # 3
407 | data_after_3 = data_3ch_test
408 | dataset_3 = data_before_3 + data_after_3
409 | data_before_3 = dataset_3[250:]
410 | data_for_graph_3 = dataset_3
411 |
412 | data_filt_numpy_high_3 = butter_highpass_filter(data_for_graph_3, highcut, fps)
413 | data_for_graph_3 = butter_lowpass_filter(data_filt_numpy_high_3, lowcut, fps)
414 |
415 | axis[2,0].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_3[250:], color = '#0a0b0c')
416 | axis[2,0].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_3[50]-y_minus_graph, data_for_graph_3[150]+y_plus_graph])
417 |
418 | # 4
419 | data_after_4 = data_4ch_test
420 | dataset_4 = data_before_4 + data_after_4
421 | data_before_4 = dataset_4[250:]
422 | data_for_graph_4 = dataset_4
423 |
424 | data_filt_numpy_high_4 = butter_highpass_filter(data_for_graph_4, highcut, fps)
425 | data_for_graph_4 = butter_lowpass_filter(data_filt_numpy_high_4, lowcut, fps)
426 |
427 | axis[3,0].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_4[250:], color = '#0a0b0c')
428 | axis[3,0].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_4[50]-y_minus_graph, data_for_graph_4[150]+y_plus_graph])
429 |
430 | #5
431 | data_after_5 = data_5ch_test
432 | dataset_5 = data_before_5 + data_after_5
433 | data_before_5 = dataset_5[250:]
434 | data_for_graph_5 = dataset_5
435 |
436 | data_filt_numpy_high_5 = butter_highpass_filter(data_for_graph_5, highcut, fps)
437 | data_for_graph_5 = butter_lowpass_filter(data_filt_numpy_high_5, lowcut, fps)
438 |
439 | axis[0,1].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_5[250:], color = '#0a0b0c')
440 | axis[0,1].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_5[50]-y_minus_graph, data_for_graph_5[150]+y_plus_graph])
441 |
442 | #6
443 | data_after_6 = data_6ch_test
444 | dataset_6 = data_before_6 + data_after_6
445 | data_before_6 = dataset_6[250:]
446 | data_for_graph_6 = dataset_6
447 |
448 | data_filt_numpy_high_6 = butter_highpass_filter(data_for_graph_6, highcut, fps)
449 | data_for_graph_6 = butter_lowpass_filter(data_filt_numpy_high_6, lowcut, fps)
450 |
451 | axis[1,1].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_6[250:], color = '#0a0b0c')
452 | axis[1,1].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_6[50]-y_minus_graph, data_for_graph_6[150]+y_plus_graph])
453 |
454 | #7
455 | data_after_7 = data_7ch_test
456 | dataset_7 = data_before_7 + data_after_7
457 | data_before_7 = dataset_7[250:]
458 | data_for_graph_7 = dataset_7
459 |
460 | data_filt_numpy_high_7 = butter_highpass_filter(data_for_graph_7, highcut, fps)
461 | data_for_graph_7 = butter_lowpass_filter(data_filt_numpy_high_7, lowcut, fps)
462 |
463 | axis[2,1].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_7[250:], color = '#0a0b0c')
464 | axis[2,1].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_7[50]-y_minus_graph, data_for_graph_1[150]+y_plus_graph])
465 |
466 | #8
467 | data_after_8 = data_8ch_test
468 | dataset_8 = data_before_8 + data_after_8
469 | data_before_8 = dataset_8[250:]
470 | data_for_graph_8 = dataset_8
471 |
472 | data_filt_numpy_high_8 = butter_highpass_filter(data_for_graph_8, highcut, fps)
473 | data_for_graph_8 = butter_lowpass_filter(data_filt_numpy_high_8, lowcut, fps)
474 |
475 | axis[3,1].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_8[250:], color = '#0a0b0c')
476 | axis[3,1].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_8[50]-y_minus_graph, data_for_graph_8[150]+y_plus_graph])
477 |
478 | # 9
479 | data_after_9 = data_9ch_test
480 | dataset_9 = data_before_9 + data_after_9
481 | data_before_9 = dataset_9[250:]
482 | data_for_graph_9 = dataset_9
483 |
484 | data_filt_numpy_high_9 = butter_highpass_filter(data_for_graph_9, highcut, fps)
485 | data_for_graph_9 = butter_lowpass_filter(data_filt_numpy_high_9, lowcut, fps)
486 |
487 | axis[0,2].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_9[250:], color = '#0a0b0c')
488 | axis[0,2].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_9[50]-y_minus_graph, data_for_graph_9[150]+y_plus_graph])
489 |
490 | # 10
491 | data_after_10 = data_10ch_test
492 | dataset_10 = data_before_10 + data_after_10
493 | data_before_10 = dataset_10[250:]
494 | data_for_graph_10 = dataset_10
495 |
496 | data_filt_numpy_high_10 = butter_highpass_filter(data_for_graph_10, highcut, fps)
497 | data_for_graph_10 = butter_lowpass_filter(data_filt_numpy_high_10, lowcut, fps)
498 |
499 | axis[1,2].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_10[250:], color = '#0a0b0c')
500 | axis[1,2].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_10[50]-y_minus_graph, data_for_graph_10[150]+y_plus_graph])
501 |
502 | # 11
503 | data_after_11 = data_11ch_test
504 | dataset_11 = data_before_11 + data_after_11
505 | data_before_11 = dataset_11[250:]
506 | data_for_graph_11 = dataset_11
507 |
508 | data_filt_numpy_high_11 = butter_highpass_filter(data_for_graph_11, highcut, fps)
509 | data_for_graph_11 = butter_lowpass_filter(data_filt_numpy_high_11, lowcut, fps)
510 |
511 | axis[2,2].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_11[250:], color = '#0a0b0c')
512 | axis[2,2].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_11[50]-y_minus_graph, data_for_graph_11[150]+y_plus_graph])
513 |
514 | # 12
515 | data_after_12 = data_12ch_test
516 | dataset_12 = data_before_12 + data_after_12
517 | data_before_12 = dataset_12[250:]
518 | data_for_graph_12 = dataset_12
519 |
520 | data_filt_numpy_high_12 = butter_highpass_filter(data_for_graph_12, highcut, fps)
521 | data_for_graph_12 = butter_lowpass_filter(data_filt_numpy_high_12, lowcut, fps)
522 |
523 | axis[3,2].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_12[250:], color = '#0a0b0c')
524 | axis[3,2].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_12[50]-y_minus_graph, data_for_graph_12[150]+y_plus_graph])
525 |
526 | # 13
527 | data_after_13 = data_13ch_test
528 | dataset_13 = data_before_13 + data_after_13
529 | data_before_13 = dataset_13[250:]
530 | data_for_graph_13 = dataset_13
531 |
532 | data_filt_numpy_high_13 = butter_highpass_filter(data_for_graph_13, highcut, fps)
533 | data_for_graph_13 = butter_lowpass_filter(data_filt_numpy_high_13, lowcut, fps)
534 |
535 | axis[0,3].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_13[250:], color = '#0a0b0c')
536 | axis[0,3].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_13[50]-y_minus_graph, data_for_graph_13[150]+y_plus_graph])
537 |
538 | # 14
539 | data_after_14 = data_14ch_test
540 | dataset_14 = data_before_14 + data_after_14
541 | data_before_14 = dataset_14[250:]
542 | data_for_graph_14 = dataset_14
543 |
544 | data_filt_numpy_high_14 = butter_highpass_filter(data_for_graph_14, highcut, fps)
545 | data_for_graph_14 = butter_lowpass_filter(data_filt_numpy_high_14, lowcut, fps)
546 |
547 | axis[1,3].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_14[250:], color = '#0a0b0c')
548 | axis[1,3].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_14[50]-y_minus_graph, data_for_graph_14[150]+y_plus_graph])
549 |
550 | # 15
551 | data_after_15 = data_15ch_test
552 | dataset_15 = data_before_15 + data_after_15
553 | data_before_15 = dataset_15[250:]
554 | data_for_graph_15 = dataset_15
555 |
556 | data_filt_numpy_high_15 = butter_highpass_filter(data_for_graph_15, highcut, fps)
557 | data_for_graph_15 = butter_lowpass_filter(data_filt_numpy_high_15, lowcut, fps)
558 |
559 | axis[2,3].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_15[250:], color = '#0a0b0c')
560 | axis[2,3].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_15[50]-y_minus_graph, data_for_graph_15[150]+y_plus_graph])
561 |
562 | # 16
563 | data_after_16 = data_16ch_test
564 | dataset_16 = data_before_16 + data_after_16
565 | data_before_16 = dataset_16[250:]
566 | data_for_graph_16 = dataset_16
567 |
568 | data_filt_numpy_high_16 = butter_highpass_filter(data_for_graph_16, highcut, fps)
569 | data_for_graph_16 = butter_lowpass_filter(data_filt_numpy_high_16, lowcut, fps)
570 |
571 | axis[3,3].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_16[250:], color = '#0a0b0c')
572 | axis[3,3].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_16[50]-y_minus_graph, data_for_graph_16[150]+y_plus_graph])
573 |
574 |
575 | plt.pause(0.0000000000001)
576 |
577 | axis_x=axis_x+sample_lens
578 | data_1ch_test = []
579 | data_2ch_test = []
580 | data_3ch_test = []
581 | data_4ch_test = []
582 | data_5ch_test = []
583 | data_6ch_test = []
584 | data_7ch_test = []
585 | data_8ch_test = []
586 | data_9ch_test = []
587 | data_10ch_test = []
588 | data_11ch_test = []
589 | data_12ch_test = []
590 | data_13ch_test = []
591 | data_14ch_test = []
592 | data_15ch_test = []
593 | data_16ch_test = []
594 |
595 |
596 | spi.close()
597 |
--------------------------------------------------------------------------------
/GUI/Graph_Gpio_D _1_5_4_not_spike.py:
--------------------------------------------------------------------------------
1 | import spidev
2 | import time
3 | #from RPi import GPIO
4 | from matplotlib import pyplot as plt
5 | from scipy.ndimage import gaussian_filter1d
6 | from scipy import signal
7 | import gpiod
8 |
9 | #GPIO.setwarnings(False)
10 | #GPIO.setmode(GPIO.BOARD)
11 |
12 | button_pin_1 = 26 #13
13 | button_pin_2 = 13
14 | cs_pin = 19
15 | #chip = gpiod.Chip("gpiochip4")
16 | chip = gpiod.chip("/dev/gpiochip4")
17 | #chip = gpiod.chip("0")
18 | #cs_line = chip.get_line(19) # GPIO19
19 | #cs_line.request(consumer="SPI_CS", type=gpiod.LINE_REQ_DIR_OUT)
20 | cs_line = chip.get_line(cs_pin)
21 | cs_line_out = gpiod.line_request()
22 | cs_line_out.consumer = "SPI_CS"
23 | cs_line_out.request_type = gpiod.line_request.DIRECTION_OUTPUT
24 | cs_line.request(cs_line_out)
25 |
26 | #cs_line.request(consumer="SPI_CS", type=gpiod.line_request.DIRECTION_OUTPUT)
27 | cs_line.set_value(1) # Set CS high initially
28 |
29 |
30 | #button_line_1 = chip.get_line(button_pin_1)
31 | #button_line_1.request(consumer = "Button", type = gpiod.LINE_REQ_DIR_IN)
32 |
33 | line_1 = chip.get_line(button_pin_1)
34 |
35 | #line_2 = chip.get_line(button_pin_2)
36 |
37 | button_line_1 = gpiod.line_request()
38 | button_line_1.consumer = "Button"
39 | button_line_1.request_type = gpiod.line_request.DIRECTION_INPUT
40 | line_1.request(button_line_1)
41 |
42 | #button_line_2 = chip.get_line(button_pin_2)
43 | #button_line_2.request(consumer = "Button", type = gpiod.LINE_REQ_DIR_IN)
44 | #button_line_2 = gpiod.line_request()
45 | #button_line_2.consumer = "Button"
46 | #button_line_2.request_type = gpiod.line_request.DIRECTION_INPUT
47 | #line_2.request(button_line_2)
48 |
49 | spi = spidev.SpiDev()
50 | spi.open(0,0)
51 | spi.max_speed_hz = 4000000#600000
52 | spi.lsbfirst=False
53 | spi.mode=0b01
54 | spi.bits_per_word = 8
55 |
56 | spi_2 = spidev.SpiDev()
57 | spi_2.open(0,1)
58 | spi_2.max_speed_hz=4000000#600000
59 | spi_2.lsbfirst=False
60 | spi_2.mode=0b01
61 | spi_2.bits_per_word = 8
62 |
63 | who_i_am=0x00
64 | config1=0x01
65 | config2=0X02
66 | config3=0X03
67 |
68 | reset=0x06
69 | stop=0x0A
70 | start=0x08
71 | sdatac=0x11
72 | rdatac=0x10
73 | wakeup=0x02
74 | rdata = 0x12
75 |
76 | ch1set=0x05
77 | ch2set=0x06
78 | ch3set=0x07
79 | ch4set=0x08
80 | ch5set=0x09
81 | ch6set=0x0A
82 | ch7set=0x0B
83 | ch8set=0x0C
84 |
85 | data_test= 0x7FFFFF
86 | data_check=0xFFFFFF
87 |
88 | def read_byte(register):
89 | write=0x20
90 | register_write=write|register
91 | data = [register_write,0x00,register]
92 | read_reg=spi.xfer(data)
93 | print ("data", read_reg)
94 |
95 | def send_command(command):
96 | send_data = [command]
97 | com_reg=spi.xfer(send_data)
98 |
99 | def write_byte(register,data):
100 | write=0x40
101 | register_write=write|register
102 | data = [register_write,0x00,data]
103 | print (data)
104 | spi.xfer(data)
105 |
106 | def read_byte_2(register):
107 | write=0x20
108 | register_write=write|register
109 | data = [register_write,0x00,register]
110 | cs_line.set_value(0)
111 | read_reg=spi.xfer(data)
112 | cs_line.set_value(1)
113 | print ("data", read_reg)
114 |
115 | def send_command_2(command):
116 | send_data = [command]
117 | cs_line.set_value(0)
118 | spi_2.xfer(send_data)
119 | cs_line.set_value(1)
120 |
121 | def write_byte_2(register,data):
122 | write=0x40
123 | register_write=write|register
124 | data = [register_write,0x00,data]
125 | print (data)
126 |
127 | cs_line.set_value(0)
128 | spi_2.xfer(data)
129 | cs_line.set_value(1)
130 |
131 |
132 |
133 | send_command (wakeup)
134 | send_command (stop)
135 | send_command (reset)
136 | send_command (sdatac)
137 |
138 | write_byte (0x14, 0x80) #GPIO 80
139 | write_byte (config1, 0x96)
140 | write_byte (config2, 0xD4)
141 | write_byte (config3, 0xFF)
142 | write_byte (0x04, 0x00)
143 | write_byte (0x0D, 0x00)
144 | write_byte (0x0E, 0x00)
145 | write_byte (0x0F, 0x00)
146 | write_byte (0x10, 0x00)
147 | write_byte (0x11, 0x00)
148 | write_byte (0x15, 0x20)
149 | #
150 | write_byte (0x17, 0x00)
151 | write_byte (ch1set, 0x00)
152 | write_byte (ch2set, 0x00)
153 | write_byte (ch3set, 0x00)
154 | write_byte (ch4set, 0x00)
155 | write_byte (ch5set, 0x00)
156 | write_byte (ch6set, 0x00)
157 | write_byte (ch7set, 0x01)
158 | write_byte (ch8set, 0x01)
159 |
160 | send_command (rdatac)
161 | send_command (start)
162 |
163 |
164 | send_command_2 (wakeup)
165 | send_command_2 (stop)
166 | send_command_2 (reset)
167 | send_command_2 (sdatac)
168 |
169 | write_byte_2 (0x14, 0x80) #GPIO 80
170 | write_byte_2 (config1, 0x96)
171 | write_byte_2 (config2, 0xD4)
172 | write_byte_2 (config3, 0xFF)
173 | write_byte_2 (0x04, 0x00)
174 | write_byte_2 (0x0D, 0x00)
175 | write_byte_2 (0x0E, 0x00)
176 | write_byte_2 (0x0F, 0x00)
177 | write_byte_2 (0x10, 0x00)
178 | write_byte_2 (0x11, 0x00)
179 | write_byte_2 (0x15, 0x20)
180 | #
181 | write_byte_2 (0x17, 0x00)
182 | write_byte_2 (ch1set, 0x00)
183 | write_byte_2 (ch2set, 0x00)
184 | write_byte_2 (ch3set, 0x00)
185 | write_byte_2 (ch4set, 0x00)
186 | write_byte_2 (ch5set, 0x00)
187 | write_byte_2 (ch6set, 0x00)
188 | write_byte_2 (ch7set, 0x01)
189 | write_byte_2 (ch8set, 0x01)
190 |
191 | send_command_2 (rdatac)
192 | send_command_2 (start)
193 |
194 | DRDY=1
195 |
196 | result=[0]*27
197 | result_2=[0]*27
198 |
199 |
200 | data_1ch_test = []
201 | data_2ch_test = []
202 | data_3ch_test = []
203 | data_4ch_test = []
204 | data_5ch_test = []
205 | data_6ch_test = []
206 | data_7ch_test = []
207 | data_8ch_test = []
208 |
209 | data_9ch_test = []
210 | data_10ch_test = []
211 | data_11ch_test = []
212 | data_12ch_test = []
213 | data_13ch_test = []
214 | data_14ch_test = []
215 | data_15ch_test = []
216 | data_16ch_test = []
217 |
218 | axis_x=0
219 | y_minus_graph=100
220 | y_plus_graph=100
221 | x_minux_graph=5000
222 | x_plus_graph=250
223 | sample_len = 250
224 |
225 | fig, axis = plt.subplots(4, 4, figsize=(5, 5))
226 | plt.subplots_adjust(hspace=1)
227 | ch_name = 0
228 | ch_name_title = [1,5,2,6,3,7,4,8]
229 | axi = [(i, j) for i in range(4) for j in range(2)]
230 | for ax_row, ax_col in axi:
231 | axis[ax_row, ax_col].set_xlabel('Time')
232 | axis[ax_row, ax_col].set_ylabel('Amplitude')
233 | axis[ax_row, ax_col].set_title('Data after pass filter Ch-' + str(ch_name_title[ch_name]))
234 | ch_name = ch_name + 1
235 |
236 | test_DRDY = 5
237 | test_DRDY_2 = 5
238 | #1.2 Band-pass filter
239 | data_before = []
240 | data_after = []
241 | just_one_time = 0
242 | data_lenght_for_Filter = 2 # how much we read data for filter, all lenght [_____] + [_____] + [_____]
243 | read_data_lenght_one_time = 1 # for one time how much read [_____]
244 | sample_len = 250
245 | sample_lens = 250
246 | fps = 250
247 | highcut = 1
248 | lowcut = 10
249 | data_before_1 = data_before_2 = data_before_3 = data_before_4 = data_before_5 = data_before_6 = data_before_7 = data_before_8 = [0]*250
250 | data_before_9 = data_before_10 = data_before_11 = data_before_12 = data_before_13 = data_before_14 = data_before_15 = data_before_16 = [0]*250
251 |
252 | print (data_lenght_for_Filter*read_data_lenght_one_time-read_data_lenght_one_time)
253 |
254 | def butter_lowpass(cutoff, fs, order=5):
255 | nyq = 0.5 * fs
256 | normal_cutoff = cutoff / nyq
257 | b, a = signal.butter(order, normal_cutoff, btype='low', analog=False)
258 | return b, a
259 | def butter_lowpass_filter(data, cutoff, fs, order=5):
260 | b, a = butter_lowpass(cutoff, fs, order=order)
261 | y = signal.lfilter(b, a, data)
262 | return y
263 | def butter_highpass(cutoff, fs, order=3):
264 | nyq = 0.5 * fs
265 | normal_cutoff = cutoff / nyq
266 | b, a = signal.butter(order, normal_cutoff, btype='high', analog=False)
267 | return b, a
268 | def butter_highpass_filter(data, cutoff, fs, order=5):
269 | b, a = butter_highpass(cutoff, fs, order=order)
270 | y = signal.filtfilt(b, a, data)
271 | return y
272 |
273 | last_valid_value = 5
274 | counter = 0
275 |
276 | def _to_signed_24bit(msb: int, middle: int, lsb: int) -> int:
277 | # Combine the bytes into a 24-bit integer
278 | combined = (msb << 16) | (middle << 8) | lsb
279 |
280 | # Check the sign bit (bit 7 of the MSB)
281 | if (msb & 0x80) != 0:
282 | # Convert to negative 24-bit signed integer
283 | combined -= 1 << 24
284 |
285 | return combined
286 |
287 | def the_input_is_valid(input_list):
288 | # print('oks')
289 | global last_valid_value, counter
290 | msb = input_list[24]
291 | middle = input_list[25]
292 | lsb = input_list[26]
293 |
294 | current_value = _to_signed_24bit(msb, middle, lsb)
295 |
296 | if last_valid_value is None:
297 | _last_valid_value = current_value
298 | #print('here')
299 | return False
300 |
301 | difference = abs(current_value - last_valid_value)
302 | if difference > 5000:
303 | print(f'Corrupted data detected _counter: {counter}')
304 | counter += 1
305 | return False
306 |
307 | else:
308 | _last_valid_value = current_value
309 |
310 | return True
311 |
312 | while 1:
313 |
314 |
315 | #print ("1", button_state)
316 | #print("2", button_state_2)
317 |
318 | #print ("ok3")
319 | button_state = line_1.get_value()
320 | #print (button_state)
321 | if button_state == 1:
322 | test_DRDY = 10
323 | if test_DRDY == 10 and button_state == 0:
324 | test_DRDY = 0
325 |
326 | output=spi.readbytes(27)
327 |
328 | cs_line.set_value(0)
329 | output_2=spi_2.readbytes(27)
330 | cs_line.set_value(1)
331 |
332 | if the_input_is_valid(output_2):
333 | #print('test')
334 | #continue
335 |
336 | # print (output[0],output[1],output[2])
337 | if output_2[0]==192 and output_2[1] == 0 and output_2[2] == 8:
338 | #print ("ok4")
339 | for a in range (3,25,3):
340 | voltage_1=(output[a]<<8)| output[a+1]
341 | voltage_1=(voltage_1<<8)| output[a+2]
342 | convert_voktage=voltage_1|data_test
343 | if convert_voktage==data_check:
344 | voltage_1_after_convert=(voltage_1-16777214)
345 | else:
346 | voltage_1_after_convert=voltage_1
347 | channel_num = (a/3)
348 |
349 | result[int (channel_num)]=round(1000000*4.5*(voltage_1_after_convert/16777215),2)
350 |
351 | data_1ch_test.append(result[1])
352 | data_2ch_test.append(result[2])
353 | data_3ch_test.append(result[3])
354 | data_4ch_test.append(result[4])
355 | data_5ch_test.append(result[5])
356 | data_6ch_test.append(result[6])
357 | data_7ch_test.append(result[7])
358 | data_8ch_test.append(result[8])
359 |
360 |
361 | for a in range (3,25,3):
362 | voltage_1=(output_2[a]<<8)| output_2[a+1]
363 | voltage_1=(voltage_1<<8)| output_2[a+2]
364 | convert_voktage=voltage_1|data_test
365 | if convert_voktage==data_check:
366 | voltage_1_after_convert=(voltage_1-16777214)
367 | else:
368 | voltage_1_after_convert=voltage_1
369 | channel_num = (a/3)
370 |
371 | result_2[int (channel_num)]=round(1000000*4.5*(voltage_1_after_convert/16777215),2)
372 |
373 | data_9ch_test.append(result_2[1])
374 | data_10ch_test.append(result_2[2])
375 | data_11ch_test.append(result_2[3])
376 | data_12ch_test.append(result_2[4])
377 | data_13ch_test.append(result_2[5])
378 | data_14ch_test.append(result_2[6])
379 | data_15ch_test.append(result_2[7])
380 | data_16ch_test.append(result_2[8])
381 |
382 |
383 |
384 |
385 | if len(data_9ch_test)==sample_len:
386 |
387 |
388 | data_after_1 = data_1ch_test
389 | dataset_1 = data_before_1 + data_after_1
390 | data_before_1 = dataset_1[250:]
391 | data_for_graph_1 = dataset_1
392 |
393 | data_filt_numpy_high_1 = butter_highpass_filter(data_for_graph_1, highcut, fps)
394 | data_for_graph_1 = butter_lowpass_filter(data_filt_numpy_high_1, lowcut, fps)
395 |
396 | axis[0,0].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_1[250:], color = '#0a0b0c')
397 | axis[0,0].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_1[50]-y_minus_graph, data_for_graph_1[150]+y_plus_graph])
398 |
399 | # 2
400 | data_after_2 = data_2ch_test
401 | dataset_2 = data_before_2 + data_after_2
402 | data_before_2 = dataset_2[250:]
403 | data_for_graph_2 = dataset_2
404 |
405 | data_filt_numpy_high_2 = butter_highpass_filter(data_for_graph_2, highcut, fps)
406 | data_for_graph_2 = butter_lowpass_filter(data_filt_numpy_high_2, lowcut, fps)
407 |
408 | axis[1,0].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_2[250:], color = '#0a0b0c')
409 | axis[1,0].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_2[50]-y_minus_graph, data_for_graph_2[150]+y_plus_graph])
410 |
411 | # 3
412 | data_after_3 = data_3ch_test
413 | dataset_3 = data_before_3 + data_after_3
414 | data_before_3 = dataset_3[250:]
415 | data_for_graph_3 = dataset_3
416 |
417 | data_filt_numpy_high_3 = butter_highpass_filter(data_for_graph_3, highcut, fps)
418 | data_for_graph_3 = butter_lowpass_filter(data_filt_numpy_high_3, lowcut, fps)
419 |
420 | axis[2,0].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_3[250:], color = '#0a0b0c')
421 | axis[2,0].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_3[50]-y_minus_graph, data_for_graph_3[150]+y_plus_graph])
422 |
423 | # 4
424 | data_after_4 = data_4ch_test
425 | dataset_4 = data_before_4 + data_after_4
426 | data_before_4 = dataset_4[250:]
427 | data_for_graph_4 = dataset_4
428 |
429 | data_filt_numpy_high_4 = butter_highpass_filter(data_for_graph_4, highcut, fps)
430 | data_for_graph_4 = butter_lowpass_filter(data_filt_numpy_high_4, lowcut, fps)
431 |
432 | axis[3,0].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_4[250:], color = '#0a0b0c')
433 | axis[3,0].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_4[50]-y_minus_graph, data_for_graph_4[150]+y_plus_graph])
434 |
435 | #5
436 | data_after_5 = data_5ch_test
437 | dataset_5 = data_before_5 + data_after_5
438 | data_before_5 = dataset_5[250:]
439 | data_for_graph_5 = dataset_5
440 |
441 | data_filt_numpy_high_5 = butter_highpass_filter(data_for_graph_5, highcut, fps)
442 | data_for_graph_5 = butter_lowpass_filter(data_filt_numpy_high_5, lowcut, fps)
443 |
444 | axis[0,1].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_5[250:], color = '#0a0b0c')
445 | axis[0,1].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_5[50]-y_minus_graph, data_for_graph_5[150]+y_plus_graph])
446 |
447 | #6
448 | data_after_6 = data_6ch_test
449 | dataset_6 = data_before_6 + data_after_6
450 | data_before_6 = dataset_6[250:]
451 | data_for_graph_6 = dataset_6
452 |
453 | data_filt_numpy_high_6 = butter_highpass_filter(data_for_graph_6, highcut, fps)
454 | data_for_graph_6 = butter_lowpass_filter(data_filt_numpy_high_6, lowcut, fps)
455 |
456 | axis[1,1].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_6[250:], color = '#0a0b0c')
457 | axis[1,1].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_6[50]-y_minus_graph, data_for_graph_6[150]+y_plus_graph])
458 |
459 | #7
460 | data_after_7 = data_7ch_test
461 | dataset_7 = data_before_7 + data_after_7
462 | data_before_7 = dataset_7[250:]
463 | data_for_graph_7 = dataset_7
464 |
465 | data_filt_numpy_high_7 = butter_highpass_filter(data_for_graph_7, highcut, fps)
466 | data_for_graph_7 = butter_lowpass_filter(data_filt_numpy_high_7, lowcut, fps)
467 |
468 | axis[2,1].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_7[250:], color = '#0a0b0c')
469 | axis[2,1].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_7[50]-y_minus_graph, data_for_graph_1[150]+y_plus_graph])
470 |
471 | #8
472 | data_after_8 = data_8ch_test
473 | dataset_8 = data_before_8 + data_after_8
474 | data_before_8 = dataset_8[250:]
475 | data_for_graph_8 = dataset_8
476 |
477 | data_filt_numpy_high_8 = butter_highpass_filter(data_for_graph_8, highcut, fps)
478 | data_for_graph_8 = butter_lowpass_filter(data_filt_numpy_high_8, lowcut, fps)
479 |
480 | axis[3,1].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_8[250:], color = '#0a0b0c')
481 | axis[3,1].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_8[50]-y_minus_graph, data_for_graph_8[150]+y_plus_graph])
482 |
483 | # 9
484 | data_after_9 = data_9ch_test
485 | dataset_9 = data_before_9 + data_after_9
486 | data_before_9 = dataset_9[250:]
487 | data_for_graph_9 = dataset_9
488 |
489 | data_filt_numpy_high_9 = butter_highpass_filter(data_for_graph_9, highcut, fps)
490 | data_for_graph_9 = butter_lowpass_filter(data_filt_numpy_high_9, lowcut, fps)
491 |
492 | axis[0,2].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_9[250:], color = '#0a0b0c')
493 | axis[0,2].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_9[50]-y_minus_graph, data_for_graph_9[150]+y_plus_graph])
494 |
495 | # 10
496 | data_after_10 = data_10ch_test
497 | dataset_10 = data_before_10 + data_after_10
498 | data_before_10 = dataset_10[250:]
499 | data_for_graph_10 = dataset_10
500 |
501 | data_filt_numpy_high_10 = butter_highpass_filter(data_for_graph_10, highcut, fps)
502 | data_for_graph_10 = butter_lowpass_filter(data_filt_numpy_high_10, lowcut, fps)
503 |
504 | axis[1,2].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_10[250:], color = '#0a0b0c')
505 | axis[1,2].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_10[50]-y_minus_graph, data_for_graph_10[150]+y_plus_graph])
506 |
507 | # 11
508 | data_after_11 = data_11ch_test
509 | dataset_11 = data_before_11 + data_after_11
510 | data_before_11 = dataset_11[250:]
511 | data_for_graph_11 = dataset_11
512 |
513 | data_filt_numpy_high_11 = butter_highpass_filter(data_for_graph_11, highcut, fps)
514 | data_for_graph_11 = butter_lowpass_filter(data_filt_numpy_high_11, lowcut, fps)
515 |
516 | axis[2,2].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_11[250:], color = '#0a0b0c')
517 | axis[2,2].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_11[50]-y_minus_graph, data_for_graph_11[150]+y_plus_graph])
518 |
519 | # 12
520 | data_after_12 = data_12ch_test
521 | dataset_12 = data_before_12 + data_after_12
522 | data_before_12 = dataset_12[250:]
523 | data_for_graph_12 = dataset_12
524 |
525 | data_filt_numpy_high_12 = butter_highpass_filter(data_for_graph_12, highcut, fps)
526 | data_for_graph_12 = butter_lowpass_filter(data_filt_numpy_high_12, lowcut, fps)
527 |
528 | axis[3,2].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_12[250:], color = '#0a0b0c')
529 | axis[3,2].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_12[50]-y_minus_graph, data_for_graph_12[150]+y_plus_graph])
530 |
531 | # 13
532 | data_after_13 = data_13ch_test
533 | dataset_13 = data_before_13 + data_after_13
534 | data_before_13 = dataset_13[250:]
535 | data_for_graph_13 = dataset_13
536 |
537 | data_filt_numpy_high_13 = butter_highpass_filter(data_for_graph_13, highcut, fps)
538 | data_for_graph_13 = butter_lowpass_filter(data_filt_numpy_high_13, lowcut, fps)
539 |
540 | axis[0,3].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_13[250:], color = '#0a0b0c')
541 | axis[0,3].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_13[50]-y_minus_graph, data_for_graph_13[150]+y_plus_graph])
542 |
543 | # 14
544 | data_after_14 = data_14ch_test
545 | dataset_14 = data_before_14 + data_after_14
546 | data_before_14 = dataset_14[250:]
547 | data_for_graph_14 = dataset_14
548 |
549 | data_filt_numpy_high_14 = butter_highpass_filter(data_for_graph_14, highcut, fps)
550 | data_for_graph_14 = butter_lowpass_filter(data_filt_numpy_high_14, lowcut, fps)
551 |
552 | axis[1,3].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_14[250:], color = '#0a0b0c')
553 | axis[1,3].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_14[50]-y_minus_graph, data_for_graph_14[150]+y_plus_graph])
554 |
555 | # 15
556 | data_after_15 = data_15ch_test
557 | dataset_15 = data_before_15 + data_after_15
558 | data_before_15 = dataset_15[250:]
559 | data_for_graph_15 = dataset_15
560 |
561 | data_filt_numpy_high_15 = butter_highpass_filter(data_for_graph_15, highcut, fps)
562 | data_for_graph_15 = butter_lowpass_filter(data_filt_numpy_high_15, lowcut, fps)
563 |
564 | axis[2,3].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_15[250:], color = '#0a0b0c')
565 | axis[2,3].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_15[50]-y_minus_graph, data_for_graph_15[150]+y_plus_graph])
566 |
567 | # 16
568 | data_after_16 = data_16ch_test
569 | dataset_16 = data_before_16 + data_after_16
570 | data_before_16 = dataset_16[250:]
571 | data_for_graph_16 = dataset_16
572 |
573 | data_filt_numpy_high_16 = butter_highpass_filter(data_for_graph_16, highcut, fps)
574 | data_for_graph_16 = butter_lowpass_filter(data_filt_numpy_high_16, lowcut, fps)
575 |
576 | axis[3,3].plot(range(axis_x,axis_x+sample_lens,1),data_for_graph_16[250:], color = '#0a0b0c')
577 | axis[3,3].axis([axis_x-x_minux_graph, axis_x+x_plus_graph, data_for_graph_16[50]-y_minus_graph, data_for_graph_16[150]+y_plus_graph])
578 |
579 |
580 | plt.pause(0.0000000000001)
581 |
582 | axis_x=axis_x+sample_lens
583 | data_1ch_test = []
584 | data_2ch_test = []
585 | data_3ch_test = []
586 | data_4ch_test = []
587 | data_5ch_test = []
588 | data_6ch_test = []
589 | data_7ch_test = []
590 | data_8ch_test = []
591 | data_9ch_test = []
592 | data_10ch_test = []
593 | data_11ch_test = []
594 | data_12ch_test = []
595 | data_13ch_test = []
596 | data_14ch_test = []
597 | data_15ch_test = []
598 | data_16ch_test = []
599 |
600 | else:
601 | pass
602 |
603 | #data_16ch_test = []
604 |
605 |
606 |
--------------------------------------------------------------------------------