From 7723427464d1c9fefc36b412f7a7b0935b0cef1a Mon Sep 17 00:00:00 2001 From: Orell-Pieter Schwarzbach <2122623@stud.hs-mannheim.de> Date: Tue, 13 Jun 2023 09:14:16 +0200 Subject: [PATCH] Upload files to 'Archiv' --- Archiv/StartUpTest.py | 17 ++++- Archiv/TotOnline.py | 43 +++++++++-- Archiv/mainTest.py | 4 + Archiv/pi_numbers_separated_I2C.py | 115 +++++++++++++++++++++++++++++ Archiv/pi_numbers_separated_USB.py | 80 ++++++++++++++++++++ 5 files changed, 251 insertions(+), 8 deletions(-) create mode 100644 Archiv/mainTest.py create mode 100644 Archiv/pi_numbers_separated_I2C.py create mode 100644 Archiv/pi_numbers_separated_USB.py diff --git a/Archiv/StartUpTest.py b/Archiv/StartUpTest.py index 9dbebe2..64fefeb 100644 --- a/Archiv/StartUpTest.py +++ b/Archiv/StartUpTest.py @@ -7,6 +7,21 @@ from scipy import stats class StartUPTest: + @staticmethod + def run_all_tests(binary_data: str): + # Run monobit_test + p_value, result = StartUPTest.monobit_test(binary_data) + if not result: + return False + + # Run chi_square + p_value, result, chi2_statistic = StartUPTest.chi_square(binary_data) + if not result: + return False + + # All tests passed + return True + @staticmethod def monobit_test(binary_data: str): @@ -42,4 +57,4 @@ class StartUPTest: chi2_statistic, p_value = stats.chisquare(observed_frequencies, f_exp=expected_frequencies) - return p_value, p_value >= 0.01, chi2_statistic \ No newline at end of file + return p_value, p_value >= 0.01, chi2_statistic diff --git a/Archiv/TotOnline.py b/Archiv/TotOnline.py index 9768ec7..3f94271 100644 --- a/Archiv/TotOnline.py +++ b/Archiv/TotOnline.py @@ -8,9 +8,41 @@ from scipy.special import gammaincc as gammaincc class TotOnline: + + @staticmethod + def run_all_tests(binary_data: str): + # Run total_failure_test + p_value, result = TotOnline.total_failure_test(binary_data, 10) + if not result: + return False + + # Run monobit_test + p_value, result = TotOnline.monobit_test(binary_data) + if not result: + return False + + # Run block_frequency_test + p_value, result = TotOnline.block_frequency_test(binary_data, 128) + if not result: + return False + + # Run run_test + p_value, result = TotOnline.run_test(binary_data) + if not result: + return False + + # Run longest_one_block_test + if len(binary_data)>127: + p_value, result = TotOnline.longest_one_block_test(binary_data) + if not result: + return False + + # All tests passed + return True + @staticmethod def total_failure_test(binary_data: str, pattern_length=10): - + length_of_binary_data = len(binary_data) # Augment the n-bit sequence to create n overlapping m-bit sequences by appending m-1 bits @@ -157,11 +189,8 @@ class TotOnline: length_of_binary_data = len(binary_data) # print('Length of binary string: ', length_of_binary_data) - # Initialized k, m. n, pi and v_values - if length_of_binary_data < 128: - # Not enough data to run this test - return 0.00000, 'Error: Not enough data to run this test' - elif length_of_binary_data < 6272: + # Initialized k, m. n, pi and v_values + if length_of_binary_data < 6272: k = 3 m = 8 v_values = [1, 2, 3, 4] @@ -223,4 +252,4 @@ class TotOnline: p_value = gammaincc(float(k / 2), float(xObs / 2)) - return p_value, (p_value > 0.01) \ No newline at end of file + return p_value, (p_value > 0.01) diff --git a/Archiv/mainTest.py b/Archiv/mainTest.py new file mode 100644 index 0000000..9b9b51d --- /dev/null +++ b/Archiv/mainTest.py @@ -0,0 +1,4 @@ +from pi_numbers_separated_I2C_function import analyze_data + +result = analyze_data(8, 10000, startup=False) +print(result) diff --git a/Archiv/pi_numbers_separated_I2C.py b/Archiv/pi_numbers_separated_I2C.py new file mode 100644 index 0000000..71e03eb --- /dev/null +++ b/Archiv/pi_numbers_separated_I2C.py @@ -0,0 +1,115 @@ +import os +import time +from smbus import SMBus +from StartUpTest import StartUPTest +from TotOnline import TotOnline +import binascii + +addr = 0x8 +bus = SMBus(1) + +# define the number of numbers and bits per number to generate +num_numbers = 6 +bits_per_number = 1000000 + +# calculate the total file size in bits +filesize = num_numbers * bits_per_number + +# define the filename +filename = f'{num_numbers}numbers_{bits_per_number}bits' + +start_time_ges = start_time = time.time() +start_time = time.time() # start time of read + +with open(filename + '.bin', 'wb') as file: + for i in range(num_numbers): + # write the separator between numbers except for the last one + #if i != num_numbers - 1: + #file.write(b'' + b'') + for j in range(bits_per_number // 8): + # read one byte from the serial port + data = bus.read_byte(addr) + file.write(bytes([data])) + time.sleep(0.0000001) + +end_time = time.time() # end time of read +elapsed_time = end_time - start_time + +seconds = int(elapsed_time) +milliseconds = int((elapsed_time % 1) * 1000) + +new_filename = f"{filename}_TimeInSeconds_{seconds}_{milliseconds}.bin" # filename in format filename_seconds_milliseconds as txt with needed time to finish read + +os.rename(filename + '.bin', new_filename) # change filename to new filename + +'''with open(new_filename, 'rb') as f: + # Read the contents of the file as bytes + content = f.read() + result = StartUPTest.monobit_test(content) + print(result[0]) + print(result[1]) + result = StartUPTest.chi_square(content) + print(result[0]) + print(result[1])''' + +with open(new_filename, 'rb') as f: + # Read the contents of the file as bytes + content = f.read() + +# Convert the bytes to a string of hexadecimal digits with zero padding +hex_str = ''.join(format(byte, '02x') for byte in content).zfill(num_numbers * (bits_per_number // 4)) + +# Write the hex string to the file with separators and newlines +with open(filename + '.txt', 'w') as f: + for i in range(num_numbers): + hex_number = hex_str[i * (bits_per_number // 4) : (i+1) * (bits_per_number // 4)] + # write the separator between numbers except for the last one + if i != num_numbers - 1: + hex_number += '//' + f.write(hex_number + '\n') + +print(f"time needed in seconds: {elapsed_time:.2f}. New filename: {new_filename}.") # console write + +# Open the file in binary mode +with open(new_filename, 'rb') as f: + # Read the contents of the file as bytes + content = f.read() + +# Convert the bytes to a string of binary digits +binary_str = ''.join(format(byte, '08b') for byte in content) + +# Write the binary string back to the file with separators +with open(filename + '_binary.txt', 'w') as f: + for i in range(0, len(binary_str), bits_per_number): + binary_number = binary_str[i:i+bits_per_number] + f.write(binary_number + '') + +# Open the file in binary mode +with open(filename + '_binary.txt', 'rb') as f: + # Read the contents of the file as bytes + content = f.read() + #print(content) + + result = TotOnline.total_failure_test(content) + print(result[0]) + print(result[1]) + result = TotOnline.block_frequency_test(content) + print(result[0]) + print(result[1]) + result = TotOnline.longest_one_block_test(content) + print(result[0]) + print(result[1]) + result = TotOnline.monobit_test(content) + print(result[0]) + print(result[1]) + result = TotOnline.run_test(content) + print(result[0]) + print(result[1]) + +end_time_ges = time.time() +elapsed_time_ges = end_time_ges - start_time_ges + + +seconds_ges = int(elapsed_time_ges) +milliseconds_ges = int((elapsed_time_ges % 1) * 1000) +print(elapsed_time_ges) \ No newline at end of file diff --git a/Archiv/pi_numbers_separated_USB.py b/Archiv/pi_numbers_separated_USB.py new file mode 100644 index 0000000..ed144d5 --- /dev/null +++ b/Archiv/pi_numbers_separated_USB.py @@ -0,0 +1,80 @@ +from math import fabs as fabs +from math import sqrt as sqrt +import os +import serial +import time +from StartUpTest import StartUPTest +from TotOnline import TotOnline + +ser = serial.Serial('/dev/ttyACM0', 115200) # change port to input port from arduino + +# define the number of numbers and bits per number to generate +num_numbers = 4 +bits_per_number = 5000 + +# calculate the total file size in bits +filesize = num_numbers * bits_per_number + +# define the filename +filename = f'{num_numbers}numbers_{bits_per_number}bits' + +start_time = time.time() # start time of read + +with open(filename + '.bin', 'wb') as file: + for i in range(num_numbers): + # write the separator between numbers except for the last one + if i != num_numbers - 1: + file.write(b'' + b'') + for j in range(bits_per_number // 8): + # read one byte from the serial port + byte = ser.read(1) + file.write(byte) + +end_time = time.time() # end time of read +elapsed_time = end_time - start_time + +seconds = int(elapsed_time) +milliseconds = int((elapsed_time % 1) * 1000) + +new_filename = f"{filename}_TimeInSeconds_{seconds}_{milliseconds}.bin" # filename in format filename_seconds_milliseconds as txt with needed time to finish read + +os.rename(filename + '.bin', new_filename) # change filename to new filename + +# Open the file in binary mode +with open(new_filename, 'rb') as f: + # Read the contents of the file as bytes + content = f.read() + result = StartUPTest.monobit_test(content) + print(result[0]) + print(result[1]) + result = StartUPTest.autocorrelation_test(content) + print(result[1]) + +# Convert the bytes to a string of hexadecimal digits with zero padding +hex_str = ''.join(format(byte, '02x') for byte in content).zfill(num_numbers * (bits_per_number // 4)) + +# Write the hex string to the file with separators and newlines +with open(filename + '.txt', 'w') as f: + for i in range(num_numbers): + hex_number = hex_str[i * (bits_per_number // 4) : (i+1) * (bits_per_number // 4)] + # write the separator between numbers except for the last one + if i != num_numbers - 1: + hex_number += '//' + f.write(hex_number + '\n') + +print(f"time needed in seconds: {elapsed_time:.2f}. New filename: {new_filename}.") # console write + +# Open the file in binary mode +with open(new_filename, 'rb') as f: + # Read the contents of the file as bytes + content = f.read() + +# Convert the bytes to a string of binary digits +binary_str = ''.join(format(byte, '08b') for byte in content) + +# Write the binary string back to the file with separators +with open(filename + '_binary.txt', 'w') as f: + for i in range(0, len(binary_str), bits_per_number): + binary_number = binary_str[i:i+bits_per_number] + f.write(binary_number + '\n') +