Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. from math import sin, pi
  3. from numpy import arange
  4.  
  5. def print_menu():
  6. print('1 - ton prosty')
  7. print('2 - suma tonów')
  8. print('3 - różnica tonów')
  9. return input()
  10.  
  11. def get_tone_params():
  12. a = float(input('Amplituda: '))
  13. b = float(input('Częstotliwość: '))
  14. c = float(input('Faza: '))
  15. return a, b, c
  16.  
  17. def pure_tone(t, A, freq, phase):
  18. a_freq = 2 * pi * freq
  19. return A * sin(t * a_freq + phase)
  20.  
  21. def transform_values(A, freq, phase):
  22. x = arange(0, 6, 0.01)
  23. y = list(map(lambda x: pure_tone(x, A, freq, phase), x))
  24. return x, y
  25.  
  26. def sum_tones(first, second):
  27. for index, val in enumerate(first):
  28. first[index] = first[index] + second[index]
  29. return first
  30.  
  31. def substract_tones(first, second):
  32. for index, val in enumerate(first):
  33. first[index] = first[index] - second[index]
  34. return first
  35.  
  36.  
  37. choice = print_menu()
  38. x1 = 0
  39. x2 = 0
  40. y1 = 0
  41. y2 = 0
  42.  
  43. if choice == '1':
  44. print('Parametry tonu:')
  45. args = get_tone_params()
  46. [x1, y1] = transform_values(*args)
  47. elif choice == '2':
  48. print('Parametry 1 tonu:')
  49. args1 = get_tone_params()
  50. [x1, y1] = transform_values(*args1)
  51. print('Parametry 2 tonu:')
  52. args2 = get_tone_params()
  53. [x2, y2] = transform_values(*args2)
  54. y1 = sum_tones(y1, y2)
  55. elif choice == '3':
  56. print('Parametry 1 tonu:')
  57. args1 = get_tone_params()
  58. [x1, y1] = transform_values(*args1)
  59. print('Parametry 2 tonu:')
  60. args2 = get_tone_params()
  61. [x2, y2] = transform_values(*args2)
  62. y1 = substract_tones(y1, y2)
  63. else:
  64. print('no such option')
  65.  
  66. plt.plot(x1, y1)
  67. plt.ylabel('sinus x')
  68. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement