# lab_2(signals)

Feb 28th, 2021 (edited)
713
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import sys
2. import matplotlib
3. import numpy as np
4. import matplotlib.pyplot as plt
5. import sounddevice as sd
6. import scipy as np
7. from numpy.dual import fft
8.
9. duration = 1
10. amplitude = -0.1
11. fs = 10000
12. timeSamples = np.arange(np.ceil(duration * fs)) / fs
13. do = amplitude * np.sin(2 * np.pi * 261 * timeSamples)
14. re = amplitude * np.sin(2 * np.pi * 293 * timeSamples)
15. mi = amplitude * np.sin(2 * np.pi * 329 * timeSamples)
16. fa = amplitude * np.sin(2 * np.pi * 348 * timeSamples)
17. sol = amplitude * np.sin(2 * np.pi * 391 * timeSamples)
18. lya = amplitude * np.sin(2 * np.pi * 439 * timeSamples)
19. si = amplitude * np.sin(2 * np.pi * 493 * timeSamples)
20. do_3 = amplitude * np.sin(2 * np.pi * 522 * timeSamples)
21. mi_3 = amplitude * np.sin(2 * np.pi * 658 * timeSamples)
22. re_3 = amplitude * np.sin(2 * np.pi * 586 * timeSamples)
23. re_3_diez = amplitude * np.sin(2 * np.pi * 621 * timeSamples)
24. fa_diez = amplitude * np.sin(2 * np.pi * 369 * timeSamples)
25. sol_diez = amplitude * np.sin(2 * np.pi * 414 * timeSamples)
26. lya_diez = amplitude * np.sin(2 * np.pi * 465 * timeSamples)
27. a = lya + re_3_diez
28. do_major = do + mi + sol
29.
30. mel = []
31. mel.extend(do)
32. mel.extend(sol)
33. mel.extend(sol)
34. mel.extend(fa_diez)
35. mel.extend(sol)
36. mel.extend(sol_diez)
37. mel.extend(sol)
38. mel.extend(fa)
39. mel.extend(do_3)
40.
41. mel.extend(fa)
42. mel.extend(do_3)
43. mel.extend(do_3)
44. mel.extend(si)
45. mel.extend(do_3)
46. mel.extend(re_3)
47. mel.extend(do_3)
48. mel.extend(a)
49.
50. sd.play(mel, fs)
51. status = sd.wait()
52.
53. '''
54. fur = np.fft.rfft(mel)
55. fr = np.fft.rfftfreq(duration[0] * fs, 1./fs)
56. plt.plot(fr, np.abs(fur), 'b')
57. plt.xlabel("Frequency")
58. plt.ylabel("Amplitude")
59. plt.show()
60.
61. r = np.fft.ifft(fur)
62. plt.plot(timeSamples[0][:1000] * 1000, r[:1000], 'b')
63. plt.show()
64.
65.
66. fs_1 = 1000
67. fs_2 = 500
68. fs_3 = 100
69. fs_4 = 50
70. timeSamples_1 = np.arange(np.ceil(duration * fs_1)) / fs_1
71. timeSamples_2 = np.arange(np.ceil(duration * fs_2)) / fs_2
72. timeSamples_3 = np.arange(np.ceil(duration * fs_3)) / fs_3
73. timeSamples_4 = np.arange(np.ceil(duration * fs_4)) / fs_4
74. do_1 = amplitude * np.sin(2 * np.pi * 261 * timeSamples_1)
75. do_2 = amplitude * np.sin(2 * np.pi * 261 * timeSamples_2)
76. do_3 = amplitude * np.sin(2 * np.pi * 261 * timeSamples_3)
77. do_4 = amplitude * np.sin(2 * np.pi * 261 * timeSamples_4)
78. plt.plot(timeSamples_1[:100] * 1000, do_1[:100])
79. plt.title("f = 1000")
80. plt.xlabel("time / milliseconds")
81. plt.show()
82. plt.plot(timeSamples_2[:100] * 1000, do_2[:100])
83. plt.title("f = 500")
84. plt.xlabel("time / milliseconds")
85. plt.show()
86. plt.plot(timeSamples_3[:100] * 1000, do_3[:100])
87. plt.title("f = 100")
88. plt.xlabel("time / milliseconds")
89. plt.show()
90. plt.plot(timeSamples_4[:100] * 1000, do_4[:100])
91. plt.title("f = 50")
92. plt.xlabel("time / milliseconds")
93. plt.show()
94.
95.
96. for i in range(7):
97.    sd.play(mi, fs)
98.    status = sd.wait()
99. sd.play(sol, fs)
100. status = sd.wait()
101. sd.play(do, fs)
102. status = sd.wait()
103. sd.play(re, fs)
104. status = sd.wait()
105. sd.play(mi, fs)
106. status = sd.wait()
107. for i in range(4):
108.    sd.play(fa, fs)
109.    status = sd.wait()
110. for i in range(3):
111.    sd.play(mi, fs)
112.    status = sd.wait()
113. for i in range(2):
114.    sd.play(re, fs)
115.    status = sd.wait()
116. sd.play(mi, fs)
117. status = sd.wait()
118. sd.play(re, fs)
119. status = sd.wait()
120. sd.play(sol, fs)
121. status = sd.wait()
122.
123.
124. plt.plot(timeSamples[:100] * 1000, do[:100])
125. plt.title("sine tone")
126. plt.xlabel("time / milliseconds")
127. plt.show()
128. plt.plot(do)
129. plt.xlabel("time samples")
130. plt.show()
131. plt.plot(timeSamples[:100] * 1000, do_major[:100])
132. plt.title("sine tone")
133. plt.xlabel("time / milliseconds")
134.
135. '''
RAW Paste Data