def hct(freq_b, duration, fs_, ratio_, which_turned_off=[False, False, False, False]): output = np.zeros(duration * fs_) if not which_turned_off[0]: output += fun.gen_simple(2, 0.25, freq_b, "sin", 0) if not which_turned_off[1]: output += fun.gen_simple(2, 0.25, 2 * ratio_ * freq_b, "sin", -6) if not which_turned_off[2]: output += fun.gen_simple(2, 0.25, 3 * ratio_ * freq_b, "sin", -9) if not which_turned_off[3]: output += fun.gen_simple(2, 0.25, 4 * ratio_ * freq_b, "sin", -12) return output.astype(np.int16)
import src.functions as fun import time import sounddevice as sd base_freq = 1000 ref_wav = fun.gen_simple(1, 0.5, base_freq, "sin", -3) delta_f = 15 choices = [] test_freq = [] correct_ans = [] prev_ans_correct = False for _ in range(0, 50): new_wav = fun.gen_simple(1, 0.5, base_freq + delta_f, "sin", -3) sd.play(ref_wav, fun.fs) time.sleep(1.5) sd.play(new_wav, fun.fs) choice = input('Który ton byl wyzszy? [1/2]') choices.append(choice) test_freq.append(delta_f) if base_freq > (base_freq + delta_f): correct = '1' else: correct = '2' user_correct = choice == correct if user_correct and prev_ans_correct:
import numpy as np def butter_bandpass_filter(data, lowcut, highcut, fs, order=5): nyq = 0.5 * fs low = lowcut / nyq high = highcut / nyq range = [low, high] b, a = butter(order, range, btype='band') y = lfilter(b, a, data) return y if __name__ == '__main__': noise_wav = fun.gen_simple(2, 0.5, 3000, "noise", -6) filtered_noise = butter_bandpass_filter(noise_wav, 800, 1200, fun.fs) noise_right = np.zeros(len(noise_wav), dtype=np.int16) stereo_noise = np.array([noise_wav, noise_right]).T user_input = '' sine_amp = -3 while user_input != 'N': sine_wav = fun.gen_simple(0.1, 0.5, 1000, "sin", sine_amp) sine_right = np.zeros(len(sine_wav), dtype=np.int16) stereo_sine = np.array([sine_wav, sine_right]).T sd.play(stereo_noise, fun.fs) time.sleep(2) sd.play(stereo_sine, fun.fs)
output = np.zeros(duration * fs_) if not which_turned_off[0]: output += fun.gen_simple(2, 0.25, freq_b, "sin", 0) if not which_turned_off[1]: output += fun.gen_simple(2, 0.25, 2 * ratio_ * freq_b, "sin", -6) if not which_turned_off[2]: output += fun.gen_simple(2, 0.25, 3 * ratio_ * freq_b, "sin", -9) if not which_turned_off[3]: output += fun.gen_simple(2, 0.25, 4 * ratio_ * freq_b, "sin", -12) return output.astype(np.int16) if __name__ == '__main__': freq = 1000 ref = fun.gen_simple(2, 0.25, freq, "sin", 0) harm_ratios = np.linspace(0.5, 2.1, num=16, endpoint=False) choices = [] task_two_ratios = [] for ratio in harm_ratios: tones = hct( freq, 2, fs, ratio, ) sd.play(ref) sleep(2.3) sd.play(tones) choice = input('do you hear the same pich y/n') choices.append(choice)
from src import functions import random import time import sounddevice as sd distances = [-3.5, -4.5, -5, -6, -8] ref_wav = functions.gen_simple(1, 0.5, 1000, "sin", -3) random_wav = [functions.gen_simple(1, 0.5, 1000, "sin", x) for x in distances] if __name__ == '__main__': tests = distances * 10 random.shuffle(tests) choices = [] for test in tests: sd.play(ref_wav, functions.fs) time.sleep(2) sd.play(random_wav[distances.index(test)], functions.fs) choice = input('Roznica? [Y/N]') choices.append(choice) with open("../output/Output1.txt", "w") as f: for (test, choice) in zip(tests, choices): f.write(f'Różnica {test}, wcisnieto {choice}\n') f.close()
import src.functions as fun import random import numpy as np import sounddevice as sd distances = [ -4.5, -4.8, -5, -5.3, -5.5, -5.8, -6, -6.5, -7, -7.5, -3.5, -3.2, -3, -2.7, -2.5, -2.2, -2, -2.5, -1, -0.5 ] ref_wav = fun.gen_simple(1, 0.5, 1000, "sin", -4) random_wav = [fun.gen_simple(1, 0.5, 1000, "sin", x) for x in distances] if __name__ == '__main__': tests = distances * 5 random.shuffle(tests) choices = [] for test in tests: data = np.array([ref_wav, random_wav[distances.index(test)]]) data = np.ndarray.transpose(data) sd.play(data, fun.fs) choice = input('Który jest głośniejszy? [1/2]') choices.append(choice) with open("../../output/lab3/zad1_results.txt", "w") as f: for (test, choice) in zip(tests, choices): f.write(f'Różnica {test}, wcisnieto {choice}\n') f.close()
import src.functions as fun import numpy as np import sounddevice as sd from time import sleep # Global Variables fs = 48000 max_int16 = 32767 if __name__ == '__main__': ref = fun.gen_simple(1, 0.2, 0, "noise", 0) choices = [] tests = [] user_input = 'y' offset = -6 while user_input == 'y': left = np.copy(ref) left[int(fs / 4):int(3 * fs / 4)] += fun.gen_simple(0.5, 0.2, 1000, "sin", offset) right = np.zeros(len(left), dtype=np.int16) output = np.array((left, right)).T sd.play(output, fs) sleep(1.2) user_input = input("Did you hear the tone? y/n ") offset -= 1 sin = fun.gen_simple(0.5, 0.2, 1100, "sin", offset) sin_right = np.zeros(len(sin), dtype=np.int16) out = np.ndarray.transpose(np.array((sin, sin_right)))
import src.functions as fun import random import numpy as np import sounddevice as sd f_dist = [2, 5, 8, 10, 15, 20, -2, -5, -8, -10, -15, -20] ref_wav = fun.gen_simple(1, 0.5, 2200, "sin", -3) random_wav = [fun.gen_simple(1, 0.5, 2200 + x, "sin", -3) for x in f_dist] if __name__ == '__main__': tests = f_dist * 5 random.shuffle(tests) choices = [] for test in tests: data = np.array([ref_wav, random_wav[f_dist.index(test)]]) data = np.ndarray.transpose(data) sd.play(data, fun.fs) choice = input('Który był wyższy [1/2]') choices.append(choice) with open("../../output/lab3/zad3_results.txt", "w") as f: for (test, choice) in zip(tests, choices): f.write(f'Różnica {test}, wcisnieto {choice}\n')
if max_value == 0: for signal in args: if max(signal) > max_value: max_value = max(signal) signal_sum = sum(list(args)) normalize_factor = max(signal_sum) / max_value normalized_signal = signal_sum / normalize_factor # print(f'Adding {len(args)} signals with max_value = {max_value}, normalize_factor = {normalize_factor}') # print(f'Summed signal with max value = {max(signal_sum)}, after normalization max value = {max(normalized_signal)}') return normalized_signal.astype(dtype) H1 = fun.gen_simple(2, 0.5, 1000, "sin", -3, dtype=np.float) H2 = fun.gen_simple(2, 0.5, 2000, "sin", -3, dtype=np.float) H3 = fun.gen_simple(2, 0.5, 3000, "sin", -3, dtype=np.float) H4 = fun.gen_simple(2, 0.5, 4000, "sin", -3, dtype=np.float) # First tone HCT1 = add_with_normalize(H1, H2, H3, H4, max_value=max(H1), dtype=np.int16) # Second tone HCT2 = add_with_normalize(H2, H3, H4, max_value=max(H1), dtype=np.int16) # Play sound sd.play(HCT1, fun.fs, blocking=True) sleep(0.3) sd.play(HCT2, fun.fs, blocking=True)
from src import functions import time import sounddevice as sd difference = 0 ref_dur = 2 first_wav = functions.gen_simple(2, 0.5, 3000, "sin", -3) choice = '' if __name__ == '__main__': while choice != 'Y': second_wav = functions.gen_simple(ref_dur + difference, 0.5, 3000, "noise", -3) sd.play(first_wav, functions.fs) time.sleep(2.3) sd.play(second_wav, functions.fs) time.sleep(ref_dur + difference + 0.3) sd.play(first_wav, functions.fs) choice = input('Różnica w głośności tonów --> [Y/N]') if choice == 'Y': print(ref_dur + difference) difference += 0.5
from scipy.signal import butter, lfilter import src.functions as fun import numpy as np import sounddevice as sd from time import sleep if __name__ == '__main__': sine_wav = fun.gen_simple(2, 0.5, 1000, "sin", -20, dtype=np.int32) sine_right = np.zeros(len(sine_wav), dtype=np.int16) stereo_sine = np.array([sine_wav.astype(np.int16), sine_right]).T h1 = fun.gen_simple(2, 0.5, 2000, "sin", -20, dtype=np.int32) h2 = fun.gen_simple(2, 0.5, 3000, "sin", -20, dtype=np.int32) h3 = fun.gen_simple(2, 0.5, 4000, "sin", -20, dtype=np.int32) hct = ((sine_wav + h1 + h2 + h3) / 4).astype(np.int16) stereo_hct = np.array([hct, sine_right]).T tone_amp = -20 user_input = '' while user_input != 'y': sine = fun.gen_simple(2, 0.5, 1000, "sin", tone_amp, dtype=np.int32) stereo_sine = np.array([sine_wav.astype(np.int16), sine_right]).T sd.play(stereo_sine, fun.fs) sleep(2.5) sd.play(stereo_hct, fun.fs) user_input = input("Were the sound equal in loudness? y/n ") tone_amp += 1 print(tone_amp - 1)